KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > filter > RequestContextFilter


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */

16
17 package org.springframework.web.filter;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.springframework.context.i18n.LocaleContextHolder;
27 import org.springframework.web.context.request.RequestContextHolder;
28 import org.springframework.web.context.request.ServletRequestAttributes;
29
30 /**
31  * Servlet 2.3+ filter that exposes the request to the current thread,
32  * through both {@link org.springframework.context.i18n.LocaleContextHolder} and
33  * {@link RequestContextHolder}. To be registered as filter in <code>web.xml</code>.
34  *
35  * <p>Alternatively, Spring's {@link org.springframework.web.context.request.RequestContextListener}
36  * and Spring's {@link org.springframework.web.servlet.DispatcherServlet} also expose
37  * the same request context to the current thread.
38  *
39  * <p>This filter is mainly for use with third-party servlets, e.g. the JSF FacesServlet.
40  * Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.
41  *
42  * @author Juergen Hoeller
43  * @author Rod Johnson
44  * @since 2.0
45  * @see org.springframework.context.i18n.LocaleContextHolder
46  * @see org.springframework.web.context.request.RequestContextHolder
47  * @see org.springframework.web.context.request.RequestContextListener
48  * @see org.springframework.web.servlet.DispatcherServlet
49  */

50 public class RequestContextFilter extends OncePerRequestFilter {
51
52     protected void doFilterInternal(
53             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
54             throws ServletException JavaDoc, IOException JavaDoc {
55
56         ServletRequestAttributes attributes = new ServletRequestAttributes(request);
57         LocaleContextHolder.setLocale(request.getLocale());
58         RequestContextHolder.setRequestAttributes(attributes);
59         if (logger.isDebugEnabled()) {
60             logger.debug("Bound request context to thread: " + request);
61         }
62         try {
63             filterChain.doFilter(request, response);
64         }
65         finally {
66             RequestContextHolder.setRequestAttributes(null);
67             LocaleContextHolder.setLocale(null);
68             attributes.requestCompleted();
69             if (logger.isDebugEnabled()) {
70                 logger.debug("Cleared thread-bound request context: " + request);
71             }
72         }
73     }
74
75 }
76
Popular Tags