]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
05c12b23f16492d53adfeeb8ce24f8de4e0cb045
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.authorization;
25
26 import java.util.StringTokenizer;
27 import org.collectionspace.services.authorization.perms.ActionType;
28
29 /**
30  * A security resource that represents URI and method invoked on it
31  * @author 
32  */
33 public class URIResourceImpl extends CSpaceResourceImpl {
34
35     private String uri;
36     private String method;
37
38     /**
39      * constructor that is usually called from service runtime
40      * uses current tenant id from the context
41      * @param uri
42      * @param method an http method
43      */
44     public URIResourceImpl(String uri, String method) {
45         super(buildId(uri, getAction(method)),
46                 getAction(method), TYPE.URI);
47         this.uri = uri;
48         this.method = method;
49     }
50
51     /**
52      * constructor that is usually called from service runtime
53      * @param tenantId id of the tenant to which this resource is associated
54      * @param uri
55      * @param method an http method
56      */
57     public URIResourceImpl(String tenantId, String uri, String method) {
58         super(tenantId, buildId(uri, getAction(method)),
59                 getAction(method), TYPE.URI);
60         this.uri = uri;
61         this.method = method;
62     }
63
64     /**
65      * constructor that is usually called from administrative interface
66      * uses current tenant id from the context
67      * @param resourceName no leading / and no trailing / needed
68      * @param actionType
69      */
70     public URIResourceImpl(String resourceName, CSpaceAction action) {
71         //FIXME more validation might be needed
72         super(buildId(resourceName, action),
73                 action, TYPE.URI);
74     }
75
76     /**
77      * constructor that is usually called from administrative interface
78      * @param tenantId id of the tenant to which this resource is associated
79      * @param resourceName no leading / and no trailing / needed
80      * @param actionType
81      */
82     public URIResourceImpl(String tenantId, String resourceName, CSpaceAction action) {
83         super(tenantId, buildId(resourceName, action),
84                 action, TYPE.URI);
85     }
86
87     /**
88      * @return the uri
89      */
90     public String getUri() {
91         return uri;
92     }
93
94     /**
95      * @return the method
96      */
97     public String getMethod() {
98         return method;
99     }
100
101     private static String buildId(String resourceName, CSpaceAction action) {
102         return sanitize(resourceName) + SEPARATOR_HASH + action.toString();
103     }
104
105     private static String getParent(String uri) {
106         StringTokenizer stz = new StringTokenizer(uri, "/");
107         //FIXME the following ignores sub resources as well as object instances
108         return stz.nextToken();
109     }
110
111     private static String sanitize(String uri) {
112         uri = uri.trim();
113         if (uri.startsWith("/")) {
114             uri = uri.substring(1);
115         }
116         if (uri.endsWith("/")) {
117             uri = uri.substring(0, uri.length() - 1);
118         }
119         if (uri.endsWith("/*")) {
120             uri = uri.substring(0, uri.length() - 2);
121         }
122         return uri;
123     }
124
125     /*
126      * Map a Permission ActionType to a CSpaceAction
127      */
128     //FIXME: This method is duplicated in PermissionDocumentHandler.java class.  The class loader was having
129     //trouble with the ActionType class file.  Not sure why?
130     public static CSpaceAction getAction(ActionType action) {
131         if (ActionType.CREATE.name().equals(action.name())) {
132             return CSpaceAction.CREATE;
133         } else if (ActionType.READ.equals(action)) {
134             return CSpaceAction.READ;
135         } else if (ActionType.UPDATE.equals(action)) {
136             return CSpaceAction.UPDATE;
137         } else if (ActionType.DELETE.equals(action)) {
138             return CSpaceAction.DELETE;
139         } else if (ActionType.SEARCH.equals(action)) {
140             return CSpaceAction.SEARCH;
141         } else if (ActionType.ADMIN.equals(action)) {
142             return CSpaceAction.ADMIN;
143         } else if (ActionType.START.equals(action)) {
144             return CSpaceAction.START;
145         } else if (ActionType.STOP.equals(action)) {
146             return CSpaceAction.STOP;
147         }
148         throw new IllegalArgumentException("action = " + action.toString());
149     }
150     
151     /**
152      * getAction is a conveneniece method to get action
153      * for given HTTP method invoked on the resource
154      * @param method http method
155      * @return
156      */
157     public static CSpaceAction getAction(String method) {
158
159         if ("POST".equalsIgnoreCase(method)) {
160             return CSpaceAction.CREATE;
161         } else if ("GET".equalsIgnoreCase(method)) {
162             return CSpaceAction.READ;
163         } else if ("PUT".equalsIgnoreCase(method)) {
164             return CSpaceAction.UPDATE;
165         } else if ("DELETE".equalsIgnoreCase(method)) {
166             return CSpaceAction.DELETE;
167         } else {
168             //for HEAD, OPTIONS, etc. return READ
169             return CSpaceAction.READ;
170         }
171     }
172
173     @Override
174     public String toString() {
175         StringBuilder builder = new StringBuilder();
176         builder.append("URIResourceImpl [");
177         builder.append(", method=");
178         builder.append(method);
179         builder.append(", uri=");
180         builder.append(uri);
181         builder.append("]");
182         return builder.toString() + " " + super.toString();
183     }
184 }