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.context.ServiceContext;
44 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
45 import org.collectionspace.services.common.context.ServiceContextFactory;
46 import org.collectionspace.services.common.document.BadRequestException;
47 import org.collectionspace.services.common.document.DocumentFilter;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.security.UnauthorizedException;
51 import org.collectionspace.services.common.storage.StorageClient;
52 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
53 import org.jboss.resteasy.util.HttpResponseCodes;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * The Class PermissionResource.
60 @Path("/authorization/permissions")
61 @Consumes("application/xml")
62 @Produces("application/xml")
63 public class PermissionResource
64 extends AbstractCollectionSpaceResourceImpl<Permission, Permission> {
66 /** The service name. */
67 final private String serviceName = "authorization/permissions";
70 final Logger logger = LoggerFactory.getLogger(PermissionResource.class);
72 /** The storage client. */
73 final StorageClient storageClient = new JpaStorageClientImpl(Permission.class);
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;
126 * Creates the permission.
128 * @param input the input
130 * @return the response
133 public Response createPermission(Permission input) {
135 ServiceContext<Permission, Permission> ctx = createServiceContext(input, Permission.class);
136 DocumentHandler handler = createDocumentHandler(ctx);
137 String csid = getStorageClient(ctx).create(ctx, handler);
138 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
139 path.path("" + csid);
140 Response response = Response.created(path.build()).build();
142 } catch (BadRequestException bre) {
143 Response response = Response.status(
144 Response.Status.BAD_REQUEST).entity("Create failed reason "
145 + bre.getErrorReason()).type("text/plain").build();
146 throw new WebApplicationException(response);
147 } catch (UnauthorizedException ue) {
148 Response response = Response.status(
149 Response.Status.UNAUTHORIZED).entity("Create failed reason "
150 + ue.getErrorReason()).type("text/plain").build();
151 throw new WebApplicationException(response);
152 } catch (Exception e) {
153 if (logger.isDebugEnabled()) {
154 logger.debug("Caught exception in createPermission", e);
156 Response response = Response.status(
157 Response.Status.INTERNAL_SERVER_ERROR).entity(
158 "Create failed").type("text/plain").build();
159 throw new WebApplicationException(response);
164 * Gets the permission.
166 * @param csid the csid
168 * @return the permission
172 public Permission getPermission(
173 @PathParam("csid") String csid) {
174 if (logger.isDebugEnabled()) {
175 logger.debug("getPermission with csid=" + csid);
177 if (csid == null || "".equals(csid)) {
178 logger.error("getPermission: missing csid!");
179 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
180 "get failed on Permission csid=" + csid).type(
181 "text/plain").build();
182 throw new WebApplicationException(response);
184 Permission result = null;
186 ServiceContext<Permission, Permission> ctx = createServiceContext();
187 DocumentHandler handler = createDocumentHandler(ctx);
188 getStorageClient(ctx).get(ctx, csid, handler);
189 result = (Permission) ctx.getOutput();
190 } catch (UnauthorizedException ue) {
191 Response response = Response.status(
192 Response.Status.UNAUTHORIZED).entity("Get failed reason "
193 + ue.getErrorReason()).type("text/plain").build();
194 throw new WebApplicationException(response);
195 } catch (DocumentNotFoundException dnfe) {
196 if (logger.isDebugEnabled()) {
197 logger.debug("getPermission", dnfe);
199 Response response = Response.status(Response.Status.NOT_FOUND).entity(
200 "Get failed on Permission csid=" + csid).type(
201 "text/plain").build();
202 throw new WebApplicationException(response);
203 } catch (Exception e) {
204 if (logger.isDebugEnabled()) {
205 logger.debug("getPermission", e);
207 Response response = Response.status(
208 Response.Status.INTERNAL_SERVER_ERROR).entity(
209 "Get failed").type("text/plain").build();
210 throw new WebApplicationException(response);
213 if (result == null) {
214 Response response = Response.status(Response.Status.NOT_FOUND).entity(
215 "Get failed, the requested Permission CSID:" + csid + ": was not found.").type(
216 "text/plain").build();
217 throw new WebApplicationException(response);
223 * Gets the permission list.
227 * @return the permission list
230 @Produces("application/xml")
231 public PermissionsList getPermissionList(
232 @Context UriInfo ui) {
233 PermissionsList permissionList = new PermissionsList();
235 ServiceContext<Permission, Permission> ctx = createServiceContext();
236 DocumentHandler handler = createDocumentHandler(ctx);
237 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
238 DocumentFilter myFilter = handler.createDocumentFilter();
239 myFilter.setPagination(queryParams);
240 myFilter.setQueryParams(queryParams);
241 handler.setDocumentFilter(myFilter);
242 getStorageClient(ctx).getFiltered(ctx, handler);
243 permissionList = (PermissionsList) handler.getCommonPartList();
244 } catch (UnauthorizedException ue) {
245 Response response = Response.status(
246 Response.Status.UNAUTHORIZED).entity("Index failed reason "
247 + ue.getErrorReason()).type("text/plain").build();
248 throw new WebApplicationException(response);
250 } catch (Exception e) {
251 if (logger.isDebugEnabled()) {
252 logger.debug("Caught exception in getPermissionsList", e);
254 Response response = Response.status(
255 Response.Status.INTERNAL_SERVER_ERROR).entity(
256 "Index failed").type("text/plain").build();
257 throw new WebApplicationException(response);
259 return permissionList;
265 * @param csid the csid
266 * @param theUpdate the the update
268 * @return the permission
272 public Permission updatePermission(
273 @PathParam("csid") String csid,
274 Permission theUpdate) {
275 if (logger.isDebugEnabled()) {
276 logger.debug("updatePermission with csid=" + csid);
278 if (csid == null || "".equals(csid)) {
279 logger.error("updatePermission: missing csid!");
280 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
281 "update failed on Permission csid=" + csid).type(
282 "text/plain").build();
283 throw new WebApplicationException(response);
285 Permission result = null;
287 ServiceContext<Permission, Permission> ctx = createServiceContext(theUpdate);
288 DocumentHandler handler = createDocumentHandler(ctx);
289 getStorageClient(ctx).update(ctx, csid, handler);
290 result = (Permission) ctx.getOutput();
291 } catch (BadRequestException bre) {
292 Response response = Response.status(
293 Response.Status.BAD_REQUEST).entity("Update failed reason "
294 + bre.getErrorReason()).type("text/plain").build();
295 throw new WebApplicationException(response);
296 } catch (UnauthorizedException ue) {
297 Response response = Response.status(
298 Response.Status.UNAUTHORIZED).entity("Update failed reason "
299 + ue.getErrorReason()).type("text/plain").build();
300 throw new WebApplicationException(response);
301 } catch (DocumentNotFoundException dnfe) {
302 if (logger.isDebugEnabled()) {
303 logger.debug("caugth exception in updatePermission", dnfe);
305 Response response = Response.status(Response.Status.NOT_FOUND).entity(
306 "Update failed on Permission csid=" + csid).type(
307 "text/plain").build();
308 throw new WebApplicationException(response);
309 } catch (Exception e) {
310 Response response = Response.status(
311 Response.Status.INTERNAL_SERVER_ERROR).entity(
312 "Update failed").type("text/plain").build();
313 throw new WebApplicationException(response);
321 * @param csid the csid
323 * @return the response
327 public Response deletePermission(@PathParam("csid") String csid) {
329 if (logger.isDebugEnabled()) {
330 logger.debug("deletePermission with csid=" + csid);
332 if (csid == null || "".equals(csid)) {
333 logger.error("deletePermission: missing csid!");
334 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
335 "delete failed on Permission csid=" + csid).type(
336 "text/plain").build();
337 throw new WebApplicationException(response);
340 ServiceContext<Permission, Permission> ctx = createServiceContext();
341 getStorageClient(ctx).delete(ctx, csid);
342 return Response.status(HttpResponseCodes.SC_OK).build();
343 } catch (UnauthorizedException ue) {
344 Response response = Response.status(
345 Response.Status.UNAUTHORIZED).entity("Delete failed reason "
346 + ue.getErrorReason()).type("text/plain").build();
347 throw new WebApplicationException(response);
349 } catch (DocumentNotFoundException dnfe) {
350 if (logger.isDebugEnabled()) {
351 logger.debug("caught exception in deletePermission", dnfe);
353 Response response = Response.status(Response.Status.NOT_FOUND).entity(
354 "Delete failed on Permission csid=" + csid).type(
355 "text/plain").build();
356 throw new WebApplicationException(response);
357 } catch (Exception e) {
358 Response response = Response.status(
359 Response.Status.INTERNAL_SERVER_ERROR).entity(
360 "Delete failed").type("text/plain").build();
361 throw new WebApplicationException(response);
367 @Path("{csid}/permroles")
368 public Response createPermissionRole(@PathParam("csid") String permCsid,
369 PermissionRole input) {
370 if (logger.isDebugEnabled()) {
371 logger.debug("createPermissionRole with permCsid=" + permCsid);
373 if (permCsid == null || "".equals(permCsid)) {
374 logger.error("createPermissionRole: missing permCsid!");
375 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
376 "create failed on PermissionRole permCsid=" + permCsid).type(
377 "text/plain").build();
378 throw new WebApplicationException(response);
381 PermissionRoleSubResource subResource = new PermissionRoleSubResource();
382 String permrolecsid = subResource.createPermissionRole(input, SubjectType.ROLE);
383 UriBuilder path = UriBuilder.fromResource(PermissionResource.class);
384 path.path(permCsid + "/permroles/" + permrolecsid);
385 Response response = Response.created(path.build()).build();
387 } catch (BadRequestException bre) {
388 Response response = Response.status(
389 Response.Status.BAD_REQUEST).entity("Create failed reason "
390 + bre.getErrorReason()).type("text/plain").build();
391 throw new WebApplicationException(response);
392 } catch (UnauthorizedException ue) {
393 Response response = Response.status(
394 Response.Status.UNAUTHORIZED).entity("Create failed reason "
395 + ue.getErrorReason()).type("text/plain").build();
396 throw new WebApplicationException(response);
397 } catch (Exception e) {
398 if (logger.isDebugEnabled()) {
399 logger.debug("Caught exception in createPermissionRole", e);
401 Response response = Response.status(
402 Response.Status.INTERNAL_SERVER_ERROR).entity(
403 "Create failed").type("text/plain").build();
404 throw new WebApplicationException(response);
409 @Path("{csid}/permroles/{permrolecsid}")
410 public PermissionRole getPermissionRole(
411 @PathParam("csid") String permCsid,
412 @PathParam("permrolecsid") String permrolecsid) {
413 if (logger.isDebugEnabled()) {
414 logger.debug("getPermissionRole with permCsid=" + permCsid);
416 if (permCsid == null || "".equals(permCsid)) {
417 logger.error("getPermissionRole: missing permCsid!");
418 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
419 "get failed on PermissionRole permCsid=" + permCsid).type(
420 "text/plain").build();
421 throw new WebApplicationException(response);
423 PermissionRole result = null;
425 PermissionRoleSubResource subResource = new PermissionRoleSubResource();
426 //get relationships for a permission
427 result = subResource.getPermissionRole(permCsid, SubjectType.ROLE);
428 } catch (UnauthorizedException ue) {
429 Response response = Response.status(
430 Response.Status.UNAUTHORIZED).entity("Get failed reason "
431 + ue.getErrorReason()).type("text/plain").build();
432 throw new WebApplicationException(response);
433 } catch (DocumentNotFoundException dnfe) {
434 if (logger.isDebugEnabled()) {
435 logger.debug("getPermissionRole", dnfe);
437 Response response = Response.status(Response.Status.NOT_FOUND).entity(
438 "Get failed on PermissionRole permrolecsid=" + permrolecsid).type(
439 "text/plain").build();
440 throw new WebApplicationException(response);
441 } catch (Exception e) {
442 if (logger.isDebugEnabled()) {
443 logger.debug("getPermissionRole", e);
445 Response response = Response.status(
446 Response.Status.INTERNAL_SERVER_ERROR).entity(
447 "Get failed").type("text/plain").build();
448 throw new WebApplicationException(response);
450 if (result == null) {
451 Response response = Response.status(Response.Status.NOT_FOUND).entity(
452 "Get failed, the requested PermissionRole permrolecsid:" + permrolecsid
453 + ": was not found.").type(
454 "text/plain").build();
455 throw new WebApplicationException(response);
461 @Path("{csid}/permroles/{permrolecsid}")
462 public Response deletePermissionRole(
463 @PathParam("csid") String permCsid,
464 @PathParam("permrolecsid") String permrolecsid) {
465 if (logger.isDebugEnabled()) {
466 logger.debug("deletePermissionRole with permCsid=" + permCsid);
468 if (permCsid == null || "".equals(permCsid)) {
469 logger.error("deletePermissionRole: missing permCsid!");
470 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
471 "delete failed on PermissionRole permCsid=" + permCsid).type(
472 "text/plain").build();
473 throw new WebApplicationException(response);
476 PermissionRoleSubResource subResource = new PermissionRoleSubResource();
477 //delete all relationships for a permission
478 subResource.deletePermissionRole(permCsid, SubjectType.ROLE);
479 return Response.status(HttpResponseCodes.SC_OK).build();
480 } catch (UnauthorizedException ue) {
481 Response response = Response.status(
482 Response.Status.UNAUTHORIZED).entity("Delete failed reason "
483 + ue.getErrorReason()).type("text/plain").build();
484 throw new WebApplicationException(response);
485 } catch (DocumentNotFoundException dnfe) {
486 if (logger.isDebugEnabled()) {
487 logger.debug("caught exception in deletePermissionRole", dnfe);
489 Response response = Response.status(Response.Status.NOT_FOUND).entity(
490 "Delete failed on PermissionRole permrolecsid=" + permrolecsid).type(
491 "text/plain").build();
492 throw new WebApplicationException(response);
493 } catch (Exception e) {
494 Response response = Response.status(
495 Response.Status.INTERNAL_SERVER_ERROR).entity(
496 "Delete failed").type("text/plain").build();
497 throw new WebApplicationException(response);