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.AbstractCollectionSpaceResourceImpl;
41 import org.collectionspace.services.dimension.DimensionsCommonList.*;
43 import org.collectionspace.services.common.ClientType;
44 import org.collectionspace.services.common.ServiceMain;
45 import org.collectionspace.services.common.context.MultipartServiceContext;
46 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
51 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
52 import org.jboss.resteasy.util.HttpResponseCodes;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 @Consumes("multipart/mixed")
58 @Produces("multipart/mixed")
59 public class DimensionResource extends AbstractCollectionSpaceResourceImpl {
61 private final static String serviceName = "dimensions";
62 final Logger logger = LoggerFactory.getLogger(DimensionResource.class);
63 //FIXME retrieve client type from configuration
64 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
66 public DimensionResource() {
71 protected String getVersionString() {
72 /** The last change revision. */
73 final String lastChangeRevision = "$LastChangedRevision$";
74 return lastChangeRevision;
78 public String getServiceName() {
83 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
84 DocumentHandler docHandler = ctx.getDocumentHandler();
85 if (ctx.getInput() != null) {
86 Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), DimensionsCommon.class);
88 docHandler.setCommonPart((DimensionsCommon) obj);
95 public Response createDimension(MultipartInput input) {
97 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
98 DocumentHandler handler = createDocumentHandler(ctx);
99 String csid = getRepositoryClient(ctx).create(ctx, handler);
100 //dimensionObject.setCsid(csid);
101 UriBuilder path = UriBuilder.fromResource(DimensionResource.class);
102 path.path("" + csid);
103 Response response = Response.created(path.build()).build();
105 } catch (Exception e) {
106 if (logger.isDebugEnabled()) {
107 logger.debug("Caught exception in createDimension", e);
109 Response response = Response.status(
110 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
111 throw new WebApplicationException(response);
117 public MultipartOutput getDimension(
118 @PathParam("csid") String csid) {
119 if (logger.isDebugEnabled()) {
120 logger.debug("getDimension with csid=" + csid);
122 if (csid == null || "".equals(csid)) {
123 logger.error("getDimension: missing csid!");
124 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
125 "get failed on Dimension csid=" + csid).type(
126 "text/plain").build();
127 throw new WebApplicationException(response);
129 MultipartOutput result = null;
131 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
132 DocumentHandler handler = createDocumentHandler(ctx);
133 getRepositoryClient(ctx).get(ctx, csid, handler);
134 result = (MultipartOutput) ctx.getOutput();
135 } catch (DocumentNotFoundException dnfe) {
136 if (logger.isDebugEnabled()) {
137 logger.debug("getDimension", dnfe);
139 Response response = Response.status(Response.Status.NOT_FOUND).entity(
140 "Get failed on Dimension csid=" + csid).type(
141 "text/plain").build();
142 throw new WebApplicationException(response);
143 } catch (Exception e) {
144 if (logger.isDebugEnabled()) {
145 logger.debug("getDimension", e);
147 Response response = Response.status(
148 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
149 throw new WebApplicationException(response);
151 if (result == null) {
152 Response response = Response.status(Response.Status.NOT_FOUND).entity(
153 "Get failed, the requested Dimension CSID:" + csid + ": was not found.").type(
154 "text/plain").build();
155 throw new WebApplicationException(response);
161 @Produces("application/xml")
162 public DimensionsCommonList getDimensionList(@Context UriInfo ui) {
163 DimensionsCommonList dimensionObjectList = new DimensionsCommonList();
165 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
166 DocumentHandler handler = createDocumentHandler(ctx);
167 getRepositoryClient(ctx).getAll(ctx, handler);
168 dimensionObjectList = (DimensionsCommonList) handler.getCommonPartList();
169 } catch (Exception e) {
170 if (logger.isDebugEnabled()) {
171 logger.debug("Caught exception in getDimensionList", e);
173 Response response = Response.status(
174 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
175 throw new WebApplicationException(response);
177 return dimensionObjectList;
182 public MultipartOutput updateDimension(
183 @PathParam("csid") String csid,
184 MultipartInput theUpdate) {
185 if (logger.isDebugEnabled()) {
186 logger.debug("updateDimension with csid=" + csid);
188 if (csid == null || "".equals(csid)) {
189 logger.error("updateDimension: missing csid!");
190 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
191 "update failed on Dimension csid=" + csid).type(
192 "text/plain").build();
193 throw new WebApplicationException(response);
195 MultipartOutput result = null;
197 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
198 DocumentHandler handler = createDocumentHandler(ctx);
199 getRepositoryClient(ctx).update(ctx, csid, handler);
200 result = (MultipartOutput) ctx.getOutput();
201 } catch (DocumentNotFoundException dnfe) {
202 if (logger.isDebugEnabled()) {
203 logger.debug("caugth exception in updateDimension", dnfe);
205 Response response = Response.status(Response.Status.NOT_FOUND).entity(
206 "Update failed on Dimension csid=" + csid).type(
207 "text/plain").build();
208 throw new WebApplicationException(response);
209 } catch (Exception e) {
210 Response response = Response.status(
211 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
212 throw new WebApplicationException(response);
219 public Response deleteDimension(@PathParam("csid") String csid) {
221 if (logger.isDebugEnabled()) {
222 logger.debug("deleteDimension with csid=" + csid);
224 if (csid == null || "".equals(csid)) {
225 logger.error("deleteDimension: missing csid!");
226 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
227 "delete failed on Dimension csid=" + csid).type(
228 "text/plain").build();
229 throw new WebApplicationException(response);
232 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
233 getRepositoryClient(ctx).delete(ctx, csid);
234 return Response.status(HttpResponseCodes.SC_OK).build();
235 } catch (DocumentNotFoundException dnfe) {
236 if (logger.isDebugEnabled()) {
237 logger.debug("caught exception in deleteDimension", dnfe);
239 Response response = Response.status(Response.Status.NOT_FOUND).entity(
240 "Delete failed on Dimension csid=" + csid).type(
241 "text/plain").build();
242 throw new WebApplicationException(response);
243 } catch (Exception e) {
244 Response response = Response.status(
245 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
246 throw new WebApplicationException(response);