1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
|
|
| 77.6% |
Uncovered Elements: 13 (58) |
Complexity: 15 |
Complexity Density: 0.38 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
71 |
3 |
@Inject... |
72 |
|
public ImagingServlet(final ImagingModule imagingModule) { |
73 |
3 |
this.imagingModule = imagingModule; |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
76 |
2 |
@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 |
|
|
|
|
| 71.9% |
Uncovered Elements: 9 (32) |
Complexity: 7 |
Complexity Density: 0.29 |
|
83 |
3 |
@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(), "/"); |
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 |
|
|
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 |
|
|
117 |
|
|
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 |
|
|
124 |
|
} catch (ImagingException e) { |
125 |
0 |
throw new ServletException(e); |
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
133 |
3 |
protected String getImageGeneratorName(HttpServletRequest request) {... |
134 |
3 |
final String pathInfo = request.getPathInfo(); |
135 |
3 |
return new PathSplitter(pathInfo).skipTo(0); |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
138 |
3 |
protected ImageGenerator getGenerator(String generatorName) {... |
139 |
3 |
final ImagingModule module = getImagingModule(); |
140 |
3 |
return module.getGenerators().get(generatorName); |
141 |
|
} |
142 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
143 |
2 |
protected ImageStreamer getStreamer(ParameterProviderFactory parameterProviderFactory) {... |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
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@link |
163 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
164 |
0 |
@Deprecated... |
165 |
|
protected ImagingModuleConfig getImagingConfiguration() { |
166 |
0 |
return (ImagingModuleConfig) imagingModule; |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
169 |
3 |
protected ImagingModule getImagingModule() {... |
170 |
3 |
return imagingModule; |
171 |
|
} |
172 |
|
} |