]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9e33df598ca466d46e00a64da62fe0787144971f
[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.account.AccountRoleSubResource;
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.ServiceContextFactory;
48 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
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 // TODO: Auto-generated Javadoc
61 /**
62  * The Class RoleResource.
63  */
64 @Path("/authorization/roles")
65 @Consumes("application/xml")
66 @Produces("application/xml")
67 public class RoleResource
68         extends AbstractCollectionSpaceResourceImpl {
69
70     /** The service name. */
71     final private String serviceName = "authorization/roles";
72     /** The logger. */
73     final Logger logger = LoggerFactory.getLogger(RoleResource.class);
74     /** The storage client. */
75     final StorageClient storageClient = new JpaStorageClientImpl();
76
77     /* (non-Javadoc)
78      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
79      */
80     @Override
81     protected String getVersionString() {
82         /** The last change revision. */
83         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
84         return lastChangeRevision;
85     }
86
87     /* (non-Javadoc)
88      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
89      */
90     @Override
91     public String getServiceName() {
92         return serviceName;
93     }
94
95     /* (non-Javadoc)
96      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
97      */
98     @Override
99     public Class<RoleResource> getCommonPartClass() {
100         return RoleResource.class;
101     }
102
103     /* (non-Javadoc)
104      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
105      */
106     @Override
107     public ServiceContextFactory getServiceContextFactory() {
108         return RemoteServiceContextFactory.get();
109     }
110
111 //    private <T> ServiceContext createServiceContext(T obj) throws Exception {
112 //        ServiceContext ctx = new RemoteServiceContextImpl<T, T>(getServiceName());
113 //        ctx.setInput(obj);
114 //        ctx.setDocumentType(Role.class.getPackage().getName()); //persistence unit
115 //        ctx.setProperty("entity-name", Role.class.getName());
116 //        return ctx;
117 //    }
118
119     /* (non-Javadoc)
120      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
121      */
122     @Override
123     public StorageClient getStorageClient(ServiceContext ctx) {
124         //FIXME use ctx to identify storage client
125         return storageClient;
126     }
127
128 //    @Override
129 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
130 //        DocumentHandler docHandler = ctx.getDocumentHandler();
131 //        docHandler.setCommonPart(ctx.getInput());
132 //        return docHandler;
133 //    }
134     /**
135      * Creates the role.
136      *
137      * @param input the input
138      *
139      * @return the response
140      */
141     @POST
142     public Response createRole(Role input) {
143         try {
144             ServiceContext ctx = createServiceContext(input, Role.class);
145             DocumentHandler handler = createDocumentHandler(ctx);
146             String csid = getStorageClient(ctx).create(ctx, handler);
147             UriBuilder path = UriBuilder.fromResource(RoleResource.class);
148             path.path("" + csid);
149             Response response = Response.created(path.build()).build();
150             return response;
151         } catch (BadRequestException bre) {
152             Response response = Response.status(
153                     Response.Status.BAD_REQUEST).entity(ServiceMessages.POST_FAILED
154                     + bre.getErrorReason()).type("text/plain").build();
155             throw new WebApplicationException(response);
156         } catch (UnauthorizedException ue) {
157             Response response = Response.status(
158                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.POST_FAILED
159                     + ue.getErrorReason()).type("text/plain").build();
160             throw new WebApplicationException(response);
161         } catch (Exception e) {
162             if (logger.isDebugEnabled()) {
163                 logger.debug("Caught exception in createRole", e);
164             }
165             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
166             Response response = Response.status(
167                     Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.POST_FAILED
168                     + e.getMessage()).type("text/plain").build();
169             throw new WebApplicationException(e, response);
170         }
171     }
172
173     /**
174      * Gets the role.
175      * 
176      * @param csid the csid
177      * 
178      * @return the role
179      */
180     @GET
181     @Path("{csid}")
182     public Role getRole(
183             @PathParam("csid") String csid) {
184         if (logger.isDebugEnabled()) {
185             logger.debug("getRole with csid=" + csid);
186         }
187         if (csid == null || "".equals(csid)) {
188             logger.error("getRole: missing csid!");
189             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
190                     ServiceMessages.GET_FAILED + "role csid=").type(
191                     "text/plain").build();
192             throw new WebApplicationException(response);
193         }
194         Role result = null;
195         try {
196             ServiceContext ctx = createServiceContext((Role) null, Role.class);
197             DocumentHandler handler = createDocumentHandler(ctx);
198             getStorageClient(ctx).get(ctx, csid, handler);
199             result = (Role) ctx.getOutput();
200         } catch (UnauthorizedException ue) {
201             Response response = Response.status(
202                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
203                     + ue.getErrorReason()).type("text/plain").build();
204             throw new WebApplicationException(response);
205         } catch (DocumentNotFoundException dnfe) {
206             if (logger.isDebugEnabled()) {
207                 logger.debug("getRole", dnfe);
208             }
209             Response response = Response.status(Response.Status.NOT_FOUND).entity(
210                     ServiceMessages.GET_FAILED + "role csid=" + csid).type(
211                     "text/plain").build();
212             throw new WebApplicationException(response);
213         } catch (Exception e) {
214             if (logger.isDebugEnabled()) {
215                 logger.debug("getRole", e);
216             }
217             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
218             Response response = Response.status(
219                     Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.GET_FAILED
220                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
221             throw new WebApplicationException(response);
222         }
223
224         if (result == null) {
225             Response response = Response.status(Response.Status.NOT_FOUND).entity(
226                     ServiceMessages.GET_FAILED + "role csid=" + csid + ": was not found.").type(
227                     "text/plain").build();
228             throw new WebApplicationException(response);
229         }
230         return result;
231     }
232
233     /**
234      * Gets the role list.
235      * 
236      * @param ui the ui
237      * 
238      * @return the role list
239      */
240     @GET
241     @Produces("application/xml")
242     public RolesList getRoleList(
243             @Context UriInfo ui) {
244         RolesList roleList = new RolesList();
245         try {
246             ServiceContext ctx = createServiceContext((Role) null, Role.class);
247             DocumentHandler handler = createDocumentHandler(ctx);
248             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
249             DocumentFilter myFilter = handler.createDocumentFilter();
250             myFilter.setPagination(queryParams);
251             myFilter.setQueryParams(queryParams);
252             handler.setDocumentFilter(myFilter);
253             getStorageClient(ctx).getFiltered(ctx, handler);
254             roleList = (RolesList) handler.getCommonPartList();
255         } catch (UnauthorizedException ue) {
256             Response response = Response.status(
257                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.LIST_FAILED
258                     + ue.getErrorReason()).type("text/plain").build();
259             throw new WebApplicationException(response);
260
261         } catch (Exception e) {
262             if (logger.isDebugEnabled()) {
263                 logger.debug("Caught exception in getRoleList", e);
264             }
265             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
266             Response response = Response.status(
267                     Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.LIST_FAILED
268                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
269             throw new WebApplicationException(response);
270         }
271         return roleList;
272     }
273
274     /**
275      * Update role.
276      * 
277      * @param csid the csid
278      * @param theUpdate the the update
279      * 
280      * @return the role
281      */
282     @PUT
283     @Path("{csid}")
284     public Role updateRole(
285             @PathParam("csid") String csid,
286             Role theUpdate) {
287         if (logger.isDebugEnabled()) {
288             logger.debug("updateRole with csid=" + csid);
289         }
290         if (csid == null || "".equals(csid)) {
291             logger.error("updateRole: missing csid!");
292             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
293                     ServiceMessages.PUT_FAILED + "role "
294                     + ServiceMessages.MISSING_INVALID_CSID + csid).type(
295                     "text/plain").build();
296             throw new WebApplicationException(response);
297         }
298         Role result = null;
299         try {
300             ServiceContext ctx = createServiceContext(theUpdate, Role.class);
301             DocumentHandler handler = createDocumentHandler(ctx);
302             getStorageClient(ctx).update(ctx, csid, handler);
303             result = (Role) ctx.getOutput();
304         } catch (BadRequestException bre) {
305             Response response = Response.status(
306                     Response.Status.BAD_REQUEST).entity(ServiceMessages.PUT_FAILED
307                     + bre.getErrorReason()).type("text/plain").build();
308             throw new WebApplicationException(response);
309         } catch (UnauthorizedException ue) {
310             Response response = Response.status(
311                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.PUT_FAILED
312                     + ue.getErrorReason()).type("text/plain").build();
313             throw new WebApplicationException(response);
314         } catch (DocumentNotFoundException dnfe) {
315             if (logger.isDebugEnabled()) {
316                 logger.debug("caugth exception in updateRole", dnfe);
317             }
318             Response response = Response.status(Response.Status.NOT_FOUND).entity(
319                     ServiceMessages.PUT_FAILED + "role csid=" + csid).type(
320                     "text/plain").build();
321             throw new WebApplicationException(response);
322         } catch (Exception e) {
323             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
324             Response response = Response.status(
325                     Response.Status.INTERNAL_SERVER_ERROR).entity(
326                     ServiceMessages.PUT_FAILED
327                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
328             throw new WebApplicationException(response);
329         }
330         return result;
331     }
332
333     /**
334      * Delete role.
335      * 
336      * @param csid the csid
337      * 
338      * @return the response
339      */
340     @DELETE
341     @Path("{csid}")
342     public Response deleteRole(@PathParam("csid") String csid) {
343
344         if (logger.isDebugEnabled()) {
345             logger.debug("deleteRole with csid=" + csid);
346         }
347         if (csid == null || "".equals(csid)) {
348             logger.error("deleteRole: missing csid!");
349             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
350                     ServiceMessages.DELETE_FAILED + "role csid=" + csid).type(
351                     "text/plain").build();
352             throw new WebApplicationException(response);
353         }
354         try {
355             //FIXME ideally the following three operations should be in the same tx CSPACE-658
356             //delete all relationships for this permission
357             PermissionRoleSubResource permRoleResource =
358                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
359             permRoleResource.deletePermissionRole(csid, SubjectType.PERMISSION);
360             //delete all the account/role relationships associate with this role
361             AccountRoleSubResource accountRoleResource =
362                 new AccountRoleSubResource(AccountRoleSubResource.ROLE_ACCOUNTROLE_SERVICE);
363             accountRoleResource.deleteAccountRole(csid, SubjectType.ACCOUNT);
364             //finally, delete the role itself
365             ServiceContext<Role, Role> ctx = createServiceContext((Role) null, Role.class);
366             ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
367             return Response.status(HttpResponseCodes.SC_OK).build();
368         } catch (UnauthorizedException ue) {
369             Response response = Response.status(
370                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED + ue.getErrorReason()).type("text/plain").build();
371             throw new WebApplicationException(response);
372
373         } catch (DocumentNotFoundException dnfe) {
374             if (logger.isDebugEnabled()) {
375                 logger.debug("caught exception in deleteRole", dnfe);
376             }
377             Response response = Response.status(Response.Status.NOT_FOUND).entity(
378                     ServiceMessages.DELETE_FAILED + "role csid=" + csid).type(
379                     "text/plain").build();
380             throw new WebApplicationException(response);
381         } catch (Exception e) {
382             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
383             Response response = Response.status(
384                     Response.Status.INTERNAL_SERVER_ERROR).entity(
385                     ServiceMessages.DELETE_FAILED + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
386             throw new WebApplicationException(response);
387         }
388
389     }
390
391     /**
392      * Creates the role permission.
393      *
394      * @param method the method
395      * @param roleCsid the role csid
396      * @param input the input
397      * @return the response
398      */
399     @POST
400     @Path("{csid}/permroles")
401     public Response createRolePermission(@QueryParam("_method") String method, @PathParam("csid") String roleCsid,
402             PermissionRole input) {
403         if (method != null) {
404             if ("delete".equalsIgnoreCase(method)) {
405                 return deleteRolePermission(roleCsid, input);
406             }
407         }
408         if (logger.isDebugEnabled()) {
409             logger.debug("createRolePermission with roleCsid=" + roleCsid);
410         }
411         if (roleCsid == null || "".equals(roleCsid)) {
412             logger.error("createRolePermission: missing roleCsid!");
413             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
414                     ServiceMessages.POST_FAILED + "permroles role "
415                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
416                     "text/plain").build();
417             throw new WebApplicationException(response);
418         }
419         try {
420             PermissionRoleSubResource subResource =
421                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
422             String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
423             UriBuilder path = UriBuilder.fromResource(RoleResource.class);
424             path.path(roleCsid + "/permroles/" + permrolecsid);
425             Response response = Response.created(path.build()).build();
426             return response;
427         } catch (BadRequestException bre) {
428             Response response = Response.status(
429                     Response.Status.BAD_REQUEST).entity("Create failed reason "
430                     + bre.getErrorReason()).type("text/plain").build();
431             throw new WebApplicationException(response);
432         } catch (UnauthorizedException ue) {
433             Response response = Response.status(
434                     Response.Status.UNAUTHORIZED).entity("Create failed reason "
435                     + ue.getErrorReason()).type("text/plain").build();
436             throw new WebApplicationException(response);
437         } catch (Exception e) {
438             if (logger.isDebugEnabled()) {
439                 logger.debug("Caught exception in createRolePermission", e);
440             }
441             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
442             Response response = Response.status(
443                     Response.Status.INTERNAL_SERVER_ERROR).entity(
444                     ServiceMessages.POST_FAILED
445                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
446             throw new WebApplicationException(response);
447         }
448     }
449
450     /**
451      * Gets the role permission.
452      *
453      * @param roleCsid the role csid
454      * @param permrolecsid the permrolecsid
455      * @return the role permission
456      */
457     @GET
458     @Path("{csid}/permroles")
459     public PermissionRole getRolePermission(
460             @PathParam("csid") String roleCsid) {
461         if (logger.isDebugEnabled()) {
462             logger.debug("getRolePermission with roleCsid=" + roleCsid);
463         }
464         if (roleCsid == null || "".equals(roleCsid)) {
465             logger.error("getRolePermission: missing roleCsid!");
466             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
467                     ServiceMessages.GET_FAILED + "permroles role "
468                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
469                     "text/plain").build();
470             throw new WebApplicationException(response);
471         }
472         PermissionRole result = null;
473         try {
474             PermissionRoleSubResource subResource =
475                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
476             //get relationships for a role
477             result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
478         } catch (UnauthorizedException ue) {
479             Response response = Response.status(
480                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
481                     + ue.getErrorReason()).type("text/plain").build();
482             throw new WebApplicationException(response);
483         } catch (DocumentNotFoundException dnfe) {
484             if (logger.isDebugEnabled()) {
485                 logger.debug("getRolePermission", dnfe);
486             }
487             Response response = Response.status(Response.Status.NOT_FOUND).entity(
488                     ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid).type(
489                     "text/plain").build();
490             throw new WebApplicationException(response);
491         } catch (Exception e) {
492             if (logger.isDebugEnabled()) {
493                 logger.debug("getRolePermission", e);
494             }
495             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
496             Response response = Response.status(
497                     Response.Status.INTERNAL_SERVER_ERROR).entity(
498                     ServiceMessages.GET_FAILED
499                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
500             throw new WebApplicationException(response);
501         }
502         if (result == null) {
503             Response response = Response.status(Response.Status.NOT_FOUND).entity(
504                     ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid
505                     + ": was not found.").type(
506                     "text/plain").build();
507             throw new WebApplicationException(response);
508         }
509         return result;
510     }
511
512     @GET
513     @Path("{csid}/permroles/{id}")
514     public PermissionRoleRel getRolePermission(
515             @PathParam("csid") String roleCsid,
516             @PathParam("id") String permrolecsid) {
517         if (logger.isDebugEnabled()) {
518             logger.debug("getRolePermission with roleCsid=" + roleCsid);
519         }
520         if (roleCsid == null || "".equals(roleCsid)) {
521             logger.error("getRolePermission: missing roleCsid!");
522             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
523                     ServiceMessages.GET_FAILED + "permroles role "
524                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
525                     "text/plain").build();
526             throw new WebApplicationException(response);
527         }
528         PermissionRoleRel result = null;
529         try {
530             PermissionRoleSubResource subResource =
531                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
532             //get relationships for a role
533             result = subResource.getPermissionRoleRel(roleCsid, SubjectType.PERMISSION, permrolecsid);
534         } catch (UnauthorizedException ue) {
535             Response response = Response.status(
536                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
537                     + ue.getErrorReason()).type("text/plain").build();
538             throw new WebApplicationException(response);
539         } catch (DocumentNotFoundException dnfe) {
540             if (logger.isDebugEnabled()) {
541                 logger.debug("getRolePermission", dnfe);
542             }
543             Response response = Response.status(Response.Status.NOT_FOUND).entity(
544                     ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid).type(
545                     "text/plain").build();
546             throw new WebApplicationException(response);
547         } catch (Exception e) {
548             if (logger.isDebugEnabled()) {
549                 logger.debug("getRolePermission", e);
550             }
551             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
552             Response response = Response.status(
553                     Response.Status.INTERNAL_SERVER_ERROR).entity(
554                     ServiceMessages.GET_FAILED
555                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
556             throw new WebApplicationException(response);
557         }
558         if (result == null) {
559             Response response = Response.status(Response.Status.NOT_FOUND).entity(
560                     ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid
561                     + ": was not found.").type(
562                     "text/plain").build();
563             throw new WebApplicationException(response);
564         }
565         return result;
566     }
567
568     /**
569      * Delete role permission.
570      *
571      * @param roleCsid the role csid
572      * @param input the input
573      * @return the response
574      */
575     public Response deleteRolePermission(String roleCsid, PermissionRole input) {
576
577         if (logger.isDebugEnabled()) {
578             logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
579         }
580
581         if (roleCsid == null || "".equals(roleCsid)) {
582             logger.error("deleteRolePermission: missing roleCsid!");
583             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
584                     ServiceMessages.DELETE_FAILED + "permroles role "
585                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
586                     "text/plain").build();
587             throw new WebApplicationException(response);
588         }
589         try {
590             PermissionRoleSubResource subResource =
591                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
592             //delete all relationships for a permission
593             subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION, input);
594             return Response.status(HttpResponseCodes.SC_OK).build();
595         } catch (UnauthorizedException ue) {
596             Response response = Response.status(
597                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
598                     + ue.getErrorReason()).type("text/plain").build();
599             throw new WebApplicationException(response);
600         } catch (DocumentNotFoundException dnfe) {
601             if (logger.isDebugEnabled()) {
602                 logger.debug("caught exception in deleteRolePermission", dnfe);
603             }
604             Response response = Response.status(Response.Status.NOT_FOUND).entity(
605                     ServiceMessages.DELETE_FAILED + "role csid=" + roleCsid).type(
606                     "text/plain").build();
607             throw new WebApplicationException(response);
608         } catch (Exception e) {
609             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
610             Response response = Response.status(
611                     Response.Status.INTERNAL_SERVER_ERROR).entity(
612                     ServiceMessages.DELETE_FAILED
613                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
614             throw new WebApplicationException(response);
615         }
616
617     }
618
619     /**
620      * Delete role permission.
621      *
622      * @param roleCsid the role csid
623      * @return the response
624      */
625     @DELETE
626     @Path("{csid}/permroles")
627     public Response deleteRolePermission(
628                 @PathParam("csid") String roleCsid) {
629
630         if (logger.isDebugEnabled()) {
631             logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
632         }
633
634         if (roleCsid == null || "".equals(roleCsid)) {
635             logger.error("deleteRolePermission: missing roleCsid!");
636             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
637                     ServiceMessages.DELETE_FAILED + "permroles role "
638                     + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
639                     "text/plain").build();
640             throw new WebApplicationException(response);
641         }
642         try {
643             PermissionRoleSubResource subResource =
644                     new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
645             //delete all relationships for a permission
646             subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION);
647             return Response.status(HttpResponseCodes.SC_OK).build();
648         } catch (UnauthorizedException ue) {
649             Response response = Response.status(
650                     Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
651                     + ue.getErrorReason()).type("text/plain").build();
652             throw new WebApplicationException(response);
653         } catch (DocumentNotFoundException dnfe) {
654             if (logger.isDebugEnabled()) {
655                 logger.debug("caught exception in deleteRolePermission", dnfe);
656             }
657             Response response = Response.status(Response.Status.NOT_FOUND).entity(
658                     ServiceMessages.DELETE_FAILED + "role csid=" + roleCsid).type(
659                     "text/plain").build();
660             throw new WebApplicationException(response);
661         } catch (Exception e) {
662             logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
663             Response response = Response.status(
664                     Response.Status.INTERNAL_SERVER_ERROR).entity(
665                     ServiceMessages.DELETE_FAILED
666                     + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
667             throw new WebApplicationException(response);
668         }
669
670     }
671 }