2 * NewRelationResource.java
4 * {Purpose of This Class}
6 * {Other Notes Relating to This Class (Optional)}
9 * $LastChangedRevision$
12 * This document is a part of the source code and related artifacts
13 * for CollectionSpace, an open source collections management system
14 * for museums and related institutions:
16 * http://www.collectionspace.org
17 * http://wiki.collectionspace.org
19 * Copyright � 2009 {Contributing Institution}
21 * Licensed under the Educational Community License (ECL), Version 2.0.
22 * You may not use this file except in compliance with this License.
24 * You may obtain a copy of the ECL 2.0 License at
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
27 package org.collectionspace.services.relation;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.DELETE;
36 import javax.ws.rs.POST;
37 import javax.ws.rs.PUT;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.WebApplicationException;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriBuilder;
43 import javax.ws.rs.core.UriInfo;
46 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
47 import org.collectionspace.services.common.context.MultipartServiceContext;
48 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.relation.IRelationsManager;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentHandlerFactory;
54 import org.collectionspace.services.common.security.UnauthorizedException;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
57 import org.jboss.resteasy.util.HttpResponseCodes;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 @Consumes("multipart/mixed")
63 @Produces("multipart/mixed")
64 public class NewRelationResource extends AbstractCollectionSpaceResource {
66 public final static String serviceName = "relations";
67 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
70 protected String getVersionString() {
71 /** The last change revision. */
72 final String lastChangeRevision = "$LastChangedRevision$";
73 return lastChangeRevision;
77 public String getServiceName() {
82 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
83 DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
84 ctx.getDocumentHandlerClass());
85 docHandler.setServiceContext(ctx);
86 if (ctx.getInput() != null) {
87 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class);
89 docHandler.setCommonPart((RelationsCommon) obj);
96 public Response createRelation(MultipartInput input) {
99 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
100 DocumentHandler handler = createDocumentHandler(ctx);
101 String csid = getRepositoryClient(ctx).create(ctx, handler);
102 UriBuilder path = UriBuilder.fromResource(NewRelationResource.class);
103 path.path("" + csid);
104 Response response = Response.created(path.build()).build();
106 } catch (UnauthorizedException ue) {
107 Response response = Response.status(
108 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
109 throw new WebApplicationException(response);
110 } catch (Exception e) {
111 if (logger.isDebugEnabled()) {
112 logger.debug("Caught exception in createRelation", e);
114 Response response = Response.status(
115 Response.Status.INTERNAL_SERVER_ERROR).entity(
116 "Create failed").type("text/plain").build();
117 throw new WebApplicationException(response);
123 public MultipartOutput getRelation(@PathParam("csid") String csid) {
124 if (logger.isDebugEnabled()) {
125 logger.debug("getRelation with csid=" + csid);
127 if (csid == null || "".equals(csid)) {
128 logger.error("getRelation: missing csid!");
129 Response response = Response.status(Response.Status.BAD_REQUEST).entity("get failed on Relation csid=" + csid).type(
130 "text/plain").build();
131 throw new WebApplicationException(response);
133 MultipartOutput result = null;
135 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
136 DocumentHandler handler = createDocumentHandler(ctx);
137 getRepositoryClient(ctx).get(ctx, csid, handler);
138 result = (MultipartOutput) ctx.getOutput();
139 } catch (UnauthorizedException ue) {
140 Response response = Response.status(
141 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
142 throw new WebApplicationException(response);
143 } catch (DocumentNotFoundException dnfe) {
144 if (logger.isDebugEnabled()) {
145 logger.debug("getRelation", dnfe);
147 Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed on Relation csid=" + csid).type(
148 "text/plain").build();
149 throw new WebApplicationException(response);
150 } catch (Exception e) {
151 if (logger.isDebugEnabled()) {
152 logger.debug("getRelation", e);
154 Response response = Response.status(
155 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
156 throw new WebApplicationException(response);
159 if (result == null) {
160 Response response = Response.status(Response.Status.NOT_FOUND).entity(
161 "Get failed, the requested Relation CSID:" + csid + ": was not found.").type("text/plain").build();
162 throw new WebApplicationException(response);
171 @Produces("application/xml")
172 public RelationsCommonList getRelationList(@Context UriInfo ui) {
173 return this.getRelationList(null, null, null);
177 @Path("subject/{subjectCsid}")
178 @Produces("application/xml")
179 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
180 @PathParam("subjectCsid") String subjectCsid) {
181 return this.getRelationList(subjectCsid, null, null);
185 @Path("type/{predicate}")
186 @Produces("application/xml")
187 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
188 @PathParam("predicate") String predicate) {
189 return this.getRelationList(null, predicate, null);
193 @Path("object/{objectCsid}")
194 @Produces("application/xml")
195 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
196 @PathParam("objectCsid") String objectCsid) {
197 return this.getRelationList(null, null, objectCsid);
201 @Path("type/{predicate}/subject/{subjectCsid}")
202 @Produces("application/xml")
203 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
204 @PathParam("predicate") String predicate,
205 @PathParam("subjectCsid") String subjectCsid) {
206 return this.getRelationList(subjectCsid, predicate, null);
210 @Path("subject/{subjectCsid}/type/{predicate}")
211 @Produces("application/xml")
212 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
213 @PathParam("subjectCsid") String subjectCsid,
214 @PathParam("predicate") String predicate) {
215 return this.getRelationList(subjectCsid, predicate, null);
219 @Path("type/{predicate}/object/{objectCsid}")
220 @Produces("application/xml")
221 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
222 @PathParam("predicate") String predicate,
223 @PathParam("objectCsid") String objectCsid) {
224 return this.getRelationList(null, predicate, objectCsid);
228 @Path("object/{objectCsid}/type/{predicate}")
229 @Produces("application/xml")
230 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
231 @PathParam("objectCsid") String objectCsid,
232 @PathParam("predicate") String predicate) {
233 return this.getRelationList(null, predicate, objectCsid);
237 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
238 @Produces("application/xml")
239 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
240 @PathParam("predicate") String predicate,
241 @PathParam("subjectCsid") String subjectCsid,
242 @PathParam("objectCsid") String objectCsid) {
243 return this.getRelationList(subjectCsid, predicate, objectCsid);
247 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
248 @Produces("application/xml")
249 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
250 @PathParam("subjectCsid") String subjectCsid,
251 @PathParam("predicate") String predicate,
252 @PathParam("objectCsid") String objectCsid) {
253 return this.getRelationList(subjectCsid, predicate, objectCsid);
261 public MultipartOutput updateRelation(@PathParam("csid") String csid,
262 MultipartInput theUpdate) {
263 if (logger.isDebugEnabled()) {
264 logger.debug("updateRelation with csid=" + csid);
266 if (csid == null || "".equals(csid)) {
267 logger.error("updateRelation: missing csid!");
268 Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on Relation csid=" + csid).type(
269 "text/plain").build();
270 throw new WebApplicationException(response);
272 MultipartOutput result = null;
274 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
275 DocumentHandler handler = createDocumentHandler(ctx);
276 getRepositoryClient(ctx).update(ctx, csid, handler);
277 result = (MultipartOutput) ctx.getOutput();
278 } catch (UnauthorizedException ue) {
279 Response response = Response.status(
280 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
281 throw new WebApplicationException(response);
282 } catch (DocumentNotFoundException dnfe) {
283 if (logger.isDebugEnabled()) {
284 logger.debug("caugth exception in updateRelation", dnfe);
286 Response response = Response.status(Response.Status.NOT_FOUND).entity("Update failed on Relation csid=" + csid).type(
287 "text/plain").build();
288 throw new WebApplicationException(response);
289 } catch (Exception e) {
290 Response response = Response.status(
291 Response.Status.INTERNAL_SERVER_ERROR).entity(
292 "Update failed").type("text/plain").build();
293 throw new WebApplicationException(response);
300 public Response deleteRelation(@PathParam("csid") String csid) {
302 if (logger.isDebugEnabled()) {
303 logger.debug("deleteRelation with csid=" + csid);
305 if (csid == null || "".equals(csid)) {
306 logger.error("deleteRelation: missing csid!");
307 Response response = Response.status(Response.Status.BAD_REQUEST).entity("delete failed on Relation csid=" + csid).type(
308 "text/plain").build();
309 throw new WebApplicationException(response);
312 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
313 getRepositoryClient(ctx).delete(ctx, csid);
314 return Response.status(HttpResponseCodes.SC_OK).build();
315 } catch (UnauthorizedException ue) {
316 Response response = Response.status(
317 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
318 throw new WebApplicationException(response);
319 } catch (DocumentNotFoundException dnfe) {
320 if (logger.isDebugEnabled()) {
321 logger.debug("caught exception in deleteRelation", dnfe);
323 Response response = Response.status(Response.Status.NOT_FOUND).entity("Delete failed on Relation csid=" + csid).type(
324 "text/plain").build();
325 throw new WebApplicationException(response);
326 } catch (Exception e) {
327 Response response = Response.status(
328 Response.Status.INTERNAL_SERVER_ERROR).entity(
329 "Delete failed").type("text/plain").build();
330 throw new WebApplicationException(response);
339 * Gets the relation list request.
341 * @return the relation list request
343 * @throws WebApplicationException the web application exception
345 private RelationsCommonList getRelationList(String subjectCsid,
348 throws WebApplicationException {
349 RelationsCommonList relationList = new RelationsCommonList();
351 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
352 DocumentHandler handler = createDocumentHandler(ctx);
353 Map propsFromPath = handler.getProperties();
354 propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
355 propsFromPath.put(IRelationsManager.PREDICATE, predicate);
356 propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
357 getRepositoryClient(ctx).getAll(ctx, handler);
358 relationList = (RelationsCommonList) handler.getCommonPartList();
359 } catch (UnauthorizedException ue) {
360 Response response = Response.status(
361 Response.Status.UNAUTHORIZED).entity("Get relations failed reason " + ue.getErrorReason()).type("text/plain").build();
362 throw new WebApplicationException(response);
363 } catch (Exception e) {
364 if (logger.isDebugEnabled()) {
365 logger.debug("Caught exception in getRelationList", e);
367 Response response = Response.status(
368 Response.Status.INTERNAL_SERVER_ERROR).entity(
369 "Index failed").type("text/plain").build();
370 throw new WebApplicationException(response);