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.dimension;
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;
40 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
41 import org.collectionspace.services.dimension.DimensionsCommonList.*;
43 import org.collectionspace.services.dimension.nuxeo.DimensionHandlerFactory;
44 import org.collectionspace.services.common.ClientType;
45 import org.collectionspace.services.common.ServiceMain;
46 import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentNotFoundException;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
52 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
53 import org.jboss.resteasy.util.HttpResponseCodes;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 @Consumes("multipart/mixed")
59 @Produces("multipart/mixed")
60 public class DimensionResource extends AbstractCollectionSpaceResource {
62 private final static String serviceName = "dimensions";
63 final Logger logger = LoggerFactory.getLogger(DimensionResource.class);
64 //FIXME retrieve client type from configuration
65 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
67 public DimensionResource() {
72 public String getServiceName() {
77 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
78 DocumentHandler docHandler = DimensionHandlerFactory.getInstance().getHandler(
79 ctx.getRepositoryClientType().toString());
80 docHandler.setServiceContext(ctx);
81 if (ctx.getInput() != null) {
82 Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), DimensionsCommon.class);
84 docHandler.setCommonPart((DimensionsCommon) obj);
91 public Response createDimension(MultipartInput input) {
93 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
94 DocumentHandler handler = createDocumentHandler(ctx);
95 String csid = getRepositoryClient(ctx).create(ctx, handler);
96 //dimensionObject.setCsid(csid);
97 UriBuilder path = UriBuilder.fromResource(DimensionResource.class);
99 Response response = Response.created(path.build()).build();
101 } catch (Exception e) {
102 if (logger.isDebugEnabled()) {
103 logger.debug("Caught exception in createDimension", e);
105 Response response = Response.status(
106 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
107 throw new WebApplicationException(response);
113 public MultipartOutput getDimension(
114 @PathParam("csid") String csid) {
115 if (logger.isDebugEnabled()) {
116 logger.debug("getDimension with csid=" + csid);
118 if (csid == null || "".equals(csid)) {
119 logger.error("getDimension: missing csid!");
120 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
121 "get failed on Dimension csid=" + csid).type(
122 "text/plain").build();
123 throw new WebApplicationException(response);
125 MultipartOutput result = null;
127 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
128 DocumentHandler handler = createDocumentHandler(ctx);
129 getRepositoryClient(ctx).get(ctx, csid, handler);
130 result = (MultipartOutput) ctx.getOutput();
131 } catch (DocumentNotFoundException dnfe) {
132 if (logger.isDebugEnabled()) {
133 logger.debug("getDimension", dnfe);
135 Response response = Response.status(Response.Status.NOT_FOUND).entity(
136 "Get failed on Dimension csid=" + csid).type(
137 "text/plain").build();
138 throw new WebApplicationException(response);
139 } catch (Exception e) {
140 if (logger.isDebugEnabled()) {
141 logger.debug("getDimension", e);
143 Response response = Response.status(
144 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
145 throw new WebApplicationException(response);
147 if (result == null) {
148 Response response = Response.status(Response.Status.NOT_FOUND).entity(
149 "Get failed, the requested Dimension CSID:" + csid + ": was not found.").type(
150 "text/plain").build();
151 throw new WebApplicationException(response);
157 @Produces("application/xml")
158 public DimensionsCommonList getDimensionList(@Context UriInfo ui) {
159 DimensionsCommonList dimensionObjectList = new DimensionsCommonList();
161 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
162 DocumentHandler handler = createDocumentHandler(ctx);
163 getRepositoryClient(ctx).getAll(ctx, handler);
164 dimensionObjectList = (DimensionsCommonList) handler.getCommonPartList();
165 } catch (Exception e) {
166 if (logger.isDebugEnabled()) {
167 logger.debug("Caught exception in getDimensionList", e);
169 Response response = Response.status(
170 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
171 throw new WebApplicationException(response);
173 return dimensionObjectList;
178 public MultipartOutput updateDimension(
179 @PathParam("csid") String csid,
180 MultipartInput theUpdate) {
181 if (logger.isDebugEnabled()) {
182 logger.debug("updateDimension with csid=" + csid);
184 if (csid == null || "".equals(csid)) {
185 logger.error("updateDimension: missing csid!");
186 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
187 "update failed on Dimension csid=" + csid).type(
188 "text/plain").build();
189 throw new WebApplicationException(response);
191 MultipartOutput result = null;
193 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
194 DocumentHandler handler = createDocumentHandler(ctx);
195 getRepositoryClient(ctx).update(ctx, csid, handler);
196 result = (MultipartOutput) ctx.getOutput();
197 } catch (DocumentNotFoundException dnfe) {
198 if (logger.isDebugEnabled()) {
199 logger.debug("caugth exception in updateDimension", dnfe);
201 Response response = Response.status(Response.Status.NOT_FOUND).entity(
202 "Update failed on Dimension csid=" + csid).type(
203 "text/plain").build();
204 throw new WebApplicationException(response);
205 } catch (Exception e) {
206 Response response = Response.status(
207 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
208 throw new WebApplicationException(response);
215 public Response deleteDimension(@PathParam("csid") String csid) {
217 if (logger.isDebugEnabled()) {
218 logger.debug("deleteDimension with csid=" + csid);
220 if (csid == null || "".equals(csid)) {
221 logger.error("deleteDimension: missing csid!");
222 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
223 "delete failed on Dimension csid=" + csid).type(
224 "text/plain").build();
225 throw new WebApplicationException(response);
228 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
229 getRepositoryClient(ctx).delete(ctx, csid);
230 return Response.status(HttpResponseCodes.SC_OK).build();
231 } catch (DocumentNotFoundException dnfe) {
232 if (logger.isDebugEnabled()) {
233 logger.debug("caught exception in deleteDimension", dnfe);
235 Response response = Response.status(Response.Status.NOT_FOUND).entity(
236 "Delete failed on Dimension csid=" + csid).type(
237 "text/plain").build();
238 throw new WebApplicationException(response);
239 } catch (Exception e) {
240 Response response = Response.status(
241 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
242 throw new WebApplicationException(response);