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