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