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 * getAction is a conveneniece method to get action
126 * for given HTTP method invoked on the resource
127 * @param method http method
130 public static CSpaceAction getAction(String method) {
132 if ("POST".equalsIgnoreCase(method)) {
133 return CSpaceAction.CREATE;
134 } else if ("GET".equalsIgnoreCase(method)) {
135 return CSpaceAction.READ;
136 } else if ("PUT".equalsIgnoreCase(method)) {
137 return CSpaceAction.UPDATE;
138 } else if ("DELETE".equalsIgnoreCase(method)) {
139 return CSpaceAction.DELETE;
141 //for HEAD, OPTIONS, etc. return READ
142 return CSpaceAction.READ;
147 public String toString() {
148 StringBuilder builder = new StringBuilder();
149 builder.append("URIResourceImpl [");
150 builder.append(", method=");
151 builder.append(method);
152 builder.append(", uri=");
155 return builder.toString() + " " + super.toString();