1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.tiles.web.util; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.util.Enumeration; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.servlet.Filter; |
29 | |
import javax.servlet.FilterChain; |
30 | |
import javax.servlet.FilterConfig; |
31 | |
import javax.servlet.ServletContext; |
32 | |
import javax.servlet.ServletException; |
33 | |
import javax.servlet.ServletResponse; |
34 | |
import javax.servlet.http.HttpServletRequest; |
35 | |
import javax.servlet.http.HttpServletResponse; |
36 | |
|
37 | |
import org.apache.tiles.Attribute; |
38 | |
import org.apache.tiles.AttributeContext; |
39 | |
import org.apache.tiles.TilesContainer; |
40 | |
import org.apache.tiles.access.TilesAccess; |
41 | |
import org.apache.tiles.request.ApplicationContext; |
42 | |
import org.apache.tiles.request.Request; |
43 | |
import org.apache.tiles.request.reflect.CannotInstantiateObjectException; |
44 | |
import org.apache.tiles.request.reflect.ClassUtil; |
45 | |
import org.apache.tiles.request.servlet.ServletRequest; |
46 | |
import org.slf4j.Logger; |
47 | |
import org.slf4j.LoggerFactory; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 1 | public class TilesDecorationFilter implements Filter { |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public static final String CONTAINER_KEY_INIT_PARAMETER = |
92 | |
"org.apache.tiles.web.util.TilesDecorationFilter.CONTAINER_KEY"; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 1 | private Logger log = LoggerFactory.getLogger(TilesDecorationFilter.class); |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
private FilterConfig filterConfig; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private String containerKey; |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | 1 | private String definitionAttributeName = "content"; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | 1 | private String definitionName = "layout"; |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private String preventDecorationToken; |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
private Map<String, String> alternateDefinitions; |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | 1 | private AttributeContextMutator mutator = null; |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
private ServletContext servletContext; |
143 | |
|
144 | |
|
145 | |
public void init(FilterConfig config) throws ServletException { |
146 | 1 | filterConfig = config; |
147 | 1 | servletContext = filterConfig.getServletContext(); |
148 | |
|
149 | 1 | containerKey = filterConfig |
150 | |
.getInitParameter(CONTAINER_KEY_INIT_PARAMETER); |
151 | |
|
152 | 1 | String temp = config.getInitParameter("attribute-name"); |
153 | 1 | if (temp != null) { |
154 | 1 | definitionAttributeName = temp; |
155 | |
} |
156 | |
|
157 | 1 | temp = config.getInitParameter("definition"); |
158 | 1 | if (temp != null) { |
159 | 1 | definitionName = temp; |
160 | |
} |
161 | |
|
162 | 1 | temp = config.getInitParameter("prevent-token"); |
163 | 1 | preventDecorationToken = "org.apache.tiles.decoration.PREVENT:" |
164 | |
+ (temp == null ? definitionName : temp); |
165 | |
|
166 | 1 | alternateDefinitions = parseAlternateDefinitions(); |
167 | |
|
168 | 1 | temp = config.getInitParameter("mutator"); |
169 | 1 | if (temp != null) { |
170 | |
try { |
171 | 1 | mutator = (AttributeContextMutator) ClassUtil.instantiate(temp); |
172 | 0 | } catch (CannotInstantiateObjectException e) { |
173 | 0 | throw new ServletException("Unable to instantiate specified context mutator.", e); |
174 | 1 | } |
175 | |
} else { |
176 | 0 | mutator = new DefaultMutator(); |
177 | |
} |
178 | 1 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
@SuppressWarnings("unchecked") |
187 | |
protected Map<String, String> parseAlternateDefinitions() { |
188 | 1 | Map<String, String> map = new HashMap<String, String>(); |
189 | 1 | Enumeration<String> e = filterConfig.getInitParameterNames(); |
190 | 2 | while (e.hasMoreElements()) { |
191 | 1 | String parm = e.nextElement(); |
192 | 1 | if (parm.startsWith("definition(") && parm.endsWith("*)")) { |
193 | 1 | String value = filterConfig.getInitParameter(parm); |
194 | 1 | String mask = parm.substring("definition(".length()); |
195 | 1 | mask = mask.substring(0, mask.lastIndexOf("*)")); |
196 | 1 | map.put(mask, value); |
197 | 1 | log.info("Mapping all requests matching '" + mask |
198 | |
+ "*' to definition '" + value + "'"); |
199 | |
} |
200 | 1 | } |
201 | 1 | return map; |
202 | |
} |
203 | |
|
204 | |
|
205 | |
public void destroy() { |
206 | 0 | filterConfig = null; |
207 | 0 | } |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public void doFilter(javax.servlet.ServletRequest req, ServletResponse res, FilterChain filterChain) |
214 | |
throws IOException, ServletException { |
215 | |
|
216 | |
|
217 | |
|
218 | 1 | if (!isPreventTokenPresent(req)) { |
219 | 1 | ApplicationContext applicationContext = org.apache.tiles.request.servlet.ServletUtil |
220 | |
.getApplicationContext(servletContext); |
221 | 1 | Request request = new ServletRequest(applicationContext, |
222 | |
(HttpServletRequest) req, (HttpServletResponse) res); |
223 | 1 | TilesContainer container = TilesAccess.getContainer(applicationContext, |
224 | |
containerKey); |
225 | 1 | mutator.mutate(container.getAttributeContext(request), req); |
226 | 1 | if (preventDecorationToken != null) { |
227 | 1 | req.setAttribute(preventDecorationToken, Boolean.TRUE); |
228 | |
} |
229 | 1 | String definitionName = getDefinitionForRequest(req); |
230 | 1 | container.render(definitionName, request); |
231 | |
} |
232 | 1 | filterChain.doFilter(req, res); |
233 | 1 | } |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
private String getDefinitionForRequest(javax.servlet.ServletRequest request) { |
242 | 1 | if (alternateDefinitions.size() < 1) { |
243 | 0 | return definitionName; |
244 | |
} |
245 | 1 | String base = getRequestBase(request); |
246 | 1 | for (Map.Entry<String, String> pair : alternateDefinitions.entrySet()) { |
247 | 1 | if (base.startsWith(pair.getKey())) { |
248 | 0 | return pair.getValue(); |
249 | |
} |
250 | 1 | } |
251 | 1 | return definitionName; |
252 | |
} |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
private String getRequestBase(javax.servlet.ServletRequest request) { |
262 | |
|
263 | 1 | String include = (String) request.getAttribute("javax.servlet.include.servlet_path"); |
264 | 1 | if (include != null) { |
265 | 0 | return include; |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | 1 | return ((HttpServletRequest) request).getServletPath(); |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | 0 | class DefaultMutator implements AttributeContextMutator { |
277 | |
|
278 | |
|
279 | |
public void mutate(AttributeContext ctx, javax.servlet.ServletRequest req) { |
280 | 0 | Attribute attr = new Attribute(); |
281 | 0 | attr.setRenderer("template"); |
282 | 0 | attr.setValue(getRequestBase(req)); |
283 | 0 | ctx.putAttribute(definitionAttributeName, attr); |
284 | 0 | } |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
private boolean isPreventTokenPresent(javax.servlet.ServletRequest request) { |
294 | 1 | return preventDecorationToken != null && request.getAttribute(preventDecorationToken) != null; |
295 | |
} |
296 | |
} |