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.freemarker.FreemarkerHelper; |
39 |
|
import info.magnolia.jcr.util.NodeUtil; |
40 |
|
import info.magnolia.jcr.util.PropertyUtil; |
41 |
|
import info.magnolia.jcr.wrapper.HTMLEscapingNodeWrapper; |
42 |
|
import info.magnolia.module.resources.ResourcesModule; |
43 |
|
import info.magnolia.module.resources.loaders.ResourceLoader; |
44 |
|
import info.magnolia.module.resources.templates.ResourceTemplate; |
45 |
|
import info.magnolia.objectfactory.Classes; |
46 |
|
import info.magnolia.objectfactory.guice.GuiceUtils; |
47 |
|
import info.magnolia.rendering.context.RenderingContext; |
48 |
|
import info.magnolia.rendering.engine.RenderException; |
49 |
|
import info.magnolia.rendering.engine.RenderingEngine; |
50 |
|
import info.magnolia.rendering.model.RenderingModel; |
51 |
|
import info.magnolia.rendering.renderer.FreemarkerRenderer; |
52 |
|
import info.magnolia.rendering.template.RenderableDefinition; |
53 |
|
import info.magnolia.rendering.util.AppendableWriter; |
54 |
|
|
55 |
|
import java.io.IOException; |
56 |
|
import java.io.InputStream; |
57 |
|
import java.io.Reader; |
58 |
|
import java.io.StringReader; |
59 |
|
import java.util.List; |
60 |
|
import java.util.Map; |
61 |
|
|
62 |
|
import javax.inject.Inject; |
63 |
|
import javax.inject.Provider; |
64 |
|
import javax.jcr.Node; |
65 |
|
import javax.jcr.RepositoryException; |
66 |
|
import javax.servlet.http.HttpServletResponse; |
67 |
|
|
68 |
|
import org.apache.commons.io.IOUtils; |
69 |
|
import org.apache.commons.lang3.StringUtils; |
70 |
|
import org.slf4j.Logger; |
71 |
|
import org.slf4j.LoggerFactory; |
72 |
|
|
73 |
|
import freemarker.template.TemplateException; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
|
|
| 78.8% |
Uncovered Elements: 18 (85) |
Complexity: 21 |
Complexity Density: 0.34 |
|
84 |
|
public class ResourcesTextTemplateRenderer extends FreemarkerRenderer { |
85 |
|
|
86 |
|
private static final Logger log = LoggerFactory.getLogger(ResourcesTextTemplateRenderer.class); |
87 |
|
private final Provider<ResourcesModule> resourcesModule; |
88 |
|
|
89 |
|
|
90 |
|
@deprecated@link |
91 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
92 |
0 |
@Deprecated... |
93 |
|
public ResourcesTextTemplateRenderer(FreemarkerHelper fmRenderer, RenderingEngine renderingEngine) { |
94 |
0 |
this(fmRenderer, renderingEngine, GuiceUtils.providerForInstance(ResourcesModule.getInstance())); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
97 |
8 |
@Inject... |
98 |
|
public ResourcesTextTemplateRenderer(FreemarkerHelper fmRenderer, RenderingEngine renderingEngine, Provider<ResourcesModule> resourcesModule) { |
99 |
8 |
super(fmRenderer, renderingEngine); |
100 |
8 |
this.resourcesModule = resourcesModule; |
101 |
|
} |
102 |
|
|
|
|
| 87.2% |
Uncovered Elements: 6 (47) |
Complexity: 10 |
Complexity Density: 0.27 |
|
103 |
8 |
@Override... |
104 |
|
protected void onRender(Node content, RenderableDefinition definition, RenderingContext renderingCtx, Map<String, Object> ctx, String templateScript) throws RenderException { |
105 |
|
|
106 |
8 |
ResourceTemplate resourceTemplate = (ResourceTemplate) definition; |
107 |
|
|
108 |
8 |
final HttpServletResponse response = MgnlContext.getWebContext().getResponse(); |
109 |
8 |
final String contentType = resourceTemplate.getContentType(); |
110 |
8 |
final boolean processed = resourceTemplate.isProcessed(); |
111 |
|
|
112 |
8 |
response.setContentType(contentType); |
113 |
|
|
114 |
8 |
content = NodeUtil.deepUnwrap(content, HTMLEscapingNodeWrapper.class); |
115 |
|
|
116 |
8 |
StringBuffer text; |
117 |
8 |
if (shouldBypass(content)) { |
118 |
|
|
119 |
|
|
120 |
4 |
if (processed) { |
121 |
1 |
super.onRender(content, definition, renderingCtx, ctx, templateScript); |
122 |
1 |
return; |
123 |
|
} |
124 |
|
|
125 |
3 |
InputStream in = null; |
126 |
3 |
List<ResourceLoader> loaders = resourcesModule.get().getResourceLoaders(); |
127 |
|
|
128 |
3 |
for (ResourceLoader loader : loaders) { |
129 |
4 |
try { |
130 |
4 |
in = loader.getStream(templateScript); |
131 |
3 |
if (in != null) { |
132 |
3 |
break; |
133 |
|
} |
134 |
|
} catch (IOException e) { |
135 |
1 |
log.debug("Can't load resource '{}' with ResourceLoader '{}'", templateScript, loader.getClass()); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
3 |
if (in == null) { |
140 |
0 |
throw new RenderException(String.format("Template '%s' not found.", templateScript)); |
141 |
|
} |
142 |
|
|
143 |
3 |
try { |
144 |
3 |
text = new StringBuffer(); |
145 |
3 |
text.append(IOUtils.toString(in)); |
146 |
|
} catch (IOException e) { |
147 |
0 |
throw new RenderException(String.format("Can't render resource '%s'.", templateScript), e); |
148 |
|
} finally { |
149 |
3 |
IOUtils.closeQuietly(in); |
150 |
|
} |
151 |
|
|
152 |
|
} else { |
153 |
4 |
text = new StringBuffer(); |
154 |
4 |
text.append(PropertyUtil.getString(content, "text")); |
155 |
|
} |
156 |
|
|
157 |
7 |
try { |
158 |
|
|
159 |
7 |
AppendableWriter out = renderingCtx.getAppendable(); |
160 |
|
|
161 |
7 |
if (processed) { |
162 |
1 |
Reader reader = new StringReader(text.toString()); |
163 |
1 |
try { |
164 |
1 |
this.getFmHelper().render(reader, ctx, out); |
165 |
|
} catch (TemplateException e) { |
166 |
0 |
throw new RenderException(e); |
167 |
|
} |
168 |
|
} else { |
169 |
6 |
out.write(text.toString()); |
170 |
|
} |
171 |
|
} catch (IOException e) { |
172 |
0 |
throw new RenderException("Can't render resource", e); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
|
|
| 77.8% |
Uncovered Elements: 4 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
180 |
8 |
@Override... |
181 |
|
protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, final String actionResult) { |
182 |
8 |
if (shouldBypass(content)) { |
183 |
4 |
try { |
184 |
4 |
String path; |
185 |
4 |
if (StringUtils.isNotBlank(MIMEMapping.getMIMEType(StringUtils.substringAfterLast(content.getPath(), ".")))) { |
186 |
0 |
return content.getPath(); |
187 |
|
} |
188 |
4 |
String extension = PropertyUtil.getString(content, "extension"); |
189 |
4 |
path = content.getPath(); |
190 |
4 |
if (extension != null) { |
191 |
4 |
path += "." + extension; |
192 |
|
} |
193 |
4 |
return path; |
194 |
|
} catch (RepositoryException re) { |
195 |
0 |
log.error("Not able to determineTemplatePath ", re); |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
4 |
return ""; |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
|
|
| 40% |
Uncovered Elements: 6 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
208 |
8 |
@Override... |
209 |
|
public RenderingModel<?> newModel(Node content, RenderableDefinition definition, RenderingModel<?> parentModel) throws RenderException { |
210 |
|
|
211 |
8 |
String modelClass = PropertyUtil.getString(content, "modelClass"); |
212 |
|
|
213 |
8 |
if (StringUtils.isEmpty(modelClass)) { |
214 |
8 |
return super.newModel(content, definition, parentModel); |
215 |
|
} |
216 |
|
|
217 |
0 |
Class<? extends RenderingModel<?>> clazz; |
218 |
0 |
try { |
219 |
0 |
clazz = Classes.getClassFactory().forName(modelClass); |
220 |
|
} catch (ClassNotFoundException e) { |
221 |
0 |
throw new RenderException("Could not create model.", e); |
222 |
|
} |
223 |
|
|
224 |
0 |
return super.newModel(clazz, content, definition, parentModel); |
225 |
|
} |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
227 |
16 |
protected boolean shouldBypass(Node content) {... |
228 |
16 |
return PropertyUtil.getBoolean(content, "bypass", false); |
229 |
|
} |
230 |
|
|
231 |
|
} |