Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.renderers

File ResourcesBinaryRenderer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
24% of files have more coverage

Code metrics

12
36
4
1
142
88
14
0.39
9
4
3.5

Classes

Class Line # Actions
ResourcesBinaryRenderer 65 36 0% 14 5
0.9038461490.4%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2011-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.module.resources.renderers;
35   
36    import info.magnolia.cms.beans.config.MIMEMapping;
37    import info.magnolia.context.MgnlContext;
38    import info.magnolia.jcr.util.PropertyUtil;
39    import info.magnolia.module.resources.ResourcesModule;
40    import info.magnolia.module.resources.loaders.ResourceLoader;
41    import info.magnolia.rendering.context.RenderingContext;
42    import info.magnolia.rendering.engine.RenderException;
43    import info.magnolia.rendering.renderer.Renderer;
44   
45    import java.io.IOException;
46    import java.io.InputStream;
47    import java.util.List;
48    import java.util.Map;
49   
50    import javax.inject.Inject;
51    import javax.inject.Provider;
52    import javax.jcr.Binary;
53    import javax.jcr.Node;
54    import javax.jcr.RepositoryException;
55    import javax.servlet.http.HttpServletResponse;
56   
57    import org.apache.commons.io.IOUtils;
58    import org.apache.jackrabbit.JcrConstants;
59    import org.slf4j.Logger;
60    import org.slf4j.LoggerFactory;
61   
62    /**
63    * Streams the binary data stored in the content or loads them from arbitrary path.
64    */
 
65    public class ResourcesBinaryRenderer implements Renderer {
66   
67    private static final Logger log = LoggerFactory.getLogger(ResourcesBinaryRenderer.class);
68   
69    protected static final String BINARY_NODE = "binary";
70    protected static final String BYPASS_PROPERTY = "bypass";
71    protected static final String EXTENSION_PROPERTY = "extension";
72   
73    private final Provider<ResourcesModule> resourcesModule;
74   
 
75  6 toggle @Inject
76    public ResourcesBinaryRenderer(Provider<ResourcesModule> resourcesModule) {
77  6 this.resourcesModule = resourcesModule;
78    }
79   
 
80  6 toggle @Override
81    public void render(RenderingContext ctx, Map<String, Object> contextObjects) throws RenderException {
82  6 Node content = ctx.getCurrentContent();
83  6 InputStream stream = null;
84  6 try {
85  6 stream = this.getInputStream(content);
86  6 final HttpServletResponse response = MgnlContext.getWebContext().getResponse();
87  6 response.setContentLength(stream.available());
88   
89  6 if (content.hasNode(BINARY_NODE) && content.getNode(BINARY_NODE).hasProperty(JcrConstants.JCR_MIMETYPE)) {
90  4 final String mimeType = content.getNode(BINARY_NODE).getProperty(JcrConstants.JCR_MIMETYPE).getString();
91  4 response.setContentType(mimeType);
92  2 } else if (content.hasProperty(EXTENSION_PROPERTY)) {
93  1 final String extension = content.getProperty(EXTENSION_PROPERTY).getString();
94  1 final String mimeType = MIMEMapping.getMIMEType(extension);
95  1 response.setContentType(mimeType);
96    }
97  6 IOUtils.copy(stream, ctx.getOutputStream());
98    } catch (RepositoryException e) {
99  0 throw new RenderException("Can't read binary data for resource " + content, e);
100    } catch (IOException e) {
101  0 throw new RenderException("Can't write binary data to the output stream.", e);
102    } finally {
103  6 IOUtils.closeQuietly(stream);
104    }
105    }
106   
 
107  6 toggle private InputStream getInputStream(Node content) throws RenderException, RepositoryException {
108  6 InputStream inputStream = null;
109   
110  6 if (shouldBypass(content)) {
111  5 String resourcePath = content.getPath();
112  5 String extension = content.hasProperty(EXTENSION_PROPERTY) ? "." + content.getProperty(EXTENSION_PROPERTY).getString() : "";
113  5 resourcePath += extension;
114   
115  5 List<ResourceLoader> loaders = resourcesModule.get().getResourceLoaders();
116  5 for (ResourceLoader loader : loaders) {
117  6 try {
118  6 inputStream = loader.getStream(resourcePath);
119  5 if (inputStream != null) {
120  5 break;
121    }
122    } catch (IOException e) {
123  1 log.debug("Can't load resource '{}' with ResourceLoader '{}'", resourcePath, loader.getClass());
124    }
125    }
126   
127  5 if (inputStream == null) {
128  0 throw new RenderException(String.format("Binary '%s' not found.", resourcePath));
129    }
130    } else {
131  1 Binary binary = content.getNode(BINARY_NODE).getProperty(JcrConstants.JCR_DATA).getBinary();
132  1 inputStream = binary.getStream();
133    }
134   
135  6 return inputStream;
136    }
137   
 
138  6 toggle protected boolean shouldBypass(Node content) {
139  6 return PropertyUtil.getBoolean(content, BYPASS_PROPERTY, false);
140    }
141   
142    }