KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > security > WebResourceCollection


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.security;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.server.dispatch.UrlMap;
33 import com.caucho.server.util.CauchoSystem;
34 import com.caucho.util.L10N;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.regex.Pattern JavaDoc;
38 import java.util.regex.PatternSyntaxException JavaDoc;
39
40 /**
41  * Configuration for the web-resource-collection.
42  */

43 public class WebResourceCollection {
44   static L10N L = new L10N(WebResourceCollection.class);
45
46   public enum HttpMethod { GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE };
47
48   private String JavaDoc _webResourceName;
49   private String JavaDoc _description;
50   private ArrayList JavaDoc<String JavaDoc> _methodList;
51   private ArrayList JavaDoc<Pattern JavaDoc> _urlPatternList = new ArrayList JavaDoc<Pattern JavaDoc>();
52
53
54   /**
55    * Sets the web-resource-name.
56    */

57   public void setWebResourceName(String JavaDoc name)
58   {
59     _webResourceName = name;
60   }
61
62   /**
63    * Sets the description
64    */

65   public void setDescription(String JavaDoc name)
66   {
67     _description = name;
68   }
69   
70   /**
71    * Adds a url-pattern
72    */

73   public void addURLPattern(String JavaDoc pattern)
74     throws PatternSyntaxException JavaDoc
75   {
76     String JavaDoc regexpPattern = UrlMap.urlPatternToRegexpPattern(pattern);
77
78     int flags = (CauchoSystem.isCaseInsensitive() ?
79                  Pattern.CASE_INSENSITIVE :
80                  0);
81
82     Pattern JavaDoc regexp = Pattern.compile(regexpPattern, flags);
83
84     _urlPatternList.add(regexp);
85   }
86
87   /**
88    * Gets the pattern list
89    */

90   public ArrayList JavaDoc getURLPatternList()
91   {
92     return _urlPatternList;
93   }
94
95   /**
96    * Adds a method
97    */

98   public void addMethod(String JavaDoc method)
99   {
100     if (_methodList == null)
101       _methodList = new ArrayList JavaDoc<String JavaDoc>();
102
103     _methodList.add(method);
104   }
105
106   /**
107    * Adds a method
108    */

109   public void addHttpMethod(String JavaDoc method)
110   {
111     if (! Pattern.matches("[a-zA-Z]+", method)) {
112       throw new ConfigException(L.l("'{0}' is not a valid http-method.",
113                     method));
114     }
115     
116     /*
117     try {
118       HttpMethod.valueOf(method.toUpperCase());
119     }
120     catch (IllegalArgumentException e) {
121       StringBuilder builder = new StringBuilder();
122
123       for (HttpMethod validHttpMethod : EnumSet.allOf(HttpMethod.class)) {
124         if (builder.length() != 0)
125           builder.append(", ");
126
127         builder.append(validHttpMethod.name());
128       }
129
130       throw new ConfigException(L.l("'{0}' is not a valid value for '{1}', valid values are {2}", method, "http-method", builder));
131     }
132     */

133
134     if (_methodList == null)
135       _methodList = new ArrayList JavaDoc<String JavaDoc>();
136
137     _methodList.add(method);
138   }
139
140   /**
141    * Returns the methods.
142    */

143   public ArrayList JavaDoc<String JavaDoc> getMethods()
144   {
145     return _methodList;
146   }
147
148   /**
149    * Returns true if there's a pattern match.
150    */

151   public boolean isMatch(String JavaDoc url)
152   {
153     if (_urlPatternList.size() == 0)
154       return true;
155     
156     for (int i = 0; i < _urlPatternList.size(); i++) {
157       Pattern JavaDoc pattern = _urlPatternList.get(i);
158
159       if (pattern.matcher(url).find())
160         return true;
161     }
162
163     return false;
164   }
165 }
166
Popular Tags