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.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 |
|
|
64 |
|
|
|
|
| 90.4% |
Uncovered Elements: 5 (52) |
Complexity: 14 |
Complexity Density: 0.39 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
6 |
@Inject... |
76 |
|
public ResourcesBinaryRenderer(Provider<ResourcesModule> resourcesModule) { |
77 |
6 |
this.resourcesModule = resourcesModule; |
78 |
|
} |
79 |
|
|
|
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 6 |
Complexity Density: 0.35 |
|
80 |
6 |
@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 |
|
|
|
|
| 88% |
Uncovered Elements: 3 (25) |
Complexity: 6 |
Complexity Density: 0.35 |
|
107 |
6 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
138 |
6 |
protected boolean shouldBypass(Node content) {... |
139 |
6 |
return PropertyUtil.getBoolean(content, BYPASS_PROPERTY, false); |
140 |
|
} |
141 |
|
|
142 |
|
} |