]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c7c2d3909bdc907523a3c91e392da76579747b0a
[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.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;
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     protected String getVersionString() {
71         /** The last change revision. */
72         final String lastChangeRevision = "$LastChangedRevision$";
73         return lastChangeRevision;
74     }
75     
76     @Override
77     public String getServiceName() {
78         return serviceName;
79     }
80
81     @Override
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);
88             if (obj != null) {
89                 docHandler.setCommonPart((RelationsCommon) obj);
90             }
91         }
92         return docHandler;
93     }
94
95     @POST
96     public Response createRelation(MultipartInput input) {
97
98         try {
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();
105             return response;
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);
113             }
114             Response response = Response.status(
115                     Response.Status.INTERNAL_SERVER_ERROR).entity(
116                     "Create failed").type("text/plain").build();
117             throw new WebApplicationException(response);
118         }
119     }
120
121     @GET
122     @Path("{csid}")
123     public MultipartOutput getRelation(@PathParam("csid") String csid) {
124         if (logger.isDebugEnabled()) {
125             logger.debug("getRelation with csid=" + csid);
126         }
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);
132         }
133         MultipartOutput result = null;
134         try {
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);
146             }
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);
153             }
154             Response response = Response.status(
155                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
156             throw new WebApplicationException(response);
157         }
158
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);
163         }
164         return result;
165     }
166
167     /*
168      * BEGIN OF GET LIST
169      */
170     @GET
171     @Produces("application/xml")
172     public RelationsCommonList getRelationList(@Context UriInfo ui) {
173         return this.getRelationList(null, null, null);
174     }
175
176     @GET
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);
182     }
183
184     @GET
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);
190     }
191
192     @GET
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);
198     }
199
200     @GET
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);
207     }
208
209     @GET
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);
216     }
217
218     @GET
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);
225     }
226
227     @GET
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);
234     }
235
236     @GET
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);
244     }
245
246     @GET
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);
254     }
255     /*
256      * END OF GET LIST
257      */
258
259     @PUT
260     @Path("{csid}")
261     public MultipartOutput updateRelation(@PathParam("csid") String csid,
262             MultipartInput theUpdate) {
263         if (logger.isDebugEnabled()) {
264             logger.debug("updateRelation with csid=" + csid);
265         }
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);
271         }
272         MultipartOutput result = null;
273         try {
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);
285             }
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);
294         }
295         return result;
296     }
297
298     @DELETE
299     @Path("{csid}")
300     public Response deleteRelation(@PathParam("csid") String csid) {
301
302         if (logger.isDebugEnabled()) {
303             logger.debug("deleteRelation with csid=" + csid);
304         }
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);
310         }
311         try {
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);
322             }
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);
331         }
332
333     }
334
335     /*
336      * Private Methods
337      */
338     /**
339      * Gets the relation list request.
340      *
341      * @return the relation list request
342      *
343      * @throws WebApplicationException the web application exception
344      */
345     private RelationsCommonList getRelationList(String subjectCsid,
346             String predicate,
347             String objectCsid)
348             throws WebApplicationException {
349         RelationsCommonList relationList = new RelationsCommonList();
350         try {
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);
366             }
367             Response response = Response.status(
368                     Response.Status.INTERNAL_SERVER_ERROR).entity(
369                     "Index failed").type("text/plain").build();
370             throw new WebApplicationException(response);
371         }
372         return relationList;
373     }
374 }