View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.dam;
35  
36  import info.magnolia.link.LinkUtil;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.dam.api.Asset;
39  import info.magnolia.dam.api.AssetProviderCapability;
40  import info.magnolia.dam.api.RenditionAwareAssetProvider;
41  import info.magnolia.dam.core.config.DamCoreConfiguration;
42  import info.magnolia.dam.jcr.AssetNodeTypes;
43  import info.magnolia.dam.jcr.DamConstants;
44  import info.magnolia.dam.jcr.JcrAssetProvider;
45  import info.magnolia.module.site.ExtendedAggregationState;
46  import info.magnolia.module.site.Site;
47  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
48  import info.magnolia.module.templatingkit.imaging.ImagingSupport;
49  
50  import java.util.Calendar;
51  import java.util.EnumSet;
52  
53  import javax.inject.Inject;
54  import javax.jcr.Node;
55  
56  import org.apache.jackrabbit.JcrConstants;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  /**
61   * {@link info.magnolia.dam.api.AssetProvider} that delivers assets for the "dam" workspace
62   * and supports renditions of assets.
63   *
64   * @deprecated since 2.9 - use {@link info.magnolia.dam.jcr.JcrAssetProvider} and render {@link info.magnolia.dam.api.AssetRendition} with {@link info.magnolia.dam.api.AssetRenderer} magnolia-dam-imaging module.
65   */
66  @Deprecated
67  public class RenditionAwareJcrAssetProvider extends JcrAssetProvider implements RenditionAwareAssetProvider {
68  
69      private static final Logger log = LoggerFactory.getLogger(JcrAssetProvider.class);
70  
71      private STKTemplatingFunctions stkTemplatingFunctions;
72  
73      @Inject
74      public RenditionAwareJcrAssetProvider(DamCoreConfiguration configuration, STKTemplatingFunctions stkTemplatingFunctions) {
75          super(configuration);
76          this.stkTemplatingFunctions = stkTemplatingFunctions;
77      }
78  
79      @Override
80      protected EnumSet<AssetProviderCapability> setupProviderCapabilities() {
81          return EnumSet.of(AssetProviderCapability.query, AssetProviderCapability.queryWithProviderSpecificString, AssetProviderCapability.hierarchical, AssetProviderCapability.rendition);
82      }
83  
84      @Override
85      public String getLink(Asset asset, String renditionName) throws AssetRenditionException {
86          ExtendedAggregationState aggregationState = (ExtendedAggregationState) MgnlContext.getAggregationState();
87  
88          Site site = aggregationState.getSite();
89          final ImagingSupport imaging = (ImagingSupport) stkTemplatingFunctions.theme(site).getImaging();
90  
91          try {
92              // There needs to be a more generic way to generate variations from assets.
93              // Variations and ImagingSupport only supports creation of links from JCR properties.
94              String link;
95              // FIXME - MGNLIMG-107
96              Node contentNode = AssetNodeTypes.AssetResource.getResourceNodeFromAsset(MgnlContext.getJCRSession(DamConstants.WORKSPACE).getNodeByIdentifier(asset.getItemKey().getAssetId()));
97              link = imaging.createLink(contentNode.getProperty(JcrConstants.JCR_DATA), renditionName);
98  
99              // Add cache fingerprint so that browser caches asset only until asset is modified.
100             Calendar lastModified = asset.getLastModified();
101             link = LinkUtil.addFingerprintToLink(link, lastModified);
102 
103             return link;
104         } catch (Exception e) {
105             throw new AssetRenditionException("Asset rendition '" + renditionName + "' could not be generated.", e);
106         }
107     }
108 
109 }