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.RemoteServiceContext;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.document.DocumentNotFoundException;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
49 import org.jboss.resteasy.util.HttpResponseCodes;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 @Path("/collectionobjects")
54 @Consumes("multipart/mixed")
55 @Produces("multipart/mixed")
56 public class CollectionObjectResource
57 extends AbstractCollectionSpaceResource {
59 final private String serviceName = "collectionobjects";
60 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
63 public String getServiceName() {
68 public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
69 DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler(
70 ctx.getRepositoryClientType().toString());
71 docHandler.setServiceContext(ctx);
72 if(ctx.getInput() != null){
73 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class);
75 docHandler.setCommonPart((CollectionobjectsCommon) obj);
82 public Response createCollectionObject(MultipartInput input) {
84 RemoteServiceContext ctx = createServiceContext(input);
85 DocumentHandler handler = createDocumentHandler(ctx);
86 String csid = getRepositoryClient(ctx).create(ctx, handler);
87 UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
89 Response response = Response.created(path.build()).build();
92 if(logger.isDebugEnabled()){
93 logger.debug("Caught exception in createCollectionObject", e);
95 Response response = Response.status(
96 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
97 throw new WebApplicationException(response);
103 public MultipartOutput getCollectionObject(
104 @PathParam("csid") String csid) {
105 if(logger.isDebugEnabled()){
106 logger.debug("getCollectionObject with csid=" + csid);
108 if(csid == null || "".equals(csid)){
109 logger.error("getCollectionObject: missing csid!");
110 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
111 "get failed on CollectionObject csid=" + csid).type(
112 "text/plain").build();
113 throw new WebApplicationException(response);
115 MultipartOutput result = null;
117 RemoteServiceContext ctx = createServiceContext(null);
118 DocumentHandler handler = createDocumentHandler(ctx);
119 getRepositoryClient(ctx).get(ctx, csid, handler);
120 result = ctx.getOutput();
121 }catch(DocumentNotFoundException dnfe){
122 if(logger.isDebugEnabled()){
123 logger.debug("getCollectionObject", dnfe);
125 Response response = Response.status(Response.Status.NOT_FOUND).entity(
126 "Get failed on CollectionObject csid=" + csid).type(
127 "text/plain").build();
128 throw new WebApplicationException(response);
130 if(logger.isDebugEnabled()){
131 logger.debug("getCollectionObject", e);
133 Response response = Response.status(
134 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
135 throw new WebApplicationException(response);
139 Response response = Response.status(Response.Status.NOT_FOUND).entity(
140 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
141 "text/plain").build();
142 throw new WebApplicationException(response);
148 @Produces("application/xml")
149 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
150 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
152 RemoteServiceContext ctx = createServiceContext(null);
153 DocumentHandler handler = createDocumentHandler(ctx);
154 getRepositoryClient(ctx).getAll(ctx, handler);
155 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
157 if(logger.isDebugEnabled()){
158 logger.debug("Caught exception in getCollectionObjectList", e);
160 Response response = Response.status(
161 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
162 throw new WebApplicationException(response);
164 return collectionObjectList;
169 public MultipartOutput updateCollectionObject(
170 @PathParam("csid") String csid,
171 MultipartInput theUpdate) {
172 if(logger.isDebugEnabled()){
173 logger.debug("updateCollectionObject with csid=" + csid);
175 if(csid == null || "".equals(csid)){
176 logger.error("updateCollectionObject: missing csid!");
177 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
178 "update failed on CollectionObject csid=" + csid).type(
179 "text/plain").build();
180 throw new WebApplicationException(response);
182 MultipartOutput result = null;
184 RemoteServiceContext ctx = createServiceContext(theUpdate);
185 DocumentHandler handler = createDocumentHandler(ctx);
186 getRepositoryClient(ctx).update(ctx, csid, handler);
187 result = ctx.getOutput();
188 }catch(DocumentNotFoundException dnfe){
189 if(logger.isDebugEnabled()){
190 logger.debug("caugth exception in updateCollectionObject", dnfe);
192 Response response = Response.status(Response.Status.NOT_FOUND).entity(
193 "Update failed on CollectionObject csid=" + csid).type(
194 "text/plain").build();
195 throw new WebApplicationException(response);
197 Response response = Response.status(
198 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
199 throw new WebApplicationException(response);
206 public Response deleteCollectionObject(@PathParam("csid") String csid) {
208 if(logger.isDebugEnabled()){
209 logger.debug("deleteCollectionObject with csid=" + csid);
211 if(csid == null || "".equals(csid)){
212 logger.error("deleteCollectionObject: missing csid!");
213 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
214 "delete failed on CollectionObject csid=" + csid).type(
215 "text/plain").build();
216 throw new WebApplicationException(response);
219 ServiceContext ctx = createServiceContext(null);
220 getRepositoryClient(ctx).delete(ctx, csid);
221 return Response.status(HttpResponseCodes.SC_OK).build();
222 }catch(DocumentNotFoundException dnfe){
223 if(logger.isDebugEnabled()){
224 logger.debug("caught exception in deleteCollectionObject", dnfe);
226 Response response = Response.status(Response.Status.NOT_FOUND).entity(
227 "Delete failed on CollectionObject csid=" + csid).type(
228 "text/plain").build();
229 throw new WebApplicationException(response);
231 Response response = Response.status(
232 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
233 throw new WebApplicationException(response);