View Javadoc
1   /**
2    * This file Copyright (c) 2015 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.imaging.functions;
35  
36  import info.magnolia.imaging.ImagingSupport;
37  import info.magnolia.jcr.util.ContentMap;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.rendering.template.TemplateDefinition;
40  
41  import javax.inject.Inject;
42  import javax.inject.Singleton;
43  import javax.jcr.Node;
44  
45  import org.apache.commons.lang3.StringUtils;
46  import org.apache.jackrabbit.JcrConstants;
47  import org.apache.jackrabbit.util.Text;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  /**
52   * Imaging templating function exposed in ftl as "imgfn". This class exposes utility methods for freemarker templates and model classes.
53   */
54  @Singleton
55  public class ImagingTemplatingFunctions {
56  
57      private static final Logger log = LoggerFactory.getLogger(ImagingTemplatingFunctions.class);
58  
59      public static final String PROPERTY_IMAGE_VARIATION = "imageVariation";
60  
61      private ImagingSupport imagingSupport;
62  
63      @Inject
64      public ImagingTemplatingFunctions(ImagingSupport imagingSupport) {
65          this.imagingSupport = imagingSupport;
66      }
67  
68      /**
69       * Resolves an image variation name based on parameter values found in the template definition. If none is found, defaults to {@link ImagingSupport#VARIATION_ORIGINAL}.
70       */
71      public String resolveImageVariationName(TemplateDefinition definition) {
72          if (definition == null) {
73              return ImagingSupport.VARIATION_ORIGINAL;
74          }
75  
76          String variationName = (String) definition.getParameters().get(PROPERTY_IMAGE_VARIATION);
77          if (StringUtils.isNotBlank(variationName)) {
78              return variationName;
79          }
80  
81          return ImagingSupport.VARIATION_ORIGINAL;
82      }
83  
84      /**
85       * @see #getImageVariationLinkFromBinary(Node, String)
86       */
87      public String getImageVariationLinkFromBinary(ContentMap binaryContent, TemplateDefinition definition) {
88          if (binaryContent == null) {
89              return null;
90          }
91          return getImageVariationLinkFromBinary(binaryContent.getJCRNode(), definition);
92      }
93  
94      /**
95       * @see #getImageVariationLinkFromBinary(Node, String)
96       */
97      public String getImageVariationLinkFromBinary(Node binaryContent, TemplateDefinition definition) {
98          return getImageVariationLinkFromBinary(binaryContent, resolveImageVariationName(definition));
99      }
100 
101     /**
102      * @see #getImageVariationLinkFromBinary(Node, String)
103      */
104     public String getImageVariationLinkFromBinary(ContentMap binaryContent, String variation) {
105         if (binaryContent == null) {
106             return null;
107         }
108         return getImageVariationLinkFromBinary(binaryContent.getJCRNode(), variation);
109     }
110 
111     /**
112      * @return the link to an image variation for non asset nodes containing a binary. May return null
113      */
114     public String getImageVariationLinkFromBinary(Node binaryContent, String variation) {
115         if (binaryContent == null || variation == null) {
116             return null;
117         }
118 
119         try {
120             String link = imagingSupport.createLink(binaryContent.getProperty(JcrConstants.JCR_DATA), variation);
121             // URL encode the path because the user name node (=email address for ResearchAnt) in the path may contain illegal
122             // URL characters (like '+'). Fix for: https://jira.info.nl/browse/TOKUE-319
123             // for background info see: http://wiki.apache.org/jackrabbit/EncodingAndEscaping
124             if (link != null) {
125                 return Text.escapePath(link);
126             }
127         } catch (Exception e) {
128             log.warn("An error occurred while getting data for binary content at {}", NodeUtil.getPathIfPossible(binaryContent), e);
129         }
130         return null;
131     }
132 }