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