1 package org.collectionspace.hello.services;
3 import java.io.ByteArrayInputStream;
4 import org.collectionspace.hello.services.nuxeo.NuxeoRESTClient;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.Iterator;
10 import javax.ws.rs.Consumes;
11 import javax.ws.rs.GET;
12 import javax.ws.rs.Path;
13 import javax.ws.rs.Produces;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.POST;
17 import javax.ws.rs.PUT;
18 import javax.ws.rs.PathParam;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.Context;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.UriBuilder;
23 import javax.ws.rs.core.UriInfo;
24 import javax.xml.bind.JAXBContext;
25 import javax.xml.bind.Marshaller;
26 import org.collectionspace.hello.*;
29 import org.collectionspace.hello.CollectionObjectList.CollectionObjectListItem;
30 import org.dom4j.Document;
31 import org.dom4j.Element;
32 import org.dom4j.Namespace;
33 import org.dom4j.io.SAXReader;
34 import org.restlet.resource.Representation;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 @Path("/collectionobjects")
39 @Consumes("application/xml")
40 @Produces("application/xml")
41 public class CollectionObjectResource {
43 final static String NUXEO_WORKSPACE_UID = "776a8787-9d81-41b0-a02c-1ba674638c0a";
44 final static String NUXEO_DOCTYPE = "CollectionObject";
46 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
48 public CollectionObjectResource() {
53 public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
54 CollectionObjectList p = new CollectionObjectList();
56 List<CollectionObjectList.CollectionObjectListItem> list = p.getCollectionObjectListItem();
57 NuxeoRESTClient nxClient = getClient();
59 List<String> pathParams = new ArrayList<String>();
60 Map<String, String> queryParams = new HashMap<String, String>();
61 pathParams = Arrays.asList("default", NUXEO_WORKSPACE_UID, "browse");
62 Representation res = nxClient.get(pathParams, queryParams);
63 SAXReader reader = new SAXReader();
64 Document document = reader.read(res.getStream());
65 Element root = document.getRootElement();
66 for(Iterator i = root.elementIterator(); i.hasNext();){
67 Element element = (Element) i.next();
68 CollectionObjectListItem pli = new CollectionObjectListItem();
70 pli.setCsid(element.attributeValue("csid"));
71 pli.setUri(element.attributeValue("url"));
72 pli.setIdentifier(element.attributeValue("identifier"));
83 public Response createCollectionObject(CollectionObject co) {
85 NuxeoRESTClient nxClient = getClient();
87 List<String> pathParams = new ArrayList<String>();
88 Map<String, String> queryParams = new HashMap<String, String>();
89 pathParams.add("default");
90 pathParams.add(NUXEO_WORKSPACE_UID);
91 pathParams.add("createDocument");
92 queryParams.put("docType", NUXEO_DOCTYPE);
94 queryParams.put("dublincore:title", co.getIdentifier());
95 // CollectionObject core values
96 queryParams.put("collectionobject:csid", Integer.valueOf(1).toString());
97 queryParams.put("collectionobject:identifier", co.getIdentifier());
98 queryParams.put("collectionobject:description", co.getDescription());
100 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
101 Representation res = nxClient.post(pathParams, queryParams, bais);
103 SAXReader reader = new SAXReader();
105 Document document = reader.read(res.getStream());
106 Element root = document.getRootElement();
107 for (Iterator i = root.elementIterator(); i.hasNext();){
108 Element element = (Element) i.next();
109 if ("docRef".equals(element.getName())){
110 String id = (String) element.getData();
114 } catch(Exception e){
115 Response response = Response.status(Response.Status.NOT_FOUND).entity(
116 "Create failed").type("text/plain").build();
117 throw new WebApplicationException(response);
120 verbose("created collectionobject", co);
121 UriBuilder path = UriBuilder.fromResource(PersonNuxeoResource.class);
122 path.path("" + co.getCsid());
123 Response response = Response.created(path.build()).build();
130 public CollectionObject getCollectionObject(@PathParam("csid") String csid) {
132 CollectionObject co = null;
134 List<String> pathParams = new ArrayList<String>();
135 Map<String, String> queryParams = new HashMap<String, String>();
137 pathParams.add("default");
138 pathParams.add(csid);
139 pathParams.add("export");
140 queryParams.put("format", "XML");
142 NuxeoRESTClient nxClient = getClient();
143 Representation res = nxClient.get(pathParams, queryParams);
145 SAXReader reader = new SAXReader();
146 Document document = reader.read(res.getStream());
147 Element root = document.getRootElement();
148 co = new CollectionObject();
150 // TODO: recognize schema thru namespace uri
151 // Namespace ns = new Namespace("collectionobject", "http://collectionspace.org/collectionobject");
153 Iterator<Element> siter = root.elementIterator("schema");
154 while (siter.hasNext()) {
156 Element schemaElement = siter.next();
157 System.err.println("CollectionObject.getCollectionObject() called.");
159 //TODO: recognize schema thru namespace uri
160 if ("collectionobject".equals(schemaElement.attribute("name").getValue())){
162 Element ele = schemaElement.element("identifier");
164 co.setIdentifier((String) ele.getData());
166 ele = schemaElement.element("description");
168 co.setDescription((String) ele.getData());
173 } catch(Exception e){
175 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
176 "Get failed").type("text/plain").build();
177 throw new WebApplicationException(response);
180 Response response = Response.status(Response.Status.NOT_FOUND).entity(
181 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type("text/plain").build();
182 throw new WebApplicationException(response);
184 verbose("get collectionobject", co);
191 public CollectionObject updateCollectionObject(
192 @PathParam("csid") String csid,
193 CollectionObject update) {
195 verbose("updating collectionobject input", update);
197 List<String> pathParams = new ArrayList<String>();
198 Map<String, String> queryParams = new HashMap<String, String>();
199 pathParams.add("default");
200 pathParams.add(update.getCsid());
201 pathParams.add("updateDocumentRestlet");
203 //todo: intelligent merge needed
204 if(update.getIdentifier() != null){
205 queryParams.put("collectionobject:identifier", update.getIdentifier());
208 if(update.getDescription() != null){
209 queryParams.put("collectionobject:description", update.getDescription());
212 NuxeoRESTClient nxClient = getClient();
213 Representation res = nxClient.get(pathParams, queryParams);
214 SAXReader reader = new SAXReader();
217 Document document = reader.read(res.getStream());
218 Element root = document.getRootElement();
219 for(Iterator i = root.elementIterator(); i.hasNext();){
220 Element element = (Element) i.next();
221 if("docRef".equals(element.getName())){
222 status = (String) element.getData();
223 verbose("update collectionobject: response=" + status);
227 } catch(Exception e) {
229 Response response = Response.status(Response.Status.NOT_FOUND).entity(
230 "Update failed ").type("text/plain").build();
231 throw new WebApplicationException(response);
239 public void deleteCollectionObject(@PathParam("csid") String csid) {
241 verbose("deleting collectionobject with csid=" + csid);
243 NuxeoRESTClient nxClient = getClient();
244 List<String> pathParams = new ArrayList<String>();
245 Map<String, String> queryParams = new HashMap<String, String>();
247 pathParams.add("default");
248 pathParams.add(csid);
249 pathParams.add("deleteDocumentRestlet");
250 Representation res = nxClient.get(pathParams, queryParams);
251 SAXReader reader = new SAXReader();
255 Document document = reader.read(res.getStream());
256 Element root = document.getRootElement();
257 for(Iterator i = root.elementIterator(); i.hasNext();){
258 Element element = (Element) i.next();
259 if("docRef".equals(element.getName())){
260 status = (String) element.getData();
261 verbose("delete collectionobject: response=" + status);
267 Response response = Response.status(Response.Status.NOT_FOUND).entity(
268 "Delete failed ").type("text/plain").build();
269 throw new WebApplicationException(response);
274 private void verbose(String msg, CollectionObject co) {
277 JAXBContext jc = JAXBContext.newInstance(
278 CollectionObject.class);
280 Marshaller m = jc.createMarshaller();
281 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
283 m.marshal(co, System.out);
284 } catch(Exception e){
290 private NuxeoRESTClient getClient() {
291 NuxeoRESTClient nxClient = new NuxeoRESTClient("http://127.0.0.1:8080/nuxeo");
292 nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC);
293 nxClient.setBasicAuthentication("Administrator", "Administrator");
297 private void verbose(String msg) {
298 System.out.println("CollectionObjectResource: " + msg);