1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.core.container.ri.servlet;
22
23 import java.io.IOException;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.Locale;
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletContext;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpSession;
36 import org.jdtaus.core.container.ContainerFactory;
37 import org.jdtaus.core.container.Context;
38 import org.jdtaus.core.container.ContextFactory;
39 import org.jdtaus.core.container.ModelFactory;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class ServletFilter implements Filter
56 {
57
58
59
60 private static final ThreadLocal CONTEXTS = new ThreadLocal();
61
62
63 private static final ThreadLocal SESSIONS = new ThreadLocal();
64
65
66 private static final ThreadLocal LOCALES = new ThreadLocal();
67
68
69 private ServletContext servletContext;
70
71
72
73
74
75
76
77
78
79 public static ServletContext getServletContext()
80 {
81 final ServletContext context = (ServletContext) CONTEXTS.get();
82
83 if ( context == null )
84 {
85 throw new ContextLostException( getLocale(),
86 Thread.currentThread() );
87
88 }
89
90 return context;
91 }
92
93
94
95
96
97
98
99
100
101
102 public static HttpSession getHttpSession()
103 {
104 final HttpSession session = (HttpSession) SESSIONS.get();
105
106 if ( session == null )
107 {
108 throw new SessionLostException( getLocale(),
109 Thread.currentThread() );
110
111 }
112
113 return session;
114 }
115
116
117
118
119
120
121 public static Locale getLocale()
122 {
123 Locale locale = (Locale) LOCALES.get();
124 return locale == null ? Locale.getDefault() : locale;
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public void init( FilterConfig filterConfig ) throws ServletException
138 {
139 this.servletContext = filterConfig.getServletContext();
140
141
142 if ( System.getProperty( ContainerFactory.class.getName() ) == null )
143 {
144 System.setProperty( ContainerFactory.class.getName(),
145 ServletContextFactories.class.getName() );
146
147 filterConfig.getServletContext().log( this.getSystemPropertyMessage(
148 Locale.getDefault(), ContainerFactory.class.getName(),
149 ServletContextFactories.class.getName() ) );
150
151 }
152 if ( System.getProperty( ContextFactory.class.getName() ) == null )
153 {
154 System.setProperty( ContextFactory.class.getName(),
155 ServletContextFactories.class.getName() );
156
157 filterConfig.getServletContext().log( this.getSystemPropertyMessage(
158 Locale.getDefault(), ContextFactory.class.getName(),
159 ServletContextFactories.class.getName() ) );
160
161 }
162 if ( System.getProperty( ModelFactory.class.getName() ) == null )
163 {
164 System.setProperty( ModelFactory.class.getName(),
165 ServletContextFactories.class.getName() );
166
167 filterConfig.getServletContext().log( this.getSystemPropertyMessage(
168 Locale.getDefault(), ModelFactory.class.getName(),
169 ServletContextFactories.class.getName() ) );
170
171 }
172 if ( System.getProperty( Context.class.getName() ) == null )
173 {
174 System.setProperty( Context.class.getName(),
175 HttpSessionContext.class.getName() );
176
177 filterConfig.getServletContext().log( this.getSystemPropertyMessage(
178 Locale.getDefault(), Context.class.getName(),
179 ServletContextFactories.class.getName() ) );
180
181 }
182 }
183
184
185
186
187
188
189
190
191 public void doFilter( final ServletRequest req, final ServletResponse rsp,
192 final FilterChain chain )
193 throws IOException, ServletException
194 {
195 try
196 {
197 if ( req instanceof HttpServletRequest )
198 {
199 final HttpServletRequest request = (HttpServletRequest) req;
200 final HttpSession session = request.getSession( true );
201
202 CONTEXTS.set( session.getServletContext() );
203 SESSIONS.set( session );
204 }
205 else
206 {
207 CONTEXTS.set( this.servletContext );
208 SESSIONS.set( null );
209 }
210
211 LOCALES.set( req.getLocale() );
212 chain.doFilter( req, rsp );
213 }
214 finally
215 {
216 this.removeThreadLocal( CONTEXTS );
217 this.removeThreadLocal( SESSIONS );
218 this.removeThreadLocal( LOCALES );
219 }
220 }
221
222 public void destroy()
223 {
224 this.servletContext = null;
225 }
226
227
228
229
230 private void removeThreadLocal( final ThreadLocal threadLocal )
231 {
232 try
233 {
234
235 final Method removeMethod = threadLocal.getClass().
236 getDeclaredMethod( "remove", new Class[ 0 ] );
237
238 removeMethod.invoke( threadLocal, null );
239 }
240 catch ( IllegalAccessException e )
241 {
242 threadLocal.set( null );
243 this.servletContext.log( e.getMessage(), e );
244 }
245 catch ( IllegalArgumentException e )
246 {
247 threadLocal.set( null );
248 this.servletContext.log( e.getMessage(), e );
249 }
250 catch ( InvocationTargetException e )
251 {
252 threadLocal.set( null );
253 this.servletContext.log( e.getMessage(), e );
254 }
255 catch ( NoSuchMethodException e )
256 {
257 threadLocal.set( null );
258 }
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 private String getSystemPropertyMessage( final Locale locale,
279 final java.lang.String propertyName,
280 final java.lang.String value )
281 {
282 return ContainerFactory.getContainer().
283 getMessage( this, "systemProperty", locale,
284 new Object[]
285 {
286 propertyName,
287 value
288 });
289
290 }
291
292
293
294
295 }