KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > WrapperFilter


1 /*
2  * Copyright 1999, 2000 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import java.util.*;
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24
25 /**
26  * Configurable filter that will wrap the request and/or response objects
27  * it passes on with either generic or HTTP-specific wrappers.
28  *
29  * @author Craig R. McClanahan
30  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
31  */

32
33 public class WrapperFilter implements Filter {
34
35
36     // ----------------------------------------------------------- Constructors
37

38
39
40     // ----------------------------------------------------- Instance Variables
41

42
43     /**
44      * The filter configuration object for this filter.
45      */

46     protected FilterConfig config = null;
47
48
49     /**
50      * The type of wrapper for each request ("none", "generic", "http").
51      */

52     protected String JavaDoc requestWrapper = "none";
53
54
55     /**
56      * The type of wrapper for each response ("none", "generic", "http").
57      */

58     protected String JavaDoc responseWrapper = "none";
59
60
61     // --------------------------------------------------------- Public Methods
62

63
64     /**
65      * Release this Filter instance from service.
66      */

67     public void destroy() {
68
69         config = null;
70         requestWrapper = "none";
71         responseWrapper = "none";
72
73     }
74
75
76     /**
77      * Wrap this request and/or response as configured and pass it on.
78      */

79     public void doFilter(ServletRequest inRequest, ServletResponse inResponse,
80                          FilterChain chain)
81         throws IOException, ServletException {
82
83         // Create the appropriate wrappers
84
ServletRequest outRequest = inRequest;
85         ServletResponse outResponse = inResponse;
86         if (requestWrapper.equals("generic")) {
87             outRequest = new TesterServletRequestWrapper(inRequest);
88         } else if (requestWrapper.equals("http")) {
89             outRequest = new TesterHttpServletRequestWrapper
90                 ((HttpServletRequest) inRequest);
91         }
92         if (responseWrapper.equals("generic")) {
93             outResponse = new TesterServletResponseWrapper(inResponse);
94         } else if (responseWrapper.equals("http")) {
95             outResponse = new TesterHttpServletResponseWrapper
96                 ((HttpServletResponse) inResponse);
97         }
98
99         // Perform this request
100
chain.doFilter(outRequest, outResponse);
101
102     }
103
104
105     /**
106      * Place this Filter instance into service.
107      *
108      * @param config The filter configuration object
109      */

110     public void init(FilterConfig config) throws ServletException {
111
112         this.config = config;
113         String JavaDoc value = null;
114         value = config.getInitParameter("request");
115         if (value != null)
116             requestWrapper = value;
117         value = config.getInitParameter("response");
118         if (value != null)
119             responseWrapper = value;
120
121     }
122
123
124 }
125
Popular Tags