KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > filters > ExampleRequestFilter


1 package example.filters;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5 import java.io.IOException JavaDoc;
6
7 import java.util.Enumeration JavaDoc;
8 import java.security.Principal JavaDoc;
9
10 import java.util.logging.Logger JavaDoc;
11 import java.util.logging.Level JavaDoc;
12
13 /**
14  * A cut-and-paste template for implementing a Filter that wraps the request
15  */

16
17 public class ExampleRequestFilter implements Filter {
18   private static final Logger JavaDoc log = Logger.getLogger("example.filters.ExampleRequestFilter");
19
20   /**
21    * Called once to initialize the Filter. If init() does not
22    * complete successfully (it throws an exception, or takes a really
23    * long time to return), the Filter will not be placed into service.
24    */

25   public void init(FilterConfig config)
26     throws ServletException
27   {
28     ServletContext app = config.getServletContext();
29
30     // an example of getting an init-param
31
String JavaDoc myParam = config.getInitParameter("my-param");
32     if (log.isLoggable(Level.CONFIG))
33       log.log(Level.CONFIG,"my-param value is `" + myParam + "'");
34   }
35
36   /**
37    * Called by Resin each time a request/response pair is passed
38    * through the chain due to a client request for a resource at the
39    * end of the chain. The FilterChain parameter is used by the
40    * Filter to pass on the request and response to the next Filter in
41    * the chain.
42    */

43   public void doFilter(ServletRequest request, ServletResponse response,
44                        FilterChain nextFilter)
45     throws ServletException, IOException JavaDoc
46   {
47     HttpServletRequest req = (HttpServletRequest) request;
48     HttpServletResponse res = (HttpServletResponse) response;
49     
50     // "wrap" the request object. Any filter or servlet or jsp that
51
// follows in the chain will get the values returned from the
52
// wrapper instead of from the original Request.
53
req = new ExampleRequestWrapper(req);
54
55     // call the next filter in the chain
56
nextFilter.doFilter(req, res);
57   }
58   
59   /**
60    * Any cleanup for the filter. This will only happen once, right
61    * before the Filter is released by Resin for garbage collection.
62    */

63
64   public void destroy()
65   {
66   }
67   
68   /**
69    * This example request wrapper includes all of the methods you
70    * could possibly want to implement. The implementaions here just
71    * call the method in the super class, implement the ones you want
72    * and remove the ones you don't need to change.
73    */

74   static class ExampleRequestWrapper extends HttpServletRequestWrapper {
75     ExampleRequestWrapper(HttpServletRequest request)
76     {
77       super(request);
78     }
79
80     /**
81      * Returns the HTTP method, e.g. "GET" or "POST"
82      */

83     public String JavaDoc getMethod()
84     {
85       return super.getMethod();
86     }
87
88     /**
89      * Returns the entire request URI
90      */

91     public String JavaDoc getRequestURI()
92     {
93       return super.getRequestURI();
94     }
95
96     /**
97      * Reconstructs the URL the client used for the request.
98      */

99     public StringBuffer JavaDoc getRequestURL()
100     {
101       return super.getRequestURL();
102     }
103
104     /**
105      * Returns the part of the URI corresponding to the application's
106      * prefix. The first part of the URI selects applications
107      * (ServletContexts).
108      *
109      * <p><code>getContextPath()</code> is /myapp for the uri
110      * /myapp/servlet/Hello,
111      */

112     public String JavaDoc getContextPath()
113     {
114       return super.getContextPath();
115     }
116
117     /**
118      * Returns the URI part corresponding to the selected servlet.
119      * The URI is relative to the application.
120      *
121      * <code>getServletPath()</code> is /servlet/Hello for the uri
122      * /myapp/servlet/Hello/foo.
123      *
124      * <code>getServletPath()</code> is /dir/hello.jsp
125      * for the uri /myapp/dir/hello.jsp/foo,
126      */

127     public String JavaDoc getServletPath()
128     {
129       return super.getServletPath();
130     }
131
132     /**
133      * Returns the URI part after the selected servlet and null if there
134      * is no suffix.
135      *
136      * <p><code>getPathInfo()</code> is /foo for
137      * the uri /myapp/servlet/Hello/foo.
138      *
139      * <code>getPathInfo()</code> is /hello.jsp for for the uri
140      * /myapp/dir/hello.jsp/foo.
141      */

142     public String JavaDoc getPathInfo()
143     {
144       return super.getPathInfo();
145     }
146
147     /**
148      * Returns the physical path name for the path info.
149      *
150      * @return null if there is no path info.
151      */

152     public String JavaDoc getPathTranslated()
153     {
154       return super.getPathTranslated();
155     }
156
157     /**
158      * Returns the request's query string. Form based servlets will use
159      * <code>ServletRequest.getParameter()</code> to decode the form values.
160      *
161      */

162     public String JavaDoc getQueryString()
163     {
164       return super.getQueryString();
165     }
166
167     /**
168      * Returns the first value for a request header.
169      *
170      * <code><pre>
171      * String userAgent = request.getHeader("User-Agent");
172      * </pre></code>
173      *
174      * @param name the header name
175      * @return the header value
176      */

177     public String JavaDoc getHeader(String JavaDoc name)
178     {
179       return super.getHeader(name);
180     }
181
182     /**
183      * Returns all the values for a request header. In some rare cases,
184      * like cookies, browsers may return multiple headers.
185      *
186      * @param name the header name
187      * @return an enumeration of the header values.
188      */

189     public Enumeration JavaDoc getHeaders(String JavaDoc name)
190     {
191       return super.getHeaders(name);
192     }
193
194     /**
195      * Returns an enumeration of all headers sent by the client.
196      */

197     public Enumeration JavaDoc getHeaderNames()
198     {
199       return super.getHeaderNames();
200     }
201
202     /**
203      * Converts a header value to an integer.
204      *
205      * @param name the header name
206      * @return the header value converted to an integer
207      */

208     public int getIntHeader(String JavaDoc name)
209     {
210       return super.getIntHeader(name);
211     }
212
213     /**
214      * Converts a date header to milliseconds since the epoch.
215      *
216      * <pre><code>
217      * long mod = request.getDateHeader("If-Modified-Since");
218      * </code></pre>
219      *
220      * @param name the header name
221      * @return the header value converted to an date
222      */

223     public long getDateHeader(String JavaDoc name)
224     {
225       return super.getDateHeader(name);
226     }
227
228     /**
229      * Returns an array of all cookies sent by the client.
230      */

231     public Cookie []getCookies()
232     {
233       return super.getCookies();
234     }
235
236     /**
237      * Returns a session. If no session exists and create is true, then
238      * create a new session, otherwise return null.
239      *
240      * @param create If true, then create a new session if none exists.
241      */

242     public HttpSession getSession(boolean create)
243     {
244       return super.getSession(create);
245     }
246
247     /**
248      * Returns the session id. Sessions are a convenience for keeping
249      * user state across requests.
250      *
251      * <p/>The session id is the value of the JSESSION cookie.
252      */

253     public String JavaDoc getRequestedSessionId()
254     {
255       return super.getRequestedSessionId();
256     }
257     
258     /**
259      * Returns true if the session is valid.
260      */

261     public boolean isRequestedSessionIdValid()
262     {
263       return super.isRequestedSessionIdValid();
264     }
265     
266     /**
267      * Returns true if the session came from a cookie.
268      */

269     public boolean isRequestedSessionIdFromCookie()
270     {
271       return super.isRequestedSessionIdFromCookie();
272     }
273     
274     /**
275      * Returns true if the session came URL-encoding.
276      */

277     public boolean isRequestedSessionIdFromURL()
278     {
279       return super.isRequestedSessionIdFromURL();
280     }
281     
282     /**
283      * Returns the auth type, e.g. basic.
284      */

285     public String JavaDoc getAuthType()
286     {
287       return super.getAuthType();
288     }
289     
290     /**
291      * Returns the remote user if authenticated.
292      */

293     public String JavaDoc getRemoteUser()
294     {
295       return super.getRemoteUser();
296     }
297     
298     /**
299      * Returns true if the user is in the given role.
300      */

301     public boolean isUserInRole(String JavaDoc role)
302     {
303       return super.isUserInRole(role);
304     }
305     
306     /**
307      * Returns the equivalent principal object for the authenticated user.
308      */

309     public Principal JavaDoc getUserPrincipal()
310     {
311       return super.getUserPrincipal();
312     }
313   }
314 }
315
316
Popular Tags