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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.authorization;
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;
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;
60 * The Class RoleResource.
62 @Path("/authorization/roles")
63 @Consumes("application/xml")
64 @Produces("application/xml")
65 public class RoleResource
66 extends AbstractCollectionSpaceResourceImpl {
68 /** The service name. */
69 final private String serviceName = "authorization/roles";
71 final Logger logger = LoggerFactory.getLogger(RoleResource.class);
72 /** The storage client. */
73 final StorageClient storageClient = new JpaStorageClientImpl();
76 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
79 protected String getVersionString() {
80 /** The last change revision. */
81 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
82 return lastChangeRevision;
86 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
89 public String getServiceName() {
94 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
97 public Class<RoleResource> getCommonPartClass() {
98 return RoleResource.class;
102 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
105 public ServiceContextFactory getServiceContextFactory() {
106 return RemoteServiceContextFactory.get();
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());
118 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
121 public StorageClient getStorageClient(ServiceContext ctx) {
122 //FIXME use ctx to identify storage client
123 return storageClient;
127 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
128 // DocumentHandler docHandler = ctx.getDocumentHandler();
129 // docHandler.setCommonPart(ctx.getInput());
130 // return docHandler;
135 * @param input the input
137 * @return the response
140 public Response createRole(Role input) {
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();
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);
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);
174 * @param csid the csid
181 @PathParam("csid") String csid) {
182 if (logger.isDebugEnabled()) {
183 logger.debug("getRole with csid=" + csid);
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);
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);
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);
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);
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);
232 * Gets the role list.
236 * @return the role list
239 @Produces("application/xml")
240 public RolesList getRoleList(
241 @Context UriInfo ui) {
242 RolesList roleList = new RolesList();
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);
259 } catch (Exception e) {
260 if (logger.isDebugEnabled()) {
261 logger.debug("Caught exception in getRoleList", e);
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);
275 * @param csid the csid
276 * @param theUpdate the the update
282 public Role updateRole(
283 @PathParam("csid") String csid,
285 if (logger.isDebugEnabled()) {
286 logger.debug("updateRole with csid=" + csid);
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);
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);
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);
334 * @param csid the csid
336 * @return the response
340 public Response deleteRole(@PathParam("csid") String csid) {
342 if (logger.isDebugEnabled()) {
343 logger.debug("deleteRole with csid=" + csid);
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);
353 //FIXME ideally the following two ops should be in the same tx CSPACE-658
354 //delete all relationships for this permission
355 PermissionRoleSubResource subResource =
356 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
357 subResource.deletePermissionRole(csid, SubjectType.PERMISSION);
359 ServiceContext ctx = createServiceContext((Role) null, Role.class);
360 ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
361 return Response.status(HttpResponseCodes.SC_OK).build();
362 } catch (UnauthorizedException ue) {
363 Response response = Response.status(
364 Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED + ue.getErrorReason()).type("text/plain").build();
365 throw new WebApplicationException(response);
367 } catch (DocumentNotFoundException dnfe) {
368 if (logger.isDebugEnabled()) {
369 logger.debug("caught exception in deleteRole", dnfe);
371 Response response = Response.status(Response.Status.NOT_FOUND).entity(
372 ServiceMessages.DELETE_FAILED + "role csid=" + csid).type(
373 "text/plain").build();
374 throw new WebApplicationException(response);
375 } catch (Exception e) {
376 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
377 Response response = Response.status(
378 Response.Status.INTERNAL_SERVER_ERROR).entity(
379 ServiceMessages.DELETE_FAILED + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
380 throw new WebApplicationException(response);
386 @Path("{csid}/permroles")
387 public Response createRolePermission(@QueryParam("_method") String method, @PathParam("csid") String roleCsid,
388 PermissionRole input) {
389 if (method != null) {
390 if ("delete".equalsIgnoreCase(method)) {
391 return deleteRolePermission(roleCsid, input);
394 if (logger.isDebugEnabled()) {
395 logger.debug("createRolePermission with roleCsid=" + roleCsid);
397 if (roleCsid == null || "".equals(roleCsid)) {
398 logger.error("createRolePermission: missing roleCsid!");
399 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
400 ServiceMessages.POST_FAILED + "permroles role "
401 + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
402 "text/plain").build();
403 throw new WebApplicationException(response);
406 PermissionRoleSubResource subResource =
407 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
408 String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
409 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
410 path.path(roleCsid + "/permroles/" + permrolecsid);
411 Response response = Response.created(path.build()).build();
413 } catch (BadRequestException bre) {
414 Response response = Response.status(
415 Response.Status.BAD_REQUEST).entity("Create failed reason "
416 + bre.getErrorReason()).type("text/plain").build();
417 throw new WebApplicationException(response);
418 } catch (UnauthorizedException ue) {
419 Response response = Response.status(
420 Response.Status.UNAUTHORIZED).entity("Create failed reason "
421 + ue.getErrorReason()).type("text/plain").build();
422 throw new WebApplicationException(response);
423 } catch (Exception e) {
424 if (logger.isDebugEnabled()) {
425 logger.debug("Caught exception in createRolePermission", e);
427 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
428 Response response = Response.status(
429 Response.Status.INTERNAL_SERVER_ERROR).entity(
430 ServiceMessages.POST_FAILED
431 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
432 throw new WebApplicationException(response);
437 @Path("{csid}/permroles/{permrolecsid}")
438 public PermissionRole getRolePermission(
439 @PathParam("csid") String roleCsid,
440 @PathParam("permrolecsid") String permrolecsid) {
441 if (logger.isDebugEnabled()) {
442 logger.debug("getRolePermission with roleCsid=" + roleCsid);
444 if (roleCsid == null || "".equals(roleCsid)) {
445 logger.error("getRolePermission: missing roleCsid!");
446 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
447 ServiceMessages.GET_FAILED + "permroles role "
448 + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
449 "text/plain").build();
450 throw new WebApplicationException(response);
452 PermissionRole result = null;
454 PermissionRoleSubResource subResource =
455 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
456 //get relationships for a role
457 result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
458 } catch (UnauthorizedException ue) {
459 Response response = Response.status(
460 Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
461 + ue.getErrorReason()).type("text/plain").build();
462 throw new WebApplicationException(response);
463 } catch (DocumentNotFoundException dnfe) {
464 if (logger.isDebugEnabled()) {
465 logger.debug("getRolePermission", dnfe);
467 Response response = Response.status(Response.Status.NOT_FOUND).entity(
468 ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid).type(
469 "text/plain").build();
470 throw new WebApplicationException(response);
471 } catch (Exception e) {
472 if (logger.isDebugEnabled()) {
473 logger.debug("getRolePermission", e);
475 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
476 Response response = Response.status(
477 Response.Status.INTERNAL_SERVER_ERROR).entity(
478 ServiceMessages.GET_FAILED
479 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
480 throw new WebApplicationException(response);
482 if (result == null) {
483 Response response = Response.status(Response.Status.NOT_FOUND).entity(
484 ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid
485 + ": was not found.").type(
486 "text/plain").build();
487 throw new WebApplicationException(response);
492 public Response deleteRolePermission(
493 @PathParam("csid") String roleCsid,
494 PermissionRole input) {
496 if (logger.isDebugEnabled()) {
497 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
500 if (roleCsid == null || "".equals(roleCsid)) {
501 logger.error("deleteRolePermission: missing roleCsid!");
502 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
503 ServiceMessages.DELETE_FAILED + "permroles role "
504 + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
505 "text/plain").build();
506 throw new WebApplicationException(response);
509 PermissionRoleSubResource subResource =
510 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
511 //delete all relationships for a permission
512 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION, input);
513 return Response.status(HttpResponseCodes.SC_OK).build();
514 } catch (UnauthorizedException ue) {
515 Response response = Response.status(
516 Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
517 + ue.getErrorReason()).type("text/plain").build();
518 throw new WebApplicationException(response);
519 } catch (DocumentNotFoundException dnfe) {
520 if (logger.isDebugEnabled()) {
521 logger.debug("caught exception in deleteRolePermission", dnfe);
523 Response response = Response.status(Response.Status.NOT_FOUND).entity(
524 ServiceMessages.DELETE_FAILED + "role csid=" + roleCsid).type(
525 "text/plain").build();
526 throw new WebApplicationException(response);
527 } catch (Exception e) {
528 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
529 Response response = Response.status(
530 Response.Status.INTERNAL_SERVER_ERROR).entity(
531 ServiceMessages.DELETE_FAILED
532 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
533 throw new WebApplicationException(response);