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.context.request;
18:
19: import javax.servlet.ServletRequestEvent;
20: import javax.servlet.ServletRequestListener;
21: import javax.servlet.http.HttpServletRequest;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25:
26: import org.springframework.context.i18n.LocaleContextHolder;
27:
28: /**
29: * Servlet 2.4+ listener that exposes the request to the current thread,
30: * through both {@link org.springframework.context.i18n.LocaleContextHolder} and
31: * {@link RequestContextHolder}. To be registered as listener in <code>web.xml</code>.
32: *
33: * <p>Alternatively, Spring's {@link org.springframework.web.filter.RequestContextFilter}
34: * and Spring's {@link org.springframework.web.servlet.DispatcherServlet} also expose
35: * the same request context to the current thread.
36: *
37: * <p>This listener is mainly for use with third-party servlets, e.g. the JSF FacesServlet.
38: * Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.
39: *
40: * @author Juergen Hoeller
41: * @since 2.0
42: * @see javax.servlet.ServletRequestListener
43: * @see org.springframework.context.i18n.LocaleContextHolder
44: * @see org.springframework.web.context.request.RequestContextHolder
45: * @see org.springframework.web.filter.RequestContextFilter
46: * @see org.springframework.web.servlet.DispatcherServlet
47: */
48: public class RequestContextListener implements ServletRequestListener {
49:
50: private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class
51: .getName()
52: + ".REQUEST_ATTRIBUTES";
53:
54: /** Logger available to subclasses */
55: protected final Log logger = LogFactory.getLog(getClass());
56:
57: public void requestInitialized(ServletRequestEvent requestEvent) {
58: if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
59: throw new IllegalArgumentException(
60: "Request is not an HttpServletRequest: "
61: + requestEvent.getServletRequest());
62: }
63: HttpServletRequest request = (HttpServletRequest) requestEvent
64: .getServletRequest();
65: ServletRequestAttributes attributes = new ServletRequestAttributes(
66: request);
67: request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
68: LocaleContextHolder.setLocale(request.getLocale());
69: RequestContextHolder.setRequestAttributes(attributes);
70: if (logger.isDebugEnabled()) {
71: logger.debug("Bound request context to thread: " + request);
72: }
73: }
74:
75: public void requestDestroyed(ServletRequestEvent requestEvent) {
76: ServletRequestAttributes attributes = (ServletRequestAttributes) requestEvent
77: .getServletRequest().getAttribute(
78: REQUEST_ATTRIBUTES_ATTRIBUTE);
79: ServletRequestAttributes threadAttributes = (ServletRequestAttributes) RequestContextHolder
80: .getRequestAttributes();
81: if (threadAttributes != null) {
82: // We're assumably within the original request thread...
83: if (attributes == null) {
84: attributes = threadAttributes;
85: }
86: RequestContextHolder.setRequestAttributes(null);
87: LocaleContextHolder.setLocale(null);
88: }
89: if (attributes != null) {
90: attributes.requestCompleted();
91: if (logger.isDebugEnabled()) {
92: logger.debug("Cleared thread-bound request context: "
93: + requestEvent.getServletRequest());
94: }
95: }
96: }
97:
98: }
|