001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.security;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.server.dispatch.UrlMap;
033: import com.caucho.server.util.CauchoSystem;
034: import com.caucho.util.L10N;
035:
036: import java.util.ArrayList;
037: import java.util.regex.Pattern;
038: import java.util.regex.PatternSyntaxException;
039:
040: /**
041: * Configuration for the web-resource-collection.
042: */
043: public class WebResourceCollection {
044: static L10N L = new L10N(WebResourceCollection.class);
045:
046: public enum HttpMethod {
047: GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE
048: };
049:
050: private String _webResourceName;
051: private String _description;
052: private ArrayList<String> _methodList;
053: private ArrayList<Pattern> _urlPatternList = new ArrayList<Pattern>();
054:
055: /**
056: * Sets the web-resource-name.
057: */
058: public void setWebResourceName(String name) {
059: _webResourceName = name;
060: }
061:
062: /**
063: * Sets the description
064: */
065: public void setDescription(String name) {
066: _description = name;
067: }
068:
069: /**
070: * Adds a url-pattern
071: */
072: public void addURLPattern(String pattern)
073: throws PatternSyntaxException {
074: String regexpPattern = UrlMap
075: .urlPatternToRegexpPattern(pattern);
076:
077: int flags = (CauchoSystem.isCaseInsensitive() ? Pattern.CASE_INSENSITIVE
078: : 0);
079:
080: Pattern regexp = Pattern.compile(regexpPattern, flags);
081:
082: _urlPatternList.add(regexp);
083: }
084:
085: /**
086: * Gets the pattern list
087: */
088: public ArrayList getURLPatternList() {
089: return _urlPatternList;
090: }
091:
092: /**
093: * Adds a method
094: */
095: public void addMethod(String method) {
096: if (_methodList == null)
097: _methodList = new ArrayList<String>();
098:
099: _methodList.add(method);
100: }
101:
102: /**
103: * Adds a method
104: */
105: public void addHttpMethod(String method) {
106: if (!Pattern.matches("[a-zA-Z]+", method)) {
107: throw new ConfigException(L.l(
108: "'{0}' is not a valid http-method.", method));
109: }
110:
111: /*
112: try {
113: HttpMethod.valueOf(method.toUpperCase());
114: }
115: catch (IllegalArgumentException e) {
116: StringBuilder builder = new StringBuilder();
117:
118: for (HttpMethod validHttpMethod : EnumSet.allOf(HttpMethod.class)) {
119: if (builder.length() != 0)
120: builder.append(", ");
121:
122: builder.append(validHttpMethod.name());
123: }
124:
125: throw new ConfigException(L.l("'{0}' is not a valid value for '{1}', valid values are {2}", method, "http-method", builder));
126: }
127: */
128:
129: if (_methodList == null)
130: _methodList = new ArrayList<String>();
131:
132: _methodList.add(method);
133: }
134:
135: /**
136: * Returns the methods.
137: */
138: public ArrayList<String> getMethods() {
139: return _methodList;
140: }
141:
142: /**
143: * Returns true if there's a pattern match.
144: */
145: public boolean isMatch(String url) {
146: if (_urlPatternList.size() == 0)
147: return true;
148:
149: for (int i = 0; i < _urlPatternList.size(); i++) {
150: Pattern pattern = _urlPatternList.get(i);
151:
152: if (pattern.matcher(url).find())
153: return true;
154: }
155:
156: return false;
157: }
158: }
|