View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.dam.core.download;
35  
36  import info.magnolia.cms.filters.SelfMappingServlet;
37  import info.magnolia.dam.api.Asset;
38  import info.magnolia.dam.api.AssetProvider;
39  import info.magnolia.dam.api.AssetProviderRegistry;
40  import info.magnolia.dam.api.AssetProviderRegistry.NoSuchAssetProviderException;
41  import info.magnolia.dam.api.ItemKey;
42  import info.magnolia.dam.api.PathAwareAssetProvider;
43  import info.magnolia.dam.api.PathAwareAssetProvider.PathNotFoundException;
44  import info.magnolia.dam.core.config.DamCoreConfiguration;
45  import info.magnolia.link.LinkUtil;
46  
47  import java.io.IOException;
48  import java.io.InputStream;
49  import java.util.ArrayList;
50  import java.util.Enumeration;
51  import java.util.List;
52  
53  import javax.inject.Inject;
54  import javax.servlet.ServletException;
55  import javax.servlet.http.HttpServlet;
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  import org.apache.commons.io.IOUtils;
60  import org.apache.commons.lang3.StringUtils;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  /**
65   * Servlet to handle dam document downloads.
66   *
67   * <p>This servlet supports the following link patterns:
68   * <ul>
69   * <li>/dam/jcr:7ecd4045-45a0-4c81-b2b6-f4c4b0cd24ad/<whatever, this is ignored anyway</li>
70   * <li>/dam/static:7ecd4045-45a0-4c81-b2b6-f4c4b0cd24ad (compatible with dms)</li>
71   * <li>/dam/foo/bar/lol.pdf (compatible with dam 1.x)</li>
72   * <li>/dam/jcr/foo/bar/lol.pdf<ul>
73   *     <li>effectively /dam/&lt;provider-id&gt;/&lt;path&gt;.ext</li>
74   *     <li>we ignore .ext and ensure that the corresponding provider is a PathAwareProvider</li>
75   * </ul></li>
76   * </ul>
77   */
78  public class DamDownloadServlet extends HttpServlet implements SelfMappingServlet {
79      private static final Logger log = LoggerFactory.getLogger(DamDownloadServlet.class);
80  
81      static final String CONTENT_DISPOSITION = "Content-Disposition";
82      static final String LAST_MODIFIED = "Last-Modified";
83  
84      /**
85       * Name of the default provider.
86       */
87      public static final String FALL_BACK_PROVIDER_ID = "jcr";
88  
89      private final AssetProviderRegistry assetProviderRegistry;
90      private final DamCoreConfiguration configuration;
91  
92      @Inject
93      public DamDownloadServlet(final DamCoreConfiguration configuration, final AssetProviderRegistry assetProviderRegistry) {
94          this.configuration = configuration;
95          this.assetProviderRegistry = assetProviderRegistry;
96      }
97  
98      @Override
99      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
100         doGet(request, response);
101     }
102 
103     @Override
104     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
105         try {
106             process(request, response);
107         } catch (IOException e) {
108             log.error("An IO error occurred during download", e);
109             throw e;
110         } catch (Exception e) {
111             log.error("An error occurred during download", e);
112             throw new ServletException(e);
113         }
114     }
115 
116     /**
117      * The main task is to retrieve an Asset Object based on the <br>
118      * current Uri. Basically, if no Asset could be found based on the current
119      * Uri, send an error to the response. <br>
120      * If an Asset is found, set the response header and put the Asset Binary in
121      * the response.
122      */
123     protected void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
124         // Get Asset
125         Asset asset = getAsset(request);
126 
127         if (asset == null) {
128             response.sendError(HttpServletResponse.SC_NOT_FOUND);
129             return;
130         }
131         handleResourceRequest(request, response, asset);
132     }
133 
134     @Override
135     public String getSelfMappingPath() {
136         return configuration.getDownloadPath() + "/*";
137     }
138 
139     /**
140      * Retrieve the {@link Asset} related to the information contained into the pathInfo.
141      * 
142      * @return found {@link Asset}, null otherwise.
143      */
144     protected Asset getAsset(HttpServletRequest request) {
145         Asset asset = null;
146         String pathInfo = request.getPathInfo();
147         try {
148             // Try to handle Item keys based on UUID
149             asset = getAssetBasedOnIdentifier(pathInfo);
150             // Try to handle Item based on path
151             if (asset == null) {
152                 asset = getAssetBasedOnPath(pathInfo);
153             }
154         } catch (Exception e) {
155             log.warn("Could not retrieve an asset based on the following pathInfo {}", pathInfo, e);
156         }
157         return asset;
158     }
159 
160     void handleResourceRequest(HttpServletRequest req, HttpServletResponse res, Asset asset) throws Exception {
161         InputStream is = asset.getContentStream();
162         if (is == null) {
163             res.sendError(HttpServletResponse.SC_NOT_FOUND);
164             return;
165         }
166 
167         // Set the mime type to enable voting by contentType (ResponseContentTypeVoter).
168         res.setContentType(asset.getMimeType());
169 
170         // set content disposition
171         if (configuration.getContentDisposition() == null || configuration.getContentDisposition().vote(res) > 0) {
172             res.setHeader(CONTENT_DISPOSITION, "attachment; filename=\"" + asset.getFileName() + "\"");
173         }
174 
175         // unset the Mime Type
176         if (!configuration.isEnforceDocumentMimeType()) {
177             res.setContentType(null);
178         }
179 
180         // set length
181         res.setContentLength((int) asset.getFileSize());
182         // set last update
183         if (asset.getLastModified() != null) {
184             res.setDateHeader(LAST_MODIFIED, asset.getLastModified().getTimeInMillis());
185         }
186         // TODO ehechinger: always send as is, find better way to discover if resource could be compressed
187         try {
188             IOUtils.copyLarge(is, res.getOutputStream());
189         } catch (IOException e) {
190             if (log.isDebugEnabled()) {
191                 log.debug(
192                         "Download of document [" + asset.getName() + "] with headers " + getHeader(req) + " was interrupted due: " + (e.getMessage() == null ? e.getClass().getName() : e.getMessage())
193                                 + ":" + (e.getCause() == null ? "" : e.getCause().toString()) + ".", e);
194             }
195             // re-throw exception only if it's not a partial content response, else logs would fill up with (harmless)
196             // broken pipe exceptions on some app servers (e.g. Tomcat)
197             if (res.getStatus() != HttpServletResponse.SC_PARTIAL_CONTENT) {
198                throw e;
199             }
200         } finally {
201             IOUtils.closeQuietly(is);
202         }
203     }
204 
205     private List<String> getHeader(HttpServletRequest req) {
206         List<String> out = new ArrayList<String>();
207         @SuppressWarnings("unchecked")
208         Enumeration<String> names = req.getHeaderNames();
209         while (names.hasMoreElements()) {
210             String name = names.nextElement();
211             out.add(name + "=" + req.getHeader(name));
212         }
213         return out;
214     }
215 
216     /**
217      * Handle pathInfo containing identifier. The following cases are handled:<br>
218      * - /dam/jcr:7ecd4045-45a0-4c81-b2b6-f4c4b0cd24ad/<whatever...<br>
219      *
220      * @return the found {@link Asset}, null otherwise.
221      */
222     private Asset getAssetBasedOnIdentifier(String pathInfo) {
223         // extract all between the first and the second '/'
224         String keyStr = pathInfo.split("/")[1];
225         // handling of /dam/jcr:7ecd4045-45a0-4c81-b2b6-f4c4b0cd24ad/<whatever...
226         if (ItemKey.isValid(keyStr)) {
227             final ItemKey itemKey = ItemKey.from(keyStr);
228             return assetProviderRegistry.getProviderFor(itemKey).getAsset(itemKey);
229         }
230         return null;
231     }
232 
233     /**
234      * Handle pathInfo containing absolute path to dam Assets. The following cases are handled:<br>
235      * - /dam/foo/bar/lol.pdf (compatible with dam 1.x).<br>
236      * - /dam/jcr/foo/bar/lol.pdf (so /dam/<provider-id>/<path><br>
237      * 
238      * @return the found {@link Asset}, null otherwise.
239      */
240     private Asset getAssetBasedOnPath(String pathInfo) {
241         AssetProvider assetProvider = null;
242         // extract all between the first and the second '/'
243         String keyStr = pathInfo.split("/")[1];
244         String path = pathInfo;
245         // Check if the keyStr refers to a provider ID
246         try {
247             // handling of: /dam/jcr/foo/bar/lol.pdf (so /dam/<provider-id>/<path>.ext
248             assetProvider = assetProviderRegistry.getProviderById(keyStr);
249             path = path.replaceFirst("/" + assetProvider.getIdentifier() + "/", "/");
250         } catch (NoSuchAssetProviderException nsape) {
251             // handling of: /dam/foo/bar/lol.pdf (compatible with dam 1.x)
252             assetProvider = assetProviderRegistry.getProviderById(FALL_BACK_PROVIDER_ID);
253         }
254         // ensure that the provider is an instance of PathAwareAssetProvider
255         if (assetProvider instanceof PathAwareAssetProvider) {
256             // remove the extension (.pdf) and cache fingerPrint if present
257             // (print-logo.2012-11-20-12-15-20.pdf --> print-logo)
258             String extension = StringUtils.substringAfterLast(path, ".");
259             String assetPath = LinkUtil.removeFingerprintAndExtensionFromLink(path);
260             // try to retrieve without extension
261             try {
262                 return ((PathAwareAssetProvider) assetProvider).getAsset(assetPath);
263             } catch (PathNotFoundException e) {
264                 // Do nothing, try to search with extension
265             }
266             // try with the extension
267             try {
268                 return ((PathAwareAssetProvider) assetProvider).getAsset(assetPath + "." + extension);
269             } catch (PathNotFoundException e) {
270                 log.warn("No asset could be found for the following path {}", assetPath);
271             }
272         }
273         log.warn("Provider {} can not support assets search based on path", assetProvider.getIdentifier());
274         return null;
275     }
276 }