2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.authorization;
26 import java.util.StringTokenizer;
29 * A security resource that represents URI and method invoked on it
32 public class URIResourceImpl extends CSpaceResourceImpl {
35 private String method;
38 * constructor that is usually called from service runtime
39 * uses current tenant id from the context
41 * @param method an http method
43 public URIResourceImpl(String uri, String method) {
44 super(buildId(uri, getAction(method)),
45 getAction(method), TYPE.URI);
51 * constructor that is usually called from service runtime
52 * @param tenantId id of the tenant to which this resource is associated
54 * @param method an http method
56 public URIResourceImpl(String tenantId, String uri, String method) {
57 super(tenantId, buildId(uri, getAction(method)),
58 getAction(method), TYPE.URI);
64 * constructor that is usually called from administrative interface
65 * uses current tenant id from the context
66 * @param resourceName no leading / and no trailing / needed
69 public URIResourceImpl(String resourceName, CSpaceAction action) {
70 //FIXME more validation might be needed
71 super(buildId(resourceName, action),
76 * constructor that is usually called from administrative interface
77 * @param tenantId id of the tenant to which this resource is associated
78 * @param resourceName no leading / and no trailing / needed
81 public URIResourceImpl(String tenantId, String resourceName, CSpaceAction action) {
82 super(tenantId, buildId(resourceName, action),
89 public String getUri() {
96 public String getMethod() {
100 private static String buildId(String resourceName, CSpaceAction action) {
101 return sanitize(resourceName) + SEPARATOR_HASH + action.toString();
104 private static String getParent(String uri) {
105 StringTokenizer stz = new StringTokenizer(uri, "/");
106 //FIXME the following ignores sub resources as well as object instances
107 return stz.nextToken();
110 private static String sanitize(String uri) {
112 if (uri.startsWith("/")) {
113 uri = uri.substring(1);
115 if (uri.endsWith("/")) {
116 uri = uri.substring(0, uri.length() - 1);
118 if (uri.endsWith("/*")) {
119 uri = uri.substring(0, uri.length() - 2);
125 * Map a Permission ActionType to a CSpaceAction
127 //FIXME: This method is duplicated in PermissionDocumentHandler.java class. The class loader was having
128 //trouble with the ActionType class file. Not sure why?
129 public static CSpaceAction getAction(ActionType action) {
130 if (ActionType.CREATE.name().equals(action.name())) {
131 return CSpaceAction.CREATE;
132 } else if (ActionType.READ.equals(action)) {
133 return CSpaceAction.READ;
134 } else if (ActionType.UPDATE.equals(action)) {
135 return CSpaceAction.UPDATE;
136 } else if (ActionType.DELETE.equals(action)) {
137 return CSpaceAction.DELETE;
138 } else if (ActionType.SEARCH.equals(action)) {
139 return CSpaceAction.SEARCH;
140 } else if (ActionType.ADMIN.equals(action)) {
141 return CSpaceAction.ADMIN;
142 } else if (ActionType.START.equals(action)) {
143 return CSpaceAction.START;
144 } else if (ActionType.STOP.equals(action)) {
145 return CSpaceAction.STOP;
147 throw new IllegalArgumentException("action = " + action.toString());
151 * getAction is a conveneniece method to get action
152 * for given HTTP method invoked on the resource
153 * @param method http method
156 public static CSpaceAction getAction(String method) {
158 if ("POST".equalsIgnoreCase(method)) {
159 return CSpaceAction.CREATE;
160 } else if ("GET".equalsIgnoreCase(method)) {
161 return CSpaceAction.READ;
162 } else if ("PUT".equalsIgnoreCase(method)) {
163 return CSpaceAction.UPDATE;
164 } else if ("DELETE".equalsIgnoreCase(method)) {
165 return CSpaceAction.DELETE;
167 //for HEAD, OPTIONS, etc. return READ
168 return CSpaceAction.READ;
173 public String toString() {
174 StringBuilder builder = new StringBuilder();
175 builder.append("URIResourceImpl [");
176 builder.append(", method=");
177 builder.append(method);
178 builder.append(", uri=");
181 return builder.toString() + " " + super.toString();