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.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.collectionspace.services.common.document.DocumentHandlerFactory;
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 protected String getVersionString() {
73 /** The last change revision. */
74 final String lastChangeRevision = "$LastChangedRevision$";
75 return lastChangeRevision;
79 public String getServiceName() {
84 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
85 DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
86 ctx.getDocumentHandlerClass());
87 docHandler.setServiceContext(ctx);
88 if (ctx.getInput() != null) {
89 Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), DimensionsCommon.class);
91 docHandler.setCommonPart((DimensionsCommon) obj);
98 public Response createDimension(MultipartInput input) {
100 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
101 DocumentHandler handler = createDocumentHandler(ctx);
102 String csid = getRepositoryClient(ctx).create(ctx, handler);
103 //dimensionObject.setCsid(csid);
104 UriBuilder path = UriBuilder.fromResource(DimensionResource.class);
105 path.path("" + csid);
106 Response response = Response.created(path.build()).build();
108 } catch (Exception e) {
109 if (logger.isDebugEnabled()) {
110 logger.debug("Caught exception in createDimension", e);
112 Response response = Response.status(
113 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
114 throw new WebApplicationException(response);
120 public MultipartOutput getDimension(
121 @PathParam("csid") String csid) {
122 if (logger.isDebugEnabled()) {
123 logger.debug("getDimension with csid=" + csid);
125 if (csid == null || "".equals(csid)) {
126 logger.error("getDimension: missing csid!");
127 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
128 "get failed on Dimension csid=" + csid).type(
129 "text/plain").build();
130 throw new WebApplicationException(response);
132 MultipartOutput result = null;
134 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
135 DocumentHandler handler = createDocumentHandler(ctx);
136 getRepositoryClient(ctx).get(ctx, csid, handler);
137 result = (MultipartOutput) ctx.getOutput();
138 } catch (DocumentNotFoundException dnfe) {
139 if (logger.isDebugEnabled()) {
140 logger.debug("getDimension", dnfe);
142 Response response = Response.status(Response.Status.NOT_FOUND).entity(
143 "Get failed on Dimension csid=" + csid).type(
144 "text/plain").build();
145 throw new WebApplicationException(response);
146 } catch (Exception e) {
147 if (logger.isDebugEnabled()) {
148 logger.debug("getDimension", e);
150 Response response = Response.status(
151 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
152 throw new WebApplicationException(response);
154 if (result == null) {
155 Response response = Response.status(Response.Status.NOT_FOUND).entity(
156 "Get failed, the requested Dimension CSID:" + csid + ": was not found.").type(
157 "text/plain").build();
158 throw new WebApplicationException(response);
164 @Produces("application/xml")
165 public DimensionsCommonList getDimensionList(@Context UriInfo ui) {
166 DimensionsCommonList dimensionObjectList = new DimensionsCommonList();
168 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
169 DocumentHandler handler = createDocumentHandler(ctx);
170 getRepositoryClient(ctx).getAll(ctx, handler);
171 dimensionObjectList = (DimensionsCommonList) handler.getCommonPartList();
172 } catch (Exception e) {
173 if (logger.isDebugEnabled()) {
174 logger.debug("Caught exception in getDimensionList", e);
176 Response response = Response.status(
177 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
178 throw new WebApplicationException(response);
180 return dimensionObjectList;
185 public MultipartOutput updateDimension(
186 @PathParam("csid") String csid,
187 MultipartInput theUpdate) {
188 if (logger.isDebugEnabled()) {
189 logger.debug("updateDimension with csid=" + csid);
191 if (csid == null || "".equals(csid)) {
192 logger.error("updateDimension: missing csid!");
193 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
194 "update failed on Dimension csid=" + csid).type(
195 "text/plain").build();
196 throw new WebApplicationException(response);
198 MultipartOutput result = null;
200 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
201 DocumentHandler handler = createDocumentHandler(ctx);
202 getRepositoryClient(ctx).update(ctx, csid, handler);
203 result = (MultipartOutput) ctx.getOutput();
204 } catch (DocumentNotFoundException dnfe) {
205 if (logger.isDebugEnabled()) {
206 logger.debug("caugth exception in updateDimension", dnfe);
208 Response response = Response.status(Response.Status.NOT_FOUND).entity(
209 "Update failed on Dimension csid=" + csid).type(
210 "text/plain").build();
211 throw new WebApplicationException(response);
212 } catch (Exception e) {
213 Response response = Response.status(
214 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
215 throw new WebApplicationException(response);
222 public Response deleteDimension(@PathParam("csid") String csid) {
224 if (logger.isDebugEnabled()) {
225 logger.debug("deleteDimension with csid=" + csid);
227 if (csid == null || "".equals(csid)) {
228 logger.error("deleteDimension: missing csid!");
229 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
230 "delete failed on Dimension csid=" + csid).type(
231 "text/plain").build();
232 throw new WebApplicationException(response);
235 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
236 getRepositoryClient(ctx).delete(ctx, csid);
237 return Response.status(HttpResponseCodes.SC_OK).build();
238 } catch (DocumentNotFoundException dnfe) {
239 if (logger.isDebugEnabled()) {
240 logger.debug("caught exception in deleteDimension", dnfe);
242 Response response = Response.status(Response.Status.NOT_FOUND).entity(
243 "Delete failed on Dimension csid=" + csid).type(
244 "text/plain").build();
245 throw new WebApplicationException(response);
246 } catch (Exception e) {
247 Response response = Response.status(
248 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
249 throw new WebApplicationException(response);