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
60
61
62
63
64
65
66
67
68
69
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
78 public void setAttributeName(String attributeName) {
79 this.attributeName = attributeName;
80 }
81
82 @Override
83 public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
84
85 String paragraphUuid = getUuidOfParagraphToExecute();
86
87 if (paragraphUuid == null) {
88 chain.doFilter(request, response);
89 return;
90 }
91
92 Content content = getContent(paragraphUuid);
93
94 Content orgMainContent = null;
95 Content orgCurrentContent = null;
96
97 AggregationState state = getAggregationStateSafely();
98 if (state != null) {
99 orgMainContent = state.getMainContent();
100 orgCurrentContent = state.getCurrentContent();
101
102 state.setCurrentContent(content);
103
104 if (orgMainContent == null) {
105 state.setMainContent(content);
106 }
107 }
108 try {
109
110 Paragraph paragraph = getParagraph(content);
111
112 RenderingModelBasedRenderer renderingModelBasedRenderer = getRenderingModelBasedRenderer(paragraph);
113
114 RenderingModel renderingModel;
115 try {
116 renderingModel = renderingModelBasedRenderer.newModel(content, paragraph, null);
117 }
118 catch (RenderException e) {
119 throw new ServletException(e.getMessage(), e);
120 }
121
122 String actionResult;
123 if (renderingModel instanceof EarlyExecutionAware) {
124 actionResult = ((EarlyExecutionAware)renderingModel).executeEarly();
125 } else {
126 actionResult = renderingModel.execute();
127 }
128
129
130 if (response.isCommitted())
131 return;
132
133 if (handleActionResult(actionResult, request, response))
134 return;
135
136
137 MgnlContext.setAttribute(MODEL_ATTRIBUTE_PREFIX + paragraphUuid, renderingModel);
138 MgnlContext.setAttribute(ACTION_RESULT_ATTRIBUTE_PREFIX + paragraphUuid, actionResult);
139 try {
140 chain.doFilter(request, response);
141 } finally {
142 MgnlContext.removeAttribute(MODEL_ATTRIBUTE_PREFIX + paragraphUuid);
143 MgnlContext.removeAttribute(ACTION_RESULT_ATTRIBUTE_PREFIX + paragraphUuid);
144 }
145
146 } finally {
147 if (state != null) {
148 state.setMainContent(orgMainContent);
149 state.setCurrentContent(orgCurrentContent);
150 }
151 }
152 }
153
154 protected static AggregationState getAggregationStateSafely() {
155 if (MgnlContext.isWebContext()) {
156 return MgnlContext.getAggregationState();
157 }
158 return null;
159 }
160
161 protected String getUuidOfParagraphToExecute() {
162 return (String) MgnlContext.getInstance().getAttribute(attributeName);
163 }
164
165
166
167
168 protected Content getContent(String uuid) throws ServletException {
169
170 String repository = MgnlContext.getAggregationState().getRepository();
171 HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
172
173 try {
174 return hm.getContentByUUID(uuid);
175 } catch (RepositoryException e) {
176 throw new ServletException("Can't read content for paragraph: " + uuid, e);
177 }
178 }
179
180
181
182
183 protected Paragraph getParagraph(Content content) throws ServletException {
184
185 String templateName = content.getMetaData().getTemplate();
186
187 if (StringUtils.isEmpty(templateName)) {
188 throw new ServletException("No paragraph name set for paragraph with UUID: " + content.getUUID());
189 }
190
191 Paragraph paragraph = ParagraphManager.getInstance().getParagraphDefinition(templateName);
192
193 if (paragraph == null) {
194 throw new ServletException("Paragraph does not exist: " + templateName);
195 }
196
197 return paragraph;
198 }
199
200
201
202
203
204
205 protected RenderingModelBasedRenderer getRenderingModelBasedRenderer(Paragraph paragraph) throws ServletException {
206
207 ParagraphRenderer renderer = ParagraphRendererManager.getInstance().getRenderer(paragraph.getType());
208
209 if (!(renderer instanceof RenderingModelBasedRenderer))
210 throw new ServletException("Renderer [" + paragraph.getName() + "] does not support RenderingModel");
211
212 return (RenderingModelBasedRenderer) renderer;
213 }
214
215
216
217
218 protected boolean handleActionResult(String actionResult, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
219
220 if (actionResult == null)
221 return false;
222
223 if (actionResult.equals(RenderingModel.SKIP_RENDERING))
224 return true;
225
226 if (RequestDispatchUtil.dispatch(actionResult, request, response))
227 return true;
228
229 return false;
230 }
231 }