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