View Javadoc
1   /**
2    * This file Copyright (c) 2015-2016 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.imaging.variation;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.imaging.ParameterProvider;
39  import info.magnolia.imaging.operations.ImageOperation;
40  import info.magnolia.imaging.variation.DefaultVariation;
41  import info.magnolia.jcr.util.PropertyUtil;
42  import info.magnolia.module.site.SiteManager;
43  import info.magnolia.templating.imaging.ThemeAwareImageGenerator;
44  
45  import java.net.URI;
46  import java.net.URISyntaxException;
47  
48  import javax.inject.Inject;
49  import javax.jcr.Node;
50  import javax.jcr.Property;
51  import javax.jcr.RepositoryException;
52  
53  import org.apache.commons.lang3.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  /**
58   * {@link info.magnolia.imaging.variation.Variation} providing {@link info.magnolia.imaging.operations.ImageOperation}s.
59   *
60   * The generator name is "mte".
61   */
62  public class ImageOperationProvidingVariation extends DefaultVariation {
63  
64      private static final Logger log = LoggerFactory.getLogger(ImageOperationProvidingVariation.class);
65  
66      private String generatorName;
67      private ImageOperation<ParameterProvider<Node>> imageOperation;
68  
69      private final SiteManager siteManager;
70  
71      @Inject
72      public ImageOperationProvidingVariation(SiteManager siteManager) {
73          this.siteManager = siteManager;
74          setGeneratorName(ThemeAwareImageGenerator.GENERATOR_NAME);
75      }
76  
77      public ImageOperation<ParameterProvider<Node>> getImageOperation() {
78          return this.imageOperation;
79      }
80  
81      public void setImageOperation(ImageOperation<ParameterProvider<Node>> imageOperation) {
82          this.imageOperation = imageOperation;
83      }
84  
85      public String getGeneratorName() {
86          return generatorName;
87      }
88  
89      public void setGeneratorName(String generatorName) {
90          this.generatorName = generatorName;
91      }
92  
93      @Override
94      public String createLink(Property binaryProperty) {
95          String fileName = null;
96  
97          try {
98              final String workspaceName = binaryProperty.getSession().getWorkspace().getName();
99              final Node parent = binaryProperty.getParent();
100             final String path = parent.getPath();
101             final String extension = PropertyUtil.getString(parent, FileProperties.EXTENSION);
102             final String themeName = siteManager.getAssignedSite(parent).getTheme().getName();
103 
104             fileName = PropertyUtil.getString(parent, FileProperties.PROPERTY_FILENAME);
105 
106             if (StringUtils.isNotEmpty(extension)) {
107                 fileName += "." + extension;
108             }
109 
110             fileName = new URI(null, null, fileName, null).toASCIIString();
111 
112             return MgnlContext.getContextPath() + "/.imaging/" + getGeneratorName() + "/" + themeName + "/" + getName() + "/" + workspaceName + path + "/" + fileName;
113         } catch (RepositoryException e) {
114             log.warn("Could not create link for property [{}].", binaryProperty, e);
115         } catch (URISyntaxException e) {
116             log.warn("Could not create link for image due to illegal property file name [{}].", fileName, e);
117         }
118 
119         return null;
120     }
121 
122 }