1 package org.collectionspace.hello.services;
3 import java.io.ByteArrayInputStream;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Iterator;
9 import javax.ws.rs.Consumes;
10 import javax.ws.rs.GET;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.Produces;
14 import javax.ws.rs.DELETE;
15 import javax.ws.rs.POST;
16 import javax.ws.rs.PUT;
17 import javax.ws.rs.PathParam;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.Context;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.UriBuilder;
22 import javax.ws.rs.core.UriInfo;
23 import javax.xml.bind.JAXBContext;
24 import javax.xml.bind.Marshaller;
25 import org.collectionspace.hello.*;
28 import org.collectionspace.hello.services.nuxeo.NuxeoRESTClient;
29 import org.collectionspace.hello.CollectionObjectList.CollectionObjectListItem;
30 import org.collectionspace.hello.services.CollectionObjectJAXBSchema;
31 import org.collectionspace.hello.services.CollectionObjectListItemJAXBSchema;
33 import org.dom4j.Document;
34 import org.dom4j.Element;
35 import org.dom4j.Namespace;
36 import org.dom4j.io.SAXReader;
37 import org.restlet.resource.Representation;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 @Path("/collectionobjects")
42 @Consumes("application/xml")
43 @Produces("application/xml")
44 public class CollectionObjectResource implements CollectionSpaceResource {
46 final static String CO_NUXEO_DOCTYPE = "CollectionObject";
47 final static String CO_NUXEO_SCHEMA_NAME = "collectionobject";
48 final static String CO_NUXEO_DC_TITLE = "CollectionSpace-CollectionObject";
50 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
52 public CollectionObjectResource() {
57 public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
58 CollectionObjectList p = new CollectionObjectList();
60 NuxeoRESTClient nxClient = getClient();
62 List<String> pathParams = new ArrayList<String>();
63 Map<String, String> queryParams = new HashMap<String, String>();
64 pathParams = Arrays.asList("default", CS_NUXEO_WORKSPACE_UID, "browse");
65 Representation res = nxClient.get(pathParams, queryParams);
66 SAXReader reader = new SAXReader();
67 Document document = reader.read(res.getStream());
68 Element root = document.getRootElement();
70 System.err.println(res.toString());
71 System.err.println(document.toString());
73 List<CollectionObjectList.CollectionObjectListItem> list = p.getCollectionObjectListItem();
74 for(Iterator i = root.elementIterator(); i.hasNext();){
75 Element element = (Element) i.next();
77 // set the CollectionObject list item entity elements
78 CollectionObjectListItem pli = new CollectionObjectListItem();
79 pli.setObjectNumber(element.attributeValue("title"));
80 pli.setUri(element.attributeValue("url"));
81 pli.setCsid(element.attributeValue("id"));
93 public Response createCollectionObject(CollectionObject co) {
95 NuxeoRESTClient nxClient = getClient();
97 List<String> pathParams = new ArrayList<String>();
98 Map<String, String> queryParams = new HashMap<String, String>();
99 pathParams.add("default");
100 pathParams.add(CS_NUXEO_WORKSPACE_UID);
101 pathParams.add("createDocument");
102 queryParams.put("docType", CO_NUXEO_DOCTYPE);
104 // a default title for the Dublin Core schema
105 queryParams.put("dublincore:title", CO_NUXEO_DC_TITLE);
107 // CollectionObject core values
108 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER,
109 co.getObjectNumber());
110 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OTHER_NUMBER,
111 co.getOtherNumber());
112 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION,
113 co.getBriefDescription());
114 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.COMMENTS,
116 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.DIST_FEATURES,
117 co.getDistFeatures());
118 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NAME,
120 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT,
121 co.getResponsibleDept());
122 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.TITLE,
125 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
126 Representation res = nxClient.post(pathParams, queryParams, bais);
129 SAXReader reader = new SAXReader();
131 Document document = reader.read(res.getStream());
132 Element root = document.getRootElement();
133 for (Iterator i = root.elementIterator(); i.hasNext();){
134 Element element = (Element) i.next();
135 if ("docRef".equals(element.getName())){
136 csid = (String) element.getData();
140 } catch(Exception e){
141 Response response = Response.status(Response.Status.NOT_FOUND).entity(
142 "Create failed").type("text/plain").build();
143 throw new WebApplicationException(response);
147 verbose("createCollectionObject: ", co);
148 UriBuilder path = UriBuilder.fromResource(PersonNuxeoResource.class);
149 path.path("" + csid);
150 Response response = Response.created(path.build()).build();
157 public CollectionObject getCollectionObject(@PathParam("csid") String csid) {
159 CollectionObject co = null;
161 List<String> pathParams = new ArrayList<String>();
162 Map<String, String> queryParams = new HashMap<String, String>();
164 pathParams.add("default");
165 pathParams.add(csid);
166 pathParams.add("export");
167 queryParams.put("format", "XML");
169 NuxeoRESTClient nxClient = getClient();
170 Representation res = nxClient.get(pathParams, queryParams);
172 SAXReader reader = new SAXReader();
173 Document document = reader.read(res.getStream());
174 Element root = document.getRootElement();
175 co = new CollectionObject();
177 // TODO: recognize schema thru namespace uri
178 // Namespace ns = new Namespace("collectionobject", "http://collectionspace.org/collectionobject");
180 Iterator<Element> siter = root.elementIterator("schema");
181 while (siter.hasNext()) {
183 Element schemaElement = siter.next();
184 System.err.println("CollectionObject.getCollectionObject() called.");
186 //TODO: recognize schema thru namespace uri
187 if (CO_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){
188 Element ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NUMBER);
190 co.setObjectNumber((String) ele.getData());
192 ele = schemaElement.element(CollectionObjectJAXBSchema.OTHER_NUMBER);
194 co.setOtherNumber((String) ele.getData());
196 ele = schemaElement.element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
198 co.setBriefDescription((String) ele.getData());
200 ele = schemaElement.element(CollectionObjectJAXBSchema.COMMENTS);
202 co.setComments((String) ele.getData());
204 ele = schemaElement.element(CollectionObjectJAXBSchema.DIST_FEATURES);
206 co.setDistFeatures((String) ele.getData());
208 ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NAME);
210 co.setObjectName((String) ele.getData());
212 ele = schemaElement.element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
214 co.setResponsibleDept((String) ele.getData());
216 ele = schemaElement.element(CollectionObjectJAXBSchema.TITLE);
218 co.setTitle((String) ele.getData());
222 } catch(Exception e){
224 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
225 "Get failed").type("text/plain").build();
226 throw new WebApplicationException(response);
229 Response response = Response.status(Response.Status.NOT_FOUND).entity(
230 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type("text/plain").build();
231 throw new WebApplicationException(response);
233 verbose("getCollectionObject: ", co);
240 public CollectionObject updateCollectionObject(
241 @PathParam("csid") String csid,
242 CollectionObject theUpdate) {
244 verbose("updateCollectionObject with input: ", theUpdate);
246 List<String> pathParams = new ArrayList<String>();
247 Map<String, String> queryParams = new HashMap<String, String>();
248 pathParams.add("default");
249 pathParams.add(csid);
250 pathParams.add("updateDocumentRestlet");
252 //todo: intelligent merge needed
253 if(theUpdate.getObjectNumber() != null){
254 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER,
255 theUpdate.getObjectNumber());
258 if(theUpdate.getOtherNumber() != null){
259 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OTHER_NUMBER,
260 theUpdate.getOtherNumber());
263 if(theUpdate.getBriefDescription()!= null){
264 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION,
265 theUpdate.getBriefDescription());
268 if(theUpdate.getComments() != null){
269 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.COMMENTS,
270 theUpdate.getComments());
273 if(theUpdate.getDistFeatures() != null){
274 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.DIST_FEATURES,
275 theUpdate.getDistFeatures());
278 if(theUpdate.getObjectName() != null){
279 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NAME,
280 theUpdate.getObjectName());
283 if(theUpdate.getResponsibleDept() != null){
284 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT,
285 theUpdate.getResponsibleDept());
288 if(theUpdate.getTitle() != null){
289 queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.TITLE,
290 theUpdate.getTitle());
293 NuxeoRESTClient nxClient = getClient();
294 Representation res = nxClient.get(pathParams, queryParams);
295 SAXReader reader = new SAXReader();
296 String status = null;
298 Document document = reader.read(res.getStream());
299 Element root = document.getRootElement();
300 for(Iterator i = root.elementIterator(); i.hasNext();){
301 Element element = (Element) i.next();
302 if("docRef".equals(element.getName())){
303 status = (String) element.getData();
304 verbose("updateCollectionObject response: " + status);
307 } catch(Exception e) {
309 Response response = Response.status(Response.Status.NOT_FOUND).entity(
310 "Update failed ").type("text/plain").build();
311 throw new WebApplicationException(response);
319 public void deleteCollectionObject(@PathParam("csid") String csid) {
321 verbose("deleteCollectionObject with csid=" + csid);
323 NuxeoRESTClient nxClient = getClient();
324 List<String> pathParams = new ArrayList<String>();
325 Map<String, String> queryParams = new HashMap<String, String>();
327 pathParams.add("default");
328 pathParams.add(csid);
329 pathParams.add("deleteDocumentRestlet");
330 Representation res = nxClient.get(pathParams, queryParams);
331 SAXReader reader = new SAXReader();
335 Document document = reader.read(res.getStream());
336 Element root = document.getRootElement();
337 for(Iterator i = root.elementIterator(); i.hasNext();){
338 Element element = (Element) i.next();
339 if("docRef".equals(element.getName())){
340 status = (String) element.getData();
341 verbose("deleteCollectionObjectt response: " + status);
346 Response response = Response.status(Response.Status.NOT_FOUND).entity(
347 "Delete failed ").type("text/plain").build();
348 throw new WebApplicationException(response);
353 private void verbose(String msg, CollectionObject co) {
356 JAXBContext jc = JAXBContext.newInstance(
357 CollectionObject.class);
359 Marshaller m = jc.createMarshaller();
360 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
362 m.marshal(co, System.out);
363 } catch(Exception e){
368 private NuxeoRESTClient getClient() {
369 NuxeoRESTClient nxClient = new NuxeoRESTClient("http://127.0.0.1:8080/nuxeo");
370 nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC);
371 nxClient.setBasicAuthentication("Administrator", "Administrator");
375 private void verbose(String msg) {
376 System.out.println("CollectionObjectResource. " + msg);