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