01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20:
21: import javax.servlet.FilterChain;
22: import javax.servlet.ServletException;
23: import javax.servlet.http.HttpServletRequest;
24: import javax.servlet.http.HttpServletResponse;
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(HttpServletRequest request,
53: HttpServletResponse response, FilterChain filterChain)
54: throws ServletException, IOException {
55:
56: ServletRequestAttributes attributes = new ServletRequestAttributes(
57: request);
58: LocaleContextHolder.setLocale(request.getLocale());
59: RequestContextHolder.setRequestAttributes(attributes);
60: if (logger.isDebugEnabled()) {
61: logger.debug("Bound request context to thread: " + request);
62: }
63: try {
64: filterChain.doFilter(request, response);
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: "
71: + request);
72: }
73: }
74: }
75:
76: }
|