]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d861fc89c56d290dc9a821871d4da395ede14c8f
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.services;
2
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;
9 import java.util.List;
10 import javax.ws.rs.Consumes;
11 import javax.ws.rs.GET;
12 import javax.ws.rs.Path;
13 import javax.ws.rs.Produces;
14 import java.util.Map;
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.*;
27
28
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;
37
38 @Path("/collectionobjects")
39 @Consumes("application/xml")
40 @Produces("application/xml")
41 public class CollectionObjectResource {
42
43         final static String NUXEO_WORKSPACE_UID = "776a8787-9d81-41b0-a02c-1ba674638c0a";
44         final static String NUXEO_DOCTYPE = "CollectionObject";
45         
46     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
47
48     public CollectionObjectResource() {
49         // do nothing
50     }
51
52     @GET
53     public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
54         CollectionObjectList p = new CollectionObjectList();
55         try{
56             List<CollectionObjectList.CollectionObjectListItem> list = p.getCollectionObjectListItem();
57             NuxeoRESTClient nxClient = getClient();
58
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();
69                 //
70                 pli.setCsid(element.attributeValue("csid"));
71                 pli.setUri(element.attributeValue("url"));
72                 pli.setIdentifier(element.attributeValue("identifier"));
73                 list.add(pli);
74             }
75
76         }catch(Exception e){
77             e.printStackTrace();
78         }
79         return p;
80     }
81
82     @POST
83     public Response createCollectionObject(CollectionObject co) {
84
85         NuxeoRESTClient nxClient = getClient();
86
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);
93         
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());
99
100         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
101         Representation res = nxClient.post(pathParams, queryParams, bais);
102
103         SAXReader reader = new SAXReader();
104         try {
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();
111                     co.setCsid(id);
112                 }
113             }
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);
118         }
119
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();
124         
125         return response;
126     }
127
128     @GET
129     @Path("{csid}")
130     public CollectionObject getCollectionObject(@PathParam("csid") String csid) {
131
132         CollectionObject co = null;
133         try {
134             List<String> pathParams = new ArrayList<String>();
135             Map<String, String> queryParams = new HashMap<String, String>();
136
137             pathParams.add("default");
138             pathParams.add(csid);
139             pathParams.add("export");
140             queryParams.put("format", "XML");
141
142             NuxeoRESTClient nxClient = getClient();
143             Representation res = nxClient.get(pathParams, queryParams);
144
145             SAXReader reader = new SAXReader();
146             Document document = reader.read(res.getStream());
147             Element root = document.getRootElement();
148             co = new CollectionObject();
149
150  //                     TODO: recognize schema thru namespace uri
151 //          Namespace ns = new Namespace("collectionobject", "http://collectionspace.org/collectionobject");
152
153             Iterator<Element> siter = root.elementIterator("schema");
154             while (siter.hasNext()) {
155
156                 Element schemaElement = siter.next();
157                 System.err.println("CollectionObject.getCollectionObject() called.");
158
159                 //TODO: recognize schema thru namespace uri
160                 if ("collectionobject".equals(schemaElement.attribute("name").getValue())){
161                     co.setCsid(csid);
162                     Element ele = schemaElement.element("identifier");
163                     if(ele != null){
164                         co.setIdentifier((String) ele.getData());
165                     }
166                     ele = schemaElement.element("description");
167                     if(ele != null){
168                         co.setDescription((String) ele.getData());
169                     }
170                 }
171             }
172
173         } catch(Exception e){
174             e.printStackTrace();
175             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
176                     "Get failed").type("text/plain").build();
177             throw new WebApplicationException(response);
178         }
179         if (co == null) {
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);
183         }
184         verbose("get collectionobject", co);
185         
186         return co;
187     }
188
189     @PUT
190     @Path("{csid}")
191     public CollectionObject updateCollectionObject(
192             @PathParam("csid") String csid,
193             CollectionObject update) {
194
195         verbose("updating collectionobject input", update);
196
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");
202         
203         //todo: intelligent merge needed
204         if(update.getIdentifier() != null){
205             queryParams.put("collectionobject:identifier", update.getIdentifier());
206         }
207
208         if(update.getDescription() != null){
209             queryParams.put("collectionobject:description", update.getDescription());
210         }
211
212         NuxeoRESTClient nxClient = getClient();
213         Representation res = nxClient.get(pathParams, queryParams);
214         SAXReader reader = new SAXReader();
215         String status = "";
216         try {
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);
224                 }
225
226             }
227         } catch(Exception e) {
228             //FIXME: NOT_FOUND?
229             Response response = Response.status(Response.Status.NOT_FOUND).entity(
230                     "Update failed ").type("text/plain").build();
231             throw new WebApplicationException(response);
232         }
233         
234         return update;
235     }
236
237     @DELETE
238     @Path("{csid}")
239     public void deleteCollectionObject(@PathParam("csid") String csid) {
240
241         verbose("deleting collectionobject with csid=" + csid);
242         
243         NuxeoRESTClient nxClient = getClient();
244         List<String> pathParams = new ArrayList<String>();
245         Map<String, String> queryParams = new HashMap<String, String>();
246
247         pathParams.add("default");
248         pathParams.add(csid);
249         pathParams.add("deleteDocumentRestlet");
250         Representation res = nxClient.get(pathParams, queryParams);
251         SAXReader reader = new SAXReader();
252         String status = "";
253         
254         try {
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);
262                 }
263
264             }
265         }catch(Exception e){
266             //FIXME: NOT_FOUND?
267             Response response = Response.status(Response.Status.NOT_FOUND).entity(
268                     "Delete failed ").type("text/plain").build();
269             throw new WebApplicationException(response);
270         }
271
272     }
273
274     private void verbose(String msg, CollectionObject co) {
275         try {
276             verbose(msg);
277             JAXBContext jc = JAXBContext.newInstance(
278                     CollectionObject.class);
279
280             Marshaller m = jc.createMarshaller();
281             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
282                     Boolean.TRUE);
283             m.marshal(co, System.out);
284         } catch(Exception e){
285             e.printStackTrace();
286         }
287
288     }
289
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");
294         return nxClient;
295     }
296
297     private void verbose(String msg) {
298         System.out.println("CollectionObjectResource: " + msg);
299     }
300 }