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