View Javadoc

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