KickJava   Java API By Example, From Geeks To Geeks.

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


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: MethodsDesc.java,v 1.2 2004/06/01 11:24:01 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_web.deployment.api;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * Defines the methods class managing all http methods
37  * @author Florent Benoit
38  */

39 public class MethodsDesc {
40
41     /**
42      * List of Methods
43      */

44     private Map JavaDoc httpMethods = null;
45
46     /**
47      * Available HTTP METHODS
48      */

49     public static final String JavaDoc[] METHODS = new String JavaDoc[] {
50         "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "TRACE"
51     };
52
53     /**
54      * Size of all actions enumerated : "DELETE,GET,HEAD,OPTIONS,POST,PUT,TRACE"
55      */

56     private static final int ALL_ACTIONS_LENGTH = 38;
57
58     /**
59      * Constructor
60      * Build new Http Methods
61      */

62     public MethodsDesc() {
63         httpMethods = new HashMap JavaDoc();
64         for (int m = 0; m < METHODS.length; m++) {
65             httpMethods.put(METHODS[m], new MethodDesc(METHODS[m]));
66         }
67     }
68
69     /**
70      * Add Http methods (Excluded or Unchecked)
71      * @param methods array of methods to add
72      * @param transportGuarantee Transport Guarantee for these methods
73      * @param isExcluded if true add methods as excluded else as unchecked
74      */

75     public void addMethods(String JavaDoc[] methods, String JavaDoc transportGuarantee, boolean isExcluded) {
76         MethodDesc method = null;
77         for (int m = 0; m < methods.length; m++) {
78             method = getMethod(methods[m]);
79             method.addTransportGuarantee(transportGuarantee);
80             if (isExcluded) {
81                 method.setExcluded();
82             } else {
83                 method.setUnchecked();
84             }
85         }
86     }
87
88     /**
89      * Add pattern information for a given role
90      * @param methods methods to add to the given role
91      * @param role role which have the given methods
92      * @param transportGuarantee Transport Guarantee for these methods
93      */

94     public void addMethodsOnRole(String JavaDoc[] methods, String JavaDoc role, String JavaDoc transportGuarantee) {
95         for (int m = 0; m < methods.length; m++) {
96             getMethod(methods[m]).addRole(role, transportGuarantee);
97         }
98     }
99
100
101     /**
102      * Gets the method object
103      * @param methodName name of the method
104      * @return Method object
105      */

106     private MethodDesc getMethod(String JavaDoc methodName) {
107         MethodDesc m = (MethodDesc) httpMethods.get(methodName.toUpperCase());
108         if (m == null) {
109             throw new IllegalStateException JavaDoc("No such method named '" + methodName + "'.");
110         } else {
111             return m;
112         }
113     }
114
115
116
117     /**
118      * Gets the excluded actions in order to build permissions
119      * @return actions in order to build permissions
120      */

121     public String JavaDoc getExcludedActions() {
122         StringBuffer JavaDoc actions = new StringBuffer JavaDoc();
123         MethodDesc method = null;
124         for (Iterator JavaDoc it = httpMethods.values().iterator(); it.hasNext();) {
125             method = (MethodDesc) it.next();
126             if (method.isExcluded() && !method.hasRole()) {
127                 // first item or append item ?
128
if (actions.length() > 0) {
129                     actions.append(",");
130                 }
131                 actions.append(method.getName());
132             }
133         }
134         return actions.toString();
135     }
136
137
138     /**
139      * Gets the unchecked actions in order to build permissions
140      * @return actions in order to build permissions
141      */

142     public String JavaDoc getUncheckedActions() {
143         StringBuffer JavaDoc actions = new StringBuffer JavaDoc();
144         MethodDesc method = null;
145         for (Iterator JavaDoc it = httpMethods.values().iterator(); it.hasNext();) {
146             method = (MethodDesc) it.next();
147             if (method.isUnchecked() && !method.hasRole()) {
148                 // first item or append item ?
149
if (actions.length() > 0) {
150                     actions.append(",");
151                 }
152                 actions.append(method.getName());
153             }
154         }
155         // Contains all actions
156
if (actions.length() == ALL_ACTIONS_LENGTH) {
157             return null;
158         } else {
159             return actions.toString();
160         }
161     }
162
163     /**
164      * Gets the Map between roles and their actions
165      * @return map between roles and their actions in order to build permissions
166      */

167     public Map JavaDoc getRoleMapActions() {
168         MethodDesc method = null;
169         Map JavaDoc rolesMap = new HashMap JavaDoc();
170         for (Iterator JavaDoc it = httpMethods.values().iterator(); it.hasNext();) {
171             method = (MethodDesc) it.next();
172             if (method.hasRole()) {
173                 for (Iterator JavaDoc itRoles = method.getRolesIterator(); itRoles.hasNext();) {
174
175                     String JavaDoc roleName = (String JavaDoc) itRoles.next();
176                     String JavaDoc actions = (String JavaDoc) rolesMap.get(roleName);
177                     if (actions == null) {
178                         actions = method.getName();
179                     } else {
180                         actions += ",";
181                         actions += method.getName();
182                     }
183                     rolesMap.put(roleName, actions);
184                 }
185             }
186         }
187         return rolesMap;
188     }
189
190
191     /**
192      * Gets the list of unchecked permissions for all element
193      * that do not contain an excluding auth-constraint
194      * @see 3.1.3.1 WebUserDataPermission for excluding auth constraint
195      * @return list of actions for WebUserData permissions
196      */

197     public List JavaDoc getUncheckedWebUserDataActionsRoleList() {
198
199         MethodDesc method = null;
200         // Temporary list.
201
StringBuffer JavaDoc nones = null;
202         StringBuffer JavaDoc confidentials = null;
203         StringBuffer JavaDoc integrals = null;
204
205         // Build unchecked actions
206
for (Iterator JavaDoc it = httpMethods.values().iterator(); it.hasNext();) {
207             method = (MethodDesc) it.next();
208             String JavaDoc methodName = method.getName();
209             // Only if not contain an excluding auth-constraint
210
if (method.isUnchecked() && method.hasRole()) {
211                 // Add NONE for each method found
212
if (method.getTransportGuarantee().hasNone()) {
213                     if (nones == null) {
214                         nones = new StringBuffer JavaDoc(methodName);
215                     } else {
216                         nones.append(",");
217                         nones.append(methodName);
218                     }
219                 }
220                 // Add INTEGRAL on method if it is the case
221
if (method.getTransportGuarantee().isIntegral()) {
222                     if (integrals == null) {
223                         integrals = new StringBuffer JavaDoc(methodName);
224                     } else {
225                         integrals.append(",");
226                         integrals.append(methodName);
227                     }
228                 }
229
230                 // Add CONFIDENTIAL on method if it is the case
231
if (method.getTransportGuarantee().isConfidential()) {
232                     if (confidentials == null) {
233                         confidentials = new StringBuffer JavaDoc(methodName);
234                     } else {
235                         confidentials.append(",");
236                         confidentials.append(methodName);
237                     }
238                 }
239             }
240         }
241
242         // Now there are 3 StringBuffer (which can be null) with all the actions
243
// by transport Guarantee
244
// Build the list of actions with adding transport guarantee name.
245

246         List JavaDoc unchecked = new ArrayList JavaDoc();
247         if (nones != null) {
248             unchecked.add(nones.toString());
249         }
250         if (integrals != null) {
251             integrals.append(":");
252             integrals.append(TransportGuaranteeDesc.INTEGRAL_TRANSPORT);
253             unchecked.add(integrals.toString());
254         }
255         if (confidentials != null) {
256             confidentials.append(":");
257             confidentials.append(TransportGuaranteeDesc.CONFIDENTIAL_TRANSPORT);
258             unchecked.add(confidentials.toString());
259         }
260
261         return unchecked;
262     }
263
264
265
266 }
267
Popular Tags