001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/deploy/SecurityCollection.java,v 1.3 2001/07/22 20:25:10 pier Exp $
003: * $Revision: 1.3 $
004: * $Date: 2001/07/22 20:25:10 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.deploy;
065:
066: import org.apache.catalina.util.RequestUtil;
067:
068: /**
069: * Representation of a web resource collection for a web application's security
070: * constraint, as represented in a <code><web-resource-collection></code>
071: * element in the deployment descriptor.
072: * <p>
073: * <b>WARNING</b>: It is assumed that instances of this class will be created
074: * and modified only within the context of a single thread, before the instance
075: * is made visible to the remainder of the application. After that, only read
076: * access is expected. Therefore, none of the read and write access within
077: * this class is synchronized.
078: *
079: * @author Craig R. McClanahan
080: * @version $Revision: 1.3 $ $Date: 2001/07/22 20:25:10 $
081: */
082:
083: public final class SecurityCollection {
084:
085: // ----------------------------------------------------------- Constructors
086:
087: /**
088: * Construct a new security collection instance with default values.
089: */
090: public SecurityCollection() {
091:
092: this (null, null);
093:
094: }
095:
096: /**
097: * Construct a new security collection instance with specified values.
098: *
099: * @param name Name of this security collection
100: */
101: public SecurityCollection(String name) {
102:
103: this (name, null);
104:
105: }
106:
107: /**
108: * Construct a new security collection instance with specified values.
109: *
110: * @param name Name of this security collection
111: * @param description Description of this security collection
112: */
113: public SecurityCollection(String name, String description) {
114:
115: super ();
116: setName(name);
117: setDescription(description);
118:
119: }
120:
121: // ----------------------------------------------------- Instance Variables
122:
123: /**
124: * Description of this web resource collection.
125: */
126: private String description = null;
127:
128: /**
129: * The HTTP methods covered by this web resource collection.
130: */
131: private String methods[] = new String[0];
132:
133: /**
134: * The name of this web resource collection.
135: */
136: private String name = null;
137:
138: /**
139: * The URL patterns protected by this security collection.
140: */
141: private String patterns[] = new String[0];
142:
143: // ------------------------------------------------------------- Properties
144:
145: /**
146: * Return the description of this web resource collection.
147: */
148: public String getDescription() {
149:
150: return (this .description);
151:
152: }
153:
154: /**
155: * Set the description of this web resource collection.
156: *
157: * @param description The new description
158: */
159: public void setDescription(String description) {
160:
161: this .description = description;
162:
163: }
164:
165: /**
166: * Return the name of this web resource collection.
167: */
168: public String getName() {
169:
170: return (this .name);
171:
172: }
173:
174: /**
175: * Set the name of this web resource collection
176: *
177: * @param name The new name
178: */
179: public void setName(String name) {
180:
181: this .name = name;
182:
183: }
184:
185: // --------------------------------------------------------- Public Methods
186:
187: /**
188: * Add an HTTP request method to be part of this web resource collection.
189: */
190: public void addMethod(String method) {
191:
192: if (method == null)
193: return;
194: String results[] = new String[methods.length + 1];
195: for (int i = 0; i < methods.length; i++)
196: results[i] = methods[i];
197: results[methods.length] = method;
198: methods = results;
199:
200: }
201:
202: /**
203: * Add a URL pattern to be part of this web resource collection.
204: */
205: public void addPattern(String pattern) {
206:
207: if (pattern == null)
208: return;
209: pattern = RequestUtil.URLDecode(pattern);
210: String results[] = new String[patterns.length + 1];
211: for (int i = 0; i < patterns.length; i++)
212: results[i] = patterns[i];
213: results[patterns.length] = pattern;
214: patterns = results;
215:
216: }
217:
218: /**
219: * Return <code>true</code> if the specified HTTP request method is
220: * part of this web resource collection.
221: *
222: * @param method Request method to check
223: */
224: public boolean findMethod(String method) {
225:
226: if (methods.length == 0)
227: return (true);
228: for (int i = 0; i < methods.length; i++) {
229: if (methods[i].equals(method))
230: return (true);
231: }
232: return (false);
233:
234: }
235:
236: /**
237: * Return the set of HTTP request methods that are part of this web
238: * resource collection, or a zero-length array if all request methods
239: * are included.
240: */
241: public String[] findMethods() {
242:
243: return (methods);
244:
245: }
246:
247: /**
248: * Is the specified pattern part of this web resource collection?
249: *
250: * @param pattern Pattern to be compared
251: */
252: public boolean findPattern(String pattern) {
253:
254: for (int i = 0; i < patterns.length; i++) {
255: if (patterns[i].equals(pattern))
256: return (true);
257: }
258: return (false);
259:
260: }
261:
262: /**
263: * Return the set of URL patterns that are part of this web resource
264: * collection. If none have been specified, a zero-length array is
265: * returned.
266: */
267: public String[] findPatterns() {
268:
269: return (patterns);
270:
271: }
272:
273: /**
274: * Remove the specified HTTP request method from those that are part
275: * of this web resource collection.
276: *
277: * @param method Request method to be removed
278: */
279: public void removeMethod(String method) {
280:
281: if (method == null)
282: return;
283: int n = -1;
284: for (int i = 0; i < methods.length; i++) {
285: if (methods[i].equals(method)) {
286: n = i;
287: break;
288: }
289: }
290: if (n >= 0) {
291: int j = 0;
292: String results[] = new String[methods.length - 1];
293: for (int i = 0; i < methods.length; i++) {
294: if (i != n)
295: results[j++] = methods[i];
296: }
297: methods = results;
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 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 results[] = new String[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: * Return a String representation of this security collection.
333: */
334: public String toString() {
335:
336: StringBuffer sb = new StringBuffer("SecurityCollection[");
337: sb.append(name);
338: if (description != null) {
339: sb.append(", ");
340: sb.append(description);
341: }
342: sb.append("]");
343: return (sb.toString());
344:
345: }
346:
347: }
|