001: package example.filters;
002:
003: import javax.servlet.*;
004: import javax.servlet.http.*;
005: import java.io.IOException;
006:
007: import java.util.logging.Logger;
008: import java.util.logging.Level;
009:
010: /**
011: * A cut-and-paste template for implementing a Filter that wraps the Response
012: */
013:
014: public class ExampleResponseFilter implements Filter {
015: private static final Logger log = Logger
016: .getLogger("example.filters.ExampleResponseFilter");
017:
018: /**
019: * Called once to initialize the Filter. If init() does not
020: * complete successfully (it throws an exception, or takes a really
021: * long time to return), the Filter will not be placed into service.
022: */
023: public void init(FilterConfig config) throws ServletException {
024: ServletContext app = config.getServletContext();
025:
026: // an example of getting an init-param
027: String myParam = config.getInitParameter("my-param");
028: if (log.isLoggable(Level.CONFIG))
029: log
030: .log(Level.CONFIG, "my-param value is `" + myParam
031: + "'");
032: }
033:
034: /**
035: * Called by Resin each time a request/response pair is passed
036: * through the chain due to a client request for a resource at the
037: * end of the chain. The FilterChain parameter is used by the
038: * Filter to pass on the request and response to the next Filter in
039: * the chain.
040: */
041: public void doFilter(ServletRequest request,
042: ServletResponse response, FilterChain nextFilter)
043: throws ServletException, IOException {
044: HttpServletRequest req = (HttpServletRequest) request;
045: HttpServletResponse res = (HttpServletResponse) response;
046:
047: // "wrap" the response object. Any filter or servlet or jsp that
048: // follows in the chain will get this response object
049: // instead of the original Response.
050: res = new ExampleResponseWrapper(res);
051:
052: // call the next filter in the chain
053: nextFilter.doFilter(req, res);
054: }
055:
056: /**
057: * Any cleanup for the filter. This will only happen once, right
058: * before the Filter is released by Resin for garbage collection.
059: */
060: public void destroy() {
061: }
062:
063: /**
064: * This example response wrapper includes all of the methods you
065: * could possibly want to implement. The implementaions here just
066: * call the method in the super class, implement the ones you want
067: * and remove the ones you don't need to change.
068: */
069: class ExampleResponseWrapper extends HttpServletResponseWrapper {
070: ExampleResponseWrapper(HttpServletResponse response) {
071: super (response);
072: }
073:
074: /**
075: * Sets the HTTP status
076: *
077: * @param sc the HTTP status code
078: */
079: public void setStatus(int sc) {
080: super .setStatus(sc);
081: }
082:
083: public void setStatus(int sc, String msg) {
084: super .setStatus(sc, msg);
085: }
086:
087: /**
088: * Sends an HTTP error page based on the status code
089: *
090: * @param sc the HTTP status code
091: */
092: public void sendError(int sc, String msg) throws IOException {
093: super .sendError(sc, msg);
094: }
095:
096: /**
097: * Sends an HTTP error page based on the status code
098: *
099: * @param sc the HTTP status code
100: */
101: public void sendError(int sc) throws IOException {
102: super .sendError(sc);
103: }
104:
105: /**
106: * Redirects the client to another page.
107: *
108: * @param location the location to redirect to.
109: */
110: public void sendRedirect(String location) throws IOException {
111: super .sendRedirect(location);
112: }
113:
114: /**
115: * Sets a header. This will override a previous header
116: * with the same name.
117: *
118: * @param name the header name
119: * @param value the header value
120: */
121: public void setHeader(String name, String value) {
122: super .setHeader(name, value);
123: }
124:
125: /**
126: * Adds a header. If another header with the same name exists, both
127: * will be sent to the client.
128: *
129: * @param name the header name
130: * @param value the header value
131: */
132: public void addHeader(String name, String value) {
133: super .addHeader(name, value);
134: }
135:
136: /**
137: * Returns true if the output headers include <code>name</code>
138: *
139: * @param name the header name to test
140: */
141: public boolean containsHeader(String name) {
142: return super .containsHeader(name);
143: }
144:
145: /**
146: * Sets a header by converting a date to a string.
147: *
148: * <p>To set the page to expire in 15 seconds use the following:
149: * <pre><code>
150: * long now = System.currentTime();
151: * response.setDateHeader("Expires", now + 15000);
152: * </code></pre>
153: *
154: * @param name name of the header
155: * @param date the date in milliseconds since the epoch.
156: */
157: public void setDateHeader(String name, long date) {
158: super .setDateHeader(name, date);
159: }
160:
161: /**
162: * Adds a header by converting a date to a string.
163: *
164: * @param name name of the header
165: * @param date the date in milliseconds since the epoch.
166: */
167: public void addDateHeader(String name, long date) {
168: super .addDateHeader(name, date);
169: }
170:
171: /**
172: * Sets a header by converting an integer value to a string.
173: *
174: * @param name name of the header
175: * @param value the value as an integer
176: */
177: public void setIntHeader(String name, int value) {
178: super .setIntHeader(name, value);
179: }
180:
181: /**
182: * Adds a header by converting an integer value to a string.
183: *
184: * @param name name of the header
185: * @param value the value as an integer
186: */
187: public void addIntHeader(String name, int value) {
188: super .addIntHeader(name, value);
189: }
190:
191: /**
192: * Sends a new cookie to the client.
193: */
194: public void addCookie(Cookie cookie) {
195: super .addCookie(cookie);
196: }
197:
198: /**
199: * Encodes session information in a URL. Calling this will enable
200: * sessions for users who have disabled cookies.
201: *
202: * @param url the url to encode
203: * @return a url with session information encoded
204: */
205: public String encodeURL(String url) {
206: return super .encodeURL(url);
207: }
208:
209: /**
210: * Encodes session information in a URL suitable for
211: * <code>sendRedirect()</code>
212: *
213: * @param url the url to encode
214: * @return a url with session information encoded
215: */
216: public String encodeRedirectURL(String name) {
217: return super.encodeRedirectURL(name);
218: }
219:
220: }
221: }
|