]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0a04791958e307684f9efef3d5d77dd4149ae9bc
[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         PayloadOutputPart ppo = new PayloadOutputPart(PermissionsList.class.getName(), result);
106         System.out.println(ppo.asXML());
107         
108         return result;
109     }
110
111     @PUT
112     @Path("{csid}")
113     public Permission updatePermission(@PathParam("csid") String csid,Permission theUpdate) {
114          return (Permission)update(csid, theUpdate, Permission.class);
115     }
116
117     @DELETE
118     @Path("{csid}")
119     public Response deletePermission(@PathParam("csid") String csid) {
120         logger.debug("deletePermission with csid=" + csid);
121         ensureCSID(csid, ServiceMessages.DELETE_FAILED + "permission ");
122         try {
123             //FIXME ideally the following two ops should be in the same tx CSPACE-658
124             //delete all relationships for this permission
125             PermissionRoleSubResource subResource =
126                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
127             subResource.deletePermissionRole(csid, SubjectType.ROLE);
128             //NOTE for delete permissions in the authz provider
129             //at the PermissionRoleSubResource/DocHandler level, there is no visibility
130             //if permission is deleted, so do it here, however,
131             //this is a very dangerous operation as it deletes the Spring ACL instead of ACE(s)
132             //the ACL might be needed for other ACEs roles...
133             AuthorizationDelegate.deletePermissions(csid);
134
135             ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
136             getStorageClient(ctx).delete(ctx, csid);
137             return Response.status(HttpResponseCodes.SC_OK).build();
138         } catch (Exception e) {
139             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
140         }
141     }
142
143     @POST
144     @Path("{csid}/permroles")
145     public Response createPermissionRole(@QueryParam("_method") String method,
146             @PathParam("csid") String permCsid,
147             PermissionRole input) {
148                 if (method != null) {
149             if ("delete".equalsIgnoreCase(method)) {
150                 return deletePermissionRole(permCsid, input);
151             }
152         }
153         logger.debug("createPermissionRole with permCsid=" + permCsid);
154         ensureCSID(permCsid, ServiceMessages.POST_FAILED + "permroles permission ");
155         try {
156             PermissionRoleSubResource subResource =
157                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
158             String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
159             UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
160             path.path(permCsid + "/permroles/" + permrolecsid);
161             Response response = Response.created(path.build()).build();
162             return response;
163         } catch (Exception e) {
164             throw bigReThrow(e, ServiceMessages.POST_FAILED, permCsid);
165         }
166     }
167
168     @GET
169     @Path("{csid}/permroles/{id}")
170     public PermissionRoleRel getPermissionRole(
171             @PathParam("csid") String permCsid,
172             @PathParam("id") String permrolecsid) {
173         logger.debug("getPermissionRole with permCsid=" + permCsid);
174         ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
175         PermissionRoleRel result = null;
176         try {
177             PermissionRoleSubResource subResource =
178                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
179             //get relationships for a permission
180             result = subResource.getPermissionRoleRel(permCsid, SubjectType.ROLE, permrolecsid);
181         } catch (Exception e) {
182             throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
183         }
184         checkResult(result, permCsid, ServiceMessages.GET_FAILED);
185         return result;
186     }
187
188     @GET
189     @Path("{csid}/permroles")
190     public PermissionRole getPermissionRole(
191             @PathParam("csid") String permCsid) {
192         logger.debug("getPermissionRole with permCsid=" + permCsid);
193         ensureCSID(permCsid, ServiceMessages.GET_FAILED + "permroles permission ");
194         PermissionRole result = null;
195         try {
196             PermissionRoleSubResource subResource =
197                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
198             //get relationships for a permission
199             result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
200         } catch (Exception e) {
201             throw bigReThrow(e, ServiceMessages.GET_FAILED, permCsid);
202         }
203         checkResult(result, permCsid, ServiceMessages.GET_FAILED);
204         return result;
205     }
206
207     public Response deletePermissionRole(String permCsid, PermissionRole input) {
208         logger.debug("Delete payload of permrole relationships with permission permCsid=" + permCsid);
209         ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
210         try {
211             PermissionRoleSubResource subResource =
212                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
213             //delete all relationships for a permission
214             subResource.deletePermissionRole(permCsid, SubjectType.ROLE, input);
215             return Response.status(HttpResponseCodes.SC_OK).build();
216         } catch (Exception e) {
217             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
218         }
219     }
220     
221     @DELETE
222     @Path("{csid}/permroles")    
223     public Response deletePermissionRole(
224             @PathParam("csid") String permCsid) {
225         logger.debug("Delete all the role relationships of the permissions with permCsid=" + permCsid);
226          ensureCSID(permCsid, ServiceMessages.DELETE_FAILED + "permroles permission ");
227         try {
228             PermissionRoleSubResource subResource =
229                     new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
230             //delete all relationships for a permission
231             subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
232             return Response.status(HttpResponseCodes.SC_OK).build();
233         } catch (Exception e) {
234             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, permCsid);
235         }
236     }
237     
238 }