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 java.io.IOException; |
37 |
|
import javax.jcr.RepositoryException; |
38 |
|
import javax.servlet.FilterChain; |
39 |
|
import javax.servlet.ServletException; |
40 |
|
import javax.servlet.http.HttpServletRequest; |
41 |
|
import javax.servlet.http.HttpServletResponse; |
42 |
|
|
43 |
|
import org.apache.commons.lang.StringUtils; |
44 |
|
|
45 |
|
import info.magnolia.cms.core.AggregationState; |
46 |
|
import info.magnolia.cms.core.Content; |
47 |
|
import info.magnolia.cms.core.HierarchyManager; |
48 |
|
import info.magnolia.cms.filters.OncePerRequestAbstractMgnlFilter; |
49 |
|
import info.magnolia.cms.util.RequestDispatchUtil; |
50 |
|
import info.magnolia.context.MgnlContext; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@link |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@author |
66 |
|
@see |
67 |
|
@see |
68 |
|
@see |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 102 (102) |
Complexity: 24 |
Complexity Density: 0.36 |
|
70 |
|
public class ModelExecutionFilter extends OncePerRequestAbstractMgnlFilter { |
71 |
|
|
72 |
|
public static final String MODEL_ATTRIBUTE_PREFIX = ModelExecutionFilter.class.getName() + "-model-"; |
73 |
|
public static final String ACTION_RESULT_ATTRIBUTE_PREFIX = ModelExecutionFilter.class.getName() + "-actionResult-"; |
74 |
|
public static final String DEFAULT_MODEL_EXECUTION_ATTRIBUTE_NAME = "mgnlModelExecutionUUID"; |
75 |
|
|
76 |
|
private String attributeName = DEFAULT_MODEL_EXECUTION_ATTRIBUTE_NAME; |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
0
|
public void setAttributeName(String attributeName) {... |
79 |
0
|
this.attributeName = attributeName; |
80 |
|
} |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 52 (52) |
Complexity: 9 |
Complexity Density: 0.24 |
|
82 |
0
|
@Override... |
83 |
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
84 |
|
|
85 |
0
|
String paragraphUuid = getUuidOfParagraphToExecute(); |
86 |
|
|
87 |
0
|
if (paragraphUuid == null) { |
88 |
0
|
chain.doFilter(request, response); |
89 |
0
|
return; |
90 |
|
} |
91 |
|
|
92 |
0
|
Content content = getContent(paragraphUuid); |
93 |
|
|
94 |
0
|
Content orgMainContent = null; |
95 |
0
|
Content orgCurrentContent = null; |
96 |
|
|
97 |
0
|
AggregationState state = getAggregationStateSafely(); |
98 |
0
|
if (state != null) { |
99 |
0
|
orgMainContent = state.getMainContent(); |
100 |
0
|
orgCurrentContent = state.getCurrentContent(); |
101 |
|
|
102 |
0
|
state.setCurrentContent(content); |
103 |
|
|
104 |
0
|
if (orgMainContent == null) { |
105 |
0
|
state.setMainContent(content); |
106 |
|
} |
107 |
|
} |
108 |
0
|
try { |
109 |
|
|
110 |
0
|
Paragraph paragraph = getParagraph(content); |
111 |
|
|
112 |
0
|
RenderingModelBasedRenderer renderingModelBasedRenderer = getRenderingModelBasedRenderer(paragraph); |
113 |
|
|
114 |
0
|
RenderingModel renderingModel; |
115 |
0
|
try { |
116 |
0
|
renderingModel = renderingModelBasedRenderer.newModel(content, paragraph, null); |
117 |
|
} |
118 |
|
catch (RenderException e) { |
119 |
0
|
throw new ServletException(e.getMessage(), e); |
120 |
|
} |
121 |
|
|
122 |
0
|
String actionResult; |
123 |
0
|
if (renderingModel instanceof EarlyExecutionAware) { |
124 |
0
|
actionResult = ((EarlyExecutionAware)renderingModel).executeEarly(); |
125 |
|
} else { |
126 |
0
|
actionResult = renderingModel.execute(); |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
0
|
if (response.isCommitted()) |
131 |
0
|
return; |
132 |
|
|
133 |
0
|
if (handleActionResult(actionResult, request, response)) |
134 |
0
|
return; |
135 |
|
|
136 |
|
|
137 |
0
|
MgnlContext.setAttribute(MODEL_ATTRIBUTE_PREFIX + paragraphUuid, renderingModel); |
138 |
0
|
MgnlContext.setAttribute(ACTION_RESULT_ATTRIBUTE_PREFIX + paragraphUuid, actionResult); |
139 |
0
|
try { |
140 |
0
|
chain.doFilter(request, response); |
141 |
|
} finally { |
142 |
0
|
MgnlContext.removeAttribute(MODEL_ATTRIBUTE_PREFIX + paragraphUuid); |
143 |
0
|
MgnlContext.removeAttribute(ACTION_RESULT_ATTRIBUTE_PREFIX + paragraphUuid); |
144 |
|
} |
145 |
|
|
146 |
|
} finally { |
147 |
0
|
if (state != null) { |
148 |
0
|
state.setMainContent(orgMainContent); |
149 |
0
|
state.setCurrentContent(orgCurrentContent); |
150 |
|
} |
151 |
|
} |
152 |
|
} |
153 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
154 |
0
|
protected static AggregationState getAggregationStateSafely() {... |
155 |
0
|
if (MgnlContext.isWebContext()) { |
156 |
0
|
return MgnlContext.getAggregationState(); |
157 |
|
} |
158 |
0
|
return null; |
159 |
|
} |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
0
|
protected String getUuidOfParagraphToExecute() {... |
162 |
0
|
return (String) MgnlContext.getInstance().getAttribute(attributeName); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
168 |
0
|
protected Content getContent(String uuid) throws ServletException {... |
169 |
|
|
170 |
0
|
String repository = MgnlContext.getAggregationState().getRepository(); |
171 |
0
|
HierarchyManager hm = MgnlContext.getHierarchyManager(repository); |
172 |
|
|
173 |
0
|
try { |
174 |
0
|
return hm.getContentByUUID(uuid); |
175 |
|
} catch (RepositoryException e) { |
176 |
0
|
throw new ServletException("Can't read content for paragraph: " + uuid, e); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
183 |
0
|
protected Paragraph getParagraph(Content content) throws ServletException {... |
184 |
|
|
185 |
0
|
String templateName = content.getMetaData().getTemplate(); |
186 |
|
|
187 |
0
|
if (StringUtils.isEmpty(templateName)) { |
188 |
0
|
throw new ServletException("No paragraph name set for paragraph with UUID: " + content.getUUID()); |
189 |
|
} |
190 |
|
|
191 |
0
|
Paragraph paragraph = ParagraphManager.getInstance().getParagraphDefinition(templateName); |
192 |
|
|
193 |
0
|
if (paragraph == null) { |
194 |
0
|
throw new ServletException("Paragraph does not exist: " + templateName); |
195 |
|
} |
196 |
|
|
197 |
0
|
return paragraph; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
@throws |
203 |
|
@throws |
204 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
205 |
0
|
protected RenderingModelBasedRenderer getRenderingModelBasedRenderer(Paragraph paragraph) throws ServletException {... |
206 |
|
|
207 |
0
|
ParagraphRenderer renderer = ParagraphRendererManager.getInstance().getRenderer(paragraph.getType()); |
208 |
|
|
209 |
0
|
if (!(renderer instanceof RenderingModelBasedRenderer)) |
210 |
0
|
throw new ServletException("Renderer [" + paragraph.getName() + "] does not support RenderingModel"); |
211 |
|
|
212 |
0
|
return (RenderingModelBasedRenderer) renderer; |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
218 |
0
|
protected boolean handleActionResult(String actionResult, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {... |
219 |
|
|
220 |
0
|
if (actionResult == null) |
221 |
0
|
return false; |
222 |
|
|
223 |
0
|
if (actionResult.equals(RenderingModel.SKIP_RENDERING)) |
224 |
0
|
return true; |
225 |
|
|
226 |
0
|
if (RequestDispatchUtil.dispatch(actionResult, request, response)) |
227 |
0
|
return true; |
228 |
|
|
229 |
0
|
return false; |
230 |
|
} |
231 |
|
} |