001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: MethodsDesc.java 4854 2004-06-01 11:24:01Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_web.deployment.api;
026:
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: /**
034: * Defines the methods class managing all http methods
035: * @author Florent Benoit
036: */
037: public class MethodsDesc {
038:
039: /**
040: * List of Methods
041: */
042: private Map httpMethods = null;
043:
044: /**
045: * Available HTTP METHODS
046: */
047: public static final String[] METHODS = new String[] { "DELETE",
048: "GET", "HEAD", "OPTIONS", "POST", "PUT", "TRACE" };
049:
050: /**
051: * Size of all actions enumerated : "DELETE,GET,HEAD,OPTIONS,POST,PUT,TRACE"
052: */
053: private static final int ALL_ACTIONS_LENGTH = 38;
054:
055: /**
056: * Constructor
057: * Build new Http Methods
058: */
059: public MethodsDesc() {
060: httpMethods = new HashMap();
061: for (int m = 0; m < METHODS.length; m++) {
062: httpMethods.put(METHODS[m], new MethodDesc(METHODS[m]));
063: }
064: }
065:
066: /**
067: * Add Http methods (Excluded or Unchecked)
068: * @param methods array of methods to add
069: * @param transportGuarantee Transport Guarantee for these methods
070: * @param isExcluded if true add methods as excluded else as unchecked
071: */
072: public void addMethods(String[] methods, String transportGuarantee,
073: boolean isExcluded) {
074: MethodDesc method = null;
075: for (int m = 0; m < methods.length; m++) {
076: method = getMethod(methods[m]);
077: method.addTransportGuarantee(transportGuarantee);
078: if (isExcluded) {
079: method.setExcluded();
080: } else {
081: method.setUnchecked();
082: }
083: }
084: }
085:
086: /**
087: * Add pattern information for a given role
088: * @param methods methods to add to the given role
089: * @param role role which have the given methods
090: * @param transportGuarantee Transport Guarantee for these methods
091: */
092: public void addMethodsOnRole(String[] methods, String role,
093: String transportGuarantee) {
094: for (int m = 0; m < methods.length; m++) {
095: getMethod(methods[m]).addRole(role, transportGuarantee);
096: }
097: }
098:
099: /**
100: * Gets the method object
101: * @param methodName name of the method
102: * @return Method object
103: */
104: private MethodDesc getMethod(String methodName) {
105: MethodDesc m = (MethodDesc) httpMethods.get(methodName
106: .toUpperCase());
107: if (m == null) {
108: throw new IllegalStateException("No such method named '"
109: + methodName + "'.");
110: } else {
111: return m;
112: }
113: }
114:
115: /**
116: * Gets the excluded actions in order to build permissions
117: * @return actions in order to build permissions
118: */
119: public String getExcludedActions() {
120: StringBuffer actions = new StringBuffer();
121: MethodDesc method = null;
122: for (Iterator it = httpMethods.values().iterator(); it
123: .hasNext();) {
124: method = (MethodDesc) it.next();
125: if (method.isExcluded() && !method.hasRole()) {
126: // first item or append item ?
127: if (actions.length() > 0) {
128: actions.append(",");
129: }
130: actions.append(method.getName());
131: }
132: }
133: return actions.toString();
134: }
135:
136: /**
137: * Gets the unchecked actions in order to build permissions
138: * @return actions in order to build permissions
139: */
140: public String getUncheckedActions() {
141: StringBuffer actions = new StringBuffer();
142: MethodDesc method = null;
143: for (Iterator it = httpMethods.values().iterator(); it
144: .hasNext();) {
145: method = (MethodDesc) it.next();
146: if (method.isUnchecked() && !method.hasRole()) {
147: // first item or append item ?
148: if (actions.length() > 0) {
149: actions.append(",");
150: }
151: actions.append(method.getName());
152: }
153: }
154: // Contains all actions
155: if (actions.length() == ALL_ACTIONS_LENGTH) {
156: return null;
157: } else {
158: return actions.toString();
159: }
160: }
161:
162: /**
163: * Gets the Map between roles and their actions
164: * @return map between roles and their actions in order to build permissions
165: */
166: public Map getRoleMapActions() {
167: MethodDesc method = null;
168: Map rolesMap = new HashMap();
169: for (Iterator it = httpMethods.values().iterator(); it
170: .hasNext();) {
171: method = (MethodDesc) it.next();
172: if (method.hasRole()) {
173: for (Iterator itRoles = method.getRolesIterator(); itRoles
174: .hasNext();) {
175:
176: String roleName = (String) itRoles.next();
177: String actions = (String) rolesMap.get(roleName);
178: if (actions == null) {
179: actions = method.getName();
180: } else {
181: actions += ",";
182: actions += method.getName();
183: }
184: rolesMap.put(roleName, actions);
185: }
186: }
187: }
188: return rolesMap;
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 getUncheckedWebUserDataActionsRoleList() {
198:
199: MethodDesc method = null;
200: // Temporary list.
201: StringBuffer nones = null;
202: StringBuffer confidentials = null;
203: StringBuffer integrals = null;
204:
205: // Build unchecked actions
206: for (Iterator it = httpMethods.values().iterator(); it
207: .hasNext();) {
208: method = (MethodDesc) it.next();
209: String methodName = method.getName();
210: // Only if not contain an excluding auth-constraint
211: if (method.isUnchecked() && method.hasRole()) {
212: // Add NONE for each method found
213: if (method.getTransportGuarantee().hasNone()) {
214: if (nones == null) {
215: nones = new StringBuffer(methodName);
216: } else {
217: nones.append(",");
218: nones.append(methodName);
219: }
220: }
221: // Add INTEGRAL on method if it is the case
222: if (method.getTransportGuarantee().isIntegral()) {
223: if (integrals == null) {
224: integrals = new StringBuffer(methodName);
225: } else {
226: integrals.append(",");
227: integrals.append(methodName);
228: }
229: }
230:
231: // Add CONFIDENTIAL on method if it is the case
232: if (method.getTransportGuarantee().isConfidential()) {
233: if (confidentials == null) {
234: confidentials = new StringBuffer(methodName);
235: } else {
236: confidentials.append(",");
237: confidentials.append(methodName);
238: }
239: }
240: }
241: }
242:
243: // Now there are 3 StringBuffer (which can be null) with all the actions
244: // by transport Guarantee
245: // Build the list of actions with adding transport guarantee name.
246:
247: List unchecked = new ArrayList();
248: if (nones != null) {
249: unchecked.add(nones.toString());
250: }
251: if (integrals != null) {
252: integrals.append(":");
253: integrals.append(TransportGuaranteeDesc.INTEGRAL_TRANSPORT);
254: unchecked.add(integrals.toString());
255: }
256: if (confidentials != null) {
257: confidentials.append(":");
258: confidentials
259: .append(TransportGuaranteeDesc.CONFIDENTIAL_TRANSPORT);
260: unchecked.add(confidentials.toString());
261: }
262:
263: return unchecked;
264: }
265:
266: }
|