]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
efc67f77d2fe55cddae8eeedc5a029371db60779
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.collectionobject;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39
40
41 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectHandlerFactory;
42 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
43 import org.collectionspace.services.common.context.MultipartServiceContext;
44 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
47 import org.collectionspace.services.common.document.DocumentHandler;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
49 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
50 import org.jboss.resteasy.util.HttpResponseCodes;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 @Path("/collectionobjects")
55 @Consumes("multipart/mixed")
56 @Produces("multipart/mixed")
57 public class CollectionObjectResource
58         extends AbstractCollectionSpaceResource {
59
60     final private String serviceName = "collectionobjects";
61     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
62
63     @Override
64     public String getServiceName() {
65         return serviceName;
66     }
67
68     @Override
69     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
70         DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler(
71                 ctx.getRepositoryClientType().toString());
72         docHandler.setServiceContext(ctx);
73         if (ctx.getInput() != null) {
74             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class);
75             if (obj != null) {
76                 docHandler.setCommonPart((CollectionobjectsCommon) obj);
77             }
78         }
79         return docHandler;
80     }
81
82     @POST
83     public Response createCollectionObject(MultipartInput input) {
84         try {
85             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
86             DocumentHandler handler = createDocumentHandler(ctx);
87             String csid = getRepositoryClient(ctx).create(ctx, handler);
88             UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
89             path.path("" + csid);
90             Response response = Response.created(path.build()).build();
91             return response;
92         } catch (Exception e) {
93             if (logger.isDebugEnabled()) {
94                 logger.debug("Caught exception in createCollectionObject", e);
95             }
96             Response response = Response.status(
97                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
98             throw new WebApplicationException(response);
99         }
100     }
101
102     @GET
103     @Path("{csid}")
104     public MultipartOutput getCollectionObject(
105             @PathParam("csid") String csid) {
106         if (logger.isDebugEnabled()) {
107             logger.debug("getCollectionObject with csid=" + csid);
108         }
109         if (csid == null || "".equals(csid)) {
110             logger.error("getCollectionObject: missing csid!");
111             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
112                     "get failed on CollectionObject csid=" + csid).type(
113                     "text/plain").build();
114             throw new WebApplicationException(response);
115         }
116         MultipartOutput result = null;
117         try {
118             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
119             DocumentHandler handler = createDocumentHandler(ctx);
120             getRepositoryClient(ctx).get(ctx, csid, handler);
121             result = (MultipartOutput) ctx.getOutput();
122         } catch (DocumentNotFoundException dnfe) {
123             if (logger.isDebugEnabled()) {
124                 logger.debug("getCollectionObject", dnfe);
125             }
126             Response response = Response.status(Response.Status.NOT_FOUND).entity(
127                     "Get failed on CollectionObject csid=" + csid).type(
128                     "text/plain").build();
129             throw new WebApplicationException(response);
130         } catch (Exception e) {
131             if (logger.isDebugEnabled()) {
132                 logger.debug("getCollectionObject", e);
133             }
134             Response response = Response.status(
135                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
136             throw new WebApplicationException(response);
137         }
138
139         if (result == null) {
140             Response response = Response.status(Response.Status.NOT_FOUND).entity(
141                     "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
142                     "text/plain").build();
143             throw new WebApplicationException(response);
144         }
145         return result;
146     }
147
148     @GET
149     @Produces("application/xml")
150     public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
151         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
152         try {
153             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
154             DocumentHandler handler = createDocumentHandler(ctx);
155             getRepositoryClient(ctx).getAll(ctx, handler);
156             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
157         } catch (Exception e) {
158             if (logger.isDebugEnabled()) {
159                 logger.debug("Caught exception in getCollectionObjectList", e);
160             }
161             Response response = Response.status(
162                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
163             throw new WebApplicationException(response);
164         }
165         return collectionObjectList;
166     }
167
168     @PUT
169     @Path("{csid}")
170     public MultipartOutput updateCollectionObject(
171             @PathParam("csid") String csid,
172             MultipartInput theUpdate) {
173         if (logger.isDebugEnabled()) {
174             logger.debug("updateCollectionObject with csid=" + csid);
175         }
176         if (csid == null || "".equals(csid)) {
177             logger.error("updateCollectionObject: missing csid!");
178             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
179                     "update failed on CollectionObject csid=" + csid).type(
180                     "text/plain").build();
181             throw new WebApplicationException(response);
182         }
183         MultipartOutput result = null;
184         try {
185             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
186             DocumentHandler handler = createDocumentHandler(ctx);
187             getRepositoryClient(ctx).update(ctx, csid, handler);
188             result = (MultipartOutput) ctx.getOutput();
189         } catch (DocumentNotFoundException dnfe) {
190             if (logger.isDebugEnabled()) {
191                 logger.debug("caugth exception in updateCollectionObject", dnfe);
192             }
193             Response response = Response.status(Response.Status.NOT_FOUND).entity(
194                     "Update failed on CollectionObject csid=" + csid).type(
195                     "text/plain").build();
196             throw new WebApplicationException(response);
197         } catch (Exception e) {
198             Response response = Response.status(
199                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
200             throw new WebApplicationException(response);
201         }
202         return result;
203     }
204
205     @DELETE
206     @Path("{csid}")
207     public Response deleteCollectionObject(@PathParam("csid") String csid) {
208
209         if (logger.isDebugEnabled()) {
210             logger.debug("deleteCollectionObject with csid=" + csid);
211         }
212         if (csid == null || "".equals(csid)) {
213             logger.error("deleteCollectionObject: missing csid!");
214             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
215                     "delete failed on CollectionObject csid=" + csid).type(
216                     "text/plain").build();
217             throw new WebApplicationException(response);
218         }
219         try {
220             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
221             getRepositoryClient(ctx).delete(ctx, csid);
222             return Response.status(HttpResponseCodes.SC_OK).build();
223         } catch (DocumentNotFoundException dnfe) {
224             if (logger.isDebugEnabled()) {
225                 logger.debug("caught exception in deleteCollectionObject", dnfe);
226             }
227             Response response = Response.status(Response.Status.NOT_FOUND).entity(
228                     "Delete failed on CollectionObject csid=" + csid).type(
229                     "text/plain").build();
230             throw new WebApplicationException(response);
231         } catch (Exception e) {
232             Response response = Response.status(
233                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
234             throw new WebApplicationException(response);
235         }
236
237     }
238 }