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.util.Enumeration;
24 import java.util.Hashtable;
25 import java.util.Vector;
26
27 import javax.servlet.FilterConfig;
28 import javax.servlet.ServletContext;
29
30 /**
31 * Wrapper around <code>FilterConfig</code> which overrides the
32 * <code>getServletContext()</code> method to return our own wrapper around
33 * <code>ServletContext</code>.
34 *
35 * @version $Id: FilterConfigWrapper.java 239054 2004-10-24 01:30:23Z felipeal $
36 * @see ServletContext
37 */
38 public class FilterConfigWrapper implements FilterConfig
39 {
40 /**
41 * The original filter config object.
42 */
43 private FilterConfig originalConfig;
44
45 /**
46 * List of parameters set using the <code>setInitParameter()</code> method.
47 */
48 private Hashtable initParameters;
49
50 /**
51 * Simulated name of the filter.
52 */
53 private String filterName;
54
55 /**
56 * @param theOriginalConfig the original filter config object
57 */
58 public FilterConfigWrapper(FilterConfig theOriginalConfig)
59 {
60 this.originalConfig = theOriginalConfig;
61 this.initParameters = new Hashtable();
62 }
63
64 /**
65 * Sets a parameter as if it were set in the <code>web.xml</code> file.
66 *
67 * @param theName the parameter's name
68 * @param theValue the parameter's value
69 */
70 public void setInitParameter(String theName, String theValue)
71 {
72 this.initParameters.put(theName, theValue);
73 }
74
75 /**
76 * Sets the filter name. That will be the value returned by the
77 * <code>getFilterName()</code> method.
78 *
79 * @param theFilterName the filter name
80 */
81 public void setFilterName(String theFilterName)
82 {
83 this.filterName = theFilterName;
84 }
85
86 //--Overridden methods ----------------------------------------------------
87
88 /**
89 * @return the simulated filter's name if defined or the redirector
90 * filter's name
91 */
92 public String getFilterName()
93 {
94 if (this.filterName != null)
95 {
96 return this.filterName;
97 }
98
99 return this.originalConfig.getFilterName();
100 }
101
102 /**
103 * @return our own wrapped servlet context object
104 */
105 public ServletContext getServletContext()
106 {
107 return new ServletContextWrapper(
108 this.originalConfig.getServletContext());
109 }
110
111 /**
112 * Return the union of the parameters defined in the Redirector
113 * <code>web.xml</code> file and the one set using the
114 * <code>setInitParameter()</code> method. The parameters with the same
115 * name (and same case) are only returned once.
116 *
117 * @return the init parameters
118 */
119 public Enumeration getInitParameterNames()
120 {
121 Vector names = new Vector();
122
123 // Add parameters that were added using setInitParameter()
124 Enumeration en = this.initParameters.keys();
125
126 while (en.hasMoreElements())
127 {
128 String value = (String) en.nextElement();
129
130 names.add(value);
131 }
132
133 // Add parameters from web.xml
134 en = this.originalConfig.getInitParameterNames();
135
136 while (en.hasMoreElements())
137 {
138 String value = (String) en.nextElement();
139
140 if (!names.contains(value))
141 {
142 names.add(value);
143 }
144 }
145
146 return names.elements();
147 }
148
149 /**
150 * @param theName the name of the parameter's value to return
151 * @return the value of the parameter, looking for it first in the list of
152 * parameters set using the <code>setInitParameter()</code> method
153 * and then in those set in <code>web.xml</code>.
154 */
155 public String getInitParameter(String theName)
156 {
157 // Look first in the list of parameters set using the
158 // setInitParameter() method.
159 String value = (String) this.initParameters.get(theName);
160
161 if (value == null)
162 {
163 value = this.originalConfig.getInitParameter(theName);
164 }
165
166 return value;
167 }
168 }