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.caching

File CachingImageStreamer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

20
87
9
2
315
189
27
0.31
9.67
4.5
3
2.5% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
CachingImageStreamer 79 86 0% 26 39
0.6578947365.8%
CachingImageStreamer.TemporaryImageResponse 298 1 60% 1 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

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.caching;
35   
36    import info.magnolia.cms.beans.config.MIMEMapping;
37    import info.magnolia.context.SystemContext;
38    import info.magnolia.imaging.AbstractImageStreamer;
39    import info.magnolia.imaging.ImageGenerator;
40    import info.magnolia.imaging.ImageResponse;
41    import info.magnolia.imaging.ImageStreamer;
42    import info.magnolia.imaging.ImagingException;
43    import info.magnolia.imaging.OutputFormat;
44    import info.magnolia.imaging.ParameterProvider;
45    import info.magnolia.jcr.util.NodeTypes;
46    import info.magnolia.jcr.util.NodeUtil;
47   
48    import java.io.ByteArrayInputStream;
49    import java.io.ByteArrayOutputStream;
50    import java.io.IOException;
51    import java.io.InputStream;
52    import java.io.OutputStream;
53    import java.util.Calendar;
54    import java.util.concurrent.ExecutionException;
55    import java.util.concurrent.TimeUnit;
56    import java.util.concurrent.locks.ReentrantLock;
57   
58    import javax.jcr.InvalidItemStateException;
59    import javax.jcr.Node;
60    import javax.jcr.Property;
61    import javax.jcr.RepositoryException;
62    import javax.jcr.Session;
63   
64    import org.apache.commons.io.IOUtils;
65    import org.apache.jackrabbit.JcrConstants;
66    import org.slf4j.Logger;
67    import org.slf4j.LoggerFactory;
68   
69    import com.google.common.cache.CacheBuilder;
70    import com.google.common.cache.CacheLoader;
71    import com.google.common.cache.LoadingCache;
72    import com.google.common.net.MediaType;
73   
74    /**
75    * An ImageStreamer which stores and serves generated images to/from a specific workspace.
76    *
77    * @param <P> type of ParameterProvider's parameter
78    */
 
79    public class CachingImageStreamer<P> extends AbstractImageStreamer<P> {
80   
81    private static final Logger log = LoggerFactory.getLogger(CachingImageStreamer.class);
82   
83    private static final String GENERATED_IMAGE_NODE_NAME = "generated-image";
84   
85    private final Session session;
86    private final CachingStrategy<P> cachingStrategy;
87    private final ImageStreamer<P> delegate;
88    private final SystemContext systemContext;
89   
90    /**
91    * This LoadingCache is the key to understanding how this class works.
92    * By using a LoadingCache, we are essentially locking all requests
93    * coming in for the same image (ImageGenerationJob) except the first one.
94    *
95    * CacheBuilder.build() returns a LoadingCache implemented as such that the
96    * first call to get(K) will generate the value (by calling <V> Function.apply(<K>).
97    * Further calls are blocked until the value is generated, and they all retrieve the same value.
98    *
99    * TODO: make static if we don't use the exact same instance for all threads ?
100    */
101    private final LoadingCache<ImageGenerationJob<P>, Property> currentJobs;
102   
103    /**
104    * Despite the currentJobs doing quite a good job at avoiding multiple requests
105    * for the same job, we still need to lock around JCR operations, otherwise multiple
106    * requests end up creating the same cachePath (or parts of it), thus yielding
107    * InvalidItemStateException: "Item cannot be saved because it has been modified externally".
108    * TODO - this is currently static because we *know* ImagingServlet uses a different instance
109    * of CachingImageStreamer for every request. This is not exactly the most elegant.
110    * TODO - see related TODO in currentJobs and info.magnolia.imaging.ImagingServlet#getStreamer
111    */
112    private static final ReentrantLock lock = new ReentrantLock();
113   
 
114  3 toggle public CachingImageStreamer(Session session, CachingStrategy<P> cachingStrategy, ImageStreamer<P> delegate, SystemContext systemContext) {
115  3 this.session = session;
116  3 this.cachingStrategy = cachingStrategy;
117  3 this.delegate = delegate;
118  3 this.systemContext = systemContext;
119   
120  3 CacheBuilder<Object, Object> cb = CacheBuilder.newBuilder();
121  3 this.currentJobs = cb
122    // entries from the LoadingCache will be removed 500ms after their creation,
123    // thus unblocking further requests for an equivalent job.
124    .expireAfterWrite(500, TimeUnit.MILLISECONDS)
125   
126    // We're (ab)using CacheLoader -- this is NOT the cache. We're merely using it to schedule concurrent image generation jobs.
127    .build(new CacheLoader<ImageGenerationJob<P>, Property>() {
128   
 
129  1 toggle @Override
130    public Property load(ImageGenerationJob<P> job) throws Exception {
131  1 try {
132  1 return generateAndStore(job.getGenerator(), job.getParams());
133    } catch (IOException | ImagingException e) {
134    // the LoadingCache will further wrap these in ExecutionExceptions, and we will, in turn, unwrap them ...
135  0 throw new RuntimeException(e);
136    }
137    }
138   
139    });
140    }
141   
 
142  16 toggle @Override
143    public void serveImage(ImageGenerator<ParameterProvider<P>> generator, ParameterProvider<P> params, ImageResponse imageResponse) throws ImagingException, IOException {
144  15 Property imgProp = fetchFromCache(generator, params);
145  16 if (imgProp == null) {
146    // image is not in cache or should be regenerated
147  8 try {
148  8 imgProp = currentJobs.get(new ImageGenerationJob<>(generator, params));
149    } catch (ExecutionException e) {
150    // thrown if the LoadingCache's Function failed
151  0 unwrapRuntimeException(e);
152    }
153    }
154  14 serve(imgProp, imageResponse);
155    }
156   
157    /**
158    * @deprecate Since Imaging 3.3 (Magnolia 5.5) use {@link CachingImageStreamer#serveImage(ImageGenerator, ParameterProvider, ImageResponse)} instead.
159    */
 
160  0 toggle @Deprecated
161    public void serveImage(ImageGenerator<ParameterProvider<P>> generator, ParameterProvider<P> params, OutputStream out) throws IOException, ImagingException {
162  0 serveImage(generator, params, new TemporaryImageResponse(out));
163    }
164   
165    /**
166    * Gets the binary property (NodeData) for the appropriate image, ready to be served,
167    * or null if the image should be regenerated.
168    */
 
169  16 toggle protected Property fetchFromCache(ImageGenerator<ParameterProvider<P>> generator, ParameterProvider<P> parameterProvider) {
170  16 final String cachePath = cachingStrategy.getCachePath(generator, parameterProvider);
171  16 if (cachePath == null) {
172    // the CachingStrategy decided it doesn't want us to cache :(
173  0 return null;
174    }
175  16 try {
176  16 if (!session.itemExists(cachePath)) {
177  8 return null;
178    }
179  8 final Node imageNode = session.getNode(cachePath);
180  8 if (!imageNode.hasNode(GENERATED_IMAGE_NODE_NAME)) {
181  0 return null;
182    }
183  8 final Property property = imageNode.getNode(GENERATED_IMAGE_NODE_NAME).getProperty(JcrConstants.JCR_DATA);
184  8 InputStream in;
185  8 try {
186  8 in = property.getBinary().getStream();
187    } catch (RepositoryException e) {
188    // will happen, when stream is not yet stored properly (generateAndStore)
189    // we prefer this handling over having to lock because of better performance especially with big images
190  0 return null;
191    }
192  8 IOUtils.closeQuietly(in);
193   
194  8 if (cachingStrategy.shouldRegenerate(property, parameterProvider)) {
195  0 return null;
196    }
197  8 return property;
198    } catch (RepositoryException e) {
199  0 throw new RuntimeException(e); // TODO
200    }
201    }
202   
 
203  16 toggle protected void serve(Property binary, ImageResponse imageResponse) throws IOException {
204  16 final InputStream in;
205  16 try {
206  16 in = binary.getBinary().getStream();
207    } catch (RepositoryException e) {
208  0 throw new IllegalStateException("Can't get InputStream from " + binary);
209    }
210  15 final String contentType;
211  16 try {
212  15 contentType = binary.getParent().getProperty(JcrConstants.JCR_MIMETYPE).getString();
213    } catch (RepositoryException e) {
214  0 throw new IllegalStateException("Can't get content-type from " + binary);
215    }
216  16 imageResponse.setMediaType(MediaType.parse(contentType));
217   
218  16 final OutputStream out = imageResponse.getOutputStream();
219  16 IOUtils.copy(in, out);
220  16 IOUtils.closeQuietly(in);
221    }
222   
 
223  3 toggle protected Property generateAndStore(final ImageGenerator<ParameterProvider<P>> generator, final ParameterProvider<P> parameterProvider) throws IOException, ImagingException {
224    // generate
225  3 final ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
226  3 delegate.serveImage(generator, parameterProvider, new TemporaryImageResponse(tempOut));
227   
228    // it's time to lock now, we can only save one node at a time, since we'll be working on the same nodes as other threads
229  3 lock.lock();
230  3 log.debug("Locked imaging JCR storage in order to store data corresponding to {}", parameterProvider);
231  3 try {
232  3 final OutputFormat outputFormat = generator.getOutputFormat(parameterProvider);
233  3 final MediaType contentType = getMediaType(outputFormat);
234   
235  3 final Session systemSession = systemContext.getJCRSession(session.getWorkspace().getName());
236    // create cachePath if needed
237  3 final String cachePath = cachingStrategy.getCachePath(generator, parameterProvider);
238  3 final Node cacheNode = NodeUtil.createPath(session.getRootNode(), cachePath, NodeTypes.Content.NAME);
239  3 final Node resourceNode = NodeUtil.createPath(cacheNode, GENERATED_IMAGE_NODE_NAME, NodeTypes.Resource.NAME);
240    // store generated image
241  3 final ByteArrayInputStream tempIn = new ByteArrayInputStream(tempOut.toByteArray());
242  3 Property imageData = resourceNode.setProperty(JcrConstants.JCR_DATA, cacheNode.getSession().getValueFactory().createBinary(tempIn));
243   
244  3 final String formatName = generator.getOutputFormat(parameterProvider).getFormatName();
245  3 final String mimeType = MIMEMapping.getMIMEType(formatName);
246  3 resourceNode.setProperty(JcrConstants.JCR_MIMETYPE, mimeType);
247  3 resourceNode.setProperty(JcrConstants.JCR_MIMETYPE, contentType.toString());
248  3 resourceNode.setProperty(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
249   
250    // Update metadata of the cache *after* a succesfull image generation (creationDate has been set when creating
251    // Since this might be called from a different thread than the actual request, we can't call cacheNode.updateMetaData(), which by default tries to set the authorId by using the current context
252  3 NodeTypes.LastModified.update(cacheNode);
253    // We need to explicitly update lastModified property of resourceNode
254    // when update is done to JcrConstants.JCR_DATA property, updating of lastMofified by MgnlPropertySettingContentDecorator is not invoked, because all JCR properties are ignored by info.magnolia.jcr.wrapper.MgnlPropertySettingContentDecorator#shouldIgnoreUpdate(final String propertyName)
255  3 NodeTypes.LastModified.update(resourceNode);
256   
257    // finally save it all
258  3 try {
259  3 systemSession.save();
260    } catch (InvalidItemStateException e){
261  0 log.warn("Item found in invalid state. Attempting to refresh the session.");
262  0 systemSession.refresh(false);
263  0 return systemSession.getNode(cachePath + "/" + GENERATED_IMAGE_NODE_NAME).getProperty(JcrConstants.JCR_DATA);
264    }
265   
266  3 return imageData;
267    } catch (RepositoryException e) {
268  0 throw new ImagingException("Can't store rendered image: " + e.getMessage(), e);
269    } finally {
270  3 log.debug("Unlocked after storing data for {}", parameterProvider);
271  3 lock.unlock();
272    }
273    }
274   
275    /**
276    * Unwrap ExecutionExceptions wrapping a RuntimeException wrapping an ImagingException or IOException,
277    * as thrown by the Function of the computing map.
278    *
279    * @see #currentJobs
280    */
 
281  0 toggle private void unwrapRuntimeException(Exception e) throws ImagingException, IOException {
282  0 final Throwable cause = e.getCause();
283  0 if (cause instanceof ImagingException) {
284  0 throw (ImagingException) cause;
285  0 } else if (cause instanceof IOException) {
286  0 throw (IOException) cause;
287  0 } else if (cause instanceof RuntimeException) {
288  0 unwrapRuntimeException((RuntimeException) cause);
289  0 } else if (cause == null) {
290    // This really, really, should not happen... but we'll let this exception bubble up
291  0 throw new IllegalStateException("Unexpected and unhandled exception: " + (e.getMessage() != null ? e.getMessage() : ""), e);
292    } else {
293    // this shouldn't happen either, actually.
294  0 throw new ImagingException(e.getMessage(), cause);
295    }
296    }
297   
 
298    private static class TemporaryImageResponse implements ImageResponse {
299    private final OutputStream tempOut;
300   
 
301  3 toggle public TemporaryImageResponse(OutputStream tempOut) {
302  3 this.tempOut = tempOut;
303    }
304   
 
305    toggle @Override
306    public void setMediaType(MediaType mediaType) throws IOException {
307    // Do nothing - in the case of CachingImageStreamer, we rely on the "real" ImageResponse to set the mime-type on the response
308    }
309   
 
310    toggle @Override
311    public OutputStream getOutputStream() throws IOException {
312    return tempOut;
313    }
314    }
315    }