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.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;
41 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
42 //import org.collectionspace.services.common.context.RemoteServiceContextImpl;
43 import org.collectionspace.services.common.ServiceMessages;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.context.ServiceContextFactory;
46 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
47 import org.collectionspace.services.common.document.BadRequestException;
48 import org.collectionspace.services.common.document.DocumentFilter;
49 import org.collectionspace.services.common.document.DocumentNotFoundException;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.security.UnauthorizedException;
52 import org.collectionspace.services.common.storage.StorageClient;
53 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
54 import org.jboss.resteasy.util.HttpResponseCodes;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * The Class RoleResource.
61 @Path("/authorization/roles")
62 @Consumes("application/xml")
63 @Produces("application/xml")
64 public class RoleResource
65 extends AbstractCollectionSpaceResourceImpl {
67 /** The service name. */
68 final private String serviceName = "authorization/roles";
70 final Logger logger = LoggerFactory.getLogger(RoleResource.class);
71 /** The storage client. */
72 final StorageClient storageClient = new JpaStorageClientImpl();
75 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
78 protected String getVersionString() {
79 /** The last change revision. */
80 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
81 return lastChangeRevision;
85 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
88 public String getServiceName() {
93 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
96 public Class<RoleResource> getCommonPartClass() {
97 return RoleResource.class;
101 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
104 public ServiceContextFactory getServiceContextFactory() {
105 return RemoteServiceContextFactory.get();
108 // private <T> ServiceContext createServiceContext(T obj) throws Exception {
109 // ServiceContext ctx = new RemoteServiceContextImpl<T, T>(getServiceName());
110 // ctx.setInput(obj);
111 // ctx.setDocumentType(Role.class.getPackage().getName()); //persistence unit
112 // ctx.setProperty("entity-name", Role.class.getName());
117 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
120 public StorageClient getStorageClient(ServiceContext ctx) {
121 //FIXME use ctx to identify storage client
122 return storageClient;
126 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
127 // DocumentHandler docHandler = ctx.getDocumentHandler();
128 // docHandler.setCommonPart(ctx.getInput());
129 // return docHandler;
134 * @param input the input
136 * @return the response
139 public Response createRole(Role input) {
141 ServiceContext ctx = createServiceContext(input, Role.class);
142 DocumentHandler handler = createDocumentHandler(ctx);
143 String csid = getStorageClient(ctx).create(ctx, handler);
144 UriBuilder path = UriBuilder.fromResource(RoleResource.class);
145 path.path("" + csid);
146 Response response = Response.created(path.build()).build();
148 } catch (BadRequestException bre) {
149 Response response = Response.status(
150 Response.Status.BAD_REQUEST).entity(ServiceMessages.POST_FAILED
151 + bre.getErrorReason()).type("text/plain").build();
152 throw new WebApplicationException(response);
153 } catch (UnauthorizedException ue) {
154 Response response = Response.status(
155 Response.Status.UNAUTHORIZED).entity(ServiceMessages.POST_FAILED
156 + ue.getErrorReason()).type("text/plain").build();
157 throw new WebApplicationException(response);
158 } catch (Exception e) {
159 if (logger.isDebugEnabled()) {
160 logger.debug("Caught exception in createRole", e);
162 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
163 Response response = Response.status(
164 Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.POST_FAILED
165 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
166 throw new WebApplicationException(response);
173 * @param csid the csid
180 @PathParam("csid") String csid) {
181 if (logger.isDebugEnabled()) {
182 logger.debug("getRole with csid=" + csid);
184 if (csid == null || "".equals(csid)) {
185 logger.error("getRole: missing csid!");
186 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
187 ServiceMessages.GET_FAILED + "role csid=").type(
188 "text/plain").build();
189 throw new WebApplicationException(response);
193 ServiceContext ctx = createServiceContext((Role) null, Role.class);
194 DocumentHandler handler = createDocumentHandler(ctx);
195 getStorageClient(ctx).get(ctx, csid, handler);
196 result = (Role) ctx.getOutput();
197 } catch (UnauthorizedException ue) {
198 Response response = Response.status(
199 Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
200 + ue.getErrorReason()).type("text/plain").build();
201 throw new WebApplicationException(response);
202 } catch (DocumentNotFoundException dnfe) {
203 if (logger.isDebugEnabled()) {
204 logger.debug("getRole", dnfe);
206 Response response = Response.status(Response.Status.NOT_FOUND).entity(
207 ServiceMessages.GET_FAILED + "role csid=" + csid).type(
208 "text/plain").build();
209 throw new WebApplicationException(response);
210 } catch (Exception e) {
211 if (logger.isDebugEnabled()) {
212 logger.debug("getRole", e);
214 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
215 Response response = Response.status(
216 Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.GET_FAILED
217 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
218 throw new WebApplicationException(response);
221 if (result == null) {
222 Response response = Response.status(Response.Status.NOT_FOUND).entity(
223 ServiceMessages.GET_FAILED + "role csid=" + csid + ": was not found.").type(
224 "text/plain").build();
225 throw new WebApplicationException(response);
231 * Gets the role list.
235 * @return the role list
238 @Produces("application/xml")
239 public RolesList getRoleList(
240 @Context UriInfo ui) {
241 RolesList roleList = new RolesList();
243 ServiceContext ctx = createServiceContext((Role) null, Role.class);
244 DocumentHandler handler = createDocumentHandler(ctx);
245 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
246 DocumentFilter myFilter = handler.createDocumentFilter();
247 myFilter.setPagination(queryParams);
248 myFilter.setQueryParams(queryParams);
249 handler.setDocumentFilter(myFilter);
250 getStorageClient(ctx).getFiltered(ctx, handler);
251 roleList = (RolesList) handler.getCommonPartList();
252 } catch (UnauthorizedException ue) {
253 Response response = Response.status(
254 Response.Status.UNAUTHORIZED).entity(ServiceMessages.LIST_FAILED
255 + ue.getErrorReason()).type("text/plain").build();
256 throw new WebApplicationException(response);
258 } catch (Exception e) {
259 if (logger.isDebugEnabled()) {
260 logger.debug("Caught exception in getRoleList", e);
262 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
263 Response response = Response.status(
264 Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.LIST_FAILED
265 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
266 throw new WebApplicationException(response);
274 * @param csid the csid
275 * @param theUpdate the the update
281 public Role updateRole(
282 @PathParam("csid") String csid,
284 if (logger.isDebugEnabled()) {
285 logger.debug("updateRole with csid=" + csid);
287 if (csid == null || "".equals(csid)) {
288 logger.error("updateRole: missing csid!");
289 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
290 ServiceMessages.PUT_FAILED + "role "
291 + ServiceMessages.MISSING_INVALID_CSID + csid).type(
292 "text/plain").build();
293 throw new WebApplicationException(response);
297 ServiceContext ctx = createServiceContext(theUpdate, Role.class);
298 DocumentHandler handler = createDocumentHandler(ctx);
299 getStorageClient(ctx).update(ctx, csid, handler);
300 result = (Role) ctx.getOutput();
301 } catch (BadRequestException bre) {
302 Response response = Response.status(
303 Response.Status.BAD_REQUEST).entity(ServiceMessages.PUT_FAILED
304 + bre.getErrorReason()).type("text/plain").build();
305 throw new WebApplicationException(response);
306 } catch (UnauthorizedException ue) {
307 Response response = Response.status(
308 Response.Status.UNAUTHORIZED).entity(ServiceMessages.PUT_FAILED
309 + ue.getErrorReason()).type("text/plain").build();
310 throw new WebApplicationException(response);
311 } catch (DocumentNotFoundException dnfe) {
312 if (logger.isDebugEnabled()) {
313 logger.debug("caugth exception in updateRole", dnfe);
315 Response response = Response.status(Response.Status.NOT_FOUND).entity(
316 ServiceMessages.PUT_FAILED + "role csid=" + csid).type(
317 "text/plain").build();
318 throw new WebApplicationException(response);
319 } catch (Exception e) {
320 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
321 Response response = Response.status(
322 Response.Status.INTERNAL_SERVER_ERROR).entity(
323 ServiceMessages.PUT_FAILED
324 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
325 throw new WebApplicationException(response);
333 * @param csid the csid
335 * @return the response
339 public Response deleteRole(@PathParam("csid") String csid) {
341 if (logger.isDebugEnabled()) {
342 logger.debug("deleteRole with csid=" + csid);
344 if (csid == null || "".equals(csid)) {
345 logger.error("deleteRole: missing csid!");
346 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
347 ServiceMessages.DELETE_FAILED + "role csid=" + csid).type(
348 "text/plain").build();
349 throw new WebApplicationException(response);
352 ServiceContext ctx = createServiceContext((Role) null, Role.class);
353 ((JpaStorageClientImpl) getStorageClient(ctx)).deleteWhere(ctx, csid);
354 return Response.status(HttpResponseCodes.SC_OK).build();
355 } catch (UnauthorizedException ue) {
356 Response response = Response.status(
357 Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED + ue.getErrorReason()).type("text/plain").build();
358 throw new WebApplicationException(response);
360 } catch (DocumentNotFoundException dnfe) {
361 if (logger.isDebugEnabled()) {
362 logger.debug("caught exception in deleteRole", dnfe);
364 Response response = Response.status(Response.Status.NOT_FOUND).entity(
365 ServiceMessages.DELETE_FAILED + "role csid=" + csid).type(
366 "text/plain").build();
367 throw new WebApplicationException(response);
368 } catch (Exception e) {
369 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
370 Response response = Response.status(
371 Response.Status.INTERNAL_SERVER_ERROR).entity(
372 ServiceMessages.DELETE_FAILED + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
373 throw new WebApplicationException(response);
379 @Path("{csid}/permroles")
380 public Response createRolePermission(@PathParam("csid") String roleCsid,
381 PermissionRole input) {
382 if (logger.isDebugEnabled()) {
383 logger.debug("createRolePermission with roleCsid=" + roleCsid);
385 if (roleCsid == null || "".equals(roleCsid)) {
386 logger.error("createRolePermission: missing roleCsid!");
387 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
388 ServiceMessages.POST_FAILED + "permroles role "
389 + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
390 "text/plain").build();
391 throw new WebApplicationException(response);
394 PermissionRoleSubResource subResource =
395 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
396 String permrolecsid = subResource.createPermissionRole(input, SubjectType.PERMISSION);
397 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
398 path.path(roleCsid + "/permroles/" + permrolecsid);
399 Response response = Response.created(path.build()).build();
401 } catch (BadRequestException bre) {
402 Response response = Response.status(
403 Response.Status.BAD_REQUEST).entity("Create failed reason "
404 + bre.getErrorReason()).type("text/plain").build();
405 throw new WebApplicationException(response);
406 } catch (UnauthorizedException ue) {
407 Response response = Response.status(
408 Response.Status.UNAUTHORIZED).entity("Create failed reason "
409 + ue.getErrorReason()).type("text/plain").build();
410 throw new WebApplicationException(response);
411 } catch (Exception e) {
412 if (logger.isDebugEnabled()) {
413 logger.debug("Caught exception in createRolePermission", e);
415 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
416 Response response = Response.status(
417 Response.Status.INTERNAL_SERVER_ERROR).entity(
418 ServiceMessages.POST_FAILED
419 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
420 throw new WebApplicationException(response);
425 @Path("{csid}/permroles/{permrolecsid}")
426 public PermissionRole getRolePermission(
427 @PathParam("csid") String roleCsid,
428 @PathParam("permrolecsid") String permrolecsid) {
429 if (logger.isDebugEnabled()) {
430 logger.debug("getRolePermission with roleCsid=" + roleCsid);
432 if (roleCsid == null || "".equals(roleCsid)) {
433 logger.error("getRolePermission: missing roleCsid!");
434 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
435 ServiceMessages.GET_FAILED + "permroles role "
436 + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
437 "text/plain").build();
438 throw new WebApplicationException(response);
440 PermissionRole result = null;
442 PermissionRoleSubResource subResource =
443 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
444 //get relationships for a role
445 result = subResource.getPermissionRole(roleCsid, SubjectType.PERMISSION);
446 } catch (UnauthorizedException ue) {
447 Response response = Response.status(
448 Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
449 + ue.getErrorReason()).type("text/plain").build();
450 throw new WebApplicationException(response);
451 } catch (DocumentNotFoundException dnfe) {
452 if (logger.isDebugEnabled()) {
453 logger.debug("getRolePermission", dnfe);
455 Response response = Response.status(Response.Status.NOT_FOUND).entity(
456 ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid).type(
457 "text/plain").build();
458 throw new WebApplicationException(response);
459 } catch (Exception e) {
460 if (logger.isDebugEnabled()) {
461 logger.debug("getRolePermission", e);
463 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
464 Response response = Response.status(
465 Response.Status.INTERNAL_SERVER_ERROR).entity(
466 ServiceMessages.GET_FAILED
467 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
468 throw new WebApplicationException(response);
470 if (result == null) {
471 Response response = Response.status(Response.Status.NOT_FOUND).entity(
472 ServiceMessages.GET_FAILED + "permroles role csid=" + roleCsid
473 + ": was not found.").type(
474 "text/plain").build();
475 throw new WebApplicationException(response);
481 @Path("{csid}/permroles/{permrolecsid}")
482 public Response deleteRolePermission(
483 @PathParam("csid") String roleCsid,
484 @PathParam("permrolecsid") String permrolecsid) {
485 if (logger.isDebugEnabled()) {
486 logger.debug("deleteRolePermission with roleCsid=" + roleCsid);
488 if (roleCsid == null || "".equals(roleCsid)) {
489 logger.error("deleteRolePermission: missing roleCsid!");
490 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
491 ServiceMessages.DELETE_FAILED + "permroles role "
492 + ServiceMessages.MISSING_INVALID_CSID + roleCsid).type(
493 "text/plain").build();
494 throw new WebApplicationException(response);
497 PermissionRoleSubResource subResource =
498 new PermissionRoleSubResource(PermissionRoleSubResource.ROLE_PERMROLE_SERVICE);
499 //delete all relationships for a permission
500 subResource.deletePermissionRole(roleCsid, SubjectType.PERMISSION);
501 return Response.status(HttpResponseCodes.SC_OK).build();
502 } catch (UnauthorizedException ue) {
503 Response response = Response.status(
504 Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
505 + ue.getErrorReason()).type("text/plain").build();
506 throw new WebApplicationException(response);
507 } catch (DocumentNotFoundException dnfe) {
508 if (logger.isDebugEnabled()) {
509 logger.debug("caught exception in deleteRolePermission", dnfe);
511 Response response = Response.status(Response.Status.NOT_FOUND).entity(
512 ServiceMessages.DELETE_FAILED + "role csid=" + roleCsid).type(
513 "text/plain").build();
514 throw new WebApplicationException(response);
515 } catch (Exception e) {
516 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
517 Response response = Response.status(
518 Response.Status.INTERNAL_SERVER_ERROR).entity(
519 ServiceMessages.DELETE_FAILED
520 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
521 throw new WebApplicationException(response);