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