KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26
27 import java.io.Serializable JavaDoc;
28
29
30 /**
31  * Representation of a web resource collection for a web application's security
32  * constraint, as represented in a <code>&lt;web-resource-collection&gt;</code>
33  * element in the deployment descriptor.
34  * <p>
35  * <b>WARNING</b>: It is assumed that instances of this class will be created
36  * and modified only within the context of a single thread, before the instance
37  * is made visible to the remainder of the application. After that, only read
38  * access is expected. Therefore, none of the read and write access within
39  * this class is synchronized.
40  *
41  * @author Craig R. McClanahan
42  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
43  */

44
45 public class SecurityCollection implements Serializable JavaDoc {
46
47     private static Log log = LogFactory.getLog(SecurityCollection.class);
48
49
50     // ----------------------------------------------------------- Constructors
51

52
53     /**
54      * Construct a new security collection instance with default values.
55      */

56     public SecurityCollection() {
57
58         this(null, null);
59
60     }
61
62
63     /**
64      * Construct a new security collection instance with specified values.
65      *
66      * @param name Name of this security collection
67      */

68     public SecurityCollection(String JavaDoc name) {
69
70         this(name, null);
71
72     }
73
74
75     /**
76      * Construct a new security collection instance with specified values.
77      *
78      * @param name Name of this security collection
79      * @param description Description of this security collection
80      */

81     public SecurityCollection(String JavaDoc name, String JavaDoc description) {
82
83         super();
84         setName(name);
85         setDescription(description);
86
87     }
88
89
90     // ----------------------------------------------------- Instance Variables
91

92
93     /**
94      * Description of this web resource collection.
95      */

96     private String JavaDoc description = null;
97
98
99     /**
100      * The HTTP methods covered by this web resource collection.
101      */

102     private String JavaDoc methods[] = new String JavaDoc[0];
103
104
105     /**
106      * The name of this web resource collection.
107      */

108     private String JavaDoc name = null;
109
110
111     /**
112      * The URL patterns protected by this security collection.
113      */

114     private String JavaDoc patterns[] = new String JavaDoc[0];
115
116
117     // ------------------------------------------------------------- Properties
118

119
120     /**
121      * Return the description of this web resource collection.
122      */

123     public String JavaDoc getDescription() {
124
125         return (this.description);
126
127     }
128
129
130     /**
131      * Set the description of this web resource collection.
132      *
133      * @param description The new description
134      */

135     public void setDescription(String JavaDoc description) {
136
137         this.description = description;
138
139     }
140
141
142     /**
143      * Return the name of this web resource collection.
144      */

145     public String JavaDoc getName() {
146
147         return (this.name);
148
149     }
150
151
152     /**
153      * Set the name of this web resource collection
154      *
155      * @param name The new name
156      */

157     public void setName(String JavaDoc name) {
158
159         this.name = name;
160
161     }
162
163
164     // --------------------------------------------------------- Public Methods
165

166
167     /**
168      * Add an HTTP request method to be part of this web resource collection.
169      */

170     public void addMethod(String JavaDoc method) {
171
172         if (method == null)
173             return;
174         String JavaDoc results[] = new String JavaDoc[methods.length + 1];
175         for (int i = 0; i < methods.length; i++)
176             results[i] = methods[i];
177         results[methods.length] = method;
178         methods = results;
179
180     }
181
182
183     /**
184      * Add a URL pattern to be part of this web resource collection.
185      */

186     public void addPattern(String JavaDoc pattern) {
187
188         if (pattern == null)
189             return;
190
191         // Bugzilla 34805: add friendly warning.
192
if(pattern.endsWith("*")) {
193           if (pattern.charAt(pattern.length()-1) != '/') {
194             if (log.isDebugEnabled()) {
195               log.warn("Suspicious url pattern: \"" + pattern + "\"" +
196                        " - see http://java.sun.com/aboutJava/communityprocess/first/jsr053/servlet23_PFD.pdf" +
197                        " section 11.2" );
198             }
199           }
200         }
201
202         pattern = RequestUtil.URLDecode(pattern);
203         String JavaDoc results[] = new String JavaDoc[patterns.length + 1];
204         for (int i = 0; i < patterns.length; i++) {
205             results[i] = patterns[i];
206         }
207         results[patterns.length] = pattern;
208         patterns = results;
209
210     }
211
212
213     /**
214      * Return <code>true</code> if the specified HTTP request method is
215      * part of this web resource collection.
216      *
217      * @param method Request method to check
218      */

219     public boolean findMethod(String JavaDoc method) {
220
221         if (methods.length == 0)
222             return (true);
223         for (int i = 0; i < methods.length; i++) {
224             if (methods[i].equals(method))
225                 return (true);
226         }
227         return (false);
228
229     }
230
231
232     /**
233      * Return the set of HTTP request methods that are part of this web
234      * resource collection, or a zero-length array if all request methods
235      * are included.
236      */

237     public String JavaDoc[] findMethods() {
238
239         return (methods);
240
241     }
242
243
244     /**
245      * Is the specified pattern part of this web resource collection?
246      *
247      * @param pattern Pattern to be compared
248      */

249     public boolean findPattern(String JavaDoc pattern) {
250
251         for (int i = 0; i < patterns.length; i++) {
252             if (patterns[i].equals(pattern))
253                 return (true);
254         }
255         return (false);
256
257     }
258
259
260     /**
261      * Return the set of URL patterns that are part of this web resource
262      * collection. If none have been specified, a zero-length array is
263      * returned.
264      */

265     public String JavaDoc[] findPatterns() {
266
267         return (patterns);
268
269     }
270
271
272     /**
273      * Remove the specified HTTP request method from those that are part
274      * of this web resource collection.
275      *
276      * @param method Request method to be removed
277      */

278     public void removeMethod(String JavaDoc method) {
279
280         if (method == null)
281             return;
282         int n = -1;
283         for (int i = 0; i < methods.length; i++) {
284             if (methods[i].equals(method)) {
285                 n = i;
286                 break;
287             }
288         }
289         if (n >= 0) {
290             int j = 0;
291             String JavaDoc results[] = new String JavaDoc[methods.length - 1];
292             for (int i = 0; i < methods.length; i++) {
293                 if (i != n)
294                     results[j++] = methods[i];
295             }
296             methods = results;
297         }
298
299     }
300
301
302     /**
303      * Remove the specified URL pattern from those that are part of this
304      * web resource collection.
305      *
306      * @param pattern Pattern to be removed
307      */

308     public void removePattern(String JavaDoc pattern) {
309
310         if (pattern == null)
311             return;
312         int n = -1;
313         for (int i = 0; i < patterns.length; i++) {
314             if (patterns[i].equals(pattern)) {
315                 n = i;
316                 break;
317             }
318         }
319         if (n >= 0) {
320             int j = 0;
321             String JavaDoc results[] = new String JavaDoc[patterns.length - 1];
322             for (int i = 0; i < patterns.length; i++) {
323                 if (i != n)
324                     results[j++] = patterns[i];
325             }
326             patterns = results;
327         }
328
329     }
330
331
332     /**
333      * Return a String representation of this security collection.
334      */

335     public String JavaDoc toString() {
336
337         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SecurityCollection[");
338         sb.append(name);
339         if (description != null) {
340             sb.append(", ");
341             sb.append(description);
342         }
343         sb.append("]");
344         return (sb.toString());
345
346     }
347
348
349 }
350
Popular Tags