]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
49f27fe4977ba269a9d9a4c0d6aa8b25950a7233
[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 javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriBuilder;
39 import javax.ws.rs.core.UriInfo;
40
41 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
42 //import org.collectionspace.services.common.context.RemoteServiceContextImpl;
43 import org.collectionspace.services.common.ServiceMessages;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.context.ServiceContextFactory;
46 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
47 import org.collectionspace.services.common.document.BadRequestException;
48 import org.collectionspace.services.common.document.DocumentFilter;
49 import org.collectionspace.services.common.document.DocumentNotFoundException;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.security.UnauthorizedException;
52 import org.collectionspace.services.common.storage.StorageClient;
53 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
54 import org.jboss.resteasy.util.HttpResponseCodes;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 /**
59  * The Class RoleResource.
60  */
61 @Path("/authorization/roles")
62 @Consumes("application/xml")
63 @Produces("application/xml")
64 public class RoleResource
65         extends AbstractCollectionSpaceResourceImpl {
66
67     /** The service name. */
68     final private String serviceName = "authorization/roles";
69     /** The logger. */
70     final Logger logger = LoggerFactory.getLogger(RoleResource.class);
71     /** The storage client. */
72     final StorageClient storageClient = new JpaStorageClientImpl();
73
74     /* (non-Javadoc)
75      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
76      */
77     @Override
78     protected String getVersionString() {
79         /** The last change revision. */
80         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
81         return lastChangeRevision;
82     }
83
84     /* (non-Javadoc)
85      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
86      */
87     @Override
88     public String getServiceName() {
89         return serviceName;
90     }
91
92     /* (non-Javadoc)
93      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
94      */
95     @Override
96     public Class<RoleResource> getCommonPartClass() {
97         return RoleResource.class;
98     }
99
100     /* (non-Javadoc)
101      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
102      */
103     @Override
104     public ServiceContextFactory getServiceContextFactory() {
105         return RemoteServiceContextFactory.get();
106     }
107
108 //    private <T> ServiceContext createServiceContext(T obj) throws Exception {
109 //        ServiceContext ctx = new RemoteServiceContextImpl<T, T>(getServiceName());
110 //        ctx.setInput(obj);
111 //        ctx.setDocumentType(Role.class.getPackage().getName()); //persistence unit
112 //        ctx.setProperty("entity-name", Role.class.getName());
113 //        return ctx;
114 //    }
115
116     /* (non-Javadoc)
117      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
118      */
119     @Override
120     public StorageClient getStorageClient(ServiceContext ctx) {
121         //FIXME use ctx to identify storage client
122         return storageClient;
123     }
124
125 //    @Override
126 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
127 //        DocumentHandler docHandler = ctx.getDocumentHandler();
128 //        docHandler.setCommonPart(ctx.getInput());
129 //        return docHandler;
130 //    }
131     /**
132      * Creates the role.
133      *
134      * @param input the input
135      *
136      * @return the response
137      */
138     @POST
139     public Response createRole(Role input) {
140         try {
141             ServiceContext ctx = createServiceContext(input, Role.class);
142             DocumentHandler handler = createDocumentHandler(ctx);
143             String csid = getStorageClient(ctx).create(ctx, handler);
144             UriBuilder path = UriBuilder.fromResource(RoleResource.class);
145             path.path("" + csid);
146             Response response = Response.created(path.build()).build();
147             return response;
148         } catch (BadRequestException bre) {
149             Response response = Response.status(
150                     Response.Status.BAD_REQUEST).entity(ServiceMessages.POST_FAILED
151                     + bre.getErrorReason()).type("text/plain").build();
152             throw new WebApplicationException(response);
153         } catch (UnauthorizedException ue) {
154             Response response = Response.status(
155                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.POST_FAILED
156                     + ue.getErrorReason()).type("text/plain").build();
157             throw new WebApplicationException(response);
158         } catch (Exception e) {
159             if (logger.isDebugEnabled()) {
160                 logger.debug("Caught exception in createRole", e);
161             }
162             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
163             Response response = Response.status(
164                     Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.POST_FAILED
165                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
166             throw new WebApplicationException(response);
167         }
168     }
169
170     /**
171      * Gets the role.
172      * 
173      * @param csid the csid
174      * 
175      * @return the role
176      */
177     @GET
178     @Path("{csid}")
179     public Role getRole(
180             @PathParam("csid") String csid) {
181         if (logger.isDebugEnabled()) {
182             logger.debug("getRole with csid=" + csid);
183         }
184         if (csid == null || "".equals(csid)) {
185             logger.error("getRole: missing csid!");
186             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
187                     ServiceMessages.GET_FAILED + "role csid=").type(
188                     "text/plain").build();
189             throw new WebApplicationException(response);
190         }
191         Role result = null;
192         try {
193             ServiceContext ctx = createServiceContext((Role) null, Role.class);
194             DocumentHandler handler = createDocumentHandler(ctx);
195             getStorageClient(ctx).get(ctx, csid, handler);
196             result = (Role) ctx.getOutput();
197         } catch (UnauthorizedException ue) {
198             Response response = Response.status(
199                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
200                     + ue.getErrorReason()).type("text/plain").build();
201             throw new WebApplicationException(response);
202         } catch (DocumentNotFoundException dnfe) {
203             if (logger.isDebugEnabled()) {
204                 logger.debug("getRole", dnfe);
205             }
206             Response response = Response.status(Response.Status.NOT_FOUND).entity(
207                     ServiceMessages.GET_FAILED + "role csid=" + csid).type(
208                     "text/plain").build();
209             throw new WebApplicationException(response);
210         } catch (Exception e) {
211             if (logger.isDebugEnabled()) {
212                 logger.debug("getRole", e);
213             }
214             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
215             Response response = Response.status(
216                     Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.GET_FAILED
217                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
218             throw new WebApplicationException(response);
219         }
220
221         if (result == null) {
222             Response response = Response.status(Response.Status.NOT_FOUND).entity(
223                     ServiceMessages.GET_FAILED + "role csid=" + csid + ": was not found.").type(
224                     "text/plain").build();
225             throw new WebApplicationException(response);
226         }
227         return result;
228     }
229
230     /**
231      * Gets the role list.
232      * 
233      * @param ui the ui
234      * 
235      * @return the role list
236      */
237     @GET
238     @Produces("application/xml")
239     public RolesList getRoleList(
240             @Context UriInfo ui) {
241         RolesList roleList = new RolesList();
242         try {
243             ServiceContext ctx = createServiceContext((Role) null, Role.class);
244             DocumentHandler handler = createDocumentHandler(ctx);
245             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
246             DocumentFilter myFilter = handler.createDocumentFilter();
247             myFilter.setPagination(queryParams);
248             myFilter.setQueryParams(queryParams);
249             handler.setDocumentFilter(myFilter);
250             getStorageClient(ctx).getFiltered(ctx, handler);
251             roleList = (RolesList) handler.getCommonPartList();
252         } catch (UnauthorizedException ue) {
253             Response response = Response.status(
254                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.LIST_FAILED
255                     + ue.getErrorReason()).type("text/plain").build();
256             throw new WebApplicationException(response);
257
258         } catch (Exception e) {
259             if (logger.isDebugEnabled()) {
260                 logger.debug("Caught exception in getRoleList", e);
261             }
262             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
263             Response response = Response.status(
264                     Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.LIST_FAILED
265                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
266             throw new WebApplicationException(response);
267         }
268         return roleList;
269     }
270
271     /**
272      * Update role.
273      * 
274      * @param csid the csid
275      * @param theUpdate the the update
276      * 
277      * @return the role
278      */
279     @PUT
280     @Path("{csid}")
281     public Role updateRole(
282             @PathParam("csid") String csid,
283             Role theUpdate) {
284         if (logger.isDebugEnabled()) {
285             logger.debug("updateRole with csid=" + csid);
286         }
287         if (csid == null || "".equals(csid)) {
288             logger.error("updateRole: missing csid!");
289             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
290                     ServiceMessages.PUT_FAILED + "role "
291                     + ServiceMessages.MISSING_INVALID_CSID + csid).type(
292                     "text/plain").build();
293             throw new WebApplicationException(response);
294         }
295         Role result = null;
296         try {
297             ServiceContext ctx = createServiceContext(theUpdate, Role.class);
298             DocumentHandler handler = createDocumentHandler(ctx);
299             getStorageClient(ctx).update(ctx, csid, handler);
300             result = (Role) ctx.getOutput();
301         } catch (BadRequestException bre) {
302             Response response = Response.status(
303                     Response.Status.BAD_REQUEST).entity(ServiceMessages.PUT_FAILED
304                     + bre.getErrorReason()).type("text/plain").build();
305             throw new WebApplicationException(response);
306         } catch (UnauthorizedException ue) {
307             Response response = Response.status(
308                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.PUT_FAILED
309                     + ue.getErrorReason()).type("text/plain").build();
310             throw new WebApplicationException(response);
311         } catch (DocumentNotFoundException dnfe) {
312             if (logger.isDebugEnabled()) {
313                 logger.debug("caugth exception in updateRole", dnfe);
314             }
315             Response response = Response.status(Response.Status.NOT_FOUND).entity(
316                     ServiceMessages.PUT_FAILED + "role csid=" + csid).type(
317                     "text/plain").build();
318             throw new WebApplicationException(response);
319         } catch (Exception e) {
320             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
321             Response response = Response.status(
322                     Response.Status.INTERNAL_SERVER_ERROR).entity(
323                     ServiceMessages.PUT_FAILED
324                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
325             throw new WebApplicationException(response);
326         }
327         return result;
328     }
329
330     /**
331      * Delete role.
332      * 
333      * @param csid the csid
334      * 
335      * @return the response
336      */
337     @DELETE
338     @Path("{csid}")
339     public Response deleteRole(@PathParam("csid") String csid) {
340
341         if (logger.isDebugEnabled()) {
342             logger.debug("deleteRole with csid=" + csid);
343         }
344         if (csid == null || "".equals(csid)) {
345             logger.error("deleteRole: missing csid!");
346             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
347                     ServiceMessages.DELETE_FAILED + "role csid=" + csid).type(
348                     "text/plain").build();
349             throw new WebApplicationException(response);
350         }
351         try {
352             ServiceContext ctx = createServiceContext((Role) null, Role.class);
353             ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
354             return Response.status(HttpResponseCodes.SC_OK).build();
355         } catch (UnauthorizedException ue) {
356             Response response = Response.status(
357                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED + ue.getErrorReason()).type("text/plain").build();
358             throw new WebApplicationException(response);
359
360         } catch (DocumentNotFoundException dnfe) {
361             if (logger.isDebugEnabled()) {
362                 logger.debug("caught exception in deleteRole", dnfe);
363             }
364             Response response = Response.status(Response.Status.NOT_FOUND).entity(
365                     ServiceMessages.DELETE_FAILED + "role csid=" + csid).type(
366                     "text/plain").build();
367             throw new WebApplicationException(response);
368         } catch (Exception e) {
369             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
370             Response response = Response.status(
371                     Response.Status.INTERNAL_SERVER_ERROR).entity(
372                     ServiceMessages.DELETE_FAILED + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
373             throw new WebApplicationException(response);
374         }
375
376     }
377
378     @POST
379     @Path("{csid}/permroles")
380     public Response createRolePermission(@PathParam("csid") String roleCsid,
381             PermissionRole input) {
382         if (logger.isDebugEnabled()) {
383             logger.debug("createRolePermission with roleCsid=" + roleCsid);
384         }
385         if (roleCsid == null || "".equals(roleCsid)) {
386             logger.error("createRolePermission: missing roleCsid!");
387             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
388                     ServiceMessages.POST_FAILED + "permroles role "
389                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
390                     "text/plain").build();
391             throw new WebApplicationException(response);
392         }
393         try {
394             PermissionRoleSubResource subResource =
395                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
396             String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
397             UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
398             path.path(roleCsid + "/permroles/" + permrolecsid);
399             Response response = Response.created(path.build()).build();
400             return response;
401         } catch (BadRequestException bre) {
402             Response response = Response.status(
403                     Response.Status.BAD_REQUEST).entity("Create failed reason "
404                     + bre.getErrorReason()).type("text/plain").build();
405             throw new WebApplicationException(response);
406         } catch (UnauthorizedException ue) {
407             Response response = Response.status(
408                     Response.Status.UNAUTHORIZED).entity("Create failed reason "
409                     + ue.getErrorReason()).type("text/plain").build();
410             throw new WebApplicationException(response);
411         } catch (Exception e) {
412             if (logger.isDebugEnabled()) {
413                 logger.debug("Caught exception in createRolePermission", e);
414             }
415             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
416             Response response = Response.status(
417                     Response.Status.INTERNAL_SERVER_ERROR).entity(
418                     ServiceMessages.POST_FAILED
419                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
420             throw new WebApplicationException(response);
421         }
422     }
423
424     @GET
425     @Path("{csid}/permroles/{permrolecsid}")
426     public PermissionRole getRolePermission(
427             @PathParam("csid") String roleCsid,
428             @PathParam("permrolecsid") String permrolecsid) {
429         if (logger.isDebugEnabled()) {
430             logger.debug("getRolePermission with roleCsid=" + roleCsid);
431         }
432         if (roleCsid == null || "".equals(roleCsid)) {
433             logger.error("getRolePermission: missing roleCsid!");
434             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
435                     ServiceMessages.GET_FAILED + "permroles role "
436                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
437                     "text/plain").build();
438             throw new WebApplicationException(response);
439         }
440         PermissionRole result = null;
441         try {
442             PermissionRoleSubResource subResource =
443                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
444             //get relationships for a role
445             result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
446         } catch (UnauthorizedException ue) {
447             Response response = Response.status(
448                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
449                     + ue.getErrorReason()).type("text/plain").build();
450             throw new WebApplicationException(response);
451         } catch (DocumentNotFoundException dnfe) {
452             if (logger.isDebugEnabled()) {
453                 logger.debug("getRolePermission", dnfe);
454             }
455             Response response = Response.status(Response.Status.NOT_FOUND).entity(
456                     ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid).type(
457                     "text/plain").build();
458             throw new WebApplicationException(response);
459         } catch (Exception e) {
460             if (logger.isDebugEnabled()) {
461                 logger.debug("getRolePermission", e);
462             }
463             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
464             Response response = Response.status(
465                     Response.Status.INTERNAL_SERVER_ERROR).entity(
466                     ServiceMessages.GET_FAILED
467                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
468             throw new WebApplicationException(response);
469         }
470         if (result == null) {
471             Response response = Response.status(Response.Status.NOT_FOUND).entity(
472                     ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid
473                     + ": was not found.").type(
474                     "text/plain").build();
475             throw new WebApplicationException(response);
476         }
477         return result;
478     }
479
480     @DELETE
481     @Path("{csid}/permroles/{permrolecsid}")
482     public Response deleteRolePermission(
483             @PathParam("csid") String roleCsid,
484             @PathParam("permrolecsid") String permrolecsid) {
485         if (logger.isDebugEnabled()) {
486             logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
487         }
488         if (roleCsid == null || "".equals(roleCsid)) {
489             logger.error("deleteRolePermission: missing roleCsid!");
490             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
491                     ServiceMessages.DELETE_FAILED + "permroles role "
492                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
493                     "text/plain").build();
494             throw new WebApplicationException(response);
495         }
496         try {
497             PermissionRoleSubResource subResource =
498                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
499             //delete all relationships for a permission
500             subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION);
501             return Response.status(HttpResponseCodes.SC_OK).build();
502         } catch (UnauthorizedException ue) {
503             Response response = Response.status(
504                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
505                     + ue.getErrorReason()).type("text/plain").build();
506             throw new WebApplicationException(response);
507         } catch (DocumentNotFoundException dnfe) {
508             if (logger.isDebugEnabled()) {
509                 logger.debug("caught exception in deleteRolePermission", dnfe);
510             }
511             Response response = Response.status(Response.Status.NOT_FOUND).entity(
512                     ServiceMessages.DELETE_FAILED + "role csid=" + roleCsid).type(
513                     "text/plain").build();
514             throw new WebApplicationException(response);
515         } catch (Exception e) {
516             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
517             Response response = Response.status(
518                     Response.Status.INTERNAL_SERVER_ERROR).entity(
519                     ServiceMessages.DELETE_FAILED
520                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
521             throw new WebApplicationException(response);
522         }
523
524     }
525 }