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;
40 import org.collectionspace.services.authorization.storage.AuthorizationDelegate;
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.RemoteServiceContextFactory;
47 import org.collectionspace.services.common.context.ServiceContextFactory;
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 PermissionResource.
62 @Path("/authorization/permissions")
63 @Consumes("application/xml")
64 @Produces("application/xml")
65 public class PermissionResource
66 extends AbstractCollectionSpaceResourceImpl<Permission, Permission> {
68 /** The service name. */
69 final private String serviceName = "authorization/permissions";
71 final Logger logger = LoggerFactory.getLogger(PermissionResource.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<Permission> getCommonPartClass() {
98 return Permission.class;
102 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
105 public ServiceContextFactory<Permission, Permission> getServiceContextFactory() {
106 return RemoteServiceContextFactory.get();
110 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
113 public StorageClient getStorageClient(ServiceContext ctx) {
114 //FIXME use ctx to identify storage client
115 return storageClient;
119 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
120 // DocumentHandler docHandler = ctx.getDocumentHandler();
121 // docHandler.setCommonPart(ctx.getInput());
122 // return docHandler;
125 * Creates the permission.
127 * @param input the input
129 * @return the response
132 public Response createPermission(Permission input) {
134 ServiceContext<Permission, Permission> ctx = createServiceContext(input, Permission.class);
135 DocumentHandler handler = createDocumentHandler(ctx);
136 String csid = getStorageClient(ctx).create(ctx, handler);
137 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
138 path.path("" + csid);
139 Response response = Response.created(path.build()).build();
141 } catch (BadRequestException bre) {
142 Response response = Response.status(
143 Response.Status.BAD_REQUEST).entity(ServiceMessages.POST_FAILED
144 + bre.getErrorReason()).type("text/plain").build();
145 throw new WebApplicationException(response);
146 } catch (UnauthorizedException ue) {
147 Response response = Response.status(
148 Response.Status.UNAUTHORIZED).entity(ServiceMessages.POST_FAILED
149 + ue.getErrorReason()).type("text/plain").build();
150 throw new WebApplicationException(response);
151 } catch (Exception e) {
152 if (logger.isDebugEnabled()) {
153 logger.debug("Caught exception in createPermission", e);
155 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
156 Response response = Response.status(
157 Response.Status.INTERNAL_SERVER_ERROR).entity(
158 ServiceMessages.POST_FAILED
159 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
160 throw new WebApplicationException(response);
165 * Gets the permission.
167 * @param csid the csid
169 * @return the permission
173 public Permission getPermission(
174 @PathParam("csid") String csid) {
175 if (logger.isDebugEnabled()) {
176 logger.debug("getPermission with csid=" + csid);
178 if (csid == null || "".equals(csid)) {
179 logger.error("getPermission: missing csid!");
180 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
181 ServiceMessages.GET_FAILED + "permission "
182 + ServiceMessages.MISSING_INVALID_CSID + csid).type(
183 "text/plain").build();
184 throw new WebApplicationException(response);
186 Permission result = null;
188 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
189 DocumentHandler handler = createDocumentHandler(ctx);
190 getStorageClient(ctx).get(ctx, csid, handler);
191 result = (Permission) ctx.getOutput();
192 } catch (UnauthorizedException ue) {
193 Response response = Response.status(
194 Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
195 + ue.getErrorReason()).type("text/plain").build();
196 throw new WebApplicationException(response);
197 } catch (DocumentNotFoundException dnfe) {
198 if (logger.isDebugEnabled()) {
199 logger.debug("getPermission", dnfe);
201 Response response = Response.status(Response.Status.NOT_FOUND).entity(
202 ServiceMessages.GET_FAILED + "permission csid=" + csid).type(
203 "text/plain").build();
204 throw new WebApplicationException(response);
205 } catch (Exception e) {
206 if (logger.isDebugEnabled()) {
207 logger.debug("getPermission", e);
209 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
210 Response response = Response.status(
211 Response.Status.INTERNAL_SERVER_ERROR).entity(
212 ServiceMessages.GET_FAILED
213 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
214 throw new WebApplicationException(response);
217 if (result == null) {
218 Response response = Response.status(Response.Status.NOT_FOUND).entity(
219 ServiceMessages.GET_FAILED + " permission csid=" + csid + ": was not found.").type(
220 "text/plain").build();
221 throw new WebApplicationException(response);
227 * Gets the permission list.
231 * @return the permission list
234 @Produces("application/xml")
235 public PermissionsList getPermissionList(
236 @Context UriInfo ui) {
237 PermissionsList permissionList = new PermissionsList();
239 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
240 DocumentHandler handler = createDocumentHandler(ctx);
241 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
242 DocumentFilter myFilter = handler.createDocumentFilter();
243 myFilter.setPagination(queryParams);
244 myFilter.setQueryParams(queryParams);
245 handler.setDocumentFilter(myFilter);
246 getStorageClient(ctx).getFiltered(ctx, handler);
247 permissionList = (PermissionsList) handler.getCommonPartList();
248 } catch (UnauthorizedException ue) {
249 Response response = Response.status(
250 Response.Status.UNAUTHORIZED).entity(ServiceMessages.LIST_FAILED
251 + ue.getErrorReason()).type("text/plain").build();
252 throw new WebApplicationException(response);
254 } catch (Exception e) {
255 if (logger.isDebugEnabled()) {
256 logger.debug("Caught exception in getPermissionsList", e);
258 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
259 Response response = Response.status(
260 Response.Status.INTERNAL_SERVER_ERROR).entity(
261 ServiceMessages.LIST_FAILED
262 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
263 throw new WebApplicationException(response);
265 return permissionList;
271 * @param csid the csid
272 * @param theUpdate the the update
274 * @return the permission
278 public Permission updatePermission(
279 @PathParam("csid") String csid,
280 Permission theUpdate) {
281 if (logger.isDebugEnabled()) {
282 logger.debug("updatePermission with csid=" + csid);
284 if (csid == null || "".equals(csid)) {
285 logger.error("updatePermission: missing csid!");
286 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
287 ServiceMessages.PUT_FAILED + "permission "
288 + ServiceMessages.MISSING_INVALID_CSID + csid).type(
289 "text/plain").build();
290 throw new WebApplicationException(response);
292 Permission result = null;
294 ServiceContext<Permission, Permission> ctx = createServiceContext(theUpdate, Permission.class);
295 DocumentHandler handler = createDocumentHandler(ctx);
296 getStorageClient(ctx).update(ctx, csid, handler);
297 result = (Permission) ctx.getOutput();
298 } catch (BadRequestException bre) {
299 Response response = Response.status(
300 Response.Status.BAD_REQUEST).entity(ServiceMessages.PUT_FAILED
301 + bre.getErrorReason()).type("text/plain").build();
302 throw new WebApplicationException(response);
303 } catch (UnauthorizedException ue) {
304 Response response = Response.status(
305 Response.Status.UNAUTHORIZED).entity(ServiceMessages.PUT_FAILED
306 + ue.getErrorReason()).type("text/plain").build();
307 throw new WebApplicationException(response);
308 } catch (DocumentNotFoundException dnfe) {
309 if (logger.isDebugEnabled()) {
310 logger.debug("caugth exception in updatePermission", dnfe);
312 Response response = Response.status(Response.Status.NOT_FOUND).entity(
313 ServiceMessages.PUT_FAILED + "permission csid=" + csid).type(
314 "text/plain").build();
315 throw new WebApplicationException(response);
316 } catch (Exception e) {
317 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
318 Response response = Response.status(
319 Response.Status.INTERNAL_SERVER_ERROR).entity(
320 ServiceMessages.PUT_FAILED
321 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
322 throw new WebApplicationException(response);
330 * @param csid the csid
332 * @return the response
336 public Response deletePermission(@PathParam("csid") String csid) {
338 if (logger.isDebugEnabled()) {
339 logger.debug("deletePermission with csid=" + csid);
341 if (csid == null || "".equals(csid)) {
342 logger.error("deletePermission: missing csid!");
343 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
344 ServiceMessages.DELETE_FAILED + "permission "
345 + ServiceMessages.MISSING_INVALID_CSID + csid).type(
346 "text/plain").build();
347 throw new WebApplicationException(response);
350 //FIXME ideally the following two ops shoudl be in the same tx CSPACE-658
351 //delete all relationships for this permission
352 PermissionRoleSubResource subResource =
353 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
354 subResource.deletePermissionRole(csid, SubjectType.ROLE);
355 //delete permissions at the provider too
356 //at the PermissionRoleSubResource/DocHandler levels, there is no visibility
357 //if permission is deleted
358 AuthorizationDelegate.deletePermissions(csid);
360 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission) null, Permission.class);
361 getStorageClient(ctx).delete(ctx, csid);
362 return Response.status(HttpResponseCodes.SC_OK).build();
363 } catch (UnauthorizedException ue) {
364 Response response = Response.status(
365 Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
366 + ue.getErrorReason()).type("text/plain").build();
367 throw new WebApplicationException(response);
369 } catch (DocumentNotFoundException dnfe) {
370 if (logger.isDebugEnabled()) {
371 logger.debug("caught exception in deletePermission", dnfe);
373 Response response = Response.status(Response.Status.NOT_FOUND).entity(
374 ServiceMessages.DELETE_FAILED + "permission csid=" + csid).type(
375 "text/plain").build();
376 throw new WebApplicationException(response);
377 } catch (Exception e) {
378 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
379 Response response = Response.status(
380 Response.Status.INTERNAL_SERVER_ERROR).entity(
381 ServiceMessages.DELETE_FAILED
382 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
383 throw new WebApplicationException(response);
389 @Path("{csid}/permroles")
390 public Response createPermissionRole(@PathParam("csid") String permCsid,
391 PermissionRole input) {
392 if (logger.isDebugEnabled()) {
393 logger.debug("createPermissionRole with permCsid=" + permCsid);
395 if (permCsid == null || "".equals(permCsid)) {
396 logger.error("createPermissionRole: missing permCsid!");
397 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
398 ServiceMessages.POST_FAILED + "permroles permission "
399 + ServiceMessages.MISSING_INVALID_CSID + permCsid).type(
400 "text/plain").build();
401 throw new WebApplicationException(response);
404 PermissionRoleSubResource subResource =
405 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
406 String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
407 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
408 path.path(permCsid + "/permroles/" + permrolecsid);
409 Response response = Response.created(path.build()).build();
411 } catch (BadRequestException bre) {
412 Response response = Response.status(
413 Response.Status.BAD_REQUEST).entity("Create failed reason "
414 + bre.getErrorReason()).type("text/plain").build();
415 throw new WebApplicationException(response);
416 } catch (UnauthorizedException ue) {
417 Response response = Response.status(
418 Response.Status.UNAUTHORIZED).entity("Create failed reason "
419 + ue.getErrorReason()).type("text/plain").build();
420 throw new WebApplicationException(response);
421 } catch (Exception e) {
422 if (logger.isDebugEnabled()) {
423 logger.debug("Caught exception in createPermissionRole", e);
425 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
426 Response response = Response.status(
427 Response.Status.INTERNAL_SERVER_ERROR).entity(
428 ServiceMessages.POST_FAILED
429 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
430 throw new WebApplicationException(response);
435 @Path("{csid}/permroles/{permrolecsid}")
436 public PermissionRole getPermissionRole(
437 @PathParam("csid") String permCsid,
438 @PathParam("permrolecsid") String permrolecsid) {
439 if (logger.isDebugEnabled()) {
440 logger.debug("getPermissionRole with permCsid=" + permCsid);
442 if (permCsid == null || "".equals(permCsid)) {
443 logger.error("getPermissionRole: missing permCsid!");
444 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
445 ServiceMessages.GET_FAILED + "permroles permission "
446 + ServiceMessages.MISSING_INVALID_CSID + permCsid).type(
447 "text/plain").build();
448 throw new WebApplicationException(response);
450 PermissionRole result = null;
452 PermissionRoleSubResource subResource =
453 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
454 //get relationships for a permission
455 result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
456 } catch (UnauthorizedException ue) {
457 Response response = Response.status(
458 Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
459 + ue.getErrorReason()).type("text/plain").build();
460 throw new WebApplicationException(response);
461 } catch (DocumentNotFoundException dnfe) {
462 if (logger.isDebugEnabled()) {
463 logger.debug("getPermissionRole", dnfe);
465 Response response = Response.status(Response.Status.NOT_FOUND).entity(
466 ServiceMessages.GET_FAILED + "permroles permission csid=" + permCsid).type(
467 "text/plain").build();
468 throw new WebApplicationException(response);
469 } catch (Exception e) {
470 if (logger.isDebugEnabled()) {
471 logger.debug("getPermissionRole", e);
473 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
474 Response response = Response.status(
475 Response.Status.INTERNAL_SERVER_ERROR).entity(
476 ServiceMessages.GET_FAILED
477 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
478 throw new WebApplicationException(response);
480 if (result == null) {
481 Response response = Response.status(Response.Status.NOT_FOUND).entity(
482 ServiceMessages.GET_FAILED + "permroles permisison csid=" + permCsid
483 + ": was not found.").type(
484 "text/plain").build();
485 throw new WebApplicationException(response);
491 @Path("{csid}/permroles/{permrolecsid}")
492 public Response deletePermissionRole(
493 @PathParam("csid") String permCsid,
494 @PathParam("permrolecsid") String permrolecsid) {
495 if (logger.isDebugEnabled()) {
496 logger.debug("deletePermissionRole with permCsid=" + permCsid);
498 if (permCsid == null || "".equals(permCsid)) {
499 logger.error("deletePermissionRole: missing permCsid!");
500 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
501 ServiceMessages.DELETE_FAILED + "permroles permission "
502 + ServiceMessages.MISSING_INVALID_CSID + permCsid).type(
503 "text/plain").build();
504 throw new WebApplicationException(response);
507 PermissionRoleSubResource subResource =
508 new PermissionRoleSubResource(PermissionRoleSubResource.PERMISSION_PERMROLE_SERVICE);
509 //delete all relationships for a permission
510 subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
511 return Response.status(HttpResponseCodes.SC_OK).build();
512 } catch (UnauthorizedException ue) {
513 Response response = Response.status(
514 Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
515 + ue.getErrorReason()).type("text/plain").build();
516 throw new WebApplicationException(response);
517 } catch (DocumentNotFoundException dnfe) {
518 if (logger.isDebugEnabled()) {
519 logger.debug("caught exception in deletePermissionRole", dnfe);
521 Response response = Response.status(Response.Status.NOT_FOUND).entity(
522 ServiceMessages.DELETE_FAILED + "permisison csid=" + permCsid).type(
523 "text/plain").build();
524 throw new WebApplicationException(response);
525 } catch (Exception e) {
526 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
527 Response response = Response.status(
528 Response.Status.INTERNAL_SERVER_ERROR).entity(
529 ServiceMessages.DELETE_FAILED
530 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
531 throw new WebApplicationException(response);