KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > filters > ExampleResponseFilter


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.logging.Logger JavaDoc;
8 import java.util.logging.Level JavaDoc;
9
10 /**
11  * A cut-and-paste template for implementing a Filter that wraps the Response
12  */

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

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

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

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

71   class ExampleResponseWrapper extends HttpServletResponseWrapper {
72     ExampleResponseWrapper(HttpServletResponse response)
73     {
74       super(response);
75     }
76
77     /**
78      * Sets the HTTP status
79      *
80      * @param sc the HTTP status code
81      */

82     public void setStatus(int sc)
83     {
84       super.setStatus(sc);
85     }
86
87     public void setStatus(int sc, String JavaDoc msg)
88     {
89       super.setStatus(sc, msg);
90     }
91
92     /**
93      * Sends an HTTP error page based on the status code
94      *
95      * @param sc the HTTP status code
96      */

97     public void sendError(int sc, String JavaDoc msg)
98       throws IOException JavaDoc
99     {
100       super.sendError(sc, msg);
101     }
102     
103     /**
104      * Sends an HTTP error page based on the status code
105      *
106      * @param sc the HTTP status code
107      */

108     public void sendError(int sc)
109       throws IOException JavaDoc
110     {
111       super.sendError(sc);
112     }
113     
114     /**
115      * Redirects the client to another page.
116      *
117      * @param location the location to redirect to.
118      */

119     public void sendRedirect(String JavaDoc location)
120       throws IOException JavaDoc
121     {
122       super.sendRedirect(location);
123     }
124     
125     /**
126      * Sets a header. This will override a previous header
127      * with the same name.
128      *
129      * @param name the header name
130      * @param value the header value
131      */

132     public void setHeader(String JavaDoc name, String JavaDoc value)
133     {
134       super.setHeader(name, value);
135     }
136     
137     /**
138      * Adds a header. If another header with the same name exists, both
139      * will be sent to the client.
140      *
141      * @param name the header name
142      * @param value the header value
143      */

144     public void addHeader(String JavaDoc name, String JavaDoc value)
145     {
146       super.addHeader(name, value);
147     }
148     
149     /**
150      * Returns true if the output headers include <code>name</code>
151      *
152      * @param name the header name to test
153      */

154     public boolean containsHeader(String JavaDoc name)
155     {
156       return super.containsHeader(name);
157     }
158     
159     /**
160      * Sets a header by converting a date to a string.
161      *
162      * <p>To set the page to expire in 15 seconds use the following:
163      * <pre><code>
164      * long now = System.currentTime();
165      * response.setDateHeader("Expires", now + 15000);
166      * </code></pre>
167      *
168      * @param name name of the header
169      * @param date the date in milliseconds since the epoch.
170      */

171     public void setDateHeader(String JavaDoc name, long date)
172     {
173       super.setDateHeader(name, date);
174     }
175     
176     /**
177      * Adds a header by converting a date to a string.
178      *
179      * @param name name of the header
180      * @param date the date in milliseconds since the epoch.
181      */

182     public void addDateHeader(String JavaDoc name, long date)
183     {
184       super.addDateHeader(name, date);
185     }
186     
187     /**
188      * Sets a header by converting an integer value to a string.
189      *
190      * @param name name of the header
191      * @param value the value as an integer
192      */

193     public void setIntHeader(String JavaDoc name, int value)
194     {
195       super.setIntHeader(name, value);
196     }
197     
198     /**
199      * Adds a header by converting an integer value to a string.
200      *
201      * @param name name of the header
202      * @param value the value as an integer
203      */

204     public void addIntHeader(String JavaDoc name, int value)
205     {
206       super.addIntHeader(name, value);
207     }
208     
209     /**
210      * Sends a new cookie to the client.
211      */

212     public void addCookie(Cookie cookie)
213     {
214       super.addCookie(cookie);
215     }
216     
217     /**
218      * Encodes session information in a URL. Calling this will enable
219      * sessions for users who have disabled cookies.
220      *
221      * @param url the url to encode
222      * @return a url with session information encoded
223      */

224     public String JavaDoc encodeURL(String JavaDoc url)
225     {
226       return super.encodeURL(url);
227     }
228     
229     /**
230      * Encodes session information in a URL suitable for
231      * <code>sendRedirect()</code>
232      *
233      * @param url the url to encode
234      * @return a url with session information encoded
235      */

236     public String JavaDoc encodeRedirectURL(String JavaDoc name)
237     {
238       return super.encodeRedirectURL(name);
239     }
240   
241   }
242 }
243
Popular Tags