001: package example.filters;
002:
003: import javax.servlet.*;
004: import javax.servlet.http.*;
005: import java.io.IOException;
006:
007: import java.util.Enumeration;
008: import java.security.Principal;
009:
010: import java.util.logging.Logger;
011: import java.util.logging.Level;
012:
013: /**
014: * A cut-and-paste template for implementing a Filter that wraps the request
015: */
016:
017: public class ExampleRequestFilter implements Filter {
018: private static final Logger log = Logger
019: .getLogger("example.filters.ExampleRequestFilter");
020:
021: /**
022: * Called once to initialize the Filter. If init() does not
023: * complete successfully (it throws an exception, or takes a really
024: * long time to return), the Filter will not be placed into service.
025: */
026: public void init(FilterConfig config) throws ServletException {
027: ServletContext app = config.getServletContext();
028:
029: // an example of getting an init-param
030: String myParam = config.getInitParameter("my-param");
031: if (log.isLoggable(Level.CONFIG))
032: log
033: .log(Level.CONFIG, "my-param value is `" + myParam
034: + "'");
035: }
036:
037: /**
038: * Called by Resin each time a request/response pair is passed
039: * through the chain due to a client request for a resource at the
040: * end of the chain. The FilterChain parameter is used by the
041: * Filter to pass on the request and response to the next Filter in
042: * the chain.
043: */
044: public void doFilter(ServletRequest request,
045: ServletResponse response, FilterChain nextFilter)
046: throws ServletException, IOException {
047: HttpServletRequest req = (HttpServletRequest) request;
048: HttpServletResponse res = (HttpServletResponse) response;
049:
050: // "wrap" the request object. Any filter or servlet or jsp that
051: // follows in the chain will get the values returned from the
052: // wrapper instead of from the original Request.
053: req = new ExampleRequestWrapper(req);
054:
055: // call the next filter in the chain
056: nextFilter.doFilter(req, res);
057: }
058:
059: /**
060: * Any cleanup for the filter. This will only happen once, right
061: * before the Filter is released by Resin for garbage collection.
062: */
063:
064: public void destroy() {
065: }
066:
067: /**
068: * This example request wrapper includes all of the methods you
069: * could possibly want to implement. The implementaions here just
070: * call the method in the super class, implement the ones you want
071: * and remove the ones you don't need to change.
072: */
073: static class ExampleRequestWrapper extends
074: HttpServletRequestWrapper {
075: ExampleRequestWrapper(HttpServletRequest request) {
076: super (request);
077: }
078:
079: /**
080: * Returns the HTTP method, e.g. "GET" or "POST"
081: */
082: public String getMethod() {
083: return super .getMethod();
084: }
085:
086: /**
087: * Returns the entire request URI
088: */
089: public String getRequestURI() {
090: return super .getRequestURI();
091: }
092:
093: /**
094: * Reconstructs the URL the client used for the request.
095: */
096: public StringBuffer getRequestURL() {
097: return super .getRequestURL();
098: }
099:
100: /**
101: * Returns the part of the URI corresponding to the application's
102: * prefix. The first part of the URI selects applications
103: * (ServletContexts).
104: *
105: * <p><code>getContextPath()</code> is /myapp for the uri
106: * /myapp/servlet/Hello,
107: */
108: public String getContextPath() {
109: return super .getContextPath();
110: }
111:
112: /**
113: * Returns the URI part corresponding to the selected servlet.
114: * The URI is relative to the application.
115: *
116: * <code>getServletPath()</code> is /servlet/Hello for the uri
117: * /myapp/servlet/Hello/foo.
118: *
119: * <code>getServletPath()</code> is /dir/hello.jsp
120: * for the uri /myapp/dir/hello.jsp/foo,
121: */
122: public String getServletPath() {
123: return super .getServletPath();
124: }
125:
126: /**
127: * Returns the URI part after the selected servlet and null if there
128: * is no suffix.
129: *
130: * <p><code>getPathInfo()</code> is /foo for
131: * the uri /myapp/servlet/Hello/foo.
132: *
133: * <code>getPathInfo()</code> is /hello.jsp for for the uri
134: * /myapp/dir/hello.jsp/foo.
135: */
136: public String getPathInfo() {
137: return super .getPathInfo();
138: }
139:
140: /**
141: * Returns the physical path name for the path info.
142: *
143: * @return null if there is no path info.
144: */
145: public String getPathTranslated() {
146: return super .getPathTranslated();
147: }
148:
149: /**
150: * Returns the request's query string. Form based servlets will use
151: * <code>ServletRequest.getParameter()</code> to decode the form values.
152: *
153: */
154: public String getQueryString() {
155: return super .getQueryString();
156: }
157:
158: /**
159: * Returns the first value for a request header.
160: *
161: * <code><pre>
162: * String userAgent = request.getHeader("User-Agent");
163: * </pre></code>
164: *
165: * @param name the header name
166: * @return the header value
167: */
168: public String getHeader(String name) {
169: return super .getHeader(name);
170: }
171:
172: /**
173: * Returns all the values for a request header. In some rare cases,
174: * like cookies, browsers may return multiple headers.
175: *
176: * @param name the header name
177: * @return an enumeration of the header values.
178: */
179: public Enumeration getHeaders(String name) {
180: return super .getHeaders(name);
181: }
182:
183: /**
184: * Returns an enumeration of all headers sent by the client.
185: */
186: public Enumeration getHeaderNames() {
187: return super .getHeaderNames();
188: }
189:
190: /**
191: * Converts a header value to an integer.
192: *
193: * @param name the header name
194: * @return the header value converted to an integer
195: */
196: public int getIntHeader(String name) {
197: return super .getIntHeader(name);
198: }
199:
200: /**
201: * Converts a date header to milliseconds since the epoch.
202: *
203: * <pre><code>
204: * long mod = request.getDateHeader("If-Modified-Since");
205: * </code></pre>
206: *
207: * @param name the header name
208: * @return the header value converted to an date
209: */
210: public long getDateHeader(String name) {
211: return super .getDateHeader(name);
212: }
213:
214: /**
215: * Returns an array of all cookies sent by the client.
216: */
217: public Cookie[] getCookies() {
218: return super .getCookies();
219: }
220:
221: /**
222: * Returns a session. If no session exists and create is true, then
223: * create a new session, otherwise return null.
224: *
225: * @param create If true, then create a new session if none exists.
226: */
227: public HttpSession getSession(boolean create) {
228: return super .getSession(create);
229: }
230:
231: /**
232: * Returns the session id. Sessions are a convenience for keeping
233: * user state across requests.
234: *
235: * <p/>The session id is the value of the JSESSION cookie.
236: */
237: public String getRequestedSessionId() {
238: return super .getRequestedSessionId();
239: }
240:
241: /**
242: * Returns true if the session is valid.
243: */
244: public boolean isRequestedSessionIdValid() {
245: return super .isRequestedSessionIdValid();
246: }
247:
248: /**
249: * Returns true if the session came from a cookie.
250: */
251: public boolean isRequestedSessionIdFromCookie() {
252: return super .isRequestedSessionIdFromCookie();
253: }
254:
255: /**
256: * Returns true if the session came URL-encoding.
257: */
258: public boolean isRequestedSessionIdFromURL() {
259: return super .isRequestedSessionIdFromURL();
260: }
261:
262: /**
263: * Returns the auth type, e.g. basic.
264: */
265: public String getAuthType() {
266: return super .getAuthType();
267: }
268:
269: /**
270: * Returns the remote user if authenticated.
271: */
272: public String getRemoteUser() {
273: return super .getRemoteUser();
274: }
275:
276: /**
277: * Returns true if the user is in the given role.
278: */
279: public boolean isUserInRole(String role) {
280: return super .isUserInRole(role);
281: }
282:
283: /**
284: * Returns the equivalent principal object for the authenticated user.
285: */
286: public Principal getUserPrincipal() {
287: return super.getUserPrincipal();
288: }
289: }
290: }
|