]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b1fd2c79616649c86b4d201319b127f16d54f802
[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.collectionspace.services.common.security.UnauthorizedException;
49 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
50 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
51 import org.jboss.resteasy.util.HttpResponseCodes;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 @Path("/collectionobjects")
56 @Consumes("multipart/mixed")
57 @Produces("multipart/mixed")
58 public class CollectionObjectResource
59         extends AbstractCollectionSpaceResource {
60
61     final private String serviceName = "collectionobjects";
62     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
63
64     @Override
65     public String getServiceName() {
66         return serviceName;
67     }
68
69     @Override
70     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
71         DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler(
72                 ctx.getRepositoryClientType().toString());
73         docHandler.setServiceContext(ctx);
74         if (ctx.getInput() != null) {
75             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class);
76             if (obj != null) {
77                 docHandler.setCommonPart((CollectionobjectsCommon) obj);
78             }
79         }
80         return docHandler;
81     }
82
83     @POST
84     public Response createCollectionObject(MultipartInput input) {
85         try {
86             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
87             DocumentHandler handler = createDocumentHandler(ctx);
88             String csid = getRepositoryClient(ctx).create(ctx, handler);
89             UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
90             path.path("" + csid);
91             Response response = Response.created(path.build()).build();
92             return response;
93         } catch (UnauthorizedException ue) {
94             Response response = Response.status(
95                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
96             throw new WebApplicationException(response);
97         } catch (Exception e) {
98             if (logger.isDebugEnabled()) {
99                 logger.debug("Caught exception in createCollectionObject", e);
100             }
101             Response response = Response.status(
102                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
103             throw new WebApplicationException(response);
104         }
105     }
106
107     @GET
108     @Path("{csid}")
109     public MultipartOutput getCollectionObject(
110             @PathParam("csid") String csid) {
111         if (logger.isDebugEnabled()) {
112             logger.debug("getCollectionObject with csid=" + csid);
113         }
114         if (csid == null || "".equals(csid)) {
115             logger.error("getCollectionObject: missing csid!");
116             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
117                     "get failed on CollectionObject csid=" + csid).type(
118                     "text/plain").build();
119             throw new WebApplicationException(response);
120         }
121         MultipartOutput result = null;
122         try {
123             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
124             DocumentHandler handler = createDocumentHandler(ctx);
125             getRepositoryClient(ctx).get(ctx, csid, handler);
126             result = (MultipartOutput) ctx.getOutput();
127         } catch (UnauthorizedException ue) {
128             Response response = Response.status(
129                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
130             throw new WebApplicationException(response);
131         } catch (DocumentNotFoundException dnfe) {
132             if (logger.isDebugEnabled()) {
133                 logger.debug("getCollectionObject", dnfe);
134             }
135             Response response = Response.status(Response.Status.NOT_FOUND).entity(
136                     "Get failed on CollectionObject csid=" + csid).type(
137                     "text/plain").build();
138             throw new WebApplicationException(response);
139         } catch (Exception e) {
140             if (logger.isDebugEnabled()) {
141                 logger.debug("getCollectionObject", e);
142             }
143             Response response = Response.status(
144                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
145             throw new WebApplicationException(response);
146         }
147
148         if (result == null) {
149             Response response = Response.status(Response.Status.NOT_FOUND).entity(
150                     "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
151                     "text/plain").build();
152             throw new WebApplicationException(response);
153         }
154         return result;
155     }
156
157     @GET
158     @Produces("application/xml")
159     public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
160         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
161         try {
162             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
163             DocumentHandler handler = createDocumentHandler(ctx);
164             getRepositoryClient(ctx).getAll(ctx, handler);
165             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
166         } catch (UnauthorizedException ue) {
167             Response response = Response.status(
168                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
169             throw new WebApplicationException(response);
170         } catch (Exception e) {
171             if (logger.isDebugEnabled()) {
172                 logger.debug("Caught exception in getCollectionObjectList", e);
173             }
174             Response response = Response.status(
175                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
176             throw new WebApplicationException(response);
177         }
178         return collectionObjectList;
179     }
180
181     @PUT
182     @Path("{csid}")
183     public MultipartOutput updateCollectionObject(
184             @PathParam("csid") String csid,
185             MultipartInput theUpdate) {
186         if (logger.isDebugEnabled()) {
187             logger.debug("updateCollectionObject with csid=" + csid);
188         }
189         if (csid == null || "".equals(csid)) {
190             logger.error("updateCollectionObject: missing csid!");
191             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
192                     "update failed on CollectionObject csid=" + csid).type(
193                     "text/plain").build();
194             throw new WebApplicationException(response);
195         }
196         MultipartOutput result = null;
197         try {
198             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
199             DocumentHandler handler = createDocumentHandler(ctx);
200             getRepositoryClient(ctx).update(ctx, csid, handler);
201             result = (MultipartOutput) ctx.getOutput();
202         } catch (UnauthorizedException ue) {
203             Response response = Response.status(
204                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
205             throw new WebApplicationException(response);
206         } catch (DocumentNotFoundException dnfe) {
207             if (logger.isDebugEnabled()) {
208                 logger.debug("caugth exception in updateCollectionObject", dnfe);
209             }
210             Response response = Response.status(Response.Status.NOT_FOUND).entity(
211                     "Update failed on CollectionObject csid=" + csid).type(
212                     "text/plain").build();
213             throw new WebApplicationException(response);
214         } catch (Exception e) {
215             Response response = Response.status(
216                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
217             throw new WebApplicationException(response);
218         }
219         return result;
220     }
221
222     @DELETE
223     @Path("{csid}")
224     public Response deleteCollectionObject(@PathParam("csid") String csid) {
225
226         if (logger.isDebugEnabled()) {
227             logger.debug("deleteCollectionObject with csid=" + csid);
228         }
229         if (csid == null || "".equals(csid)) {
230             logger.error("deleteCollectionObject: missing csid!");
231             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
232                     "delete failed on CollectionObject csid=" + csid).type(
233                     "text/plain").build();
234             throw new WebApplicationException(response);
235         }
236         try {
237             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
238             getRepositoryClient(ctx).delete(ctx, csid);
239             return Response.status(HttpResponseCodes.SC_OK).build();
240         } catch (UnauthorizedException ue) {
241             Response response = Response.status(
242                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
243             throw new WebApplicationException(response);
244         } catch (DocumentNotFoundException dnfe) {
245             if (logger.isDebugEnabled()) {
246                 logger.debug("caught exception in deleteCollectionObject", dnfe);
247             }
248             Response response = Response.status(Response.Status.NOT_FOUND).entity(
249                     "Delete failed on CollectionObject csid=" + csid).type(
250                     "text/plain").build();
251             throw new WebApplicationException(response);
252         } catch (Exception e) {
253             Response response = Response.status(
254                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
255             throw new WebApplicationException(response);
256         }
257
258     }
259 }