]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7a8d04bc934b8221a74c53fbc309b88d822254a7
[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.account.AccountRoleSubResource;
27 import org.collectionspace.services.client.RoleClient;
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.WebApplicationException;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.Response;
51 import javax.ws.rs.core.UriBuilder;
52 import javax.ws.rs.core.UriInfo;
53
54 @Path(RoleClient.SERVICE_PATH)
55 @Consumes("application/xml")
56 @Produces("application/xml")
57 public class RoleResource extends SecurityResourceBase {
58
59     final Logger logger = LoggerFactory.getLogger(RoleResource.class);
60     final StorageClient storageClient = new JpaStorageClientImpl();
61
62     @Override
63     protected String getVersionString() {
64         return  "$LastChangedRevision: 1165 $";
65     }
66
67     @Override
68     public String getServiceName() {
69         return RoleClient.SERVICE_NAME;
70     }
71
72     @Override
73     public Class<RoleResource> getCommonPartClass() {
74         return RoleResource.class;
75     }
76
77     @Override
78     public ServiceContextFactory getServiceContextFactory() {
79         return RemoteServiceContextFactory.get();
80     }
81
82     @Override
83     public StorageClient getStorageClient(ServiceContext ctx) {
84         //FIXME use ctx to identify storage client
85         return storageClient;
86     }
87
88     @POST
89     public Response createRole(Role input) {
90         return create(input);
91     }
92
93     @GET
94     @Path("{csid}")
95     public Role getRole(@PathParam("csid") String csid) {
96         return (Role)get(csid, Role.class);
97     }
98
99     @GET
100     @Produces("application/xml")
101     public RolesList getRoleList(@Context UriInfo ui) {
102         return (RolesList)getList(ui, Role.class);
103     }
104
105     @PUT
106     @Path("{csid}")
107     public Role updateRole(@PathParam("csid") String csid, Role theUpdate) {
108         try {
109                 Role role = (Role)get(csid, Role.class);
110                 // If marked as metadata immutable, do not update
111                 if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
112                     Response response = 
113                         Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
114                 throw new WebApplicationException(response);
115                 }
116                 return (Role)update(csid, theUpdate, Role.class);
117         } catch (Exception e) {
118             throw bigReThrow(e, ServiceMessages.UPDATE_FAILED, csid);
119         }
120     }
121
122     @DELETE
123     @Path("{csid}")
124     public Response deleteRole(@PathParam("csid") String csid) {
125         logger.debug("deleteRole with csid=" + csid);
126         ensureCSID(csid, ServiceMessages.DELETE_FAILED + "deleteRole ");
127
128         try {
129                 Role role = (Role)get(csid, Role.class);
130             // If marked as metadata immutable, do not delete
131             if(RoleClient.IMMUTABLE.equals(role.getMetadataProtection())) {
132                 Response response = 
133                         Response.status(Response.Status.FORBIDDEN).entity("Role: "+csid+" is immutable.").type("text/plain").build();
134                 return response;
135             }
136             //FIXME ideally the following three operations should be in the same tx CSPACE-658
137             //delete all relationships for this permission
138             PermissionRoleSubResource permRoleResource =
139                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
140             permRoleResource.deletePermissionRole(csid, SubjectType.PERMISSION);
141             //delete all the account/role relationships associate with this role
142             AccountRoleSubResource accountRoleResource =
143                 new AccountRoleSubResource(AccountRoleSubResource.ROLE_ACCOUNTROLE_SERVICE);
144             accountRoleResource.deleteAccountRole(csid, SubjectType.ACCOUNT);
145             //finally, delete the role itself
146             ServiceContext<Role, Role> ctx = createServiceContext((Role) null, Role.class);
147             ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
148             return Response.status(HttpResponseCodes.SC_OK).build();
149         } catch (Exception e) {
150             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
151         }
152     }
153
154     @POST
155     @Path("{csid}/permroles")
156     public Response createRolePermission(@QueryParam("_method") String method, @PathParam("csid") String roleCsid,
157             PermissionRole input) {
158         if (method != null) {
159             if ("delete".equalsIgnoreCase(method)) {
160                 return deleteRolePermission(roleCsid, input);
161             }
162         }
163         logger.debug("createRolePermission with roleCsid=" + roleCsid);
164         ensureCSID(roleCsid, ServiceMessages.PUT_FAILED + "permroles role ");
165         try {
166                 Role role = (Role)get(roleCsid, Role.class);
167             // If marked as metadata immutable, do not delete
168             if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
169                 Response response = 
170                         Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
171                 return response;
172             }
173             PermissionRoleSubResource subResource =
174                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
175             String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
176             UriBuilder path = UriBuilder.fromResource(RoleResource.class);
177             path.path(roleCsid + "/permroles/" + permrolecsid);
178             Response response = Response.created(path.build()).build();
179             return response;
180         } catch (Exception e) {
181             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
182         }
183     }
184
185     @GET
186     @Path("{csid}/permroles")
187     public PermissionRole getRolePermission(
188             @PathParam("csid") String roleCsid) {
189         logger.debug("getRolePermission with roleCsid=" + roleCsid);
190         ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
191         PermissionRole result = null;
192         try {
193             PermissionRoleSubResource subResource =
194                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
195             //get relationships for a role
196             result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
197         } catch (Exception e) {
198             throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
199         }
200         checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
201         return result;
202     }
203
204     @GET
205     @Path("{csid}/permroles/{id}")
206     public PermissionRoleRel getRolePermission(
207             @PathParam("csid") String roleCsid,
208             @PathParam("id") String permrolecsid) {
209         logger.debug("getRolePermission with roleCsid=" + roleCsid);
210         ensureCSID(roleCsid, ServiceMessages.GET_FAILED + "permroles role ");
211         PermissionRoleRel result = null;
212         try {
213             PermissionRoleSubResource subResource =
214                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
215             //get relationships for a role
216             result = subResource.getPermissionRoleRel(roleCsid, SubjectType.PERMISSION, permrolecsid);
217         } catch (Exception e) {
218             throw bigReThrow(e, ServiceMessages.GET_FAILED, roleCsid);
219         }
220         checkResult(result, roleCsid, ServiceMessages.GET_FAILED);
221         return result;
222     }
223
224     public Response deleteRolePermission(String roleCsid, PermissionRole input) {
225         logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
226         ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
227         try {
228                 Role role = (Role)get(roleCsid, Role.class);
229             // If marked as metadata immutable, do not delete
230             if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
231                 Response response = 
232                         Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
233                 return response;
234             }
235             PermissionRoleSubResource subResource =
236                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
237             //delete all relationships for a permission
238             subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION, input);
239             return Response.status(HttpResponseCodes.SC_OK).build();
240         } catch (Exception e) {
241             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
242         }
243     }
244
245     @DELETE
246     @Path("{csid}/permroles")
247     public Response deleteRolePermission(
248                 @PathParam("csid") String roleCsid) {
249         logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
250         ensureCSID(roleCsid, ServiceMessages.DELETE_FAILED + "permroles role ");
251         try {
252                 Role role = (Role)get(roleCsid, Role.class);
253             // If marked as metadata immutable, do not delete
254             if(RoleClient.IMMUTABLE.equals(role.getPermsProtection())) {
255                 Response response = 
256                         Response.status(Response.Status.FORBIDDEN).entity("Role: "+roleCsid+" is immutable.").type("text/plain").build();
257                 return response;
258             }
259             PermissionRoleSubResource subResource =
260                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
261             //delete all relationships for a permission
262             subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION);
263             return Response.status(HttpResponseCodes.SC_OK).build();
264         } catch (Exception e) {
265             throw bigReThrow(e, ServiceMessages.DELETE_FAILED, roleCsid);
266         }
267     }
268 }