001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.deploy;
018:
019: import org.apache.catalina.util.RequestUtil;
020: import java.io.Serializable;
021:
022: /**
023: * Representation of a web resource collection for a web application's security
024: * constraint, as represented in a <code><web-resource-collection></code>
025: * element in the deployment descriptor.
026: * <p>
027: * <b>WARNING</b>: It is assumed that instances of this class will be created
028: * and modified only within the context of a single thread, before the instance
029: * is made visible to the remainder of the application. After that, only read
030: * access is expected. Therefore, none of the read and write access within
031: * this class is synchronized.
032: *
033: * @author Craig R. McClanahan
034: * @version $Revision: 1.4 $ $Date: 2004/05/13 20:40:49 $
035: */
036:
037: public class SecurityCollection implements Serializable {
038:
039: // ----------------------------------------------------------- Constructors
040:
041: /**
042: * Construct a new security collection instance with default values.
043: */
044: public SecurityCollection() {
045:
046: this (null, null);
047:
048: }
049:
050: /**
051: * Construct a new security collection instance with specified values.
052: *
053: * @param name Name of this security collection
054: */
055: public SecurityCollection(String name) {
056:
057: this (name, null);
058:
059: }
060:
061: /**
062: * Construct a new security collection instance with specified values.
063: *
064: * @param name Name of this security collection
065: * @param description Description of this security collection
066: */
067: public SecurityCollection(String name, String description) {
068:
069: super ();
070: setName(name);
071: setDescription(description);
072:
073: }
074:
075: // ----------------------------------------------------- Instance Variables
076:
077: /**
078: * Description of this web resource collection.
079: */
080: private String description = null;
081:
082: /**
083: * The HTTP methods covered by this web resource collection.
084: */
085: private String methods[] = new String[0];
086:
087: /**
088: * The name of this web resource collection.
089: */
090: private String name = null;
091:
092: /**
093: * The URL patterns protected by this security collection.
094: */
095: private String patterns[] = new String[0];
096:
097: // ------------------------------------------------------------- Properties
098:
099: /**
100: * Return the description of this web resource collection.
101: */
102: public String getDescription() {
103:
104: return (this .description);
105:
106: }
107:
108: /**
109: * Set the description of this web resource collection.
110: *
111: * @param description The new description
112: */
113: public void setDescription(String description) {
114:
115: this .description = description;
116:
117: }
118:
119: /**
120: * Return the name of this web resource collection.
121: */
122: public String getName() {
123:
124: return (this .name);
125:
126: }
127:
128: /**
129: * Set the name of this web resource collection
130: *
131: * @param name The new name
132: */
133: public void setName(String name) {
134:
135: this .name = name;
136:
137: }
138:
139: // --------------------------------------------------------- Public Methods
140:
141: /**
142: * Add an HTTP request method to be part of this web resource collection.
143: */
144: public void addMethod(String method) {
145:
146: if (method == null)
147: return;
148: String results[] = new String[methods.length + 1];
149: for (int i = 0; i < methods.length; i++)
150: results[i] = methods[i];
151: results[methods.length] = method;
152: methods = results;
153:
154: }
155:
156: /**
157: * Add a URL pattern to be part of this web resource collection.
158: */
159: public void addPattern(String pattern) {
160:
161: if (pattern == null)
162: return;
163: pattern = RequestUtil.URLDecode(pattern);
164: String results[] = new String[patterns.length + 1];
165: for (int i = 0; i < patterns.length; i++)
166: results[i] = patterns[i];
167: results[patterns.length] = pattern;
168: patterns = results;
169:
170: }
171:
172: /**
173: * Return <code>true</code> if the specified HTTP request method is
174: * part of this web resource collection.
175: *
176: * @param method Request method to check
177: */
178: public boolean findMethod(String method) {
179:
180: if (methods.length == 0)
181: return (true);
182: for (int i = 0; i < methods.length; i++) {
183: if (methods[i].equals(method))
184: return (true);
185: }
186: return (false);
187:
188: }
189:
190: /**
191: * Return the set of HTTP request methods that are part of this web
192: * resource collection, or a zero-length array if all request methods
193: * are included.
194: */
195: public String[] findMethods() {
196:
197: return (methods);
198:
199: }
200:
201: /**
202: * Is the specified pattern part of this web resource collection?
203: *
204: * @param pattern Pattern to be compared
205: */
206: public boolean findPattern(String pattern) {
207:
208: for (int i = 0; i < patterns.length; i++) {
209: if (patterns[i].equals(pattern))
210: return (true);
211: }
212: return (false);
213:
214: }
215:
216: /**
217: * Return the set of URL patterns that are part of this web resource
218: * collection. If none have been specified, a zero-length array is
219: * returned.
220: */
221: public String[] findPatterns() {
222:
223: return (patterns);
224:
225: }
226:
227: /**
228: * Remove the specified HTTP request method from those that are part
229: * of this web resource collection.
230: *
231: * @param method Request method to be removed
232: */
233: public void removeMethod(String method) {
234:
235: if (method == null)
236: return;
237: int n = -1;
238: for (int i = 0; i < methods.length; i++) {
239: if (methods[i].equals(method)) {
240: n = i;
241: break;
242: }
243: }
244: if (n >= 0) {
245: int j = 0;
246: String results[] = new String[methods.length - 1];
247: for (int i = 0; i < methods.length; i++) {
248: if (i != n)
249: results[j++] = methods[i];
250: }
251: methods = results;
252: }
253:
254: }
255:
256: /**
257: * Remove the specified URL pattern from those that are part of this
258: * web resource collection.
259: *
260: * @param pattern Pattern to be removed
261: */
262: public void removePattern(String pattern) {
263:
264: if (pattern == null)
265: return;
266: int n = -1;
267: for (int i = 0; i < patterns.length; i++) {
268: if (patterns[i].equals(pattern)) {
269: n = i;
270: break;
271: }
272: }
273: if (n >= 0) {
274: int j = 0;
275: String results[] = new String[patterns.length - 1];
276: for (int i = 0; i < patterns.length; i++) {
277: if (i != n)
278: results[j++] = patterns[i];
279: }
280: patterns = results;
281: }
282:
283: }
284:
285: /**
286: * Return a String representation of this security collection.
287: */
288: public String toString() {
289:
290: StringBuffer sb = new StringBuffer("SecurityCollection[");
291: sb.append(name);
292: if (description != null) {
293: sb.append(", ");
294: sb.append(description);
295: }
296: sb.append("]");
297: return (sb.toString());
298:
299: }
300:
301: }
|