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.collectionspace.services.relation.nuxeo.RelationHandlerFactory;
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 public String getServiceName() {
75 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
76 DocumentHandler docHandler = RelationHandlerFactory.getInstance().getHandler(
77 ctx.getRepositoryClientType().toString());
78 docHandler.setServiceContext(ctx);
79 if (ctx.getInput() != null) {
80 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class);
82 docHandler.setCommonPart((RelationsCommon) obj);
89 public Response createRelation(MultipartInput input) {
92 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
93 DocumentHandler handler = createDocumentHandler(ctx);
94 String csid = getRepositoryClient(ctx).create(ctx, handler);
95 UriBuilder path = UriBuilder.fromResource(NewRelationResource.class);
97 Response response = Response.created(path.build()).build();
99 } catch (UnauthorizedException ue) {
100 Response response = Response.status(
101 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
102 throw new WebApplicationException(response);
103 } catch (Exception e) {
104 if (logger.isDebugEnabled()) {
105 logger.debug("Caught exception in createRelation", e);
107 Response response = Response.status(
108 Response.Status.INTERNAL_SERVER_ERROR).entity(
109 "Create failed").type("text/plain").build();
110 throw new WebApplicationException(response);
116 public MultipartOutput getRelation(@PathParam("csid") String csid) {
117 if (logger.isDebugEnabled()) {
118 logger.debug("getRelation with csid=" + csid);
120 if (csid == null || "".equals(csid)) {
121 logger.error("getRelation: missing csid!");
122 Response response = Response.status(Response.Status.BAD_REQUEST).entity("get failed on Relation csid=" + csid).type(
123 "text/plain").build();
124 throw new WebApplicationException(response);
126 MultipartOutput result = null;
128 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
129 DocumentHandler handler = createDocumentHandler(ctx);
130 getRepositoryClient(ctx).get(ctx, csid, handler);
131 result = (MultipartOutput) ctx.getOutput();
132 } catch (UnauthorizedException ue) {
133 Response response = Response.status(
134 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
135 throw new WebApplicationException(response);
136 } catch (DocumentNotFoundException dnfe) {
137 if (logger.isDebugEnabled()) {
138 logger.debug("getRelation", dnfe);
140 Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed on Relation csid=" + csid).type(
141 "text/plain").build();
142 throw new WebApplicationException(response);
143 } catch (Exception e) {
144 if (logger.isDebugEnabled()) {
145 logger.debug("getRelation", e);
147 Response response = Response.status(
148 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
149 throw new WebApplicationException(response);
152 if (result == null) {
153 Response response = Response.status(Response.Status.NOT_FOUND).entity(
154 "Get failed, the requested Relation CSID:" + csid + ": was not found.").type("text/plain").build();
155 throw new WebApplicationException(response);
164 @Produces("application/xml")
165 public RelationsCommonList getRelationList(@Context UriInfo ui) {
166 return this.getRelationList(null, null, null);
170 @Path("subject/{subjectCsid}")
171 @Produces("application/xml")
172 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
173 @PathParam("subjectCsid") String subjectCsid) {
174 return this.getRelationList(subjectCsid, null, null);
178 @Path("type/{predicate}")
179 @Produces("application/xml")
180 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
181 @PathParam("predicate") String predicate) {
182 return this.getRelationList(null, predicate, null);
186 @Path("object/{objectCsid}")
187 @Produces("application/xml")
188 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
189 @PathParam("objectCsid") String objectCsid) {
190 return this.getRelationList(null, null, objectCsid);
194 @Path("type/{predicate}/subject/{subjectCsid}")
195 @Produces("application/xml")
196 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
197 @PathParam("predicate") String predicate,
198 @PathParam("subjectCsid") String subjectCsid) {
199 return this.getRelationList(subjectCsid, predicate, null);
203 @Path("subject/{subjectCsid}/type/{predicate}")
204 @Produces("application/xml")
205 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
206 @PathParam("subjectCsid") String subjectCsid,
207 @PathParam("predicate") String predicate) {
208 return this.getRelationList(subjectCsid, predicate, null);
212 @Path("type/{predicate}/object/{objectCsid}")
213 @Produces("application/xml")
214 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
215 @PathParam("predicate") String predicate,
216 @PathParam("objectCsid") String objectCsid) {
217 return this.getRelationList(null, predicate, objectCsid);
221 @Path("object/{objectCsid}/type/{predicate}")
222 @Produces("application/xml")
223 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
224 @PathParam("objectCsid") String objectCsid,
225 @PathParam("predicate") String predicate) {
226 return this.getRelationList(null, predicate, objectCsid);
230 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
231 @Produces("application/xml")
232 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
233 @PathParam("predicate") String predicate,
234 @PathParam("subjectCsid") String subjectCsid,
235 @PathParam("objectCsid") String objectCsid) {
236 return this.getRelationList(subjectCsid, predicate, objectCsid);
240 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
241 @Produces("application/xml")
242 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
243 @PathParam("subjectCsid") String subjectCsid,
244 @PathParam("predicate") String predicate,
245 @PathParam("objectCsid") String objectCsid) {
246 return this.getRelationList(subjectCsid, predicate, objectCsid);
254 public MultipartOutput updateRelation(@PathParam("csid") String csid,
255 MultipartInput theUpdate) {
256 if (logger.isDebugEnabled()) {
257 logger.debug("updateRelation with csid=" + csid);
259 if (csid == null || "".equals(csid)) {
260 logger.error("updateRelation: missing csid!");
261 Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on Relation csid=" + csid).type(
262 "text/plain").build();
263 throw new WebApplicationException(response);
265 MultipartOutput result = null;
267 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
268 DocumentHandler handler = createDocumentHandler(ctx);
269 getRepositoryClient(ctx).update(ctx, csid, handler);
270 result = (MultipartOutput) ctx.getOutput();
271 } catch (UnauthorizedException ue) {
272 Response response = Response.status(
273 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
274 throw new WebApplicationException(response);
275 } catch (DocumentNotFoundException dnfe) {
276 if (logger.isDebugEnabled()) {
277 logger.debug("caugth exception in updateRelation", dnfe);
279 Response response = Response.status(Response.Status.NOT_FOUND).entity("Update failed on Relation csid=" + csid).type(
280 "text/plain").build();
281 throw new WebApplicationException(response);
282 } catch (Exception e) {
283 Response response = Response.status(
284 Response.Status.INTERNAL_SERVER_ERROR).entity(
285 "Update failed").type("text/plain").build();
286 throw new WebApplicationException(response);
293 public Response deleteRelation(@PathParam("csid") String csid) {
295 if (logger.isDebugEnabled()) {
296 logger.debug("deleteRelation with csid=" + csid);
298 if (csid == null || "".equals(csid)) {
299 logger.error("deleteRelation: missing csid!");
300 Response response = Response.status(Response.Status.BAD_REQUEST).entity("delete failed on Relation csid=" + csid).type(
301 "text/plain").build();
302 throw new WebApplicationException(response);
305 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
306 getRepositoryClient(ctx).delete(ctx, csid);
307 return Response.status(HttpResponseCodes.SC_OK).build();
308 } catch (UnauthorizedException ue) {
309 Response response = Response.status(
310 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
311 throw new WebApplicationException(response);
312 } catch (DocumentNotFoundException dnfe) {
313 if (logger.isDebugEnabled()) {
314 logger.debug("caught exception in deleteRelation", dnfe);
316 Response response = Response.status(Response.Status.NOT_FOUND).entity("Delete failed on Relation csid=" + csid).type(
317 "text/plain").build();
318 throw new WebApplicationException(response);
319 } catch (Exception e) {
320 Response response = Response.status(
321 Response.Status.INTERNAL_SERVER_ERROR).entity(
322 "Delete failed").type("text/plain").build();
323 throw new WebApplicationException(response);
332 * Gets the relation list request.
334 * @return the relation list request
336 * @throws WebApplicationException the web application exception
338 private RelationsCommonList getRelationList(String subjectCsid,
341 throws WebApplicationException {
342 RelationsCommonList relationList = new RelationsCommonList();
344 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
345 DocumentHandler handler = createDocumentHandler(ctx);
346 Map propsFromPath = handler.getProperties();
347 propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
348 propsFromPath.put(IRelationsManager.PREDICATE, predicate);
349 propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
350 getRepositoryClient(ctx).getAll(ctx, handler);
351 relationList = (RelationsCommonList) handler.getCommonPartList();
352 } catch (UnauthorizedException ue) {
353 Response response = Response.status(
354 Response.Status.UNAUTHORIZED).entity("Get relations failed reason " + ue.getErrorReason()).type("text/plain").build();
355 throw new WebApplicationException(response);
356 } catch (Exception e) {
357 if (logger.isDebugEnabled()) {
358 logger.debug("Caught exception in getRelationList", e);
360 Response response = Response.status(
361 Response.Status.INTERNAL_SERVER_ERROR).entity(
362 "Index failed").type("text/plain").build();
363 throw new WebApplicationException(response);