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 final 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( final 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
231 public ServletFilter()
232 {
233 super();
234 }
235
236 private void removeThreadLocal( final ThreadLocal threadLocal )
237 {
238 try
239 {
240
241 final Method removeMethod = threadLocal.getClass().
242 getDeclaredMethod( "remove", new Class[ 0 ] );
243
244 removeMethod.invoke( threadLocal, null );
245 }
246 catch ( final IllegalAccessException e )
247 {
248 threadLocal.set( null );
249 this.servletContext.log( e.getMessage(), e );
250 }
251 catch ( final IllegalArgumentException e )
252 {
253 threadLocal.set( null );
254 this.servletContext.log( e.getMessage(), e );
255 }
256 catch ( final InvocationTargetException e )
257 {
258 threadLocal.set( null );
259 this.servletContext.log( e.getMessage(), e );
260 }
261 catch ( final NoSuchMethodException e )
262 {
263 threadLocal.set( null );
264 }
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 private String getSystemPropertyMessage( final Locale locale,
285 final java.lang.String propertyName,
286 final java.lang.String value )
287 {
288 return ContainerFactory.getContainer().
289 getMessage( this, "systemProperty", locale,
290 new Object[]
291 {
292 propertyName,
293 value
294 });
295
296 }
297
298
299
300
301 }