]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
cf7b7e2768601e3ddf9d454617b3fa124ac497fa
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.vocabulary;
25
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;
39
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;
53
54 @Path("/vocabularies")
55 @Consumes("multipart/mixed")
56 @Produces("multipart/mixed")
57 public class VocabularyResource extends AbstractCollectionSpaceResource {
58
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();
63
64     public VocabularyResource() {
65         // do nothing
66     }
67
68     @Override
69     public String getServiceName() {
70         return serviceName;
71     }
72
73     @Override
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);
80             if(obj != null){
81                 docHandler.setCommonPart((VocabulariesCommon) obj);
82             }
83         }
84         return docHandler;
85     }
86
87     @POST
88     public Response createVocabulary(MultipartInput input) {
89         try{
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);
95             path.path("" + csid);
96             Response response = Response.created(path.build()).build();
97             return response;
98         }catch(Exception e){
99             if(logger.isDebugEnabled()){
100                 logger.debug("Caught exception in createVocabulary", e);
101             }
102             Response response = Response.status(
103                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
104             throw new WebApplicationException(response);
105         }
106     }
107
108     @GET
109     @Path("{csid}")
110     public MultipartOutput getVocabulary(
111             @PathParam("csid") String csid) {
112         if(logger.isDebugEnabled()){
113             logger.debug("getVocabulary with csid=" + csid);
114         }
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);
121         }
122         MultipartOutput result = null;
123         try{
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);
131             }
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);
136         }catch(Exception e){
137             if(logger.isDebugEnabled()){
138                 logger.debug("getVocabulary", e);
139             }
140             Response response = Response.status(
141                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
142             throw new WebApplicationException(response);
143         }
144         if(result == null){
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);
149         }
150         return result;
151     }
152
153     @GET
154     @Produces("application/xml")
155     public VocabulariesCommonList getVocabularyList(@Context UriInfo ui) {
156         VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList();
157         try{
158             RemoteServiceContext ctx = createServiceContext(null);
159             DocumentHandler handler = createDocumentHandler(ctx);
160             getRepositoryClient(ctx).getAll(ctx, handler);
161             vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList();
162         }catch(Exception e){
163             if(logger.isDebugEnabled()){
164                 logger.debug("Caught exception in getVocabularyList", e);
165             }
166             Response response = Response.status(
167                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
168             throw new WebApplicationException(response);
169         }
170         return vocabularyObjectList;
171     }
172
173     @PUT
174     @Path("{csid}")
175     public MultipartOutput updateVocabulary(
176             @PathParam("csid") String csid,
177             MultipartInput theUpdate) {
178         if(logger.isDebugEnabled()){
179             logger.debug("updateVocabulary with csid=" + csid);
180         }
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);
187         }
188         MultipartOutput result = null;
189         try{
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);
197             }
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);
202         }catch(Exception e){
203             Response response = Response.status(
204                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
205             throw new WebApplicationException(response);
206         }
207         return result;
208     }
209
210     @DELETE
211     @Path("{csid}")
212     public Response deleteVocabulary(@PathParam("csid") String csid) {
213
214         if(logger.isDebugEnabled()){
215             logger.debug("deleteVocabulary with csid=" + csid);
216         }
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);
223         }
224         try{
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);
231             }
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);
236         }catch(Exception e){
237             Response response = Response.status(
238                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
239             throw new WebApplicationException(response);
240         }
241
242     }
243 }