]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4f7b0e6f57bac629b0501bbb352de331cd440eb5
[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 org.collectionspace.services.authorization.perms.FooPermissionsList;
27 import org.collectionspace.services.authorization.perms.Permission;
28 import org.collectionspace.services.authorization.perms.PermissionsList;
29 import org.collectionspace.services.authorization.storage.AuthorizationDelegate;
30 import org.collectionspace.services.client.PayloadOutputPart;
31 import org.collectionspace.services.client.PermissionClient;
32 import org.collectionspace.services.common.SecurityResourceBase;
33 import org.collectionspace.services.common.ServiceMessages;
34 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
35 import org.collectionspace.services.common.context.ServiceContext;
36 import org.collectionspace.services.common.context.ServiceContextFactory;
37 import org.collectionspace.services.common.storage.StorageClient;
38 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
39 import org.jboss.resteasy.util.HttpResponseCodes;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import javax.ws.rs.Consumes;
44 import javax.ws.rs.DELETE;
45 import javax.ws.rs.GET;
46 import javax.ws.rs.POST;
47 import javax.ws.rs.PUT;
48 import javax.ws.rs.Path;
49 import javax.ws.rs.PathParam;
50 import javax.ws.rs.Produces;
51 import javax.ws.rs.QueryParam;
52 import javax.ws.rs.core.Context;
53 import javax.ws.rs.core.Response;
54 import javax.ws.rs.core.UriBuilder;
55 import javax.ws.rs.core.UriInfo;
56
57 @Path(PermissionClient.SERVICE_PATH)
58 @Consumes("application/xml")
59 @Produces("application/xml")
60 public class PermissionResource extends SecurityResourceBase {
61
62     final Logger logger = LoggerFactory.getLogger(PermissionResource.class);
63     final StorageClient storageClient = new JpaStorageClientImpl();
64
65     @Override
66     protected String getVersionString() {
67         return "$LastChangedRevision: 1165 $";
68     }
69
70     @Override
71     public String getServiceName() {
72         return  PermissionClient.SERVICE_NAME;
73     }
74
75     @Override
76     public Class<Permission> getCommonPartClass() {
77         return Permission.class;
78     }
79
80     @Override
81     public ServiceContextFactory<Permission, Permission> getServiceContextFactory() {
82         return RemoteServiceContextFactory.get();
83     }
84
85     @Override
86     public StorageClient getStorageClient(ServiceContext ctx) {
87         //FIXME use ctx to identify storage client
88         return storageClient;
89     }
90
91     @POST
92     public Response createPermission(Permission input) {
93         return create(input);
94     }
95
96     @GET
97     @Path("{csid}")
98     public Permission getPermission(@PathParam("csid") String csid) {
99         return (Permission)get(csid, Permission.class);
100     }
101
102     @GET
103     @Produces("application/xml")
104     public PermissionsList getPermissionList(@Context UriInfo ui) {
105         PermissionsList result = (PermissionsList)getList(ui, Permission.class);
106         PayloadOutputPart ppo = new PayloadOutputPart(PermissionsList.class.getName(), result);
107         System.out.println(ppo.asXML());
108         
109         return result;
110     }
111
112     @GET
113     @Path("{csid}/foo")
114     @Produces("application/xml")
115     public FooPermissionsList getPermissionFooList(@Context UriInfo ui) {
116         FooPermissionsList result = new FooPermissionsList();           
117         return result;
118     }
119
120     @PUT
121     @Path("{csid}")
122     public Permission updatePermission(@PathParam("csid") String csid,Permission theUpdate) {
123          return (Permission)update(csid, theUpdate, Permission.class);
124     }
125
126     @DELETE
127     @Path("{csid}")
128     public Response deletePermission(@PathParam("csid") String csid) {
129         logger.debug("deletePermission with csid=" + csid);
130         ensureCSID(csid, ServiceMessages.DELETE_FAILED + "permission ");
131         try {
132             //FIXME ideally the following two ops should be in the same tx CSPACE-658
133             //delete all relationships for this permission
134             PermissionRoleSubResource subResource =
135                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
136             subResource.deletePermissionRole(csid, SubjectType.ROLE);
137             //NOTE for delete permissions in the authz provider
138             //at the PermissionRoleSubResource/DocHandler level, there is no visibility
139             //if permission is deleted, so do it here, however,
140             //this is a very dangerous operation as it deletes the Spring ACL instead of ACE(s)
141             //the ACL might be needed for other ACEs roles...
142             AuthorizationDelegate.deletePermissions(csid);
143
144             ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
145             getStorageClient(ctx).delete(ctx, csid);
146             return Response.status(HttpResponseCodes.SC_OK).build();
147         } catch (Exception e) {
148             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
149         }
150     }
151
152     @POST
153     @Path("{csid}/permroles")
154     public Response createPermissionRole(@QueryParam("_method") String method,
155             @PathParam("csid") String permCsid,
156             PermissionRole input) {
157                 if (method != null) {
158             if ("delete".equalsIgnoreCase(method)) {
159                 return deletePermissionRole(permCsid, input);
160             }
161         }
162         logger.debug("createPermissionRole with permCsid=" + permCsid);
163         ensureCSID(permCsid, ServiceMessages.POST_FAILED + "permroles permission ");
164         try {
165             PermissionRoleSubResource subResource =
166                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
167             String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
168             UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
169             path.path(permCsid + "/permroles/" + permrolecsid);
170             Response response = Response.created(path.build()).build();
171             return response;
172         } catch (Exception e) {
173             throw bigReThrow(e, ServiceMessages.POST_FAILED, permCsid);
174         }
175     }
176
177     @GET
178     @Path("{csid}/permroles/{id}")
179     public PermissionRoleRel getPermissionRole(
180             @PathParam("csid") String permCsid,
181             @PathParam("id") String permrolecsid) {
182         logger.debug("getPermissionRole with permCsid=" + permCsid);
183         ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
184         PermissionRoleRel result = null;
185         try {
186             PermissionRoleSubResource subResource =
187                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
188             //get relationships for a permission
189             result = subResource.getPermissionRoleRel(permCsid, SubjectType.ROLE, permrolecsid);
190         } catch (Exception e) {
191             throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
192         }
193         checkResult(result, permCsid, ServiceMessages.GET_FAILED);
194         return result;
195     }
196
197     @GET
198     @Path("{csid}/permroles")
199     public PermissionRole getPermissionRole(
200             @PathParam("csid") String permCsid) {
201         logger.debug("getPermissionRole with permCsid=" + permCsid);
202         ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
203         PermissionRole result = null;
204         try {
205             PermissionRoleSubResource subResource =
206                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
207             //get relationships for a permission
208             result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
209         } catch (Exception e) {
210             throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
211         }
212         checkResult(result, permCsid, ServiceMessages.GET_FAILED);
213         return result;
214     }
215
216     public Response deletePermissionRole(String permCsid, PermissionRole input) {
217         logger.debug("Delete payload of permrole relationships with permission permCsid=" + permCsid);
218         ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
219         try {
220             PermissionRoleSubResource subResource =
221                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
222             //delete all relationships for a permission
223             subResource.deletePermissionRole(permCsid, SubjectType.ROLE, input);
224             return Response.status(HttpResponseCodes.SC_OK).build();
225         } catch (Exception e) {
226             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
227         }
228     }
229     
230     @DELETE
231     @Path("{csid}/permroles")    
232     public Response deletePermissionRole(
233             @PathParam("csid") String permCsid) {
234         logger.debug("Delete all the role relationships of the permissions with permCsid=" + permCsid);
235          ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
236         try {
237             PermissionRoleSubResource subResource =
238                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
239             //delete all relationships for a permission
240             subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
241             return Response.status(HttpResponseCodes.SC_OK).build();
242         } catch (Exception e) {
243             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
244         }
245     }
246     
247 }