]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4415611d4519a699f8343de23433e2d8849d68da
[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(getParent(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(getParent(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
67      * @param actionType
68      */
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);
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
79      * @param actionType
80      */
81     public URIResourceImpl(String tenantId, String resourceName, ActionType actionType) {
82         super(tenantId, buildId(resourceName, getAction(actionType)),
83                 getAction(actionType), 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 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     /**
111      * getAction is a conveneniece method to get action
112      * for given HTTP method invoked on the resource
113      * @param method http method
114      * @return
115      */
116     public static CSpaceAction getAction(String method) {
117
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;
126         }
127         throw new IllegalStateException("no method found!");
128     }
129
130     /**
131      * getAction is a convenience method to get corresponding action for
132      * given ActionType
133      * @param action
134      * @return
135      */
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;
153         }
154         throw new IllegalArgumentException("action = " + action.toString());
155     }
156
157     @Override
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=");
164         builder.append(uri);
165         builder.append("]");
166         return builder.toString() + " " + super.toString();
167     }
168 }