KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > authorization > permissions > URLParameter


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name$
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library 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. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.core.authorization.permissions;
29
30 import java.io.Serializable JavaDoc;
31 import java.net.URI JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.regex.Pattern JavaDoc;
38
39 import org.apache.commons.lang.StringEscapeUtils;
40
41 /**
42  * represents an url key and her associated value.
43  * @author <a HREF="mailto:diabolo512@users.sourceforge.net ">Charles Gay</a>
44  * @author <a HREF="mailto:vberetti@users.sourceforge.net">Vincent Beretti</a>
45  */

46 public class URLParameter implements Serializable JavaDoc,Cloneable JavaDoc{
47
48      /**
49       * serial version id.
50       */

51       private static final long serialVersionUID = 3835156176932384821L;
52       private String JavaDoc key = null;
53
54       /**
55        * the HttpservlerRequest.getParameterValues() return a values Array.
56        * when the URL contains copies of a parameter name,
57        * this method return an array of all the values for the same parameter name.
58        */

59       private String JavaDoc[] value = null;
60       private String JavaDoc permissionName = null;
61
62      /**
63       * constructor.
64       */

65      protected URLParameter(){
66
67      }
68
69     /**
70      * override the java.lang.Object 's <i>clone</i> method.
71      * @return new URLParameter.
72      */

73     public Object JavaDoc clone()throws CloneNotSupportedException JavaDoc{
74         URLParameter param = new URLParameter();
75         param.setKey(new String JavaDoc(this.key));
76         param.setValue(this.value);
77         param.setPermissionName(new String JavaDoc(this.permissionName));
78         return param;
79     }
80
81     /**
82      * @return
83      */

84     protected String JavaDoc getKey() {
85         return key;
86     }
87
88     /**
89      * @return
90      */

91     protected String JavaDoc[] getValue() {
92         return value;
93     }
94
95     /**
96      * concat the array String in a single string with a ";" separator.
97      * @return concat String
98      */

99     protected String JavaDoc getValuesAsString(){
100         String JavaDoc[] valuesArray = this.value;
101         StringBuffer JavaDoc concatValues=new StringBuffer JavaDoc();
102         for(int i=0;i<valuesArray.length;i++){
103             if(i!=0){
104                 concatValues.append(";");
105             }
106                 concatValues.append(valuesArray[i]);
107         }
108         return concatValues.toString();
109     }
110     /**
111      * @param string
112      */

113     protected void setKey(String JavaDoc string) {
114         key = string;
115     }
116
117     /**
118      * @param string
119      */

120     protected void setValue(String JavaDoc[] string) {
121         value = string;
122     }
123
124     /**
125      * override java.lang.Object's <i>hashCode</i> method.
126      * @return int to compare quickly to URLParameters.
127      */

128     public int hashCode(){
129         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(this.key);
130         sb.append(this.getValuesAsString());
131         return sb.toString().hashCode();
132     }
133
134
135     /**
136      * override Object equals method.
137      */

138     public boolean equals(Object JavaDoc obj){
139         if(!(obj instanceof URLParameter)){
140             return false;
141         }
142         URLParameter urlp = (URLParameter)obj;
143
144
145         if(urlp.getKey().equals(this.key)==false){
146             return false;
147         }
148
149         String JavaDoc[] targetArray = urlp.getValue();
150         String JavaDoc[] myArray = this.value;
151
152         if(myArray.length !=targetArray.length){
153             return false;
154         }
155         //we sort these String arrays before comparing them
156
Arrays.sort(targetArray);
157         Arrays.sort(myArray);
158         for(int i=0;i< myArray.length;i++){
159             if(!(myArray[i]).equals(targetArray[i])){
160                 return false;
161             }
162         }
163
164         return true;
165     }
166
167
168
169
170
171     /**
172      * @return
173      */

174     protected String JavaDoc getPermissionName() {
175         return permissionName;
176     }
177
178     /**
179      * @param string
180      */

181     protected void setPermissionName(String JavaDoc string) {
182         permissionName = string;
183     }
184
185     /**
186      * from an URI and a Permission associated
187      * @param uri
188      * @return
189      */

190     protected static Set JavaDoc getURLParameters(URI JavaDoc uri){
191
192         Set JavaDoc parameters = new HashSet JavaDoc();
193            String JavaDoc query = uri.getQuery();
194            query = StringEscapeUtils.unescapeHtml(query);
195            if(query!=null && query!=""){
196            List JavaDoc tokens = Arrays.asList(query.split("&"));
197            Iterator JavaDoc itTokens = tokens.iterator();
198                while(itTokens.hasNext()){
199                    String JavaDoc token = (String JavaDoc)itTokens.next();
200                    String JavaDoc[] parts = token.split("=");
201                    if(parts.length==2){
202                        URLParameter param = new URLParameter();
203                        param.setKey(parts[0]);
204                        String JavaDoc[] values = parts[1].split(";");
205                        param.setValue(values);
206                        parameters.add(param);
207                    }
208                }
209
210            }
211            return parameters;
212     }
213
214     public boolean impliesKey(String JavaDoc key) {
215         String JavaDoc regexpKey = this.key.replaceAll("\\*","\\.\\*");
216         if(!key.equals(this.key) && !Pattern.matches(regexpKey, key)){
217             return false;
218         }
219         return true;
220     }
221
222     public boolean impliesValues(String JavaDoc[] value) {
223         if(value.length != this.value.length){
224             return false;
225         }
226         //we sort these String arrays before comparing them
227
Arrays.sort(value);
228         Arrays.sort(this.value);
229         for(int i=0;i< value.length;i++){
230             String JavaDoc regexpValue = this.value[i].replaceAll("\\*","\\.\\*");
231             if(!(value[i]).equals(this.value[i]) && !Pattern.matches(regexpValue, value[i])){
232                 return false;
233             }
234         }
235         return true;
236     }
237 }
238
Popular Tags