1 package org.collectionspace.services.collectionobject;
3 import javax.ws.rs.Consumes;
4 import javax.ws.rs.GET;
5 import javax.ws.rs.Path;
6 import javax.ws.rs.Produces;
7 import javax.ws.rs.DELETE;
8 import javax.ws.rs.POST;
9 import javax.ws.rs.PUT;
10 import javax.ws.rs.PathParam;
11 import javax.ws.rs.WebApplicationException;
12 import javax.ws.rs.core.Context;
13 import javax.ws.rs.core.Response;
14 import javax.ws.rs.core.UriBuilder;
15 import javax.ws.rs.core.UriInfo;
16 import javax.xml.bind.JAXBContext;
17 import javax.xml.bind.Marshaller;
19 import org.collectionspace.services.collectionobject.CollectionObjectList.*;
21 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
22 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectHandlerFactory;
23 import org.collectionspace.services.common.NuxeoClientType;
24 import org.collectionspace.services.common.ServiceMain;
25 import org.collectionspace.services.common.repository.DocumentNotFoundException;
26 import org.collectionspace.services.common.repository.DocumentHandler;
27 import org.collectionspace.services.common.repository.RepositoryClient;
28 import org.collectionspace.services.common.repository.RepositoryClientFactory;
29 import org.jboss.resteasy.util.HttpResponseCodes;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 @Path("/collectionobjects")
34 @Consumes("application/xml")
35 @Produces("application/xml")
36 public class CollectionObjectResource {
38 public final static String CO_SERVICE_NAME = "collectionobjects";
39 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
40 //FIXME retrieve client type from configuration
41 final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance().getNuxeoClientType();
43 public CollectionObjectResource() {
48 public Response createCollectionObject(
49 CollectionObject collectionObject) {
53 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
54 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
55 CollectionObjectHandlerFactory handlerFactory = CollectionObjectHandlerFactory.getInstance();
56 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
57 handler.setCommonObject(collectionObject);
58 csid = client.create(CO_SERVICE_NAME, handler);
59 collectionObject.setCsid(csid);
60 if(logger.isDebugEnabled()){
61 verbose("createCollectionObject: ", collectionObject);
63 UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
65 Response response = Response.created(path.build()).build();
68 if(logger.isDebugEnabled()){
69 logger.debug("Caught exception in createCollectionObject", e);
71 Response response = Response.status(
72 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
73 throw new WebApplicationException(response);
79 public CollectionObject getCollectionObject(
80 @PathParam("csid") String csid) {
81 if(logger.isDebugEnabled()){
82 verbose("getCollectionObject with csid=" + csid);
84 if(csid == null || "".equals(csid)){
85 logger.error("getCollectionObject: missing csid!");
86 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
87 "get failed on CollectionObject csid=" + csid).type(
88 "text/plain").build();
89 throw new WebApplicationException(response);
91 CollectionObject collectionObject = null;
93 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
94 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
95 CollectionObjectHandlerFactory handlerFactory = CollectionObjectHandlerFactory.getInstance();
96 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
97 client.get(csid, handler);
98 collectionObject = (CollectionObject) handler.getCommonObject();
99 }catch(DocumentNotFoundException dnfe){
100 if(logger.isDebugEnabled()){
101 logger.debug("getCollectionObject", dnfe);
103 Response response = Response.status(Response.Status.NOT_FOUND).entity(
104 "Get failed on CollectionObject csid=" + csid).type(
105 "text/plain").build();
106 throw new WebApplicationException(response);
108 if(logger.isDebugEnabled()){
109 logger.debug("getCollectionObject", e);
111 Response response = Response.status(
112 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
113 throw new WebApplicationException(response);
116 if(collectionObject == null){
117 Response response = Response.status(Response.Status.NOT_FOUND).entity(
118 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
119 "text/plain").build();
120 throw new WebApplicationException(response);
122 if(logger.isDebugEnabled()){
123 verbose("getCollectionObject: ", collectionObject);
125 return collectionObject;
129 public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
130 CollectionObjectList collectionObjectList = new CollectionObjectList();
132 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
133 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
134 CollectionObjectHandlerFactory handlerFactory = CollectionObjectHandlerFactory.getInstance();
135 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
136 client.getAll(CO_SERVICE_NAME, handler);
137 collectionObjectList = (CollectionObjectList) handler.getCommonObjectList();
139 if(logger.isDebugEnabled()){
140 logger.debug("Caught exception in getCollectionObjectList", e);
142 Response response = Response.status(
143 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
144 throw new WebApplicationException(response);
146 return collectionObjectList;
151 public CollectionObject updateCollectionObject(
152 @PathParam("csid") String csid,
153 CollectionObject theUpdate) {
154 if(logger.isDebugEnabled()){
155 verbose("updateCollectionObject with csid=" + csid);
157 if(csid == null || "".equals(csid)){
158 logger.error("updateCollectionObject: missing csid!");
159 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
160 "update failed on CollectionObject csid=" + csid).type(
161 "text/plain").build();
162 throw new WebApplicationException(response);
164 if(logger.isDebugEnabled()){
165 verbose("updateCollectionObject with input: ", theUpdate);
168 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
169 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
170 CollectionObjectHandlerFactory handlerFactory = CollectionObjectHandlerFactory.getInstance();
171 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
172 handler.setCommonObject(theUpdate);
173 client.update(csid, handler);
174 }catch(DocumentNotFoundException dnfe){
175 if(logger.isDebugEnabled()){
176 logger.debug("caugth exception in updateCollectionObject", dnfe);
178 Response response = Response.status(Response.Status.NOT_FOUND).entity(
179 "Update failed on CollectionObject csid=" + csid).type(
180 "text/plain").build();
181 throw new WebApplicationException(response);
183 Response response = Response.status(
184 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
185 throw new WebApplicationException(response);
192 public Response deleteCollectionObject(@PathParam("csid") String csid) {
194 if(logger.isDebugEnabled()){
195 verbose("deleteCollectionObject with csid=" + csid);
197 if(csid == null || "".equals(csid)){
198 logger.error("deleteCollectionObject: missing csid!");
199 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
200 "delete failed on CollectionObject csid=" + csid).type(
201 "text/plain").build();
202 throw new WebApplicationException(response);
205 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
206 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
208 return Response.status(HttpResponseCodes.SC_OK).build();
209 }catch(DocumentNotFoundException dnfe){
210 if(logger.isDebugEnabled()){
211 logger.debug("caught exception in deleteCollectionObject", dnfe);
213 Response response = Response.status(Response.Status.NOT_FOUND).entity(
214 "Delete failed on CollectionObject csid=" + csid).type(
215 "text/plain").build();
216 throw new WebApplicationException(response);
218 Response response = Response.status(
219 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
220 throw new WebApplicationException(response);
225 private void verbose(String msg, CollectionObject collectionObject) {
228 JAXBContext jc = JAXBContext.newInstance(
229 CollectionObject.class);
231 Marshaller m = jc.createMarshaller();
232 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
233 m.marshal(collectionObject, System.out);
240 private void verbose(String msg) {
241 System.out.println("CollectionObjectResource. " + msg);