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 = new PermissionRoleSubResource();
353 subResource.deletePermissionRole(csid, SubjectType.ROLE);
354 //delete permissions at the provider too
355 //at the PermissionRoleSubResource/DocHandler levels, there is no visibility
356 //if permission is deleted
357 AuthorizationDelegate.deletePermissions(csid);
359 ServiceContext<Permission, Permission> ctx = createServiceContext((Permission)null, Permission.class);
360 getStorageClient(ctx).delete(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
365 + ue.getErrorReason()).type("text/plain").build();
366 throw new WebApplicationException(response);
368 } catch (DocumentNotFoundException dnfe) {
369 if (logger.isDebugEnabled()) {
370 logger.debug("caught exception in deletePermission", dnfe);
372 Response response = Response.status(Response.Status.NOT_FOUND).entity(
373 ServiceMessages.DELETE_FAILED + "permission csid=" + csid).type(
374 "text/plain").build();
375 throw new WebApplicationException(response);
376 } catch (Exception e) {
377 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
378 Response response = Response.status(
379 Response.Status.INTERNAL_SERVER_ERROR).entity(
380 ServiceMessages.DELETE_FAILED
381 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
382 throw new WebApplicationException(response);
388 @Path("{csid}/permroles")
389 public Response createPermissionRole(@PathParam("csid") String permCsid,
390 PermissionRole input) {
391 if (logger.isDebugEnabled()) {
392 logger.debug("createPermissionRole with permCsid=" + permCsid);
394 if (permCsid == null || "".equals(permCsid)) {
395 logger.error("createPermissionRole: missing permCsid!");
396 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
397 ServiceMessages.POST_FAILED + "permroles permission "
398 + ServiceMessages.MISSING_INVALID_CSID + permCsid).type(
399 "text/plain").build();
400 throw new WebApplicationException(response);
403 PermissionRoleSubResource subResource = new PermissionRoleSubResource();
404 String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
405 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
406 path.path(permCsid + "/permroles/" + permrolecsid);
407 Response response = Response.created(path.build()).build();
409 } catch (BadRequestException bre) {
410 Response response = Response.status(
411 Response.Status.BAD_REQUEST).entity("Create failed reason "
412 + bre.getErrorReason()).type("text/plain").build();
413 throw new WebApplicationException(response);
414 } catch (UnauthorizedException ue) {
415 Response response = Response.status(
416 Response.Status.UNAUTHORIZED).entity("Create failed reason "
417 + ue.getErrorReason()).type("text/plain").build();
418 throw new WebApplicationException(response);
419 } catch (Exception e) {
420 if (logger.isDebugEnabled()) {
421 logger.debug("Caught exception in createPermissionRole", e);
423 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
424 Response response = Response.status(
425 Response.Status.INTERNAL_SERVER_ERROR).entity(
426 ServiceMessages.POST_FAILED
427 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
428 throw new WebApplicationException(response);
433 @Path("{csid}/permroles/{permrolecsid}")
434 public PermissionRole getPermissionRole(
435 @PathParam("csid") String permCsid,
436 @PathParam("permrolecsid") String permrolecsid) {
437 if (logger.isDebugEnabled()) {
438 logger.debug("getPermissionRole with permCsid=" + permCsid);
440 if (permCsid == null || "".equals(permCsid)) {
441 logger.error("getPermissionRole: missing permCsid!");
442 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
443 ServiceMessages.GET_FAILED + "permroles permission "
444 + ServiceMessages.MISSING_INVALID_CSID + permCsid).type(
445 "text/plain").build();
446 throw new WebApplicationException(response);
448 PermissionRole result = null;
450 PermissionRoleSubResource subResource = new PermissionRoleSubResource();
451 //get relationships for a permission
452 result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
453 } catch (UnauthorizedException ue) {
454 Response response = Response.status(
455 Response.Status.UNAUTHORIZED).entity(ServiceMessages.GET_FAILED
456 + ue.getErrorReason()).type("text/plain").build();
457 throw new WebApplicationException(response);
458 } catch (DocumentNotFoundException dnfe) {
459 if (logger.isDebugEnabled()) {
460 logger.debug("getPermissionRole", dnfe);
462 Response response = Response.status(Response.Status.NOT_FOUND).entity(
463 ServiceMessages.GET_FAILED + "permroles permission csid=" + permCsid).type(
464 "text/plain").build();
465 throw new WebApplicationException(response);
466 } catch (Exception e) {
467 if (logger.isDebugEnabled()) {
468 logger.debug("getPermissionRole", e);
470 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
471 Response response = Response.status(
472 Response.Status.INTERNAL_SERVER_ERROR).entity(
473 ServiceMessages.GET_FAILED
474 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
475 throw new WebApplicationException(response);
477 if (result == null) {
478 Response response = Response.status(Response.Status.NOT_FOUND).entity(
479 ServiceMessages.GET_FAILED + "permroles permisison csid=" + permCsid
480 + ": was not found.").type(
481 "text/plain").build();
482 throw new WebApplicationException(response);
488 @Path("{csid}/permroles/{permrolecsid}")
489 public Response deletePermissionRole(
490 @PathParam("csid") String permCsid,
491 @PathParam("permrolecsid") String permrolecsid) {
492 if (logger.isDebugEnabled()) {
493 logger.debug("deletePermissionRole with permCsid=" + permCsid);
495 if (permCsid == null || "".equals(permCsid)) {
496 logger.error("deletePermissionRole: missing permCsid!");
497 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
498 ServiceMessages.DELETE_FAILED + "permroles permission "
499 + ServiceMessages.MISSING_INVALID_CSID + permCsid).type(
500 "text/plain").build();
501 throw new WebApplicationException(response);
504 PermissionRoleSubResource subResource = new PermissionRoleSubResource();
505 //delete all relationships for a permission
506 subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
507 return Response.status(HttpResponseCodes.SC_OK).build();
508 } catch (UnauthorizedException ue) {
509 Response response = Response.status(
510 Response.Status.UNAUTHORIZED).entity(ServiceMessages.DELETE_FAILED
511 + ue.getErrorReason()).type("text/plain").build();
512 throw new WebApplicationException(response);
513 } catch (DocumentNotFoundException dnfe) {
514 if (logger.isDebugEnabled()) {
515 logger.debug("caught exception in deletePermissionRole", dnfe);
517 Response response = Response.status(Response.Status.NOT_FOUND).entity(
518 ServiceMessages.DELETE_FAILED + "permisison csid=" + permCsid).type(
519 "text/plain").build();
520 throw new WebApplicationException(response);
521 } catch (Exception e) {
522 logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e);
523 Response response = Response.status(
524 Response.Status.INTERNAL_SERVER_ERROR).entity(
525 ServiceMessages.DELETE_FAILED
526 + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build();
527 throw new WebApplicationException(response);