]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
cc4c126797907e68afadceaa7073b5f91ba50fb5
[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  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46 package org.collectionspace.services.authorization;
47
48 import java.util.StringTokenizer;
49
50 /**
51  * A security resource that represents URI and method invoked on it
52  * @author 
53  */
54 public class URIResourceImpl extends CSpaceResourceImpl {
55
56     private String uri;
57     private String method;
58     private CSpaceAction action;
59
60     /**
61      * constructor that is usually called from service runtime
62      * @param uri
63      * @param method an http method
64      */
65     public URIResourceImpl(String uri, String method) {
66         super(getParent(uri) + "#" + getAction(method).toString(), TYPE.URI);
67         action = getAction(method);
68         this.uri = uri;
69         this.method = method;
70     }
71
72     /**
73      * constructor that is usually called from administrative interface
74      * @param resourceName
75      * @param actionType
76      */
77     public URIResourceImpl(String resourceName, ActionType actionType) {
78         //FIXME more validation might be needed
79         super(resourceName + "#" + getAction(actionType).toString(), TYPE.URI);
80         action = getAction(actionType);
81     }
82
83     /**
84      * @return the uri
85      */
86     public String getUri() {
87         return uri;
88     }
89
90     /**
91      * @param uri the uri to set
92      */
93     public void setUri(String uri) {
94         this.uri = uri;
95     }
96
97     /**
98      * @return the method
99      */
100     public String getMethod() {
101         return method;
102     }
103
104     /**
105      * @param method the method to set
106      */
107     public void setMethod(String method) {
108         this.method = method;
109     }
110
111     /**
112      * getAction a convenience method to get action invoked on the resource
113      */
114     @Override
115     public CSpaceAction getAction() {
116         return action;
117     }
118
119     private static String getParent(String uri) {
120         StringTokenizer stz = new StringTokenizer(uri, "/");
121         //FIXME the following ignores sub resources as well as object instances
122         return stz.nextToken();
123     }
124
125     /**
126      * getAction is a conveneniece method to get action
127      * for given HTTP method invoked on the resource
128      * @param method http method
129      * @return
130      */
131     public static CSpaceAction getAction(String method) {
132
133         if ("POST".equalsIgnoreCase(method)) {
134             return CSpaceAction.CREATE;
135         } else if ("GET".equalsIgnoreCase(method)) {
136             return CSpaceAction.READ;
137         } else if ("PUT".equalsIgnoreCase(method)) {
138             return CSpaceAction.UPDATE;
139         } else if ("DELETE".equalsIgnoreCase(method)) {
140             return CSpaceAction.DELETE;
141         }
142         throw new IllegalStateException("no method found!");
143     }
144
145     /**
146      * getAction is a convenience method to get corresponding action for
147      * given ActionType
148      * @param action
149      * @return
150      */
151     public static CSpaceAction getAction(ActionType action) {
152         if (ActionType.CREATE.equals(action)) {
153             return CSpaceAction.CREATE;
154         } else if (ActionType.READ.equals(action)) {
155             return CSpaceAction.READ;
156         } else if (ActionType.UPDATE.equals(action)) {
157             return CSpaceAction.UPDATE;
158         } else if (ActionType.DELETE.equals(action)) {
159             return CSpaceAction.DELETE;
160         } else if (ActionType.SEARCH.equals(action)) {
161             return CSpaceAction.SEARCH;
162         } else if (ActionType.ADMIN.equals(action)) {
163             return CSpaceAction.ADMIN;
164         } else if (ActionType.START.equals(action)) {
165             return CSpaceAction.START;
166         } else if (ActionType.STOP.equals(action)) {
167             return CSpaceAction.STOP;
168         }
169         throw new IllegalArgumentException("action = " + action.toString());
170     }
171 }