KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > deploy > FilterMap


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

17
18
19 package org.apache.catalina.deploy;
20
21
22 import org.apache.catalina.util.RequestUtil;
23 import java.io.Serializable JavaDoc;
24
25
26 /**
27  * Representation of a filter mapping for a web application, as represented
28  * in a <code>&lt;filter-mapping&gt;</code> element in the deployment
29  * descriptor. Each filter mapping must contain a filter name plus either
30  * a URL pattern or a servlet name.
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
34  */

35
36 public class FilterMap implements Serializable JavaDoc {
37
38
39     // ------------------------------------------------------------- Properties
40

41
42     /**
43      * The name of this filter to be executed when this mapping matches
44      * a particular request.
45      */

46     
47     public static final int ERROR = 1;
48     public static final int FORWARD = 2;
49     public static final int FORWARD_ERROR =3;
50     public static final int INCLUDE = 4;
51     public static final int INCLUDE_ERROR = 5;
52     public static final int INCLUDE_ERROR_FORWARD =6;
53     public static final int INCLUDE_FORWARD = 7;
54     public static final int REQUEST = 8;
55     public static final int REQUEST_ERROR = 9;
56     public static final int REQUEST_ERROR_FORWARD = 10;
57     public static final int REQUEST_ERROR_FORWARD_INCLUDE = 11;
58     public static final int REQUEST_ERROR_INCLUDE = 12;
59     public static final int REQUEST_FORWARD = 13;
60     public static final int REQUEST_INCLUDE = 14;
61     public static final int REQUEST_FORWARD_INCLUDE= 15;
62     
63     // represents nothing having been set. This will be seen
64
// as equal to a REQUEST
65
private static final int NOT_SET = -1;
66     
67     private int dispatcherMapping=NOT_SET;
68     
69     private String JavaDoc filterName = null;
70
71     public String JavaDoc getFilterName() {
72         return (this.filterName);
73     }
74
75     public void setFilterName(String JavaDoc filterName) {
76         this.filterName = filterName;
77     }
78
79
80     /**
81      * The servlet name this mapping matches.
82      */

83     private String JavaDoc[] servletNames = new String JavaDoc[0];
84
85     public String JavaDoc[] getServletNames() {
86         return (this.servletNames);
87     }
88
89     public void addServletName(String JavaDoc servletName) {
90         String JavaDoc[] results = new String JavaDoc[servletNames.length + 1];
91         System.arraycopy(servletNames, 0, results, 0, servletNames.length);
92         results[servletNames.length] = servletName;
93         servletNames = results;
94     }
95
96     
97     /**
98      * The flag that indicates this mapping will match all.
99      */

100     private boolean allMatch = false;
101     
102     public boolean getAllMatch() {
103         return allMatch;
104     }
105     
106
107     /**
108      * The URL pattern this mapping matches.
109      */

110     private String JavaDoc[] urlPatterns = new String JavaDoc[0];
111
112     public String JavaDoc[] getURLPatterns() {
113         return (this.urlPatterns);
114     }
115
116     public void addURLPattern(String JavaDoc urlPattern) {
117         if ("*".equals(urlPattern)) {
118             this.allMatch = true;
119         } else {
120             String JavaDoc[] results = new String JavaDoc[urlPatterns.length + 1];
121             System.arraycopy(urlPatterns, 0, results, 0, urlPatterns.length);
122             results[urlPatterns.length] = RequestUtil.URLDecode(urlPattern);
123             urlPatterns = results;
124         }
125     }
126     
127     /**
128      *
129      * This method will be used to set the current state of the FilterMap
130      * representing the state of when filters should be applied:
131      *
132      * ERROR
133      * FORWARD
134      * FORWARD_ERROR
135      * INCLUDE
136      * INCLUDE_ERROR
137      * INCLUDE_ERROR_FORWARD
138      * REQUEST
139      * REQUEST_ERROR
140      * REQUEST_ERROR_INCLUDE
141      * REQUEST_ERROR_FORWARD_INCLUDE
142      * REQUEST_INCLUDE
143      * REQUEST_FORWARD,
144      * REQUEST_FORWARD_INCLUDE
145      *
146      */

147     public void setDispatcher(String JavaDoc dispatcherString) {
148         String JavaDoc dispatcher = dispatcherString.toUpperCase();
149         
150         if (dispatcher.equals("FORWARD")) {
151
152             // apply FORWARD to the global dispatcherMapping.
153
switch (dispatcherMapping) {
154                 case NOT_SET : dispatcherMapping = FORWARD; break;
155                 case ERROR : dispatcherMapping = FORWARD_ERROR; break;
156                 case INCLUDE : dispatcherMapping = INCLUDE_FORWARD; break;
157                 case INCLUDE_ERROR : dispatcherMapping = INCLUDE_ERROR_FORWARD; break;
158                 case REQUEST : dispatcherMapping = REQUEST_FORWARD; break;
159                 case REQUEST_ERROR : dispatcherMapping = REQUEST_ERROR_FORWARD; break;
160                 case REQUEST_ERROR_INCLUDE : dispatcherMapping = REQUEST_ERROR_FORWARD_INCLUDE; break;
161                 case REQUEST_INCLUDE : dispatcherMapping = REQUEST_FORWARD_INCLUDE; break;
162             }
163         } else if (dispatcher.equals("INCLUDE")) {
164             // apply INCLUDE to the global dispatcherMapping.
165
switch (dispatcherMapping) {
166                 case NOT_SET : dispatcherMapping = INCLUDE; break;
167                 case ERROR : dispatcherMapping = INCLUDE_ERROR; break;
168                 case FORWARD : dispatcherMapping = INCLUDE_FORWARD; break;
169                 case FORWARD_ERROR : dispatcherMapping = INCLUDE_ERROR_FORWARD; break;
170                 case REQUEST : dispatcherMapping = REQUEST_INCLUDE; break;
171                 case REQUEST_ERROR : dispatcherMapping = REQUEST_ERROR_INCLUDE; break;
172                 case REQUEST_ERROR_FORWARD : dispatcherMapping = REQUEST_ERROR_FORWARD_INCLUDE; break;
173                 case REQUEST_FORWARD : dispatcherMapping = REQUEST_FORWARD_INCLUDE; break;
174             }
175         } else if (dispatcher.equals("REQUEST")) {
176             // apply REQUEST to the global dispatcherMapping.
177
switch (dispatcherMapping) {
178                 case NOT_SET : dispatcherMapping = REQUEST; break;
179                 case ERROR : dispatcherMapping = REQUEST_ERROR; break;
180                 case FORWARD : dispatcherMapping = REQUEST_FORWARD; break;
181                 case FORWARD_ERROR : dispatcherMapping = REQUEST_ERROR_FORWARD; break;
182                 case INCLUDE : dispatcherMapping = REQUEST_INCLUDE; break;
183                 case INCLUDE_ERROR : dispatcherMapping = REQUEST_ERROR_INCLUDE; break;
184                 case INCLUDE_FORWARD : dispatcherMapping = REQUEST_FORWARD_INCLUDE; break;
185                 case INCLUDE_ERROR_FORWARD : dispatcherMapping = REQUEST_ERROR_FORWARD_INCLUDE; break;
186             }
187         } else if (dispatcher.equals("ERROR")) {
188             // apply ERROR to the global dispatcherMapping.
189
switch (dispatcherMapping) {
190                 case NOT_SET : dispatcherMapping = ERROR; break;
191                 case FORWARD : dispatcherMapping = FORWARD_ERROR; break;
192                 case INCLUDE : dispatcherMapping = INCLUDE_ERROR; break;
193                 case INCLUDE_FORWARD : dispatcherMapping = INCLUDE_ERROR_FORWARD; break;
194                 case REQUEST : dispatcherMapping = REQUEST_ERROR; break;
195                 case REQUEST_INCLUDE : dispatcherMapping = REQUEST_ERROR_INCLUDE; break;
196                 case REQUEST_FORWARD : dispatcherMapping = REQUEST_ERROR_FORWARD; break;
197                 case REQUEST_FORWARD_INCLUDE : dispatcherMapping = REQUEST_ERROR_FORWARD_INCLUDE; break;
198             }
199         }
200     }
201     
202     public int getDispatcherMapping() {
203         // per the SRV.6.2.5 absence of any dispatcher elements is
204
// equivelant to a REQUEST value
205
if (dispatcherMapping == NOT_SET) return REQUEST;
206         else return dispatcherMapping;
207     }
208
209
210     // --------------------------------------------------------- Public Methods
211

212
213     /**
214      * Render a String representation of this object.
215      */

216     public String JavaDoc toString() {
217
218         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("FilterMap[");
219         sb.append("filterName=");
220         sb.append(this.filterName);
221         for (int i = 0; i < servletNames.length; i++) {
222             sb.append(", servletName=");
223             sb.append(servletNames[i]);
224         }
225         for (int i = 0; i < urlPatterns.length; i++) {
226             sb.append(", urlPattern=");
227             sb.append(urlPatterns[i]);
228         }
229         sb.append("]");
230         return (sb.toString());
231
232     }
233
234
235 }
236
Popular Tags