View Javadoc
1   /**
2    * This file Copyright (c) 2008-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.module.templatingkit.imaging.generation;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.imaging.ParameterProvider;
40  import info.magnolia.imaging.operations.ImageOperation;
41  import info.magnolia.jcr.util.PropertyUtil;
42  import info.magnolia.module.site.SiteManager;
43  import info.magnolia.module.templatingkit.imaging.VariationImpl;
44  import info.magnolia.module.templatingkit.sites.Site;
45  import info.magnolia.objectfactory.Components;
46  
47  import java.net.URI;
48  import java.net.URISyntaxException;
49  
50  import javax.inject.Inject;
51  import javax.inject.Provider;
52  import javax.jcr.Property;
53  import javax.jcr.RepositoryException;
54  
55  import org.apache.commons.lang3.StringUtils;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * ImageOperationProvidingVariation.
61   */
62  public class ImageOperationProvidingVariation extends VariationImpl {
63  
64      private static Logger log = LoggerFactory.getLogger(ImageOperationProvidingVariation.class);
65      protected ImageOperation<ParameterProvider<NodeData>> imageOperation;
66  
67      private Provider<Site> siteProvider;
68      private final SiteManager siteManager;
69  
70      @Inject
71      public ImageOperationProvidingVariation(SiteManager siteManager) {
72          // default value
73          setGeneratorName("stk");
74          this.siteManager = siteManager;
75      }
76  
77      /**
78       * @deprecated since 2.9 - use {@link ImageOperationProvidingVariation(SiteManager)} instead.
79       */
80      @Deprecated
81      public ImageOperationProvidingVariation(Provider<Site> siteProvider) {
82          this(Components.getComponent(SiteManager.class));
83          this.siteProvider = siteProvider;
84      }
85  
86      public ImageOperation<ParameterProvider<NodeData>> getImageOperation() {
87          return this.imageOperation;
88      }
89  
90      public void setImageOperation(ImageOperation<ParameterProvider<NodeData>> imageOperation) {
91          this.imageOperation = imageOperation;
92      }
93  
94      @Override
95      public String createLink(Property binaryNodeData) {
96          String workspaceName = null;
97          String path = null;
98          String fileName = null;
99          String extension = null;
100         String themeName = null;
101         try {
102             workspaceName = binaryNodeData.getSession().getWorkspace().getName();
103             path = binaryNodeData.getParent().getPath();
104             fileName = PropertyUtil.getString(binaryNodeData.getParent(), FileProperties.PROPERTY_FILENAME);
105             extension = PropertyUtil.getString(binaryNodeData.getParent(), FileProperties.EXTENSION);
106             themeName = siteManager.getCurrentSite().getTheme().getName();
107         } catch (RepositoryException e) {
108             String propertyPath = null;
109             try {
110                 propertyPath = binaryNodeData.getPath() + binaryNodeData.getName();
111             } catch (Exception ee) {
112             }
113             log.warn("could not create Link for property :" + propertyPath, e);
114             return null;
115         }
116         if (StringUtils.isNotEmpty(extension)) {
117             fileName += "." + extension;
118         }
119         try {
120             fileName = new URI(null, null, fileName, null).toASCIIString();
121         } catch (URISyntaxException e) {
122             log.warn("could not create link for image due to illegal property file name " + fileName, e);
123             return null;
124         }
125         return MgnlContext.getContextPath() + "/.imaging/" + getGeneratorName() + "/" + themeName + "/" + getName() + "/" + workspaceName + path + "/" + fileName;
126     }
127 
128 }