00001
00027 package org.objectweb.jonas_web.deployment.api;
00028
00029 import java.util.ArrayList;
00030 import java.util.HashMap;
00031 import java.util.Iterator;
00032 import java.util.List;
00033 import java.util.Map;
00034
00039 public class MethodsDesc {
00040
00044 private Map httpMethods = null;
00045
00049 public static final String[] METHODS = new String[] {
00050 "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "TRACE"
00051 };
00052
00056 private static final int ALL_ACTIONS_LENGTH = 38;
00057
00062 public MethodsDesc() {
00063 httpMethods = new HashMap();
00064 for (int m = 0; m < METHODS.length; m++) {
00065 httpMethods.put(METHODS[m], new MethodDesc(METHODS[m]));
00066 }
00067 }
00068
00075 public void addMethods(String[] methods, String transportGuarantee, boolean isExcluded) {
00076 MethodDesc method = null;
00077 for (int m = 0; m < methods.length; m++) {
00078 method = getMethod(methods[m]);
00079 method.addTransportGuarantee(transportGuarantee);
00080 if (isExcluded) {
00081 method.setExcluded();
00082 } else {
00083 method.setUnchecked();
00084 }
00085 }
00086 }
00087
00094 public void addMethodsOnRole(String[] methods, String role, String transportGuarantee) {
00095 for (int m = 0; m < methods.length; m++) {
00096 getMethod(methods[m]).addRole(role, transportGuarantee);
00097 }
00098 }
00099
00100
00106 private MethodDesc getMethod(String methodName) {
00107 MethodDesc m = (MethodDesc) httpMethods.get(methodName.toUpperCase());
00108 if (m == null) {
00109 throw new IllegalStateException("No such method named '" + methodName + "'.");
00110 } else {
00111 return m;
00112 }
00113 }
00114
00115
00116
00121 public String getExcludedActions() {
00122 StringBuffer actions = new StringBuffer();
00123 MethodDesc method = null;
00124 for (Iterator it = httpMethods.values().iterator(); it.hasNext();) {
00125 method = (MethodDesc) it.next();
00126 if (method.isExcluded() && !method.hasRole()) {
00127
00128 if (actions.length() > 0) {
00129 actions.append(",");
00130 }
00131 actions.append(method.getName());
00132 }
00133 }
00134 return actions.toString();
00135 }
00136
00137
00142 public String getUncheckedActions() {
00143 StringBuffer actions = new StringBuffer();
00144 MethodDesc method = null;
00145 for (Iterator it = httpMethods.values().iterator(); it.hasNext();) {
00146 method = (MethodDesc) it.next();
00147 if (method.isUnchecked() && !method.hasRole()) {
00148
00149 if (actions.length() > 0) {
00150 actions.append(",");
00151 }
00152 actions.append(method.getName());
00153 }
00154 }
00155
00156 if (actions.length() == ALL_ACTIONS_LENGTH) {
00157 return null;
00158 } else {
00159 return actions.toString();
00160 }
00161 }
00162
00167 public Map getRoleMapActions() {
00168 MethodDesc method = null;
00169 Map rolesMap = new HashMap();
00170 for (Iterator it = httpMethods.values().iterator(); it.hasNext();) {
00171 method = (MethodDesc) it.next();
00172 if (method.hasRole()) {
00173 for (Iterator itRoles = method.getRolesIterator(); itRoles.hasNext();) {
00174
00175 String roleName = (String) itRoles.next();
00176 String actions = (String) rolesMap.get(roleName);
00177 if (actions == null) {
00178 actions = method.getName();
00179 } else {
00180 actions += ",";
00181 actions += method.getName();
00182 }
00183 rolesMap.put(roleName, actions);
00184 }
00185 }
00186 }
00187 return rolesMap;
00188 }
00189
00190
00197 public List getUncheckedWebUserDataActionsRoleList() {
00198
00199 MethodDesc method = null;
00200
00201 StringBuffer nones = null;
00202 StringBuffer confidentials = null;
00203 StringBuffer integrals = null;
00204
00205
00206 for (Iterator it = httpMethods.values().iterator(); it.hasNext();) {
00207 method = (MethodDesc) it.next();
00208 String methodName = method.getName();
00209
00210 if (method.isUnchecked() && method.hasRole()) {
00211
00212 if (method.getTransportGuarantee().hasNone()) {
00213 if (nones == null) {
00214 nones = new StringBuffer(methodName);
00215 } else {
00216 nones.append(",");
00217 nones.append(methodName);
00218 }
00219 }
00220
00221 if (method.getTransportGuarantee().isIntegral()) {
00222 if (integrals == null) {
00223 integrals = new StringBuffer(methodName);
00224 } else {
00225 integrals.append(",");
00226 integrals.append(methodName);
00227 }
00228 }
00229
00230
00231 if (method.getTransportGuarantee().isConfidential()) {
00232 if (confidentials == null) {
00233 confidentials = new StringBuffer(methodName);
00234 } else {
00235 confidentials.append(",");
00236 confidentials.append(methodName);
00237 }
00238 }
00239 }
00240 }
00241
00242
00243
00244
00245
00246 List unchecked = new ArrayList();
00247 if (nones != null) {
00248 unchecked.add(nones.toString());
00249 }
00250 if (integrals != null) {
00251 integrals.append(":");
00252 integrals.append(TransportGuaranteeDesc.INTEGRAL_TRANSPORT);
00253 unchecked.add(integrals.toString());
00254 }
00255 if (confidentials != null) {
00256 confidentials.append(":");
00257 confidentials.append(TransportGuaranteeDesc.CONFIDENTIAL_TRANSPORT);
00258 unchecked.add(confidentials.toString());
00259 }
00260
00261 return unchecked;
00262 }
00263
00264
00265
00266 }