Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging

File ImagingServlet.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
61% of files have more coverage

Code metrics

10
40
8
1
172
94
15
0.38
5
8
1.88

Classes

Class Line # Actions
ImagingServlet 64 40 0% 15 13
0.775862177.6%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-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.imaging;
35   
36    import info.magnolia.cms.beans.config.MIMEMapping;
37    import info.magnolia.cms.core.HierarchyManager;
38    import info.magnolia.cms.util.BooleanUtil;
39    import info.magnolia.context.MgnlContext;
40    import info.magnolia.imaging.caching.CachingImageStreamer;
41    import info.magnolia.imaging.caching.CachingStrategy;
42    import info.magnolia.imaging.util.PathSplitter;
43   
44    import java.io.IOException;
45   
46    import javax.inject.Inject;
47    import javax.servlet.ServletConfig;
48    import javax.servlet.ServletException;
49    import javax.servlet.http.HttpServlet;
50    import javax.servlet.http.HttpServletRequest;
51    import javax.servlet.http.HttpServletResponse;
52   
53    import org.apache.commons.lang3.StringUtils;
54    import org.slf4j.Logger;
55    import org.slf4j.LoggerFactory;
56   
57    /**
58    * Servlet responsible for the actual generation of the images. TODO This
59    * servlet might need some investigation - improvements; particularly how the
60    * parameterProvider, and various factories are bound together.
61    * During development / tests of generators, set the storeGeneratedImages
62    * parameter to "false".
63    */
 
64    public class ImagingServlet extends HttpServlet {
65    private static final Logger log = LoggerFactory.getLogger(ImagingServlet.class);
66   
67    private boolean storeGeneratedImages = true;
68    private boolean validateFileExtension = false;
69    private final ImagingModule imagingModule;
70   
 
71  3 toggle @Inject
72    public ImagingServlet(final ImagingModule imagingModule) {
73  3 this.imagingModule = imagingModule;
74    }
75   
 
76  2 toggle @Override
77    public void init(ServletConfig config) throws ServletException {
78  2 super.init(config);
79  2 storeGeneratedImages = BooleanUtil.toBoolean(config.getInitParameter("storeGeneratedImages"), true);
80  2 validateFileExtension = BooleanUtil.toBoolean(config.getInitParameter("validateFileExtension"), false);
81    }
82   
 
83  3 toggle @Override
84    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
85   
86  3 final String generatorName = getImageGeneratorName(request);
87  3 final ImageGenerator generator = getGenerator(generatorName);
88  3 if (generator == null) {
89  0 throw new IllegalArgumentException("No ImageGenerator with name " + generatorName);
90    }
91   
92  3 final ParameterProviderFactory parameterProviderFactory = generator.getParameterProviderFactory();
93  3 final ParameterProvider p = parameterProviderFactory.newParameterProviderFor(request);
94  3 final String outputFormat = generator.getOutputFormat(p).getFormatName();
95   
96  3 if (validateFileExtension) {
97   
98  1 String requestedPath = StringUtils.substringAfterLast(request.getPathInfo(), "/"); // don't take part of node name as extension, we support dots in node names!
99  1 String requestedFormat = StringUtils.substringAfterLast(requestedPath, ".");
100  1 if (!StringUtils.equals(outputFormat, requestedFormat)) {
101  1 response.sendError(HttpServletResponse.SC_NOT_FOUND);
102  1 return;
103    }
104    }
105   
106  2 try {
107    // we need to set mimetype here explicitly because extension (and type) is determined at runtime depending on what the image op chain does
108  2 final String contentType = MIMEMapping.getMIMEType(outputFormat);
109  2 response.setContentType(contentType);
110  2 final ImageStreamer streamer = getStreamer(parameterProviderFactory);
111   
112  2 try {
113  2 streamer.serveImage(generator, p, response.getOutputStream());
114  2 response.flushBuffer();
115    } catch (IOException e) {
116    // only log at debug level
117    // tomcat usually throws a ClientAbortException anytime the user stop loading the page
118  0 log.debug("Unable to spool resource due to a {} exception", e.getClass().getName());
119  0 if (!response.isCommitted()) {
120  0 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
121    }
122    }
123    // IOUtils.closeQuietly(response.getOutputStream());
124    } catch (ImagingException e) {
125  0 throw new ServletException(e); // TODO
126    }
127    }
128   
129    /**
130    * Determines the ImageGenerator to use, using the first path element of the
131    * pathInfo.
132    */
 
133  3 toggle protected String getImageGeneratorName(HttpServletRequest request) {
134  3 final String pathInfo = request.getPathInfo();
135  3 return new PathSplitter(pathInfo).skipTo(0);
136    }
137   
 
138  3 toggle protected ImageGenerator getGenerator(String generatorName) {
139  3 final ImagingModule module = getImagingModule();
140  3 return module.getGenerators().get(generatorName);
141    }
142   
 
143  2 toggle protected ImageStreamer getStreamer(ParameterProviderFactory parameterProviderFactory) {
144    // TODO -- CachingImageStreamer currently has a non-static "currentJobs"
145    // map.
146    // TODO -- to investigate, but I highly suspect that it's not really
147    // useful if
148    // we use a different instance of CachingImageStreamer for every
149    // request.
150   
151  2 final DefaultImageStreamer newDefaultImageStreamer = new DefaultImageStreamer();
152  2 if (!storeGeneratedImages) {
153  0 return newDefaultImageStreamer;
154    } else {
155  2 final HierarchyManager hm = MgnlContext.getHierarchyManager("imaging");
156  2 final CachingStrategy cachingStrategy = parameterProviderFactory.getCachingStrategy();
157  2 return new CachingImageStreamer(hm, cachingStrategy, newDefaultImageStreamer);
158    }
159    }
160   
161    /**
162    * @deprecated since 3.2. Use {@link #getImagingModule}.
163    */
 
164  0 toggle @Deprecated
165    protected ImagingModuleConfig getImagingConfiguration() {
166  0 return (ImagingModuleConfig) imagingModule;
167    }
168   
 
169  3 toggle protected ImagingModule getImagingModule() {
170  3 return imagingModule;
171    }
172    }