1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.server;
22
23 import java.io.IOException;
24
25 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.FilterConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.apache.cactus.internal.configuration.ConfigurationInitializer;
35 import org.apache.cactus.internal.server.FilterImplicitObjects;
36 import org.apache.cactus.internal.server.FilterTestController;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41 * Generic Filter redirector that calls a test method on the server side.
42 *
43 * @version $Id: FilterTestRedirector.java 238991 2004-05-22 11:34:50Z vmassol $
44 * @see org.apache.cactus.internal.server.FilterTestCaller
45 */
46 public class FilterTestRedirector implements Filter
47 {
48 /**
49 * As this class is the first one loaded on the server side, we ensure
50 * that the Cactus configuration has been initialized. A better
51 * implementation might be to perform this initialization in the
52 * init() method. However, that requires removing the static LOGGER
53 * object.
54 */
55 static
56 {
57 ConfigurationInitializer.initialize();
58 }
59
60 /**
61 * The logger.
62 */
63 private static final Log LOGGER =
64 LogFactory.getLog(FilterTestRedirector.class);
65
66 /**
67 * The filter configuration object passed by the container when it calls
68 * <code>init(FilterConfig)</code>.
69 */
70 private FilterConfig config;
71
72 /**
73 * Handle the request. Extract from the HTTP request paramete the
74 * Service to perform : call test method or return tests results.
75 *
76 * @param theRequest the incoming HTTP request which contains all needed
77 * information on the test case and method to call
78 * @param theResponse the response to send back to the client side
79 * @param theFilterChain contains the chain of filters.
80 * @exception IOException if an error occurred during test on server side
81 * @exception ServletException if an error occurred during test on server
82 * side
83 */
84 public void doFilter(ServletRequest theRequest,
85 ServletResponse theResponse, FilterChain theFilterChain)
86 throws IOException, ServletException
87 {
88 // Mark beginning of test on server side
89 LOGGER.debug("------------- Start Filter service");
90
91 // Create implicit object holder
92 FilterImplicitObjects objects = new FilterImplicitObjects();
93
94 objects.setHttpServletRequest((HttpServletRequest) theRequest);
95 objects.setHttpServletResponse((HttpServletResponse) theResponse);
96 objects.setFilterConfig(this.config);
97 objects.setServletContext(this.config.getServletContext());
98 objects.setFilterChain(theFilterChain);
99
100 FilterTestController controller = new FilterTestController();
101
102 controller.handleRequest(objects);
103 }
104
105 /**
106 * Initialise this filter redirector. Called by the container.
107 *
108 * @param theConfig the filter config containing initialisation
109 * parameters from web.xml
110 */
111 public void init(FilterConfig theConfig)
112 {
113 // Save the config to pass it to the test case later on
114 this.config = theConfig;
115 }
116
117 /**
118 * Provided so that it works with containers that do not support the
119 * latest Filter spec yet (ex: Orion 1.5.2).
120 *
121 * @param theConfig the Filter Config
122 */
123 public void setFilterConfig(FilterConfig theConfig)
124 {
125 this.config = theConfig;
126 }
127
128 /**
129 * Provided so that it works with containers that do not support the
130 * latest Filter spec yet (ex: Orion 1.5.2).
131 *
132 * @return the Filter Config
133 */
134 public FilterConfig getFilterConfig()
135 {
136 return this.config;
137 }
138
139 /**
140 * Destroy the filter. Called by the container.
141 */
142 public void destroy()
143 {
144 }
145 }