/
GrailsDispatcherServlet.java
548 lines (492 loc) · 25.5 KB
/
GrailsDispatcherServlet.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.web.servlet;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.groovy.grails.commons.BootstrapArtefactHandler;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.GrailsBootstrapClass;
import org.codehaus.groovy.grails.commons.GrailsClass;
import org.codehaus.groovy.grails.commons.GrailsClassUtils;
import org.codehaus.groovy.grails.commons.spring.GrailsApplicationContext;
import org.codehaus.groovy.grails.exceptions.DefaultStackTraceFilterer;
import org.codehaus.groovy.grails.exceptions.StackTraceFilterer;
import org.codehaus.groovy.grails.lifecycle.ShutdownOperations;
import org.codehaus.groovy.grails.web.context.GrailsConfigUtils;
import org.codehaus.groovy.grails.web.context.ServletContextHolder;
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
import org.codehaus.groovy.grails.web.sitemesh.GrailsContentBufferingResponse;
import org.codehaus.groovy.grails.web.sitemesh.GroovyPageLayoutFinder;
import org.codehaus.groovy.grails.web.util.WebUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.access.BootstrapException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.util.NestedServletException;
import com.opensymphony.module.sitemesh.Decorator;
import com.opensymphony.sitemesh.Content;
import com.opensymphony.sitemesh.compatability.OldDecorator2NewDecorator;
import com.opensymphony.sitemesh.webapp.SiteMeshWebAppContext;
/**
* Handles incoming requests for Grails.
* <p/>
* Loads the Spring configuration based on the Grails application
* in the parent application context.
*
* @author Steven Devijver
* @author Graeme Rocher
*
* @since Jul 2, 2005
*/
public class GrailsDispatcherServlet extends DispatcherServlet {
private static final long serialVersionUID = 8295472557856192662L;
private static final String EXCEPTION_ATTRIBUTE = "exception";
private GrailsApplication application;
private LocaleResolver localeResolver;
private StackTraceFilterer stackFilterer;
protected HandlerInterceptor[] interceptors;
protected MultipartResolver multipartResolver;
protected ViewResolver layoutViewResolver;
/**
* Constructor.
*/
public GrailsDispatcherServlet() {
setDetectAllHandlerMappings(true);
}
@Override
protected void initFrameworkServlet() throws ServletException, BeansException {
super.initFrameworkServlet();
initMultipartResolver();
}
/**
* Initialize the MultipartResolver used by this class.
* If no bean is defined with the given name in the BeanFactory
* for this namespace, no multipart handling is provided.
*
* @throws org.springframework.beans.BeansException Thrown if there is an error initializing the mutlipartResolver
*/
private void initMultipartResolver() throws BeansException {
try {
multipartResolver = getWebApplicationContext().getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);
if (logger.isInfoEnabled()) {
logger.info("Using MultipartResolver [" + multipartResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// Default is no multipart resolver.
multipartResolver = null;
if (logger.isInfoEnabled()) {
logger.info("Unable to locate MultipartResolver with name '" + MULTIPART_RESOLVER_BEAN_NAME +
"': no multipart request handling provided");
}
}
}
@Override
protected void initStrategies(ApplicationContext context) {
super.initStrategies(context);
initLocaleResolver(context);
layoutViewResolver = WebUtils.lookupViewResolver(context);
}
// copied from base class since it's private
private void initLocaleResolver(ApplicationContext context) {
try {
localeResolver = context.getBean(LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Using LocaleResolver [" + localeResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// We need to use the default.
localeResolver = getDefaultStrategy(context, LocaleResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate LocaleResolver with name '" + LOCALE_RESOLVER_BEAN_NAME +
"': using default [" + localeResolver + "]");
}
}
}
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException {
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
// construct the SpringConfig for the container managed application
Assert.notNull(parent, "Grails requires a parent ApplicationContext, is the /WEB-INF/applicationContext.xml file missing?");
setApplication(parent.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class));
ServletContextHolder.setServletContext(getServletContext());
WebApplicationContext webContext;
if (wac instanceof GrailsApplicationContext) {
webContext = wac;
}
else {
webContext = GrailsConfigUtils.configureWebApplicationContext(getServletContext(), parent);
try {
GrailsConfigUtils.executeGrailsBootstraps(application, webContext, getServletContext());
}
catch (Exception e) {
if (e instanceof BeansException) {
throw (BeansException)e;
}
throw new BootstrapException("Error executing bootstraps", e);
}
}
interceptors = establishInterceptors(webContext);
return webContext;
}
/**
* Evalutes the given WebApplicationContext for all HandlerInterceptor and WebRequestInterceptor instances
*
* @param webContext The WebApplicationContext
* @return An array of HandlerInterceptor instances
*/
protected HandlerInterceptor[] establishInterceptors(WebApplicationContext webContext) {
String[] interceptorNames = webContext.getBeanNamesForType(HandlerInterceptor.class);
String[] webRequestInterceptors = webContext.getBeanNamesForType(WebRequestInterceptor.class);
HandlerInterceptor[] interceptors = new HandlerInterceptor[interceptorNames.length + webRequestInterceptors.length];
// Merge the handler and web request interceptors into a single array. Note that we
// start with the web request interceptors to ensure that the OpenSessionInViewInterceptor
// (which is a web request interceptor) is invoked before the user-defined filters
// (which are attached to a handler interceptor). This should ensure that the Hibernate
// session is in the proper state if and when users access the database within their filters.
int j = 0;
for (String webRequestInterceptor : webRequestInterceptors) {
interceptors[j++] = new WebRequestHandlerInterceptorAdapter(
(WebRequestInterceptor) webContext.getBean(webRequestInterceptor));
}
for (String interceptorName : interceptorNames) {
interceptors[j++] = (HandlerInterceptor) webContext.getBean(interceptorName);
}
return interceptors;
}
@Override
public void destroy() {
WebApplicationContext webContext = getWebApplicationContext();
GrailsApplication grailsApplication = webContext.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);
GrailsClass[] bootstraps = grailsApplication.getArtefacts(BootstrapArtefactHandler.TYPE);
for (GrailsClass bootstrap : bootstraps) {
((GrailsBootstrapClass) bootstrap).callDestroy();
}
try {
super.destroy();
} finally {
ShutdownOperations.runOperations();
}
}
/**
* Dependency injection for the application.
* @param application the application
*/
public void setApplication(GrailsApplication application) {
this.application = application;
createStackTraceFilterer();
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.DispatcherServlet#doDispatch(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doDispatch(final HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, localeResolver);
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
int interceptorIndex = -1;
// Expose current LocaleResolver and request as LocaleContext.
LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
LocaleContextHolder.setLocaleContext(new LocaleContext() {
public Locale getLocale() {
return localeResolver.resolveLocale(request);
}
});
// If the request is an include we need to try to use the original wrapped sitemesh
// response, otherwise layouts won't work properly
if (WebUtils.isIncludeRequest(request)) {
response = useWrappedOrOriginalResponse(response);
}
GrailsWebRequest requestAttributes = null;
RequestAttributes previousRequestAttributes = null;
Exception handlerException = null;
boolean isAsyncRequest = processedRequest.getAttribute("javax.servlet.async.request_uri") != null;
try {
ModelAndView mv;
boolean errorView = false;
try {
Object exceptionAttribute = request.getAttribute(EXCEPTION_ATTRIBUTE);
// only process multipart requests if an exception hasn't occured
if (exceptionAttribute == null) {
processedRequest = checkMultipart(request);
}
// Expose current RequestAttributes to current thread.
previousRequestAttributes = RequestContextHolder.currentRequestAttributes();
GrailsWebRequest previousWebRequest = GrailsWebRequest.lookup(request);
requestAttributes = new GrailsWebRequest(processedRequest, response, getServletContext());
if( previousWebRequest != null) {
copyParamsFromPreviousRequest(previousWebRequest, requestAttributes);
}
// Update the current web request.
WebUtils.storeGrailsWebRequest(requestAttributes);
if (logger.isDebugEnabled()) {
logger.debug("Bound request context to thread: " + request);
logger.debug("Using response object: " + response.getClass());
}
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Apply preHandle methods of registered interceptors.
if (mappedHandler.getInterceptors() != null) {
for (int i = 0; i < mappedHandler.getInterceptors().length; i++) {
HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
return;
}
interceptorIndex = i;
}
}
// if this is an async request that has been resumed, then don't execute the action again instead try get the model and view and continue
if (isAsyncRequest) {
Object modelAndViewO = processedRequest.getAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW);
if (modelAndViewO != null) {
mv = (ModelAndView) modelAndViewO;
}
else {
mv = null;
}
}else {
// Actually invoke the handler.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// if an async request was started simply return
if (processedRequest.getAttribute(GrailsApplicationAttributes.ASYNC_STARTED) != null) {
processedRequest.setAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, mv);
return;
}
// Do we need view name translation?
if ((ha instanceof AnnotationMethodHandlerAdapter || ha instanceof RequestMappingHandlerAdapter) && mv != null && !mv.hasView()) {
mv.setViewName(getDefaultViewName(request));
}
}
// Apply postHandle methods of registered interceptors.
if (mappedHandler.getInterceptors() != null) {
for (int i = mappedHandler.getInterceptors().length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
}
}
}
catch (ModelAndViewDefiningException ex) {
handlerException = ex;
if (logger.isDebugEnabled()) {
logger.debug("ModelAndViewDefiningException encountered", ex);
}
mv = ex.getModelAndView();
}
catch (Exception ex) {
handlerException = ex;
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
mv = processHandlerException(request, response, handler, ex);
errorView = (mv != null);
}
// Did the handler return a view to render?
if (mv != null && !mv.wasCleared()) {
// If an exception occurs in here, like a bad closing tag,
// we have nothing to render.
try {
render(mv, processedRequest, response);
if (isAsyncRequest && (response instanceof GrailsContentBufferingResponse)) {
GroovyPageLayoutFinder groovyPageLayoutFinder = getWebApplicationContext().getBean("groovyPageLayoutFinder", GroovyPageLayoutFinder.class);
GrailsContentBufferingResponse bufferingResponse = (GrailsContentBufferingResponse) response;
HttpServletResponse targetResponse = bufferingResponse.getTargetResponse();
Content content = bufferingResponse.getContent();
if (content != null) {
Decorator decorator = groovyPageLayoutFinder.findLayout(request, content);
SiteMeshWebAppContext webAppContext = new SiteMeshWebAppContext(request, targetResponse, getServletContext());
if (decorator != null) {
if (decorator instanceof com.opensymphony.sitemesh.Decorator) {
((com.opensymphony.sitemesh.Decorator)decorator).render(content, webAppContext);
} else {
new OldDecorator2NewDecorator(decorator).render(content, webAppContext);
}
} else {
content.writeOriginal(targetResponse.getWriter());
}
}
}
if (errorView) {
WebUtils.clearErrorRequestAttributes(request);
}
} catch (Exception e) {
// Only render the error view if we're not already trying to render it.
// This prevents a recursion if the error page itself has errors.
if (request.getAttribute(GrailsApplicationAttributes.RENDERING_ERROR_ATTRIBUTE) == null) {
request.setAttribute(GrailsApplicationAttributes.RENDERING_ERROR_ATTRIBUTE, Boolean.TRUE);
mv = super.processHandlerException(processedRequest, response, mappedHandler, e);
handlerException = e;
if (mv != null) render(mv, processedRequest, response);
}
else {
request.removeAttribute(GrailsApplicationAttributes.RENDERING_ERROR_ATTRIBUTE);
logger.warn("Recursive rendering of error view detected.", e);
try {
response.setContentType("text/plain");
response.getWriter().write("Internal server error");
response.flushBuffer();
} catch (Exception e2) {
logger.error("Internal server error - problem rendering error view", e2);
}
requestAttributes.setRenderView(false);
return;
}
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Null ModelAndView returned to DispatcherServlet with name '" +
getServletName() + "': assuming HandlerAdapter completed request handling");
}
}
// Trigger after-completion for successful outcome.
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, handlerException);
}
catch (Exception ex) {
// Trigger after-completion for thrown exception.
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
throw ex;
}
catch (Error err) {
ServletException ex = new NestedServletException("Handler processing failed", err);
// Trigger after-completion for thrown exception.
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
throw ex;
}
finally {
// Clean up any resources used by a multipart request.
if (processedRequest instanceof MultipartHttpServletRequest) {
if (multipartResolver != null) {
multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
}
}
request.removeAttribute(MultipartHttpServletRequest.class.getName());
// Reset thread-bound holders
if (requestAttributes != null) {
requestAttributes.requestCompleted();
if (previousRequestAttributes instanceof GrailsWebRequest) {
WebUtils.storeGrailsWebRequest((GrailsWebRequest) previousRequestAttributes);
}
else {
RequestContextHolder.setRequestAttributes(previousRequestAttributes);
}
}
LocaleContextHolder.setLocaleContext(previousLocaleContext);
if (logger.isDebugEnabled()) {
logger.debug("Cleared thread-bound request context: " + request);
}
}
}
protected HttpServletResponse useWrappedOrOriginalResponse(HttpServletResponse response) {
HttpServletResponse r = WrappedResponseHolder.getWrappedResponse();
return r == null ? response : r;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void copyParamsFromPreviousRequest(RequestAttributes previousRequestAttributes, GrailsWebRequest requestAttributes) {
if (!(previousRequestAttributes instanceof GrailsWebRequest)) {
return;
}
Map previousParams = ((GrailsWebRequest)previousRequestAttributes).getParams();
Map params = requestAttributes.getParams();
for (Object o : previousParams.keySet()) {
String name = (String) o;
params.put(name, previousParams.get(name));
}
}
/**
* Trigger afterCompletion callbacks on the mapped HandlerInterceptors.
* Will just invoke afterCompletion for all interceptors whose preHandle
* invocation has successfully completed and returned true.
* @param mappedHandler the mapped HandlerExecutionChain
* @param interceptorIndex index of last interceptor that successfully completed
* @param ex Exception thrown on handler execution, or <code>null</code> if none
* @see HandlerInterceptor#afterCompletion
*/
protected void triggerAfterCompletion(
HandlerExecutionChain mappedHandler, int interceptorIndex,
HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {
if (mappedHandler == null || mappedHandler.getInterceptors() == null) {
return;
}
// Apply afterCompletion methods of registered interceptors.
for (int i = interceptorIndex; i >= 0; i--) {
HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
try {
interceptor.afterCompletion(request, response, mappedHandler.getHandler(), ex);
}
catch (Throwable e) {
stackFilterer.filter(e, true);
logger.error("HandlerInterceptor.afterCompletion threw exception", e);
}
}
}
/**
* Convert the request into a multipart request.
* If no multipart resolver is set, simply use the existing request.
* @param request current HTTP request
* @return the processed request (multipart wrapper if necessary)
*/
@Override
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
// Lookup from request attribute. The resolver that handles MultiPartRequest is dealt with earlier inside DefaultUrlMappingInfo with Grails
HttpServletRequest resolvedRequest = (HttpServletRequest) request.getAttribute(MultipartHttpServletRequest.class.getName());
if (resolvedRequest != null) return resolvedRequest;
return request;
}
private void createStackTraceFilterer() {
try {
stackFilterer = (StackTraceFilterer)GrailsClassUtils.instantiateFromFlatConfig(
application.getFlatConfig(), "grails.logging.stackTraceFiltererClass", DefaultStackTraceFilterer.class.getName());
}
catch (Throwable t) {
logger.error("Problem instantiating StackTraceFilterer class, using default: " + t.getMessage());
stackFilterer = new DefaultStackTraceFilterer();
}
}
@Override
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
return super.getHandler(request);
}
}