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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 * $LastChangedRevision$
26 package org.collectionspace.services.collectionobject;
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;
44 import java.util.HashMap;
45 import java.util.StringTokenizer;
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;
64 @Path("/collectionobjects")
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class CollectionObjectResource
68 extends AbstractCollectionSpaceResource {
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);
76 public String getServiceName() {
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);
88 docHandler.setCommonPart((CollectionobjectsCommon) obj);
95 public Response createCollectionObject(MultipartInput input) {
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();
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);
112 Response response = Response.status(
113 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
114 throw new WebApplicationException(response);
120 public MultipartOutput getCollectionObject(
121 @PathParam("csid") String csid) {
122 if (logger.isDebugEnabled()) {
123 logger.debug("getCollectionObject with csid=" + csid);
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);
132 MultipartOutput result = null;
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);
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);
154 Response response = Response.status(
155 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
156 throw new WebApplicationException(response);
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);
169 @Produces("application/xml")
170 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
171 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
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);
185 Response response = Response.status(
186 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
187 throw new WebApplicationException(response);
189 return collectionObjectList;
194 public MultipartOutput updateCollectionObject(
195 @PathParam("csid") String csid,
196 MultipartInput theUpdate) {
197 if (logger.isDebugEnabled()) {
198 logger.debug("updateCollectionObject with csid=" + csid);
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);
207 MultipartOutput result = null;
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);
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);
235 public Response deleteCollectionObject(@PathParam("csid") String csid) {
237 if (logger.isDebugEnabled()) {
238 logger.debug("deleteCollectionObject with csid=" + csid);
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);
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);
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);
273 @Produces("application/xml")
274 public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
275 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
276 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
278 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
279 DocumentHandler handler = createDocumentHandler(ctx);
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());
289 getRepositoryClient(ctx).getFiltered(ctx, handler);
291 getRepositoryClient(ctx).getAll(ctx, handler);
293 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
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);
303 Response response = Response.status(
304 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
305 throw new WebApplicationException(response);
307 return collectionObjectList;