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 package org.collectionspace.services.collectionobject;
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;
41 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectHandlerFactory;
42 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
43 import org.collectionspace.services.common.context.MultipartServiceContext;
44 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
47 import org.collectionspace.services.common.document.DocumentHandler;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
49 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
50 import org.jboss.resteasy.util.HttpResponseCodes;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 @Path("/collectionobjects")
55 @Consumes("multipart/mixed")
56 @Produces("multipart/mixed")
57 public class CollectionObjectResource
58 extends AbstractCollectionSpaceResource {
60 final private String serviceName = "collectionobjects";
61 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
64 public String getServiceName() {
69 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
70 DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler(
71 ctx.getRepositoryClientType().toString());
72 docHandler.setServiceContext(ctx);
73 if (ctx.getInput() != null) {
74 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class);
76 docHandler.setCommonPart((CollectionobjectsCommon) obj);
83 public Response createCollectionObject(MultipartInput input) {
85 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
86 DocumentHandler handler = createDocumentHandler(ctx);
87 String csid = getRepositoryClient(ctx).create(ctx, handler);
88 UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
90 Response response = Response.created(path.build()).build();
92 } catch (Exception e) {
93 if (logger.isDebugEnabled()) {
94 logger.debug("Caught exception in createCollectionObject", e);
96 Response response = Response.status(
97 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
98 throw new WebApplicationException(response);
104 public MultipartOutput getCollectionObject(
105 @PathParam("csid") String csid) {
106 if (logger.isDebugEnabled()) {
107 logger.debug("getCollectionObject with csid=" + csid);
109 if (csid == null || "".equals(csid)) {
110 logger.error("getCollectionObject: missing csid!");
111 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
112 "get failed on CollectionObject csid=" + csid).type(
113 "text/plain").build();
114 throw new WebApplicationException(response);
116 MultipartOutput result = null;
118 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
119 DocumentHandler handler = createDocumentHandler(ctx);
120 getRepositoryClient(ctx).get(ctx, csid, handler);
121 result = (MultipartOutput) ctx.getOutput();
122 } catch (DocumentNotFoundException dnfe) {
123 if (logger.isDebugEnabled()) {
124 logger.debug("getCollectionObject", dnfe);
126 Response response = Response.status(Response.Status.NOT_FOUND).entity(
127 "Get failed on CollectionObject csid=" + csid).type(
128 "text/plain").build();
129 throw new WebApplicationException(response);
130 } catch (Exception e) {
131 if (logger.isDebugEnabled()) {
132 logger.debug("getCollectionObject", e);
134 Response response = Response.status(
135 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
136 throw new WebApplicationException(response);
139 if (result == null) {
140 Response response = Response.status(Response.Status.NOT_FOUND).entity(
141 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
142 "text/plain").build();
143 throw new WebApplicationException(response);
149 @Produces("application/xml")
150 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
151 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
153 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
154 DocumentHandler handler = createDocumentHandler(ctx);
155 getRepositoryClient(ctx).getAll(ctx, handler);
156 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
157 } catch (Exception e) {
158 if (logger.isDebugEnabled()) {
159 logger.debug("Caught exception in getCollectionObjectList", e);
161 Response response = Response.status(
162 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
163 throw new WebApplicationException(response);
165 return collectionObjectList;
170 public MultipartOutput updateCollectionObject(
171 @PathParam("csid") String csid,
172 MultipartInput theUpdate) {
173 if (logger.isDebugEnabled()) {
174 logger.debug("updateCollectionObject with csid=" + csid);
176 if (csid == null || "".equals(csid)) {
177 logger.error("updateCollectionObject: missing csid!");
178 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
179 "update failed on CollectionObject csid=" + csid).type(
180 "text/plain").build();
181 throw new WebApplicationException(response);
183 MultipartOutput result = null;
185 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
186 DocumentHandler handler = createDocumentHandler(ctx);
187 getRepositoryClient(ctx).update(ctx, csid, handler);
188 result = (MultipartOutput) ctx.getOutput();
189 } catch (DocumentNotFoundException dnfe) {
190 if (logger.isDebugEnabled()) {
191 logger.debug("caugth exception in updateCollectionObject", dnfe);
193 Response response = Response.status(Response.Status.NOT_FOUND).entity(
194 "Update failed on CollectionObject csid=" + csid).type(
195 "text/plain").build();
196 throw new WebApplicationException(response);
197 } catch (Exception e) {
198 Response response = Response.status(
199 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
200 throw new WebApplicationException(response);
207 public Response deleteCollectionObject(@PathParam("csid") String csid) {
209 if (logger.isDebugEnabled()) {
210 logger.debug("deleteCollectionObject with csid=" + csid);
212 if (csid == null || "".equals(csid)) {
213 logger.error("deleteCollectionObject: missing csid!");
214 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
215 "delete failed on CollectionObject csid=" + csid).type(
216 "text/plain").build();
217 throw new WebApplicationException(response);
220 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
221 getRepositoryClient(ctx).delete(ctx, csid);
222 return Response.status(HttpResponseCodes.SC_OK).build();
223 } catch (DocumentNotFoundException dnfe) {
224 if (logger.isDebugEnabled()) {
225 logger.debug("caught exception in deleteCollectionObject", dnfe);
227 Response response = Response.status(Response.Status.NOT_FOUND).entity(
228 "Delete 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("Delete failed").type("text/plain").build();
234 throw new WebApplicationException(response);