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 final private String serviceName = "collectionobjects";
71 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
74 public String getServiceName() {
79 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
80 DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler(
81 ctx.getRepositoryClientType().toString());
82 docHandler.setServiceContext(ctx);
83 if (ctx.getInput() != null) {
84 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class);
86 docHandler.setCommonPart((CollectionobjectsCommon) obj);
93 public Response createCollectionObject(MultipartInput input) {
95 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
96 DocumentHandler handler = createDocumentHandler(ctx);
97 String csid = getRepositoryClient(ctx).create(ctx, handler);
98 UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
100 Response response = Response.created(path.build()).build();
102 } catch (UnauthorizedException ue) {
103 Response response = Response.status(
104 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
105 throw new WebApplicationException(response);
106 } catch (Exception e) {
107 if (logger.isDebugEnabled()) {
108 logger.debug("Caught exception in createCollectionObject", e);
110 Response response = Response.status(
111 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
112 throw new WebApplicationException(response);
118 public MultipartOutput getCollectionObject(
119 @PathParam("csid") String csid) {
120 if (logger.isDebugEnabled()) {
121 logger.debug("getCollectionObject with csid=" + csid);
123 if (csid == null || "".equals(csid)) {
124 logger.error("getCollectionObject: missing csid!");
125 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
126 "get failed on CollectionObject csid=" + csid).type(
127 "text/plain").build();
128 throw new WebApplicationException(response);
130 MultipartOutput result = null;
132 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
133 DocumentHandler handler = createDocumentHandler(ctx);
134 getRepositoryClient(ctx).get(ctx, csid, handler);
135 result = (MultipartOutput) ctx.getOutput();
136 } catch (UnauthorizedException ue) {
137 Response response = Response.status(
138 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
139 throw new WebApplicationException(response);
140 } catch (DocumentNotFoundException dnfe) {
141 if (logger.isDebugEnabled()) {
142 logger.debug("getCollectionObject", dnfe);
144 Response response = Response.status(Response.Status.NOT_FOUND).entity(
145 "Get failed on CollectionObject csid=" + csid).type(
146 "text/plain").build();
147 throw new WebApplicationException(response);
148 } catch (Exception e) {
149 if (logger.isDebugEnabled()) {
150 logger.debug("getCollectionObject", e);
152 Response response = Response.status(
153 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
154 throw new WebApplicationException(response);
157 if (result == null) {
158 Response response = Response.status(Response.Status.NOT_FOUND).entity(
159 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
160 "text/plain").build();
161 throw new WebApplicationException(response);
167 @Produces("application/xml")
168 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
169 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
171 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
172 DocumentHandler handler = createDocumentHandler(ctx);
173 getRepositoryClient(ctx).getAll(ctx, handler);
174 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
175 } catch (UnauthorizedException ue) {
176 Response response = Response.status(
177 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
178 throw new WebApplicationException(response);
179 } catch (Exception e) {
180 if (logger.isDebugEnabled()) {
181 logger.debug("Caught exception in getCollectionObjectList", e);
183 Response response = Response.status(
184 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
185 throw new WebApplicationException(response);
187 return collectionObjectList;
192 public MultipartOutput updateCollectionObject(
193 @PathParam("csid") String csid,
194 MultipartInput theUpdate) {
195 if (logger.isDebugEnabled()) {
196 logger.debug("updateCollectionObject with csid=" + csid);
198 if (csid == null || "".equals(csid)) {
199 logger.error("updateCollectionObject: missing csid!");
200 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
201 "update failed on CollectionObject csid=" + csid).type(
202 "text/plain").build();
203 throw new WebApplicationException(response);
205 MultipartOutput result = null;
207 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
208 DocumentHandler handler = createDocumentHandler(ctx);
209 getRepositoryClient(ctx).update(ctx, csid, handler);
210 result = (MultipartOutput) ctx.getOutput();
211 } catch (UnauthorizedException ue) {
212 Response response = Response.status(
213 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
214 throw new WebApplicationException(response);
215 } catch (DocumentNotFoundException dnfe) {
216 if (logger.isDebugEnabled()) {
217 logger.debug("caugth exception in updateCollectionObject", dnfe);
219 Response response = Response.status(Response.Status.NOT_FOUND).entity(
220 "Update failed on CollectionObject csid=" + csid).type(
221 "text/plain").build();
222 throw new WebApplicationException(response);
223 } catch (Exception e) {
224 Response response = Response.status(
225 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
226 throw new WebApplicationException(response);
233 public Response deleteCollectionObject(@PathParam("csid") String csid) {
235 if (logger.isDebugEnabled()) {
236 logger.debug("deleteCollectionObject with csid=" + csid);
238 if (csid == null || "".equals(csid)) {
239 logger.error("deleteCollectionObject: missing csid!");
240 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
241 "delete failed on CollectionObject csid=" + csid).type(
242 "text/plain").build();
243 throw new WebApplicationException(response);
246 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
247 getRepositoryClient(ctx).delete(ctx, csid);
248 return Response.status(HttpResponseCodes.SC_OK).build();
249 } catch (UnauthorizedException ue) {
250 Response response = Response.status(
251 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
252 throw new WebApplicationException(response);
253 } catch (DocumentNotFoundException dnfe) {
254 if (logger.isDebugEnabled()) {
255 logger.debug("caught exception in deleteCollectionObject", dnfe);
257 Response response = Response.status(Response.Status.NOT_FOUND).entity(
258 "Delete failed on CollectionObject csid=" + csid).type(
259 "text/plain").build();
260 throw new WebApplicationException(response);
261 } catch (Exception e) {
262 Response response = Response.status(
263 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
264 throw new WebApplicationException(response);
271 @Produces("application/xml")
272 public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
273 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
274 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
276 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
277 DocumentHandler handler = createDocumentHandler(ctx);
279 // perform a keyword search
280 if (keywords != null && !keywords.isEmpty()) {
281 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
282 DocumentFilter documentFilter = handler.getDocumentFilter();
283 documentFilter.setWhereClause(whereClause);
284 if (logger.isDebugEnabled()) {
285 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
287 getRepositoryClient(ctx).getFiltered(ctx, handler);
289 getRepositoryClient(ctx).getAll(ctx, handler);
291 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
293 } catch (UnauthorizedException ue) {
294 Response response = Response.status(
295 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
296 throw new WebApplicationException(response);
297 } catch (Exception e) {
298 if (logger.isDebugEnabled()) {
299 logger.debug("Caught exception in getCollectionObjectList", e);
301 Response response = Response.status(
302 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
303 throw new WebApplicationException(response);
305 return collectionObjectList;