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.security.UnauthorizedException;
54 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
56 import org.jboss.resteasy.util.HttpResponseCodes;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 @Consumes("multipart/mixed")
62 @Produces("multipart/mixed")
63 public class NewRelationResource extends AbstractCollectionSpaceResource {
65 public final static String serviceName = "relations";
66 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
69 protected String getVersionString() {
70 /** The last change revision. */
71 final String lastChangeRevision = "$LastChangedRevision$";
72 return lastChangeRevision;
76 public String getServiceName() {
81 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
82 DocumentHandler docHandler = ctx.getDocumentHandler();
83 if (ctx.getInput() != null) {
84 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class);
86 docHandler.setCommonPart((RelationsCommon) obj);
93 public Response createRelation(MultipartInput input) {
96 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
97 DocumentHandler handler = createDocumentHandler(ctx);
98 String csid = getRepositoryClient(ctx).create(ctx, handler);
99 UriBuilder path = UriBuilder.fromResource(NewRelationResource.class);
100 path.path("" + csid);
101 Response response = Response.created(path.build()).build();
103 } catch (UnauthorizedException ue) {
104 Response response = Response.status(
105 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
106 throw new WebApplicationException(response);
107 } catch (Exception e) {
108 if (logger.isDebugEnabled()) {
109 logger.debug("Caught exception in createRelation", e);
111 Response response = Response.status(
112 Response.Status.INTERNAL_SERVER_ERROR).entity(
113 "Create failed").type("text/plain").build();
114 throw new WebApplicationException(response);
120 public MultipartOutput getRelation(@PathParam("csid") String csid) {
121 if (logger.isDebugEnabled()) {
122 logger.debug("getRelation with csid=" + csid);
124 if (csid == null || "".equals(csid)) {
125 logger.error("getRelation: missing csid!");
126 Response response = Response.status(Response.Status.BAD_REQUEST).entity("get failed on Relation csid=" + csid).type(
127 "text/plain").build();
128 throw new WebApplicationException(response);
130 MultipartOutput result = null;
132 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
133 DocumentHandler handler = createDocumentHandler(ctx);
134 getRepositoryClient(ctx).get(ctx, csid, handler);
135 result = (MultipartOutput) ctx.getOutput();
136 } catch (UnauthorizedException ue) {
137 Response response = Response.status(
138 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
139 throw new WebApplicationException(response);
140 } catch (DocumentNotFoundException dnfe) {
141 if (logger.isDebugEnabled()) {
142 logger.debug("getRelation", dnfe);
144 Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed on Relation csid=" + csid).type(
145 "text/plain").build();
146 throw new WebApplicationException(response);
147 } catch (Exception e) {
148 if (logger.isDebugEnabled()) {
149 logger.debug("getRelation", e);
151 Response response = Response.status(
152 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
153 throw new WebApplicationException(response);
156 if (result == null) {
157 Response response = Response.status(Response.Status.NOT_FOUND).entity(
158 "Get failed, the requested Relation CSID:" + csid + ": was not found.").type("text/plain").build();
159 throw new WebApplicationException(response);
168 @Produces("application/xml")
169 public RelationsCommonList getRelationList(@Context UriInfo ui) {
170 return this.getRelationList(null, null, null);
174 @Path("subject/{subjectCsid}")
175 @Produces("application/xml")
176 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
177 @PathParam("subjectCsid") String subjectCsid) {
178 return this.getRelationList(subjectCsid, null, null);
182 @Path("type/{predicate}")
183 @Produces("application/xml")
184 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
185 @PathParam("predicate") String predicate) {
186 return this.getRelationList(null, predicate, null);
190 @Path("object/{objectCsid}")
191 @Produces("application/xml")
192 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
193 @PathParam("objectCsid") String objectCsid) {
194 return this.getRelationList(null, null, objectCsid);
198 @Path("type/{predicate}/subject/{subjectCsid}")
199 @Produces("application/xml")
200 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
201 @PathParam("predicate") String predicate,
202 @PathParam("subjectCsid") String subjectCsid) {
203 return this.getRelationList(subjectCsid, predicate, null);
207 @Path("subject/{subjectCsid}/type/{predicate}")
208 @Produces("application/xml")
209 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
210 @PathParam("subjectCsid") String subjectCsid,
211 @PathParam("predicate") String predicate) {
212 return this.getRelationList(subjectCsid, predicate, null);
216 @Path("type/{predicate}/object/{objectCsid}")
217 @Produces("application/xml")
218 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
219 @PathParam("predicate") String predicate,
220 @PathParam("objectCsid") String objectCsid) {
221 return this.getRelationList(null, predicate, objectCsid);
225 @Path("object/{objectCsid}/type/{predicate}")
226 @Produces("application/xml")
227 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
228 @PathParam("objectCsid") String objectCsid,
229 @PathParam("predicate") String predicate) {
230 return this.getRelationList(null, predicate, objectCsid);
234 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
235 @Produces("application/xml")
236 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
237 @PathParam("predicate") String predicate,
238 @PathParam("subjectCsid") String subjectCsid,
239 @PathParam("objectCsid") String objectCsid) {
240 return this.getRelationList(subjectCsid, predicate, objectCsid);
244 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
245 @Produces("application/xml")
246 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
247 @PathParam("subjectCsid") String subjectCsid,
248 @PathParam("predicate") String predicate,
249 @PathParam("objectCsid") String objectCsid) {
250 return this.getRelationList(subjectCsid, predicate, objectCsid);
258 public MultipartOutput updateRelation(@PathParam("csid") String csid,
259 MultipartInput theUpdate) {
260 if (logger.isDebugEnabled()) {
261 logger.debug("updateRelation with csid=" + csid);
263 if (csid == null || "".equals(csid)) {
264 logger.error("updateRelation: missing csid!");
265 Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on Relation csid=" + csid).type(
266 "text/plain").build();
267 throw new WebApplicationException(response);
269 MultipartOutput result = null;
271 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
272 DocumentHandler handler = createDocumentHandler(ctx);
273 getRepositoryClient(ctx).update(ctx, csid, handler);
274 result = (MultipartOutput) ctx.getOutput();
275 } catch (UnauthorizedException ue) {
276 Response response = Response.status(
277 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
278 throw new WebApplicationException(response);
279 } catch (DocumentNotFoundException dnfe) {
280 if (logger.isDebugEnabled()) {
281 logger.debug("caugth exception in updateRelation", dnfe);
283 Response response = Response.status(Response.Status.NOT_FOUND).entity("Update failed on Relation csid=" + csid).type(
284 "text/plain").build();
285 throw new WebApplicationException(response);
286 } catch (Exception e) {
287 Response response = Response.status(
288 Response.Status.INTERNAL_SERVER_ERROR).entity(
289 "Update failed").type("text/plain").build();
290 throw new WebApplicationException(response);
297 public Response deleteRelation(@PathParam("csid") String csid) {
299 if (logger.isDebugEnabled()) {
300 logger.debug("deleteRelation with csid=" + csid);
302 if (csid == null || "".equals(csid)) {
303 logger.error("deleteRelation: missing csid!");
304 Response response = Response.status(Response.Status.BAD_REQUEST).entity("delete failed on Relation csid=" + csid).type(
305 "text/plain").build();
306 throw new WebApplicationException(response);
309 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
310 getRepositoryClient(ctx).delete(ctx, csid);
311 return Response.status(HttpResponseCodes.SC_OK).build();
312 } catch (UnauthorizedException ue) {
313 Response response = Response.status(
314 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
315 throw new WebApplicationException(response);
316 } catch (DocumentNotFoundException dnfe) {
317 if (logger.isDebugEnabled()) {
318 logger.debug("caught exception in deleteRelation", dnfe);
320 Response response = Response.status(Response.Status.NOT_FOUND).entity("Delete failed on Relation csid=" + csid).type(
321 "text/plain").build();
322 throw new WebApplicationException(response);
323 } catch (Exception e) {
324 Response response = Response.status(
325 Response.Status.INTERNAL_SERVER_ERROR).entity(
326 "Delete failed").type("text/plain").build();
327 throw new WebApplicationException(response);
336 * Gets the relation list request.
338 * @return the relation list request
340 * @throws WebApplicationException the web application exception
342 private RelationsCommonList getRelationList(String subjectCsid,
345 throws WebApplicationException {
346 RelationsCommonList relationList = new RelationsCommonList();
348 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
349 DocumentHandler handler = createDocumentHandler(ctx);
350 Map propsFromPath = handler.getProperties();
351 propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
352 propsFromPath.put(IRelationsManager.PREDICATE, predicate);
353 propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
354 getRepositoryClient(ctx).getAll(ctx, handler);
355 relationList = (RelationsCommonList) handler.getCommonPartList();
356 } catch (UnauthorizedException ue) {
357 Response response = Response.status(
358 Response.Status.UNAUTHORIZED).entity("Get relations failed reason " + ue.getErrorReason()).type("text/plain").build();
359 throw new WebApplicationException(response);
360 } catch (Exception e) {
361 if (logger.isDebugEnabled()) {
362 logger.debug("Caught exception in getRelationList", e);
364 Response response = Response.status(
365 Response.Status.INTERNAL_SERVER_ERROR).entity(
366 "Index failed").type("text/plain").build();
367 throw new WebApplicationException(response);