KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_web > deployment > api > PatternEntry


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: PatternEntry.java,v 1.3 2004/07/09 11:44:06 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_web.deployment.api;
28
29 import java.security.Permission JavaDoc;
30 import java.security.PermissionCollection JavaDoc;
31 import java.security.Permissions JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import javax.security.jacc.WebResourcePermission JavaDoc;
40 import javax.security.jacc.WebUserDataPermission JavaDoc;
41
42
43 /**
44  * Defines a PatternEntry object for JACC URLPattern handle
45  * @author Florent Benoit
46  */

47 public class PatternEntry {
48
49     /**
50      * Pattern of this PatternEntry
51      */

52     private Pattern pattern = null;
53
54
55     /**
56      * This pattern will be unchecked (default pattern added to all others)
57      */

58     private boolean uncheckedLastEntry = false;
59
60     /**
61      * Methods for this pattern
62      */

63     private MethodsDesc methods = null;
64
65     /**
66      * Is this pattern irrelevant while adding qualified pattern
67      */

68     private boolean irrelevant = false;
69
70
71     /**
72      * Qualifying pattern
73      */

74     private StringBuffer JavaDoc qualified = null;
75
76     /**
77      * Constructor
78      * @param pattern used by this PatternEntry object
79      */

80     public PatternEntry(String JavaDoc pattern) {
81         this.pattern = new Pattern(pattern);
82         methods = new MethodsDesc();
83         qualified = new StringBuffer JavaDoc(pattern);
84     }
85
86
87     /**
88      * Add Http methods (Excluded or Unchecked)
89      * @param methods array of methods to add
90      * @param transportGuarantee Transport Guarantee for these methods
91      * @param isExcluded if true add methods as excluded else as unchecked
92      */

93     public void addMethods(String JavaDoc[] methods, String JavaDoc transportGuarantee, boolean isExcluded) {
94         this.methods.addMethods(methods, transportGuarantee, isExcluded);
95     }
96
97     /**
98      * Add Excluded Http methods
99      * @param methods array of methods to add
100      * @param transportGuarantee Transport Guarantee for these methods
101      */

102     public void addExcludedMethods(String JavaDoc[] methods, String JavaDoc transportGuarantee) {
103         addMethods(methods, transportGuarantee, true);
104     }
105
106     /**
107      * Add Unchecked Http methods
108      * @param methods array of methods to add
109      * @param transportGuarantee Transport Guarantee for these methods
110      */

111     public void addUncheckedMethods(String JavaDoc[] methods, String JavaDoc transportGuarantee) {
112         addMethods(methods, transportGuarantee, false);
113     }
114
115
116     /**
117      * Add pattern information for given roles
118      * @param methods methods to add to the given role
119      * @param roles roles which have the given methods
120      * @param transportGuarantee Transport Guarantee for these methods
121      */

122     public void addMethodsOnRoles(String JavaDoc[] methods, String JavaDoc[] roles, String JavaDoc transportGuarantee) {
123         for (int r = 0; r < roles.length; r++) {
124             addMethodsOnRole(methods, roles[r], transportGuarantee);
125         }
126     }
127
128
129     /**
130      * Add pattern information for a given role
131      * @param methods methods to add to the given role
132      * @param role role which have the given methods
133      * @param transportGuarantee Transport Guarantee for these methods
134      */

135     public void addMethodsOnRole(String JavaDoc[] methods, String JavaDoc role, String JavaDoc transportGuarantee) {
136         this.methods.addMethodsOnRole(methods, role, transportGuarantee);
137     }
138
139     /**
140      * Set the flag which indicate that this entry will be added at the end
141      * as unchecked permission
142      */

143     public void setUncheckedLastEntry() {
144         uncheckedLastEntry = true;
145     }
146
147     /**
148      * Gets the boolean value of the flag which indicate that this entry
149      * will be added at the end as unchecked permission
150      * @return true if this is the pattern to add at the end
151      */

152     public boolean isUncheckedLastEntry() {
153         return uncheckedLastEntry;
154     }
155
156
157
158
159     /**
160      * Add to this pattern a qualified pattern
161      * @see jacc 3.1.3.1 (Qualifying pattern)
162      * @param otherPattern pattern to add for the qualified pattern
163      */

164     public void addQualifiedPattern(Pattern otherPattern) {
165         /*
166          * Any pattern, qualified by a pattern that matches it, is overriden
167          * and made irrelevant (in the translation) by the qualifying pattern
168          */

169         if (otherPattern.isMatching(pattern)) {
170             irrelevant = true;
171         } else {
172             qualified.append(":");
173             qualified.append(otherPattern);
174         }
175
176     }
177
178     /**
179      * Gets the permissions for each role.
180      * Map between role and permissions
181      * @return the Map witth key = role, value = permissions
182      */

183     public Map JavaDoc getRolesPermissionsMap() {
184         Map JavaDoc roleMapActions = methods.getRoleMapActions();
185         String JavaDoc roleName = null;
186         String JavaDoc actions = null;
187         Map JavaDoc rolesPermissionsMap = new HashMap JavaDoc();
188
189         // Need to build WebResource for each role Actions
190
for (Iterator JavaDoc it = roleMapActions.keySet().iterator(); it.hasNext();) {
191             roleName = (String JavaDoc) it.next();
192             actions = (String JavaDoc) roleMapActions.get(roleName);
193             if (actions != null) {
194                 PermissionCollection JavaDoc pc = new Permissions JavaDoc();
195                 pc.add(new WebResourcePermission JavaDoc(getQualifiedPattern(), actions));
196                 rolesPermissionsMap.put(roleName, pc);
197             }
198         }
199         return rolesPermissionsMap;
200     }
201
202
203     /**
204      * Gets the excluded permissions for this pattern
205      * @return the excluded permissions for this pattern
206      */

207     public PermissionCollection JavaDoc getExcludedPermissions() {
208         // Need to build WebResource and WebuserData on these actions
209
PermissionCollection JavaDoc pc = new Permissions JavaDoc();
210         String JavaDoc actions = methods.getExcludedActions();
211         if (!actions.equals("")) {
212             pc.add(new WebResourcePermission JavaDoc(getQualifiedPattern(), actions));
213             pc.add(new WebUserDataPermission JavaDoc(getQualifiedPattern(), actions));
214         }
215         return pc;
216     }
217
218
219     /**
220      * Gets the unchecked permissions for this pattern
221      * @return the unchecked permissions for this pattern
222      */

223     public PermissionCollection JavaDoc getUncheckedPermissions() {
224         // First add unchecked permissions and then the WebUserData
225
// permissions of no excluding auth-constraint
226
String JavaDoc actions = null;
227         List JavaDoc permissions = new ArrayList JavaDoc();
228
229         actions = methods.getUncheckedActions();
230         if (actions == null || (!actions.equals(""))) {
231             permissions.add(new WebResourcePermission JavaDoc(getQualifiedPattern(), actions));
232             permissions.add(new WebUserDataPermission JavaDoc(getQualifiedPattern(), actions));
233         }
234
235         // WebUserData (no excluding auth-constraint)
236
List JavaDoc actionsList = methods.getUncheckedWebUserDataActionsRoleList();
237         for (Iterator JavaDoc it = actionsList.iterator(); it.hasNext();) {
238             actions = (String JavaDoc) it.next();
239             permissions.add(new WebUserDataPermission JavaDoc(getQualifiedPattern(), actions));
240         }
241
242         // Try to merge UserDataPermissions with same transport guarantee
243
PermissionCollection JavaDoc pc = new Permissions JavaDoc();
244         for (Iterator JavaDoc it = permissions.iterator(); it.hasNext();) {
245             Permission JavaDoc p = (Permission JavaDoc) it.next();
246             if (p instanceof WebUserDataPermission JavaDoc) {
247                 WebUserDataPermission JavaDoc wdp = (WebUserDataPermission JavaDoc) p;
248                 // get actions of this permission
249
String JavaDoc wdpName = wdp.getName();
250                 String JavaDoc wdpActions = wdp.getActions();
251                 if (wdpActions == null) {
252                     // If the permissions got all actions (null = all actions)
253
pc.add(p);
254                     continue;
255                 }
256                 boolean wasMerged = false;
257                 // Now, search all permissions with same transport guarantee
258
for (Iterator JavaDoc itLoop = permissions.iterator(); itLoop.hasNext();) {
259                     Permission JavaDoc loopPerm = (Permission JavaDoc) itLoop.next();
260                     if (loopPerm instanceof WebUserDataPermission JavaDoc) {
261                         WebUserDataPermission JavaDoc loopWdp = (WebUserDataPermission JavaDoc) loopPerm;
262                         // if same permission than our, go on
263
if (loopWdp.equals(wdp)) {
264                             continue;
265                         }
266                         String JavaDoc loopWdpName = loopWdp.getName();
267                         String JavaDoc loopWdpActions = loopWdp.getActions();
268                         if (loopWdpActions == null) {
269                             continue;
270                         }
271                         boolean wNoTransport = (wdpActions.indexOf(":") == -1);
272                         boolean loopNoWTransport = (loopWdpActions.indexOf(":") == -1);
273
274                         // Same name and no transport guarantee
275
if (wdpName.equals(loopWdpName) && wNoTransport && loopNoWTransport) {
276                             // merge actions
277
String JavaDoc newActions = wdpActions + "," + loopWdpActions;
278
279                             //Add new actions if it doesn't exists
280
Enumeration JavaDoc existingPermissions = pc.elements();
281                             boolean exist = false;
282                             Permission JavaDoc permissionToAdd = new WebUserDataPermission JavaDoc(wdpName, newActions);
283                             while (existingPermissions.hasMoreElements()) {
284                                 Permission JavaDoc perm = (Permission JavaDoc) existingPermissions.nextElement();
285                                 if (perm.equals(permissionToAdd)) {
286                                     exist = true;
287                                 }
288                             }
289                             if (!exist) {
290                                 wasMerged = true;
291                                 pc.add(permissionToAdd);
292                             }
293                         }
294                     }
295                 }
296                 // There was no merge for this permission, just add it.
297
if (!wasMerged) {
298                     pc.add(p);
299                 }
300
301             } else {
302                 // Do not merge as it is a WebResourcePermission
303
pc.add(p);
304             }
305         }
306
307
308         return pc;
309     }
310
311
312     /**
313      * Gets the state of the pattern. Irrelevant or not during the qualifying phase
314      * @return true if the pattern is irrelevant, false otherwise
315      */

316     public boolean isIrrelevant() {
317         return irrelevant;
318     }
319
320     /**
321      * Gets the qualified form of the pattern
322      * @return qualified pattern
323      */

324     public String JavaDoc getQualifiedPattern() {
325         return qualified.toString();
326     }
327
328     /**
329      * String representation
330      * @return string representation of the pattern
331      */

332     public String JavaDoc toString() {
333         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
334         sb.append("PatternEntry[pattern=");
335         sb.append(pattern);
336         sb.append(";qualified=");
337         sb.append(getQualifiedPattern());
338         sb.append(";irrelevant=");
339         sb.append(irrelevant);
340         sb.append("]");
341         return sb.toString();
342     }
343 }
344
Popular Tags