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.vocabulary;
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.vocabulary.nuxeo.VocabularyHandlerFactory;
42 import org.collectionspace.services.common.ClientType;
43 import org.collectionspace.services.common.ServiceMain;
44 import org.collectionspace.services.common.context.RemoteServiceContext;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.repository.DocumentNotFoundException;
47 import org.collectionspace.services.common.repository.DocumentHandler;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
49 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
50 import org.jboss.resteasy.util.HttpResponseCodes;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 @Path("/vocabularies")
55 @Consumes("multipart/mixed")
56 @Produces("multipart/mixed")
57 public class VocabularyResource extends AbstractCollectionSpaceResource {
59 private final static String serviceName = "vocabularies";
60 final Logger logger = LoggerFactory.getLogger(VocabularyResource.class);
61 //FIXME retrieve client type from configuration
62 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
64 public VocabularyResource() {
69 public String getServiceName() {
74 public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
75 DocumentHandler docHandler = VocabularyHandlerFactory.getInstance().getHandler(
76 ctx.getRepositoryClientType().toString());
77 docHandler.setServiceContext(ctx);
78 if(ctx.getInput() != null){
79 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), VocabulariesCommon.class);
81 docHandler.setCommonPart((VocabulariesCommon) obj);
88 public Response createVocabulary(MultipartInput input) {
90 RemoteServiceContext ctx = createServiceContext(input);
91 DocumentHandler handler = createDocumentHandler(ctx);
92 String csid = getRepositoryClient(ctx).create(ctx, handler);
93 //vocabularyObject.setCsid(csid);
94 UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
96 Response response = Response.created(path.build()).build();
99 if(logger.isDebugEnabled()){
100 logger.debug("Caught exception in createVocabulary", e);
102 Response response = Response.status(
103 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
104 throw new WebApplicationException(response);
110 public MultipartOutput getVocabulary(
111 @PathParam("csid") String csid) {
112 if(logger.isDebugEnabled()){
113 logger.debug("getVocabulary with csid=" + csid);
115 if(csid == null || "".equals(csid)){
116 logger.error("getVocabulary: missing csid!");
117 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
118 "get failed on Vocabulary csid=" + csid).type(
119 "text/plain").build();
120 throw new WebApplicationException(response);
122 MultipartOutput result = null;
124 RemoteServiceContext ctx = createServiceContext(null);
125 DocumentHandler handler = createDocumentHandler(ctx);
126 getRepositoryClient(ctx).get(ctx, csid, handler);
127 result = ctx.getOutput();
128 }catch(DocumentNotFoundException dnfe){
129 if(logger.isDebugEnabled()){
130 logger.debug("getVocabulary", dnfe);
132 Response response = Response.status(Response.Status.NOT_FOUND).entity(
133 "Get failed on Vocabulary csid=" + csid).type(
134 "text/plain").build();
135 throw new WebApplicationException(response);
137 if(logger.isDebugEnabled()){
138 logger.debug("getVocabulary", e);
140 Response response = Response.status(
141 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
142 throw new WebApplicationException(response);
145 Response response = Response.status(Response.Status.NOT_FOUND).entity(
146 "Get failed, the requested Vocabulary CSID:" + csid + ": was not found.").type(
147 "text/plain").build();
148 throw new WebApplicationException(response);
154 @Produces("application/xml")
155 public VocabulariesCommonList getVocabularyList(@Context UriInfo ui) {
156 VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList();
158 RemoteServiceContext ctx = createServiceContext(null);
159 DocumentHandler handler = createDocumentHandler(ctx);
160 getRepositoryClient(ctx).getAll(ctx, handler);
161 vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList();
163 if(logger.isDebugEnabled()){
164 logger.debug("Caught exception in getVocabularyList", e);
166 Response response = Response.status(
167 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
168 throw new WebApplicationException(response);
170 return vocabularyObjectList;
175 public MultipartOutput updateVocabulary(
176 @PathParam("csid") String csid,
177 MultipartInput theUpdate) {
178 if(logger.isDebugEnabled()){
179 logger.debug("updateVocabulary with csid=" + csid);
181 if(csid == null || "".equals(csid)){
182 logger.error("updateVocabulary: missing csid!");
183 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
184 "update failed on Vocabulary csid=" + csid).type(
185 "text/plain").build();
186 throw new WebApplicationException(response);
188 MultipartOutput result = null;
190 RemoteServiceContext ctx = createServiceContext(theUpdate);
191 DocumentHandler handler = createDocumentHandler(ctx);
192 getRepositoryClient(ctx).update(ctx, csid, handler);
193 result = ctx.getOutput();
194 }catch(DocumentNotFoundException dnfe){
195 if(logger.isDebugEnabled()){
196 logger.debug("caugth exception in updateVocabulary", dnfe);
198 Response response = Response.status(Response.Status.NOT_FOUND).entity(
199 "Update failed on Vocabulary csid=" + csid).type(
200 "text/plain").build();
201 throw new WebApplicationException(response);
203 Response response = Response.status(
204 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
205 throw new WebApplicationException(response);
212 public Response deleteVocabulary(@PathParam("csid") String csid) {
214 if(logger.isDebugEnabled()){
215 logger.debug("deleteVocabulary with csid=" + csid);
217 if(csid == null || "".equals(csid)){
218 logger.error("deleteVocabulary: missing csid!");
219 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
220 "delete failed on Vocabulary csid=" + csid).type(
221 "text/plain").build();
222 throw new WebApplicationException(response);
225 ServiceContext ctx = createServiceContext(null);
226 getRepositoryClient(ctx).delete(ctx, csid);
227 return Response.status(HttpResponseCodes.SC_OK).build();
228 }catch(DocumentNotFoundException dnfe){
229 if(logger.isDebugEnabled()){
230 logger.debug("caught exception in deleteVocabulary", dnfe);
232 Response response = Response.status(Response.Status.NOT_FOUND).entity(
233 "Delete failed on Vocabulary csid=" + csid).type(
234 "text/plain").build();
235 throw new WebApplicationException(response);
237 Response response = Response.status(
238 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
239 throw new WebApplicationException(response);