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.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;
42 import java.util.HashMap;
43 import java.util.StringTokenizer;
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;
62 @Path("/collectionobjects")
63 @Consumes("multipart/mixed")
64 @Produces("multipart/mixed")
65 public class CollectionObjectResource
66 extends AbstractCollectionSpaceResource {
68 final private String serviceName = "collectionobjects";
69 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
72 public String getServiceName() {
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);
84 docHandler.setCommonPart((CollectionobjectsCommon) obj);
91 public Response createCollectionObject(MultipartInput input) {
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);
98 Response response = Response.created(path.build()).build();
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);
108 Response response = Response.status(
109 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
110 throw new WebApplicationException(response);
116 public MultipartOutput getCollectionObject(
117 @PathParam("csid") String csid) {
118 if (logger.isDebugEnabled()) {
119 logger.debug("getCollectionObject with csid=" + csid);
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);
128 MultipartOutput result = null;
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);
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);
150 Response response = Response.status(
151 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
152 throw new WebApplicationException(response);
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);
165 @Produces("application/xml")
166 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
167 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
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);
181 Response response = Response.status(
182 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
183 throw new WebApplicationException(response);
185 return collectionObjectList;
190 public MultipartOutput updateCollectionObject(
191 @PathParam("csid") String csid,
192 MultipartInput theUpdate) {
193 if (logger.isDebugEnabled()) {
194 logger.debug("updateCollectionObject with csid=" + csid);
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);
203 MultipartOutput result = null;
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);
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);
231 public Response deleteCollectionObject(@PathParam("csid") String csid) {
233 if (logger.isDebugEnabled()) {
234 logger.debug("deleteCollectionObject with csid=" + csid);
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);
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);
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);
269 @Produces("application/xml")
270 public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
271 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
272 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
274 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
275 DocumentHandler handler = createDocumentHandler(ctx);
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());
285 getRepositoryClient(ctx).getFiltered(ctx, handler);
287 getRepositoryClient(ctx).getAll(ctx, handler);
289 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
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);
299 Response response = Response.status(
300 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
301 throw new WebApplicationException(response);
303 return collectionObjectList;