]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
436ee70be67bc9bc2bacf32ff52fb997a630b4d7
[tmp/jakarta-migration.git] /
1 /**     
2  * NewRelationResource.java
3  *
4  * {Purpose of This Class}
5  *
6  * {Other Notes Relating to This Class (Optional)}
7  *
8  * $LastChangedBy: $
9  * $LastChangedRevision: $
10  * $LastChangedDate: $
11  *
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:
15  *
16  * http://www.collectionspace.org
17  * http://wiki.collectionspace.org
18  *
19  * Copyright � 2009 {Contributing Institution}
20  *
21  * Licensed under the Educational Community License (ECL), Version 2.0.
22  * You may not use this file except in compliance with this License.
23  *
24  * You may obtain a copy of the ECL 2.0 License at
25  * https://source.collectionspace.org/collection-space/LICENSE.txt
26  */
27 package org.collectionspace.services.relation;
28
29 import java.util.Map;
30
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;
44
45
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;
60
61 @Path("/relations")
62 @Consumes("multipart/mixed")
63 @Produces("multipart/mixed")
64 public class NewRelationResource extends AbstractCollectionSpaceResource {
65
66     public final static String serviceName = "relations";
67     final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
68
69     @Override
70     public String getServiceName() {
71         return serviceName;
72     }
73
74     @Override
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);
81             if (obj != null) {
82                 docHandler.setCommonPart((RelationsCommon) obj);
83             }
84         }
85         return docHandler;
86     }
87
88     @POST
89     public Response createRelation(MultipartInput input) {
90
91         try {
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);
96             path.path("" + csid);
97             Response response = Response.created(path.build()).build();
98             return response;
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);
106             }
107             Response response = Response.status(
108                     Response.Status.INTERNAL_SERVER_ERROR).entity(
109                     "Create failed").type("text/plain").build();
110             throw new WebApplicationException(response);
111         }
112     }
113
114     @GET
115     @Path("{csid}")
116     public MultipartOutput getRelation(@PathParam("csid") String csid) {
117         if (logger.isDebugEnabled()) {
118             logger.debug("getRelation with csid=" + csid);
119         }
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);
125         }
126         MultipartOutput result = null;
127         try {
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);
139             }
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);
146             }
147             Response response = Response.status(
148                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
149             throw new WebApplicationException(response);
150         }
151
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);
156         }
157         return result;
158     }
159
160     /*
161      * BEGIN OF GET LIST
162      */
163     @GET
164     @Produces("application/xml")
165     public RelationsCommonList getRelationList(@Context UriInfo ui) {
166         return this.getRelationList(null, null, null);
167     }
168
169     @GET
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);
175     }
176
177     @GET
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);
183     }
184
185     @GET
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);
191     }
192
193     @GET
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);
200     }
201
202     @GET
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);
209     }
210
211     @GET
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);
218     }
219
220     @GET
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);
227     }
228
229     @GET
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);
237     }
238
239     @GET
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);
247     }
248     /*
249      * END OF GET LIST
250      */
251
252     @PUT
253     @Path("{csid}")
254     public MultipartOutput updateRelation(@PathParam("csid") String csid,
255             MultipartInput theUpdate) {
256         if (logger.isDebugEnabled()) {
257             logger.debug("updateRelation with csid=" + csid);
258         }
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);
264         }
265         MultipartOutput result = null;
266         try {
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);
278             }
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);
287         }
288         return result;
289     }
290
291     @DELETE
292     @Path("{csid}")
293     public Response deleteRelation(@PathParam("csid") String csid) {
294
295         if (logger.isDebugEnabled()) {
296             logger.debug("deleteRelation with csid=" + csid);
297         }
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);
303         }
304         try {
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);
315             }
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);
324         }
325
326     }
327
328     /*
329      * Private Methods
330      */
331     /**
332      * Gets the relation list request.
333      *
334      * @return the relation list request
335      *
336      * @throws WebApplicationException the web application exception
337      */
338     private RelationsCommonList getRelationList(String subjectCsid,
339             String predicate,
340             String objectCsid)
341             throws WebApplicationException {
342         RelationsCommonList relationList = new RelationsCommonList();
343         try {
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);
359             }
360             Response response = Response.status(
361                     Response.Status.INTERNAL_SERVER_ERROR).entity(
362                     "Index failed").type("text/plain").build();
363             throw new WebApplicationException(response);
364         }
365         return relationList;
366     }
367 }