]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
04b40aae7f7254680d5e5d9f2b22b73e88fd2a99
[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
28 /**
29  * A security resource that represents URI and method invoked on it
30  * @author 
31  */
32 public class URIResourceImpl extends CSpaceResourceImpl {
33
34     private String uri;
35     private String method;
36
37     /**
38      * constructor that is usually called from service runtime
39      * uses current tenant id from the context
40      * @param uri
41      * @param method an http method
42      */
43     public URIResourceImpl(String uri, String method) {
44         super(buildId(uri, getAction(method)),
45                 getAction(method), TYPE.URI);
46         this.uri = uri;
47         this.method = method;
48     }
49
50     /**
51      * constructor that is usually called from service runtime
52      * @param tenantId id of the tenant to which this resource is associated
53      * @param uri
54      * @param method an http method
55      */
56     public URIResourceImpl(String tenantId, String uri, String method) {
57         super(tenantId, buildId(uri, getAction(method)),
58                 getAction(method), TYPE.URI);
59         this.uri = uri;
60         this.method = method;
61     }
62
63     /**
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
67      * @param actionType
68      */
69     public URIResourceImpl(String resourceName, CSpaceAction action) {
70         //FIXME more validation might be needed
71         super(buildId(resourceName, action),
72                 action, TYPE.URI);
73     }
74
75     /**
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
79      * @param actionType
80      */
81     public URIResourceImpl(String tenantId, String resourceName, CSpaceAction action) {
82         super(tenantId, buildId(resourceName, action),
83                 action, TYPE.URI);
84     }
85
86     /**
87      * @return the uri
88      */
89     public String getUri() {
90         return uri;
91     }
92
93     /**
94      * @return the method
95      */
96     public String getMethod() {
97         return method;
98     }
99
100     private static String buildId(String resourceName, CSpaceAction action) {
101         return sanitize(resourceName) + SEPARATOR_HASH + action.toString();
102     }
103
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();
108     }
109
110     private static String sanitize(String uri) {
111         uri = uri.trim();
112         if (uri.startsWith("/")) {
113             uri = uri.substring(1);
114         }
115         if (uri.endsWith("/")) {
116             uri = uri.substring(0, uri.length() - 1);
117         }
118         if (uri.endsWith("/*")) {
119             uri = uri.substring(0, uri.length() - 2);
120         }
121         return uri;
122     }
123
124     /*
125      * Map a Permission ActionType to a CSpaceAction
126      */
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;
146         }
147         throw new IllegalArgumentException("action = " + action.toString());
148     }
149     
150     /**
151      * getAction is a conveneniece method to get action
152      * for given HTTP method invoked on the resource
153      * @param method http method
154      * @return
155      */
156     public static CSpaceAction getAction(String method) {
157
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;
166         } else {
167             //for HEAD, OPTIONS, etc. return READ
168             return CSpaceAction.READ;
169         }
170     }
171
172     @Override
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=");
179         builder.append(uri);
180         builder.append("]");
181         return builder.toString() + " " + super.toString();
182     }
183 }