]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
66bf635672bd2bc4c1da437ad3e601f7572de5d0
[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  *  $LastChangedRevision$
25  */
26 package org.collectionspace.services.collectionobject;
27
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.QueryParam;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriBuilder;
41 import javax.ws.rs.core.UriInfo;
42
43 import java.util.Map;
44 import java.util.HashMap;
45 import java.util.StringTokenizer;
46
47 import org.collectionspace.services.common.query.QueryManager;
48 import org.collectionspace.services.common.query.IQueryManager;
49 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectHandlerFactory;
50 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
51 import org.collectionspace.services.common.context.MultipartServiceContext;
52 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
53 import org.collectionspace.services.common.context.ServiceContext;
54 import org.collectionspace.services.common.document.DocumentNotFoundException;
55 import org.collectionspace.services.common.document.DocumentHandler;
56 import org.collectionspace.services.common.document.DocumentFilter;
57 import org.collectionspace.services.common.security.UnauthorizedException;
58 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
59 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
60 import org.jboss.resteasy.util.HttpResponseCodes;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 @Path("/collectionobjects")
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class CollectionObjectResource
68         extends AbstractCollectionSpaceResource {
69
70         //FIXME: Remove this static string
71         final private String lastChangeRevision = "$LastChangedRevision$";
72     final private String serviceName = "collectionobjects";
73     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
74
75     @Override
76     public String getServiceName() {
77         return serviceName;
78     }
79
80     @Override
81     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
82         DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler(
83                 ctx.getRepositoryClientType().toString());
84         docHandler.setServiceContext(ctx);
85         if (ctx.getInput() != null) {
86             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class);
87             if (obj != null) {
88                 docHandler.setCommonPart((CollectionobjectsCommon) obj);
89             }
90         }
91         return docHandler;
92     }
93
94     @POST
95     public Response createCollectionObject(MultipartInput input) {
96         try {
97             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
98             DocumentHandler handler = createDocumentHandler(ctx);
99             String csid = getRepositoryClient(ctx).create(ctx, handler);
100             UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
101             path.path("" + csid);
102             Response response = Response.created(path.build()).build();
103             return response;
104         } catch (UnauthorizedException ue) {
105             Response response = Response.status(
106                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
107             throw new WebApplicationException(response);
108         } catch (Exception e) {
109             if (logger.isDebugEnabled()) {
110                 logger.debug("Caught exception in createCollectionObject", e);
111             }
112             Response response = Response.status(
113                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
114             throw new WebApplicationException(response);
115         }
116     }
117
118     @GET
119     @Path("{csid}")
120     public MultipartOutput getCollectionObject(
121             @PathParam("csid") String csid) {
122         if (logger.isDebugEnabled()) {
123             logger.debug("getCollectionObject with csid=" + csid);
124         }
125         if (csid == null || "".equals(csid)) {
126             logger.error("getCollectionObject: missing csid!");
127             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
128                     "get failed on CollectionObject csid=" + csid).type(
129                     "text/plain").build();
130             throw new WebApplicationException(response);
131         }
132         MultipartOutput result = null;
133         try {
134             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
135             DocumentHandler handler = createDocumentHandler(ctx);
136             getRepositoryClient(ctx).get(ctx, csid, handler);
137             result = (MultipartOutput) ctx.getOutput();
138         } catch (UnauthorizedException ue) {
139             Response response = Response.status(
140                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
141             throw new WebApplicationException(response);
142         } catch (DocumentNotFoundException dnfe) {
143             if (logger.isDebugEnabled()) {
144                 logger.debug("getCollectionObject", dnfe);
145             }
146             Response response = Response.status(Response.Status.NOT_FOUND).entity(
147                     "Get failed on CollectionObject csid=" + csid).type(
148                     "text/plain").build();
149             throw new WebApplicationException(response);
150         } catch (Exception e) {
151             if (logger.isDebugEnabled()) {
152                 logger.debug("getCollectionObject", 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 CollectionObject CSID:" + csid + ": was not found.").type(
162                     "text/plain").build();
163             throw new WebApplicationException(response);
164         }
165         return result;
166     }
167
168     @GET
169     @Produces("application/xml")
170     public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
171         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
172         try {
173             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
174             DocumentHandler handler = createDocumentHandler(ctx);
175             getRepositoryClient(ctx).getAll(ctx, handler);
176             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
177         } catch (UnauthorizedException ue) {
178             Response response = Response.status(
179                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
180             throw new WebApplicationException(response);
181         } catch (Exception e) {
182             if (logger.isDebugEnabled()) {
183                 logger.debug("Caught exception in getCollectionObjectList", e);
184             }
185             Response response = Response.status(
186                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
187             throw new WebApplicationException(response);
188         }
189         return collectionObjectList;
190     }
191
192     @PUT
193     @Path("{csid}")
194     public MultipartOutput updateCollectionObject(
195             @PathParam("csid") String csid,
196             MultipartInput theUpdate) {
197         if (logger.isDebugEnabled()) {
198             logger.debug("updateCollectionObject with csid=" + csid);
199         }
200         if (csid == null || "".equals(csid)) {
201             logger.error("updateCollectionObject: missing csid!");
202             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
203                     "update failed on CollectionObject csid=" + csid).type(
204                     "text/plain").build();
205             throw new WebApplicationException(response);
206         }
207         MultipartOutput result = null;
208         try {
209             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
210             DocumentHandler handler = createDocumentHandler(ctx);
211             getRepositoryClient(ctx).update(ctx, csid, handler);
212             result = (MultipartOutput) ctx.getOutput();
213         } catch (UnauthorizedException ue) {
214             Response response = Response.status(
215                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
216             throw new WebApplicationException(response);
217         } catch (DocumentNotFoundException dnfe) {
218             if (logger.isDebugEnabled()) {
219                 logger.debug("caugth exception in updateCollectionObject", dnfe);
220             }
221             Response response = Response.status(Response.Status.NOT_FOUND).entity(
222                     "Update failed on CollectionObject csid=" + csid).type(
223                     "text/plain").build();
224             throw new WebApplicationException(response);
225         } catch (Exception e) {
226             Response response = Response.status(
227                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
228             throw new WebApplicationException(response);
229         }
230         return result;
231     }
232
233     @DELETE
234     @Path("{csid}")
235     public Response deleteCollectionObject(@PathParam("csid") String csid) {
236
237         if (logger.isDebugEnabled()) {
238             logger.debug("deleteCollectionObject with csid=" + csid);
239         }
240         if (csid == null || "".equals(csid)) {
241             logger.error("deleteCollectionObject: missing csid!");
242             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
243                     "delete failed on CollectionObject csid=" + csid).type(
244                     "text/plain").build();
245             throw new WebApplicationException(response);
246         }
247         try {
248             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
249             getRepositoryClient(ctx).delete(ctx, csid);
250             return Response.status(HttpResponseCodes.SC_OK).build();
251         } catch (UnauthorizedException ue) {
252             Response response = Response.status(
253                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
254             throw new WebApplicationException(response);
255         } catch (DocumentNotFoundException dnfe) {
256             if (logger.isDebugEnabled()) {
257                 logger.debug("caught exception in deleteCollectionObject", dnfe);
258             }
259             Response response = Response.status(Response.Status.NOT_FOUND).entity(
260                     "Delete failed on CollectionObject csid=" + csid).type(
261                     "text/plain").build();
262             throw new WebApplicationException(response);
263         } catch (Exception e) {
264             Response response = Response.status(
265                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
266             throw new WebApplicationException(response);
267         }
268
269     }
270     
271     @GET
272     @Path("/search")    
273     @Produces("application/xml")
274     public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
275                 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
276         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
277         try {
278             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
279             DocumentHandler handler = createDocumentHandler(ctx);
280
281             // perform a keyword search
282             if (keywords != null && !keywords.isEmpty()) {
283                 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
284                     DocumentFilter documentFilter = handler.getDocumentFilter();
285                     documentFilter.setWhereClause(whereClause);
286                     if (logger.isDebugEnabled()) {
287                         logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
288                     }
289                     getRepositoryClient(ctx).getFiltered(ctx, handler);
290             } else {
291                 getRepositoryClient(ctx).getAll(ctx, handler);
292             }            
293             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
294             
295         } catch (UnauthorizedException ue) {
296             Response response = Response.status(
297                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
298             throw new WebApplicationException(response);
299         } catch (Exception e) {
300             if (logger.isDebugEnabled()) {
301                 logger.debug("Caught exception in getCollectionObjectList", e);
302             }
303             Response response = Response.status(
304                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
305             throw new WebApplicationException(response);
306         }
307         return collectionObjectList;
308     }    
309 }