Source code: org/enhydra/servlet/filter/Filter.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * $Id: Filter.java,v 1.4.2.1.2.1 2000/10/19 17:59:10 jasona Exp $
22 */
23
24 package org.enhydra.servlet.filter;
25
26 import javax.servlet.Servlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.apache.tomcat.core.LifecycleInterceptor;
30 import org.apache.tomcat.core.ServiceInterceptor;
31 import org.apache.tomcat.core.Context;
32 import com.lutris.multiServer.MultiServer;
33 import org.enhydra.servlet.ServletContainer;
34 import org.enhydra.servlet.servletManager.ServletManager;
35 import org.enhydra.servlet.connectionMethods.Channel;
36
37 /**
38 * This replaces the BasicTransactionFilter.
39 * Also note that the BasicTransactionServlet is no longer used
40 * and that it is functionally replaced by implementing the
41 * org.apache.tomcat.core.LifecycleInterceptor and
42 * org.apache.tomcat.core.ServiceInterceptor.
43 * There is no interface defined for this, since this is a temporary
44 * halfway point between the old Enhydra TransactionFilter and the
45 * future Tomcat Interceptors.
46 * Extend this class to create a filter with additional attributes.
47 * The object model is not currently very clean because we need to
48 * bridge between the Interceptor model of Tomcat for service(), init(),
49 * and destroy(), while maintaining the Enhydra request and response
50 * wrapping (Valve model).
51 *
52 * @author Shawn McMurdo
53 */
54 abstract public class Filter {
55
56 private ServiceInterceptor serviceInterceptor;
57 private LifecycleInterceptor initInterceptor;
58 private LifecycleInterceptor destroyInterceptor;
59 private String description;
60
61 /**
62 * Default constructor.
63 * Other filter elements must be set with the set methods.
64 */
65 public Filter() {
66 this.description = "Standard Filter";
67 }
68
69 /**
70 * Constructor given a description only.
71 * Other filter elements must be set with the set methods.
72 *
73 * @param description a String describing the filter, suitable for
74 * a select list.
75 */
76 public Filter(String description) {
77 this.description = description;
78 }
79
80 /**
81 * Adds the service, init, and destroy interceptors to the
82 * Tomcat Context for the given servlet.
83 *
84 * @param id The servlet id used to find the Context.
85 */
86 public void registerInterceptors(String id) {
87 ServletContainer sc = (ServletContainer)
88 MultiServer.getService("ServletContainer");
89 ServletManager sm = sc.getServletManager();
90 Context context = sm.getContextByServletID(id);
91 if (serviceInterceptor != null) {
92 context.addServiceInterceptor(serviceInterceptor);
93 }
94 if (initInterceptor != null) {
95 context.addInitInterceptor(initInterceptor);
96 }
97 if (destroyInterceptor != null) {
98 context.addDestroyInterceptor(destroyInterceptor);
99 }
100 }
101
102 /**
103 * Removes the service, init, and destroy interceptors from the
104 * Tomcat Context for the given servlet.
105 *
106 * @param id The servlet id used to find the Context.
107 */
108 public void unregisterInterceptors(String id) {
109 ServletContainer sc = (ServletContainer)
110 MultiServer.getService("ServletContainer");
111 ServletManager sm = sc.getServletManager();
112 Context context = sm.getContextByServletID(id);
113 // Currently no way to remove Interceptors in Tomcat 3.0.
114 if (serviceInterceptor != null) {
115 context.removeServiceInterceptor(serviceInterceptor);
116 }
117 if (initInterceptor != null) {
118 context.removeInitInterceptor(initInterceptor);
119 }
120 if (destroyInterceptor != null) {
121 context.removeDestroyInterceptor(destroyInterceptor);
122 }
123 }
124
125 /**
126 * Set the String description for this Filter.
127 * This should be suitable for display in a select list.
128 *
129 * @param description is a String describing the filter.
130 */
131 public void setDescription(String description) {
132 this.description = description;
133 }
134
135 /**
136 * Get the String description for this Filter.
137 *
138 * @return a String describing the filter.
139 */
140 public String getDescription() {
141 return description;
142 }
143
144 /**
145 * Override toString since that is what the Admin used to use
146 * to get the description.
147 *
148 * @return a String describing the filter.
149 */
150 public String toString() {
151 return description;
152 }
153
154 /**
155 * Get the RequestFilter for this Filter by wrapping the given Request.
156 *
157 * @param request an HttpServletRequest to be wrapped by the filter.
158 * @param response an HttpServletResponse that is generally not used, but
159 * is available for request-response data coordination.
160 * @return a RequestFilter
161 */
162 public RequestFilter wrapRequest(HttpServletRequest request,
163 HttpServletResponse response) {
164 return new RequestFilter(request);
165 }
166
167 /**
168 * Get the ResponseFilter for this Filter by wrapping the given Response.
169 *
170 * @param request an HttpServletRequest that is generally not used, but
171 * is available for request-response data coordination.
172 * @param response an HttpServletResponse to be wrapped by the filter.
173 * @return a ResponseFilter
174 */
175 abstract public ResponseFilter wrapResponse(HttpServletRequest request,
176 HttpServletResponse response);
177
178 /**
179 * Set the ServiceInterceptor for this Filter.
180 *
181 * @param serviceInterceptor is a ServiceInterceptor
182 */
183 public void setServiceInterceptor(ServiceInterceptor serviceInterceptor) {
184 this.serviceInterceptor = serviceInterceptor;
185 }
186
187 /**
188 * Get the ServiceInterceptor for this Filter.
189 *
190 * @return a ServiceInterceptor
191 */
192 public ServiceInterceptor getServiceInterceptor() {
193 return serviceInterceptor;
194 }
195
196 /**
197 * Set the init() LifecycleInterceptor for this Filter.
198 *
199 * @param initInterceptor is a LifecycleInterceptor for init()
200 */
201 public void setInitInterceptor(LifecycleInterceptor initInterceptor) {
202 this.initInterceptor = initInterceptor;
203 }
204
205 /**
206 * Get the init() LifecycleInterceptor for this Filter.
207 *
208 * @return a LifecycleInterceptor for init()
209 */
210 public LifecycleInterceptor getInitInterceptor() {
211 return initInterceptor;
212 }
213
214 /**
215 * Set the destroy() LifecycleInterceptor for this Filter.
216 *
217 * @param destroyInterceptor is a LifecycleInterceptor for destroy()
218 */
219 public void setDestroyInterceptor(LifecycleInterceptor destroyInterceptor) {
220 this.destroyInterceptor = destroyInterceptor;
221 }
222
223 /**
224 * Get the destroy() LifecycleInterceptor for this Filter.
225 *
226 * @return a LifecycleInterceptor for destroy()
227 */
228 public LifecycleInterceptor getDestroyInterceptor() {
229 return destroyInterceptor;
230 }
231
232 }
233