Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging

File ImagingServlet.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
78% of files have more coverage

Code metrics

6
37
11
2
186
115
18
0.49
3.36
5.5
1.64
6.9% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ImagingServlet 63 36 0% 17 52
0.00%
ImagingServlet.ServletImageResponse 169 1 66.7% 1 2
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2009-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.imaging;
35   
36    import info.magnolia.context.MgnlContext;
37    import info.magnolia.imaging.util.PathSplitter;
38    import info.magnolia.init.MagnoliaConfigurationProperties;
39    import info.magnolia.objectfactory.Components;
40   
41    import java.io.IOException;
42    import java.io.OutputStream;
43    import java.util.concurrent.ExecutorService;
44    import java.util.concurrent.Executors;
45   
46    import javax.inject.Inject;
47    import javax.inject.Provider;
48    import javax.servlet.AsyncContext;
49    import javax.servlet.ServletException;
50    import javax.servlet.ServletResponse;
51    import javax.servlet.http.HttpServlet;
52    import javax.servlet.http.HttpServletRequest;
53    import javax.servlet.http.HttpServletResponse;
54   
55    import org.slf4j.Logger;
56    import org.slf4j.LoggerFactory;
57   
58    import com.google.common.net.MediaType;
59   
60    /**
61    * Servlet responsible for the actual generation of the images.
62    */
 
63    public class ImagingServlet extends HttpServlet {
64    private static final Logger log = LoggerFactory.getLogger(ImagingServlet.class);
65    private final Imaging imaging;
66    private final Provider<ImagingModule> imagingModuleProvider;
67    private ExecutorService workers;
68   
69   
 
70  0 toggle @Inject
71    public ImagingServlet(Imaging imaging, Provider<ImagingModule> imagingModuleProvider, MagnoliaConfigurationProperties mcp) {
72  0 this.imaging = imaging;
73  0 this.imagingModuleProvider = imagingModuleProvider;
74  0 int workersCount = 4;
75  0 if (mcp.hasProperty("imaging.workers.count")) {
76  0 try {
77  0 workersCount = Integer.parseInt(mcp.getProperty("imaging.workers.count"));
78    } catch (NumberFormatException e) {
79  0 log.error("Failed to parse the number of imaging workers, falling back to 4");
80    }
81    }
82  0 this.workers = Executors.newFixedThreadPool(workersCount);
83    }
84   
85    /**
86    * @deprecated since 3.4.3 - use {@link #ImagingServlet(Imaging, Provider, MagnoliaConfigurationProperties)} instead.
87    */
 
88  0 toggle @Deprecated
89    public ImagingServlet(Imaging imaging, Provider<ImagingModule> imagingModuleProvider) {
90  0 this(imaging, imagingModuleProvider, Components.getComponent(MagnoliaConfigurationProperties.class));
91    }
92   
93    /**
94    * @deprecated Since Imaging 3.3 (Magnolia 5.5), use {@link ImagingServlet(Imaging)}.
95    */
 
96  0 toggle @Deprecated
97    public ImagingServlet(final ImagingModule imagingModule) {
98  0 this.imaging = Components.getComponent(Imaging.class);
99  0 this.imagingModuleProvider = new Provider<ImagingModule>() {
 
100  0 toggle @Override
101    public ImagingModule get() {
102  0 return Components.getComponent(ImagingModule.class);
103    }
104    };
105    }
106   
107   
 
108  0 toggle @Override
109    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
110  0 AsyncContext asyncContext = request.startAsync();
111  0 log.debug("Requesting {}", request.getRequestURI());
112  0 workers.submit(() -> {
113  0 MgnlContext.doInSystemContext(new MgnlContext.VoidOp() {
114   
 
115  0 toggle @Override
116    public void doExec() {
117  0 try {
118  0 String generatorName = getImageGeneratorName(request);
119  0 imaging.generate(generatorName, request, new ServletImageResponse(asyncContext.getResponse()));
120  0 log.debug("Generated image for {}", request.getRequestURI());
121  0 asyncContext.getResponse().flushBuffer();
122    } catch (IllegalArgumentException | ImagingRuntimeException | ImagingException e) {
123  0 log.warn("Because of incorrect arguments the image couldn't be found", e);
124  0 sendError(asyncContext, HttpServletResponse.SC_NOT_FOUND, "");
125    } catch (IOException e) {
126    // only log at debug level
127    // tomcat usually throws a ClientAbortException anytime the user stop loading the page
128  0 log.debug("Unable to spool resource due to a {} exception", e.getClass().getName());
129  0 if (!asyncContext.getResponse().isCommitted()) {
130  0 sendError(asyncContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "");
131    }
132    } finally {
133  0 asyncContext.complete();
134    }
135    }
136    });
137   
138    });
139    }
140   
141   
 
142  0 toggle private void sendError(AsyncContext asyncContext, int statusCode, String message) {
143  0 if(asyncContext.getResponse() instanceof HttpServletResponse)
144  0 try {
145  0 ((HttpServletResponse) asyncContext.getResponse()).sendError(statusCode, message);
146    } catch (IOException e) {
147  0 e.printStackTrace();
148    }
149    }
150   
151    /**
152    * Determines the ImageGenerator to use, using the first path element of the
153    * pathInfo.
154    */
 
155  0 toggle protected String getImageGeneratorName(HttpServletRequest request) {
156  0 final String pathInfo = request.getPathInfo();
157  0 return new PathSplitter(pathInfo).skipTo(0);
158    }
159   
 
160  0 toggle protected ImageGenerator getGenerator(String generatorName) {
161  0 final ImagingModule module = getImagingModule();
162  0 return module.getGenerators().get(generatorName);
163    }
164   
 
165  0 toggle protected ImagingModule getImagingModule() {
166  0 return imagingModuleProvider.get();
167    }
168   
 
169    private static class ServletImageResponse implements ImageResponse {
170    private final ServletResponse response;
171   
 
172  0 toggle public ServletImageResponse(ServletResponse response) {
173  0 this.response = response;
174    }
175   
 
176    toggle @Override
177    public void setMediaType(MediaType mediaType) throws IOException {
178    response.setContentType(mediaType.toString());
179    }
180   
 
181    toggle @Override
182    public OutputStream getOutputStream() throws IOException {
183    return response.getOutputStream();
184    }
185    }
186    }