GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / web / org / codehaus / groovy / grails / web / servlet / mvc / GrailsWebRequestFilter.java
blob7dd37af1be51b541b25919ba345ecfff5c738c98
1 /*
2 * Copyright 2004-2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.codehaus.groovy.grails.web.servlet.mvc;
18 import org.codehaus.groovy.grails.web.servlet.FlashScope;
19 import org.codehaus.groovy.grails.web.util.WebUtils;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.i18n.LocaleContextHolder;
22 import org.springframework.web.context.support.WebApplicationContextUtils;
23 import org.springframework.web.filter.OncePerRequestFilter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import java.io.IOException;
31 /**
32 * A filter that binds a GrailsWebRequest to the currently executing thread
34 * @author Graeme Rocher
35 * @since 0.4
38 public class GrailsWebRequestFilter extends OncePerRequestFilter {
40 /* (non-Javadoc)
41 * @see org.springframework.web.filter.OncePerRequestFilter#doFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
43 protected void doFilterInternal(
44 HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
45 throws ServletException, IOException {
47 LocaleContextHolder.setLocale(request.getLocale());
48 GrailsWebRequest webRequest = new GrailsWebRequest(request, response, getServletContext());
49 configureParameterCreationListeners(webRequest);
51 if (logger.isDebugEnabled()) {
52 logger.debug("Bound Grails request context to thread: " + request);
54 try {
55 WebUtils.storeGrailsWebRequest(webRequest);
57 // Set the flash scope instance to its next state. We do
58 // this here so that the flash is available from Grails
59 // filters in a valid state.
60 FlashScope fs = webRequest.getAttributes().getFlashScope(request);
61 fs.next();
63 // Pass control on to the next filter (or the servlet if
64 // there are no more filters in the chain).
65 filterChain.doFilter(request, response);
67 finally {
68 webRequest.requestCompleted();
69 WebUtils.clearGrailsWebRequest();
70 LocaleContextHolder.setLocale(null);
71 if (logger.isDebugEnabled()) {
72 logger.debug("Cleared Grails thread-bound request context: " + request);
77 private void configureParameterCreationListeners(GrailsWebRequest webRequest) {
78 ApplicationContext appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
79 String[] paramListenerBeans = appCtx.getBeanNamesForType(ParameterCreationListener.class);
80 for (int i = 0; i < paramListenerBeans.length; i++) {
81 ParameterCreationListener creationListenerBean = (ParameterCreationListener)appCtx.getBean(paramListenerBeans[i]);
82 webRequest.addParameterListener(creationListenerBean);