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.dam.api.Asset;
37  import info.magnolia.dam.api.AssetProvider;
38  import info.magnolia.dam.api.AssetProviderCapability;
39  import info.magnolia.dam.api.AssetRenderer;
40  import info.magnolia.dam.api.AssetRendition;
41  import info.magnolia.dam.api.DamException;
42  import info.magnolia.dam.api.RenditionAwareAssetProvider;
43  import info.magnolia.dam.asset.LegacyAssetWrapper;
44  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
45  
46  import java.io.InputStream;
47  
48  import javax.inject.Inject;
49  
50  import com.google.common.net.MediaType;
51  
52  /**
53   * Asset renderer for STK using imaging to generate renditions.
54   *
55   * @deprecated since 2.9 - use {@link info.magnolia.dam.api.AssetRenderer} from magnolia-dam-imaging module instead.
56   */
57  @Deprecated
58  public class STKAssetRenderer implements AssetRenderer {
59  
60      private STKTemplatingFunctions stkTemplatingFunctions;
61  
62      @Inject
63      public STKAssetRenderer(STKTemplatingFunctions stkTemplatingFunctions) {
64          this.stkTemplatingFunctions = stkTemplatingFunctions;
65      }
66  
67      /**
68       * {@link AssetRendition} implementation for STK.
69       * This implementation extends {@link info.magnolia.dam.asset.LegacyAssetWrapper} in order to stay compatible with the previous Dam API definition, <br>
70       * and usage in templates.
71       *
72       * @deprecated Since 2.8. Use a proper implementation of {@link AssetRendition} and call {@link AssetRendition#getLink()}.
73       */
74      @Deprecated
75      private static class STKAssetRendition extends LegacyAssetWrapper implements AssetRendition {
76          private final Asset asset;
77          private final String url;
78          private final String renditionName;
79  
80          public STKAssetRendition(Asset asset, String url, String renditionName) {
81              super(asset);
82              this.asset = asset;
83              this.url = url;
84              this.renditionName = renditionName;
85          }
86  
87          @Override
88          public String getLink() {
89              return this.url;
90          }
91  
92          @Override
93          public String getRenditionName() {
94              return this.renditionName;
95          }
96  
97          @Override
98          public String getMimeType() {
99              return this.asset.getMimeType();
100         }
101 
102         @Override
103         public InputStream getStream() {
104             return this.asset.getContentStream();
105         }
106 
107         @Override
108         public Asset getAsset() {
109             return this.asset;
110         }
111 
112     }
113 
114     @Override
115     public boolean supports(MediaType from, MediaType to) {
116         return true;
117     }
118 
119     @Override
120     public boolean canRender(Asset asset, MediaType to) {
121         return true;
122     }
123 
124     @Override
125     public AssetRendition render(Asset asset, MediaType to, String renditionName) {
126         try {
127             String link;
128 
129             AssetProvider assetProvider = asset.getAssetProvider();
130             if (assetProvider.supports(AssetProviderCapability.rendition)) {
131                 if (asset instanceof STKAssetWrapper) { // can't easily unwrap
132                     asset = assetProvider.getAsset(asset.getItemKey());
133                 }
134                 link = ((RenditionAwareAssetProvider) assetProvider).getLink(asset, renditionName);
135             } else {
136                 link = asset.getLink();
137             }
138             return new STKAssetRendition(asset, link, renditionName);
139 
140         } catch (Exception e) {
141             throw new DamException("AssetRendition exception", e) {
142             };
143         }
144     }
145 
146 }