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