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.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.MediaType;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.UriBuilder;
22 import javax.xml.bind.JAXBContext;
23 import javax.xml.bind.Marshaller;
24 import org.collectionspace.hello.*;
26 import org.collectionspace.world.DublincoreNuxeo;
27 import org.dom4j.Document;
28 import org.dom4j.Element;
29 import org.dom4j.io.SAXReader;
30 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
31 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput;
32 import org.restlet.resource.Representation;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 @Consumes("application/xml")
38 @Produces("application/xml")
39 public class MultipartResource extends CollectionSpaceResource {
41 final Logger logger = LoggerFactory.getLogger(MultipartResource.class);
43 public MultipartResource() {
47 @Consumes("multipart/form-data")
48 public Response createPerson(MultipartFormDataInput multipart) {
50 PersonNuxeo p = new PersonNuxeo();
51 DublincoreNuxeo dc = new DublincoreNuxeo();
52 NuxeoRESTClient nxClient = getClient();
54 List<String> pathParams = new ArrayList<String>();
55 Map<String, String> queryParams = new HashMap<String, String>();
56 pathParams.add("default");
57 pathParams.add(CS_PERSON_WORKSPACE_UID);
58 pathParams.add("createDocument");
59 queryParams.put("docType", "Hello");
62 if(multipart.getFormData().containsKey("dublincore")){
63 dc = multipart.getFormDataPart("dublincore", DublincoreNuxeo.class, null);
64 if(dc.getTitle() != null){
65 queryParams.put("dublincore:title", dc.getTitle());
68 if(multipart.getFormData().containsKey("hello")){
69 p = multipart.getFormDataPart("hello", PersonNuxeo.class, null);
70 queryParams.put("hello:cversion", Integer.valueOf(1).toString());
71 if(p.getFirstName() != null){
72 queryParams.put("hello:firstName", p.getFirstName());
74 if(p.getLastName() != null){
75 queryParams.put("hello:lastName", p.getLastName());
77 if(p.getStreet() != null){
78 queryParams.put("hello:street", p.getStreet());
80 if(p.getCity() != null){
81 queryParams.put("hello:city", p.getCity());
83 if(p.getState() != null){
84 queryParams.put("hello:state", p.getState());
86 if(p.getZip() != null){
87 queryParams.put("hello:zip", p.getZip());
89 if(p.getCountry() != null){
90 queryParams.put("hello:country", p.getCountry());
93 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
94 Representation res = nxClient.post(pathParams, queryParams, bais);
96 SAXReader reader = new SAXReader();
98 Document document = reader.read(res.getStream());
99 Element root = document.getRootElement();
100 for(Iterator i = root.elementIterator(); i.hasNext();){
101 Element element = (Element) i.next();
102 if("docRef".equals(element.getName())){
103 String id = (String) element.getData();
108 Response response = Response.status(Response.Status.NOT_FOUND).entity(
109 "Create failed").type("text/plain").build();
110 throw new WebApplicationException(response);
113 verbosePerson("createPerson: person", p);
114 verboseDublin("createPerson: dublincore", dc);
115 UriBuilder path = UriBuilder.fromResource(MultipartResource.class);
116 path.path("" + p.getId());
117 Response response = Response.created(path.build()).build();
123 @Produces("multipart/form-data")
124 public MultipartFormDataOutput getPerson(@PathParam("id") String id) {
126 PersonNuxeo person = new PersonNuxeo();
127 DublincoreNuxeo dublin = new DublincoreNuxeo();
128 MultipartFormDataOutput output = new MultipartFormDataOutput();
131 NuxeoRESTClient nxClient = getClient();
133 List<String> pathParams = new ArrayList<String>();
134 Map<String, String> queryParams = new HashMap<String, String>();
136 pathParams.add("default");
138 pathParams.add("export");
139 queryParams.put("format", "XML");
141 Representation res = nxClient.get(pathParams, queryParams);
142 SAXReader reader = new SAXReader();
144 Document document = reader.read(res.getStream());
145 Element root = document.getRootElement();
147 //TODO: recognize schema thru namespace uri
148 // Namespace ns = new Namespace("hello", "http://collectionspace.org/hello");
149 Iterator<Element> siter = root.elementIterator("schema");
150 while(siter.hasNext()){
152 Element s = siter.next();
154 //TODO: recognize schema thru namespace uri
155 if("hello".equals(s.attribute("name").getValue())){
157 Element ele = s.element("cversion");
159 person.setVersion((String) ele.getData());
161 ele = s.element("firstName");
163 person.setFirstName((String) ele.getData());
165 ele = s.element("lastName");
167 person.setLastName((String) ele.getData());
169 ele = s.element("city");
171 person.setCity((String) ele.getData());
173 ele = s.element("state");
175 person.setState((String) ele.getData());
177 ele = s.element("zip");
179 person.setZip((String) ele.getData());
181 ele = s.element("country");
183 person.setCountry((String) ele.getData());
185 }else if("dublincore".equals(s.attribute("name").getValue())){
186 Element ele = s.element("title");
188 dublin.setTitle((String) ele.getData());
192 verbosePerson("getPerson:hello:", person);
193 output.addFormData("hello", person, MediaType.APPLICATION_XML_TYPE);
194 verboseDublin("getPerson:dublincore:", dublin);
195 output.addFormData("dublincore", dublin, MediaType.APPLICATION_XML_TYPE);
199 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
200 "Get failed").type("text/plain").build();
201 throw new WebApplicationException(response);
204 Response response = Response.status(Response.Status.NOT_FOUND).entity(
205 "Get failed, the requested person ID:" + id + ": was not found.").type("text/plain").build();
206 throw new WebApplicationException(response);
214 public PersonNuxeo updatePerson(
215 @PathParam("id") String id,
216 PersonNuxeo update) {
218 verbosePerson("updating person input", update);
220 NuxeoRESTClient nxClient = getClient();
222 List<String> pathParams = new ArrayList<String>();
223 Map<String, String> queryParams = new HashMap<String, String>();
224 pathParams.add("default");
225 pathParams.add(update.getId());
226 pathParams.add("updateDocumentRestlet");
227 queryParams.put("dublincore:title", "change title");
228 //todo: intelligent merge needed
229 if(update.getFirstName() != null){
230 queryParams.put("hello:firstName", update.getFirstName());
233 if(update.getLastName() != null){
234 queryParams.put("hello:lastName", update.getLastName());
237 if(update.getFirstName() != null && update.getLastName() != null){
238 queryParams.put("dublincore:title", update.getFirstName() + " " + update.getLastName());
241 if(update.getStreet() != null){
242 queryParams.put("hello:street", update.getStreet());
245 if(update.getCity() != null){
246 queryParams.put("hello:city", update.getCity());
249 if(update.getState() != null){
250 queryParams.put("hello:state", update.getState());
253 if(update.getZip() != null){
254 queryParams.put("hello:zip", update.getZip());
257 if(update.getCountry() != null){
258 queryParams.put("hello:country", update.getCountry());
261 Representation res = nxClient.get(pathParams, queryParams);
262 SAXReader reader = new SAXReader();
265 Document document = reader.read(res.getStream());
266 Element root = document.getRootElement();
267 for(Iterator i = root.elementIterator(); i.hasNext();){
268 Element element = (Element) i.next();
269 if("docRef".equals(element.getName())){
270 status = (String) element.getData();
271 verbose("updatePerson: response=" + status);
277 Response response = Response.status(Response.Status.NOT_FOUND).entity(
278 "Update failed ").type("text/plain").build();
279 throw new WebApplicationException(response);
286 public void deletePerson(@PathParam("id") String id) {
287 verbose("deleting person with id=" + id);
288 NuxeoRESTClient nxClient = getClient();
289 List<String> pathParams = new ArrayList<String>();
290 Map<String, String> queryParams = new HashMap<String, String>();
291 pathParams.add("default");
293 pathParams.add("deleteDocumentRestlet");
294 Representation res = nxClient.get(pathParams, queryParams);
295 SAXReader reader = new SAXReader();
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("deletePerson: response=" + status);
310 Response response = Response.status(Response.Status.NOT_FOUND).entity(
311 "Delete failed ").type("text/plain").build();
312 throw new WebApplicationException(response);
317 private void verbosePerson(String msg, PersonNuxeo person) {
320 JAXBContext jc = JAXBContext.newInstance(
323 Marshaller m = jc.createMarshaller();
324 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
326 m.marshal(person, System.out);
333 private void verboseDublin(String msg, DublincoreNuxeo dubin) {
336 JAXBContext jc = JAXBContext.newInstance(
337 DublincoreNuxeo.class);
339 Marshaller m = jc.createMarshaller();
340 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
342 m.marshal(dubin, System.out);
349 private void verbose(String msg) {
350 System.out.println("MultipartResource: " + msg);