]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1913533376b25dedfb4ec6c46f8a1e560bf8be78
[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.context.ServiceContext;
44 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
45 import org.collectionspace.services.common.context.ServiceContextFactory;
46 import org.collectionspace.services.common.document.BadRequestException;
47 import org.collectionspace.services.common.document.DocumentFilter;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.security.UnauthorizedException;
51 import org.collectionspace.services.common.storage.StorageClient;
52 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
53 import org.jboss.resteasy.util.HttpResponseCodes;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * The Class PermissionResource.
59  */
60 @Path("/authorization/permissions")
61 @Consumes("application/xml")
62 @Produces("application/xml")
63 public class PermissionResource
64         extends AbstractCollectionSpaceResourceImpl<Permission, Permission> {
65
66     /** The service name. */
67     final private String serviceName = "authorization/permissions";
68     
69     /** The logger. */
70     final Logger logger = LoggerFactory.getLogger(PermissionResource.class);
71     
72     /** The storage client. */
73     final StorageClient storageClient = new JpaStorageClientImpl(Permission.class);
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<Permission> getCommonPartClass() {
98         return Permission.class;
99     }
100     
101     /* (non-Javadoc)
102      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
103      */
104     @Override
105     public ServiceContextFactory<Permission, Permission> getServiceContextFactory() {
106         return RemoteServiceContextFactory.get();
107     }
108                 
109     /* (non-Javadoc)
110      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
111      */
112     @Override
113     public StorageClient getStorageClient(ServiceContext ctx) {
114         //FIXME use ctx to identify storage client
115         return storageClient;
116     }
117
118 //    @Override
119 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
120 //        DocumentHandler docHandler = ctx.getDocumentHandler();
121 //        docHandler.setCommonPart(ctx.getInput());
122 //        return docHandler;
123 //    }
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("Create failed reason "
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("Create failed reason "
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             Response response = Response.status(
157                     Response.Status.INTERNAL_SERVER_ERROR).entity(
158                     "Create failed").type("text/plain").build();
159             throw new WebApplicationException(response);
160         }
161     }
162
163     /**
164      * Gets the permission.
165      * 
166      * @param csid the csid
167      * 
168      * @return the permission
169      */
170     @GET
171     @Path("{csid}")
172     public Permission getPermission(
173             @PathParam("csid") String csid) {
174         if (logger.isDebugEnabled()) {
175             logger.debug("getPermission with csid=" + csid);
176         }
177         if (csid == null || "".equals(csid)) {
178             logger.error("getPermission: missing csid!");
179             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
180                     "get failed on Permission csid=" + csid).type(
181                     "text/plain").build();
182             throw new WebApplicationException(response);
183         }
184         Permission result = null;
185         try {
186             ServiceContext<Permission, Permission> ctx = createServiceContext();
187             DocumentHandler handler = createDocumentHandler(ctx);
188             getStorageClient(ctx).get(ctx, csid, handler);
189             result = (Permission) ctx.getOutput();
190         } catch (UnauthorizedException ue) {
191             Response response = Response.status(
192                     Response.Status.UNAUTHORIZED).entity("Get failed reason "
193                     + ue.getErrorReason()).type("text/plain").build();
194             throw new WebApplicationException(response);
195         } catch (DocumentNotFoundException dnfe) {
196             if (logger.isDebugEnabled()) {
197                 logger.debug("getPermission", dnfe);
198             }
199             Response response = Response.status(Response.Status.NOT_FOUND).entity(
200                     "Get failed on Permission csid=" + csid).type(
201                     "text/plain").build();
202             throw new WebApplicationException(response);
203         } catch (Exception e) {
204             if (logger.isDebugEnabled()) {
205                 logger.debug("getPermission", e);
206             }
207             Response response = Response.status(
208                     Response.Status.INTERNAL_SERVER_ERROR).entity(
209                     "Get failed").type("text/plain").build();
210             throw new WebApplicationException(response);
211         }
212
213         if (result == null) {
214             Response response = Response.status(Response.Status.NOT_FOUND).entity(
215                     "Get failed, the requested Permission CSID:" + csid + ": was not found.").type(
216                     "text/plain").build();
217             throw new WebApplicationException(response);
218         }
219         return result;
220     }
221
222     /**
223      * Gets the permission list.
224      * 
225      * @param ui the ui
226      * 
227      * @return the permission list
228      */
229     @GET
230     @Produces("application/xml")
231     public PermissionsList getPermissionList(
232             @Context UriInfo ui) {
233         PermissionsList permissionList = new PermissionsList();
234         try {
235             ServiceContext<Permission, Permission> ctx = createServiceContext();
236             DocumentHandler handler = createDocumentHandler(ctx);
237             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
238             DocumentFilter myFilter = handler.createDocumentFilter();
239             myFilter.setPagination(queryParams);
240             myFilter.setQueryParams(queryParams);
241             handler.setDocumentFilter(myFilter);
242             getStorageClient(ctx).getFiltered(ctx, handler);
243             permissionList = (PermissionsList) handler.getCommonPartList();
244         } catch (UnauthorizedException ue) {
245             Response response = Response.status(
246                     Response.Status.UNAUTHORIZED).entity("Index failed reason "
247                     + ue.getErrorReason()).type("text/plain").build();
248             throw new WebApplicationException(response);
249
250         } catch (Exception e) {
251             if (logger.isDebugEnabled()) {
252                 logger.debug("Caught exception in getPermissionsList", e);
253             }
254             Response response = Response.status(
255                     Response.Status.INTERNAL_SERVER_ERROR).entity(
256                     "Index failed").type("text/plain").build();
257             throw new WebApplicationException(response);
258         }
259         return permissionList;
260     }
261
262     /**
263      * Update permission.
264      * 
265      * @param csid the csid
266      * @param theUpdate the the update
267      * 
268      * @return the permission
269      */
270     @PUT
271     @Path("{csid}")
272     public Permission updatePermission(
273             @PathParam("csid") String csid,
274             Permission theUpdate) {
275         if (logger.isDebugEnabled()) {
276             logger.debug("updatePermission with csid=" + csid);
277         }
278         if (csid == null || "".equals(csid)) {
279             logger.error("updatePermission: missing csid!");
280             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
281                     "update failed on Permission csid=" + csid).type(
282                     "text/plain").build();
283             throw new WebApplicationException(response);
284         }
285         Permission result = null;
286         try {
287             ServiceContext<Permission, Permission> ctx = createServiceContext(theUpdate);
288             DocumentHandler handler = createDocumentHandler(ctx);
289             getStorageClient(ctx).update(ctx, csid, handler);
290             result = (Permission) ctx.getOutput();
291         } catch (BadRequestException bre) {
292             Response response = Response.status(
293                     Response.Status.BAD_REQUEST).entity("Update failed reason "
294                     + bre.getErrorReason()).type("text/plain").build();
295             throw new WebApplicationException(response);
296         } catch (UnauthorizedException ue) {
297             Response response = Response.status(
298                     Response.Status.UNAUTHORIZED).entity("Update failed reason "
299                     + ue.getErrorReason()).type("text/plain").build();
300             throw new WebApplicationException(response);
301         } catch (DocumentNotFoundException dnfe) {
302             if (logger.isDebugEnabled()) {
303                 logger.debug("caugth exception in updatePermission", dnfe);
304             }
305             Response response = Response.status(Response.Status.NOT_FOUND).entity(
306                     "Update failed on Permission csid=" + csid).type(
307                     "text/plain").build();
308             throw new WebApplicationException(response);
309         } catch (Exception e) {
310             Response response = Response.status(
311                     Response.Status.INTERNAL_SERVER_ERROR).entity(
312                     "Update failed").type("text/plain").build();
313             throw new WebApplicationException(response);
314         }
315         return result;
316     }
317
318     /**
319      * Delete permission.
320      * 
321      * @param csid the csid
322      * 
323      * @return the response
324      */
325     @DELETE
326     @Path("{csid}")
327     public Response deletePermission(@PathParam("csid") String csid) {
328
329         if (logger.isDebugEnabled()) {
330             logger.debug("deletePermission with csid=" + csid);
331         }
332         if (csid == null || "".equals(csid)) {
333             logger.error("deletePermission: missing csid!");
334             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
335                     "delete failed on Permission csid=" + csid).type(
336                     "text/plain").build();
337             throw new WebApplicationException(response);
338         }
339         try {
340             ServiceContext<Permission, Permission> ctx = createServiceContext();
341             getStorageClient(ctx).delete(ctx, csid);
342             return Response.status(HttpResponseCodes.SC_OK).build();
343         } catch (UnauthorizedException ue) {
344             Response response = Response.status(
345                     Response.Status.UNAUTHORIZED).entity("Delete failed reason "
346                     + ue.getErrorReason()).type("text/plain").build();
347             throw new WebApplicationException(response);
348
349         } catch (DocumentNotFoundException dnfe) {
350             if (logger.isDebugEnabled()) {
351                 logger.debug("caught exception in deletePermission", dnfe);
352             }
353             Response response = Response.status(Response.Status.NOT_FOUND).entity(
354                     "Delete failed on Permission csid=" + csid).type(
355                     "text/plain").build();
356             throw new WebApplicationException(response);
357         } catch (Exception e) {
358             Response response = Response.status(
359                     Response.Status.INTERNAL_SERVER_ERROR).entity(
360                     "Delete failed").type("text/plain").build();
361             throw new WebApplicationException(response);
362         }
363
364     }
365
366     @POST
367     @Path("{csid}/permroles")
368     public Response createPermissionRole(@PathParam("csid") String permCsid,
369             PermissionRole input) {
370         if (logger.isDebugEnabled()) {
371             logger.debug("createPermissionRole with permCsid=" + permCsid);
372         }
373         if (permCsid == null || "".equals(permCsid)) {
374             logger.error("createPermissionRole: missing permCsid!");
375             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
376                     "create failed on PermissionRole permCsid=" + permCsid).type(
377                     "text/plain").build();
378             throw new WebApplicationException(response);
379         }
380         try {
381             PermissionRoleSubResource subResource = new PermissionRoleSubResource();
382             String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
383             UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
384             path.path(permCsid + "/permroles/" + permrolecsid);
385             Response response = Response.created(path.build()).build();
386             return response;
387         } catch (BadRequestException bre) {
388             Response response = Response.status(
389                     Response.Status.BAD_REQUEST).entity("Create failed reason "
390                     + bre.getErrorReason()).type("text/plain").build();
391             throw new WebApplicationException(response);
392         } catch (UnauthorizedException ue) {
393             Response response = Response.status(
394                     Response.Status.UNAUTHORIZED).entity("Create failed reason "
395                     + ue.getErrorReason()).type("text/plain").build();
396             throw new WebApplicationException(response);
397         } catch (Exception e) {
398             if (logger.isDebugEnabled()) {
399                 logger.debug("Caught exception in createPermissionRole", e);
400             }
401             Response response = Response.status(
402                     Response.Status.INTERNAL_SERVER_ERROR).entity(
403                     "Create failed").type("text/plain").build();
404             throw new WebApplicationException(response);
405         }
406     }
407
408     @GET
409     @Path("{csid}/permroles/{permrolecsid}")
410     public PermissionRole getPermissionRole(
411             @PathParam("csid") String permCsid,
412             @PathParam("permrolecsid") String permrolecsid) {
413         if (logger.isDebugEnabled()) {
414             logger.debug("getPermissionRole with permCsid=" + permCsid);
415         }
416         if (permCsid == null || "".equals(permCsid)) {
417             logger.error("getPermissionRole: missing permCsid!");
418             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
419                     "get failed on PermissionRole permCsid=" + permCsid).type(
420                     "text/plain").build();
421             throw new WebApplicationException(response);
422         }
423         PermissionRole result = null;
424         try {
425             PermissionRoleSubResource subResource = new PermissionRoleSubResource();
426             //get relationships for a permission
427             result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
428         } catch (UnauthorizedException ue) {
429             Response response = Response.status(
430                     Response.Status.UNAUTHORIZED).entity("Get failed reason "
431                     + ue.getErrorReason()).type("text/plain").build();
432             throw new WebApplicationException(response);
433         } catch (DocumentNotFoundException dnfe) {
434             if (logger.isDebugEnabled()) {
435                 logger.debug("getPermissionRole", dnfe);
436             }
437             Response response = Response.status(Response.Status.NOT_FOUND).entity(
438                     "Get failed on PermissionRole permrolecsid=" + permrolecsid).type(
439                     "text/plain").build();
440             throw new WebApplicationException(response);
441         } catch (Exception e) {
442             if (logger.isDebugEnabled()) {
443                 logger.debug("getPermissionRole", e);
444             }
445             Response response = Response.status(
446                     Response.Status.INTERNAL_SERVER_ERROR).entity(
447                     "Get failed").type("text/plain").build();
448             throw new WebApplicationException(response);
449         }
450         if (result == null) {
451             Response response = Response.status(Response.Status.NOT_FOUND).entity(
452                     "Get failed, the requested PermissionRole permrolecsid:" + permrolecsid
453                     + ": was not found.").type(
454                     "text/plain").build();
455             throw new WebApplicationException(response);
456         }
457         return result;
458     }
459
460     @DELETE
461     @Path("{csid}/permroles/{permrolecsid}")
462     public Response deletePermissionRole(
463             @PathParam("csid") String permCsid,
464             @PathParam("permrolecsid") String permrolecsid) {
465         if (logger.isDebugEnabled()) {
466             logger.debug("deletePermissionRole with permCsid=" + permCsid);
467         }
468         if (permCsid == null || "".equals(permCsid)) {
469             logger.error("deletePermissionRole: missing permCsid!");
470             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
471                     "delete failed on PermissionRole permCsid=" + permCsid).type(
472                     "text/plain").build();
473             throw new WebApplicationException(response);
474         }
475         try {
476             PermissionRoleSubResource subResource = new PermissionRoleSubResource();
477             //delete all relationships for a permission
478             subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
479             return Response.status(HttpResponseCodes.SC_OK).build();
480         } catch (UnauthorizedException ue) {
481             Response response = Response.status(
482                     Response.Status.UNAUTHORIZED).entity("Delete failed reason "
483                     + ue.getErrorReason()).type("text/plain").build();
484             throw new WebApplicationException(response);
485         } catch (DocumentNotFoundException dnfe) {
486             if (logger.isDebugEnabled()) {
487                 logger.debug("caught exception in deletePermissionRole", dnfe);
488             }
489             Response response = Response.status(Response.Status.NOT_FOUND).entity(
490                     "Delete failed on PermissionRole permrolecsid=" + permrolecsid).type(
491                     "text/plain").build();
492             throw new WebApplicationException(response);
493         } catch (Exception e) {
494             Response response = Response.status(
495                     Response.Status.INTERNAL_SERVER_ERROR).entity(
496                     "Delete failed").type("text/plain").build();
497             throw new WebApplicationException(response);
498         }
499
500     }
501 }