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(getParent(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(getParent(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
69 public URIResourceImpl(String resourceName, ActionType actionType) {
70 //FIXME more validation might be needed
71 super(buildId(resourceName, getAction(actionType)),
72 getAction(actionType), TYPE.URI);
76 * constructor that is usually called from administrative interface
77 * @param tenantId id of the tenant to which this resource is associated
81 public URIResourceImpl(String tenantId, String resourceName, ActionType actionType) {
82 super(tenantId, buildId(resourceName, getAction(actionType)),
83 getAction(actionType), TYPE.URI);
89 public String getUri() {
96 public String getMethod() {
100 private static String buildId(String resourceName, CSpaceAction action) {
101 return 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();
111 * getAction is a conveneniece method to get action
112 * for given HTTP method invoked on the resource
113 * @param method http method
116 public static CSpaceAction getAction(String method) {
118 if ("POST".equalsIgnoreCase(method)) {
119 return CSpaceAction.CREATE;
120 } else if ("GET".equalsIgnoreCase(method)) {
121 return CSpaceAction.READ;
122 } else if ("PUT".equalsIgnoreCase(method)) {
123 return CSpaceAction.UPDATE;
124 } else if ("DELETE".equalsIgnoreCase(method)) {
125 return CSpaceAction.DELETE;
127 throw new IllegalStateException("no method found!");
131 * getAction is a convenience method to get corresponding action for
136 public static CSpaceAction getAction(ActionType action) {
137 if (ActionType.CREATE.equals(action)) {
138 return CSpaceAction.CREATE;
139 } else if (ActionType.READ.equals(action)) {
140 return CSpaceAction.READ;
141 } else if (ActionType.UPDATE.equals(action)) {
142 return CSpaceAction.UPDATE;
143 } else if (ActionType.DELETE.equals(action)) {
144 return CSpaceAction.DELETE;
145 } else if (ActionType.SEARCH.equals(action)) {
146 return CSpaceAction.SEARCH;
147 } else if (ActionType.ADMIN.equals(action)) {
148 return CSpaceAction.ADMIN;
149 } else if (ActionType.START.equals(action)) {
150 return CSpaceAction.START;
151 } else if (ActionType.STOP.equals(action)) {
152 return CSpaceAction.STOP;
154 throw new IllegalArgumentException("action = " + action.toString());
158 public String toString() {
159 StringBuilder builder = new StringBuilder();
160 builder.append("URIResourceImpl [");
161 builder.append(", method=");
162 builder.append(method);
163 builder.append(", uri=");
166 return builder.toString() + " " + super.toString();