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.templating; |
35 |
|
|
36 |
|
import info.magnolia.cms.core.AggregationState; |
37 |
|
import info.magnolia.cms.core.Content; |
38 |
|
import info.magnolia.cms.core.HierarchyManager; |
39 |
|
import info.magnolia.cms.core.NodeData; |
40 |
|
import info.magnolia.cms.filters.AbstractMgnlFilter; |
41 |
|
import info.magnolia.cms.util.LazyInitPrintWriter; |
42 |
|
import info.magnolia.context.MgnlContext; |
43 |
|
import info.magnolia.module.templating.engine.RenderingEngine; |
44 |
|
import info.magnolia.objectfactory.Components; |
45 |
|
|
46 |
|
import java.io.IOException; |
47 |
|
import java.io.InputStream; |
48 |
|
|
49 |
|
import javax.jcr.PathNotFoundException; |
50 |
|
import javax.jcr.PropertyType; |
51 |
|
import javax.jcr.RepositoryException; |
52 |
|
import javax.jcr.Value; |
53 |
|
import javax.servlet.FilterChain; |
54 |
|
import javax.servlet.ServletException; |
55 |
|
import javax.servlet.ServletOutputStream; |
56 |
|
import javax.servlet.http.HttpServletRequest; |
57 |
|
import javax.servlet.http.HttpServletResponse; |
58 |
|
|
59 |
|
import org.apache.commons.io.IOUtils; |
60 |
|
import org.apache.commons.lang.StringUtils; |
61 |
|
import org.apache.commons.lang.math.NumberUtils; |
62 |
|
import org.slf4j.Logger; |
63 |
|
import org.slf4j.LoggerFactory; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@author |
71 |
|
@version |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 91 (91) |
Complexity: 24 |
Complexity Density: 0.38 |
|
73 |
|
public class RenderingFilter extends AbstractMgnlFilter { |
74 |
|
private static final Logger log = LoggerFactory.getLogger(RenderingFilter.class); |
75 |
|
|
76 |
|
protected RenderingEngine renderingEngine = Components.getSingleton(RenderingEngine.class); |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 8 |
Complexity Density: 0.4 |
|
78 |
0
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException{... |
79 |
0
|
final AggregationState aggregationState = MgnlContext.getAggregationState(); |
80 |
|
|
81 |
0
|
String templateName = aggregationState.getTemplateName(); |
82 |
0
|
if (StringUtils.isNotEmpty(templateName)) { |
83 |
0
|
try { |
84 |
|
|
85 |
|
|
86 |
0
|
if (response != MgnlContext.getWebContext().getResponse()) { |
87 |
0
|
log.warn("Context response not synced. This may lead to discrepancies in rendering."); |
88 |
|
} |
89 |
|
|
90 |
0
|
Content content = aggregationState.getMainContent(); |
91 |
|
|
92 |
0
|
render(content, templateName, response); |
93 |
|
|
94 |
0
|
try { |
95 |
0
|
response.flushBuffer(); |
96 |
|
} |
97 |
|
catch (IOException e) { |
98 |
|
|
99 |
|
|
100 |
0
|
log.debug("Exception flushing response " + e.getClass().getName() + ": " + e.getMessage(), e); |
101 |
|
} |
102 |
|
|
103 |
|
} |
104 |
|
catch (IOException e) { |
105 |
0
|
log.error(e.getMessage(), e); |
106 |
0
|
throw e; |
107 |
|
} |
108 |
|
catch (RenderException e) { |
109 |
0
|
log.error(e.getMessage(), e); |
110 |
0
|
throw new ServletException(e); |
111 |
|
} |
112 |
|
catch (Exception e) { |
113 |
|
|
114 |
0
|
log.error(e.getMessage(), e); |
115 |
0
|
if (!response.isCommitted()) { |
116 |
0
|
response.setContentType("text/html"); |
117 |
|
} |
118 |
0
|
throw new RuntimeException(e); |
119 |
|
} |
120 |
|
} |
121 |
|
else { |
122 |
|
|
123 |
0
|
handleResourceRequest(aggregationState, request, response); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
} |
130 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
131 |
0
|
protected void render(Content content, String templateName, HttpServletResponse response) throws IOException, RenderException {... |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
0
|
LazyInitPrintWriter out = new LazyInitPrintWriter(response); |
136 |
|
|
137 |
0
|
renderingEngine.render(content, templateName, out); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
@param |
144 |
|
@param |
145 |
|
@throws |
146 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 6 |
Complexity Density: 0.3 |
|
147 |
0
|
protected void handleResourceRequest(AggregationState aggregationState, HttpServletRequest request, HttpServletResponse response) throws IOException {... |
148 |
|
|
149 |
0
|
final String resourceHandle = aggregationState.getHandle(); |
150 |
|
|
151 |
0
|
log.debug("handleResourceRequest, resourceHandle=\"{}\"", resourceHandle); |
152 |
|
|
153 |
0
|
if (StringUtils.isNotEmpty(resourceHandle)) { |
154 |
|
|
155 |
0
|
HierarchyManager hm = MgnlContext.getHierarchyManager(aggregationState.getRepository()); |
156 |
|
|
157 |
0
|
InputStream is = null; |
158 |
0
|
try { |
159 |
0
|
is = getNodedataAstream(resourceHandle, hm, response); |
160 |
0
|
if (null != is) { |
161 |
|
|
162 |
|
|
163 |
0
|
sendUnCompressed(is, response); |
164 |
0
|
IOUtils.closeQuietly(is); |
165 |
0
|
return; |
166 |
|
} |
167 |
|
} |
168 |
|
catch (IOException e) { |
169 |
|
|
170 |
|
|
171 |
0
|
log.debug("Exception while dispatching resource " + e.getClass().getName() + ": " + e.getMessage(), e); |
172 |
0
|
return; |
173 |
|
} |
174 |
|
catch (Exception e) { |
175 |
0
|
log.error("Exception while dispatching resource " + e.getClass().getName() + ": " + e.getMessage(), e); |
176 |
0
|
return; |
177 |
|
} |
178 |
|
finally { |
179 |
0
|
IOUtils.closeQuietly(is); |
180 |
|
} |
181 |
|
} |
182 |
0
|
log.debug("Resource not found, redirecting request for [{}] to 404 URI", request.getRequestURI()); |
183 |
|
|
184 |
0
|
if (!response.isCommitted()) { |
185 |
0
|
response.sendError(HttpServletResponse.SC_NOT_FOUND); |
186 |
|
} |
187 |
|
else { |
188 |
0
|
log.info("Unable to redirect to 404 page for {}, response is already committed", request.getRequestURI()); |
189 |
|
} |
190 |
|
|
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
@param |
196 |
|
@param |
197 |
|
@throws |
198 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
199 |
0
|
private void sendUnCompressed(InputStream is, HttpServletResponse response) throws IOException {... |
200 |
0
|
ServletOutputStream os = response.getOutputStream(); |
201 |
0
|
byte[] buffer = new byte[8192]; |
202 |
0
|
int read; |
203 |
0
|
while ((read = is.read(buffer)) > 0) { |
204 |
0
|
os.write(buffer, 0, read); |
205 |
|
} |
206 |
0
|
os.flush(); |
207 |
0
|
IOUtils.closeQuietly(os); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
@param |
212 |
|
@param |
213 |
|
@param |
214 |
|
@return |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 7 |
Complexity Density: 0.47 |
|
216 |
0
|
private InputStream getNodedataAstream(String path, HierarchyManager hm, HttpServletResponse res) {... |
217 |
|
|
218 |
0
|
log.debug("getNodedataAstream for path \"{}\"", path); |
219 |
|
|
220 |
0
|
try { |
221 |
0
|
NodeData atom = hm.getNodeData(path); |
222 |
0
|
if (atom != null) { |
223 |
0
|
if (atom.getType() == PropertyType.BINARY) { |
224 |
|
|
225 |
0
|
String sizeString = atom.getAttribute("size"); |
226 |
0
|
if (NumberUtils.isNumber(sizeString)) { |
227 |
0
|
res.setContentLength(Integer.parseInt(sizeString)); |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
0
|
Value value = atom.getValue(); |
232 |
0
|
if (value != null) { |
233 |
0
|
return value.getStream(); |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
0
|
log.warn("Resource not found: [{}]", path); |
238 |
|
|
239 |
|
} |
240 |
|
catch (PathNotFoundException e) { |
241 |
0
|
log.warn("Resource not found: [{}]", path); |
242 |
|
} |
243 |
|
catch (RepositoryException e) { |
244 |
0
|
log.error("RepositoryException while reading Resource [" + path + "]", e); |
245 |
|
} |
246 |
0
|
return null; |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
} |