1 package org.collectionspace.hello.services;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.util.Iterator;
7 import javax.ws.rs.Consumes;
8 import javax.ws.rs.GET;
9 import javax.ws.rs.Path;
10 import javax.ws.rs.Produces;
11 import javax.ws.rs.DELETE;
12 import javax.ws.rs.POST;
13 import javax.ws.rs.PUT;
14 import javax.ws.rs.PathParam;
15 import javax.ws.rs.WebApplicationException;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.Response;
18 import javax.ws.rs.core.UriBuilder;
19 import javax.xml.bind.JAXBContext;
20 import javax.xml.bind.Marshaller;
22 import org.collectionspace.hello.PersonNuxeo;
24 import org.collectionspace.services.nuxeo.NuxeoConnector;
25 import org.collectionspace.world.DublincoreNuxeo;
26 import org.dom4j.Document;
27 import org.dom4j.Element;
28 import org.dom4j.io.SAXReader;
29 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
30 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput;
33 import org.nuxeo.common.utils.IdUtils;
34 import org.nuxeo.ecm.core.api.CoreSession;
35 import org.nuxeo.ecm.core.api.DocumentModel;
38 import org.nuxeo.ecm.core.api.DocumentRef;
39 import org.nuxeo.ecm.core.api.IdRef;
40 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
41 import org.nuxeo.ecm.core.client.NuxeoClient;
42 import org.nuxeo.ecm.core.io.DocumentPipe;
43 import org.nuxeo.ecm.core.io.DocumentReader;
44 import org.nuxeo.ecm.core.io.DocumentWriter;
45 import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;
46 import org.nuxeo.ecm.core.io.impl.plugins.SingleDocumentReader;
47 import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 @Consumes("application/xml")
53 @Produces("application/xml")
54 public class MultischemaResource extends CollectionSpaceResource {
56 final Logger logger = LoggerFactory.getLogger(MultischemaResource.class);
58 public MultischemaResource() {
62 @Consumes("multipart/form-data")
63 public Response createPerson(MultipartFormDataInput multipart) {
65 PersonNuxeo personPart = new PersonNuxeo();
66 DublincoreNuxeo dcPart = new DublincoreNuxeo();
67 CoreSession repoSession = null;
68 RepositoryInstance repo = null;
70 if(multipart.getFormData().containsKey("dublincore")){
71 dcPart = multipart.getFormDataPart("dublincore", DublincoreNuxeo.class, null);
73 if(multipart.getFormData().containsKey("hello")){
74 personPart = multipart.getFormDataPart("hello", PersonNuxeo.class, null);
77 repo = getRepository();
78 repoSession = repo.getSession();
79 DocumentRef nuxeoWspace = new IdRef(CS_PERSON_WORKSPACE_UID);
80 DocumentModel wspacePeople = repoSession.getDocument(nuxeoWspace);
81 String wspacePath = wspacePeople.getPathAsString();
82 String docType = "Hello";
83 String id = IdUtils.generateId("New " + docType);
84 //create document model
85 DocumentModel helloDoc = repoSession.createDocumentModel(wspacePath, id, docType);
86 fillDocument(personPart, helloDoc);
87 //create document with documentmodel
88 helloDoc = repoSession.createDocument(helloDoc);
91 personPart.setId(helloDoc.getId());
95 Response response = Response.status(Response.Status.NOT_FOUND).entity(
96 "Create failed").type("text/plain").build();
97 throw new WebApplicationException(response);
100 // repo.close(repoSession);
102 logger.error("Could not close the repository session", e);
103 throw new WebApplicationException();
106 if(logger.isDebugEnabled()){
107 verbosePerson("createPerson: person", personPart);
108 verboseDublin("createPerson: dublincore", dcPart);
110 UriBuilder path = UriBuilder.fromResource(MultischemaResource.class);
111 path.path("" + personPart.getId());
112 Response response = Response.created(path.build()).build();
118 @Produces("multipart/form-data")
119 public MultipartFormDataOutput getPerson(@PathParam("id") String id) {
121 PersonNuxeo personPart = new PersonNuxeo();
122 DublincoreNuxeo dublinPart = new DublincoreNuxeo();
123 MultipartFormDataOutput output = new MultipartFormDataOutput();
124 RepositoryInstance repo = null;
125 CoreSession repoSession = null;
128 repo = getRepository();
129 repoSession = repo.getSession();
130 DocumentRef helloDocRef = new IdRef(id);
131 DocumentModel helloDoc = repoSession.getDocument(helloDocRef);
132 Document doc = getDocument(repoSession, helloDoc);
133 Element root = doc.getRootElement();
134 //TODO: recognize schema thru namespace uri
135 //Namespace ns = new Namespace("hello", "http://collectionspace.org/hello");
136 Iterator<Element> siter = root.elementIterator("schema");
137 while(siter.hasNext()){
139 Element s = siter.next();
141 //TODO: recognize schema thru namespace uri
142 if("hello".equals(s.attribute("name").getValue())){
143 personPart.setId(id);
144 Element ele = s.element("cversion");
146 personPart.setVersion((String) ele.getData());
148 ele = s.element("firstName");
150 personPart.setFirstName((String) ele.getData());
152 ele = s.element("lastName");
154 personPart.setLastName((String) ele.getData());
156 ele = s.element("city");
158 personPart.setCity((String) ele.getData());
160 ele = s.element("state");
162 personPart.setState((String) ele.getData());
164 ele = s.element("zip");
166 personPart.setZip((String) ele.getData());
168 ele = s.element("country");
170 personPart.setCountry((String) ele.getData());
172 }else if("dublincore".equals(s.attribute("name").getValue())){
173 Element ele = s.element("title");
175 dublinPart.setTitle((String) ele.getData());
179 if(logger.isDebugEnabled()){
180 verbosePerson("getPerson:hello:", personPart);
181 verboseDublin("getPerson:dublincore:", dublinPart);
183 output.addFormData("hello", personPart, MediaType.APPLICATION_XML_TYPE);
184 output.addFormData("dublincore", dublinPart, MediaType.APPLICATION_XML_TYPE);
188 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
189 "Get failed").type("text/plain").build();
190 throw new WebApplicationException(response);
193 // repo.close(repoSession);
195 logger.error("Could not close the repository session", e);
196 throw new WebApplicationException();
199 if(personPart == null){
200 Response response = Response.status(Response.Status.NOT_FOUND).entity(
201 "Get failed, the requested person ID:" + id + ": was not found.").type("text/plain").build();
202 throw new WebApplicationException(response);
210 public PersonNuxeo updatePerson(
211 @PathParam("id") String id,
212 PersonNuxeo personPart) {
213 if(logger.isDebugEnabled()){
214 verbosePerson("updating person input", personPart);
216 CoreSession repoSession = null;
217 RepositoryInstance repo = null;
219 repo = getRepository();
220 repoSession = repo.getSession();
221 DocumentRef helloDocRef = new IdRef(id);
222 DocumentModel helloDoc = repoSession.getDocument(helloDocRef);
223 fillDocument(personPart, helloDoc);
224 repoSession.saveDocument(helloDoc);
228 Response response = Response.status(Response.Status.NOT_FOUND).entity(
229 "Update failed ").type("text/plain").build();
230 throw new WebApplicationException(response);
233 // repo.close(repoSession);
235 logger.error("Could not close the repository session", e);
236 throw new WebApplicationException();
244 public void deletePerson(@PathParam("id") String id) {
245 if(logger.isDebugEnabled()){
246 logger.debug("deleting person with id=" + id);
248 CoreSession repoSession = null;
249 RepositoryInstance repo = null;
251 repo = getRepository();
252 repoSession = repo.getSession();
253 DocumentRef helloDocRef = new IdRef(id);
254 repoSession.removeDocument(helloDocRef);
258 Response response = Response.status(Response.Status.NOT_FOUND).entity(
259 "Delete failed ").type("text/plain").build();
260 throw new WebApplicationException(response);
263 // repo.close(repoSession);
265 logger.error("Could not close the repository session", e);
266 throw new WebApplicationException();
272 synchronized private RepositoryInstance getRepository() throws Exception {
273 NuxeoConnector rmiClient = NuxeoConnector.getInstance();
274 rmiClient.initialize();
275 NuxeoClient client = rmiClient.getClient();
276 RepositoryInstance repo = client.openRepository();
277 if(logger.isDebugEnabled()){
278 logger.debug("deployNuxeo: repository root: " + repo.getRootDocument());
283 private Document getDocument(CoreSession repoSession, DocumentModel helloDoc)
286 DocumentWriter writer = null;
287 DocumentReader reader = null;
288 ByteArrayOutputStream baos = null;
289 ByteArrayInputStream bais = null;
291 baos = new ByteArrayOutputStream();
292 reader = new SingleDocumentReader(repoSession,
294 writer = new XMLDocumentWriter(baos);
295 DocumentPipe pipe = new DocumentPipeImpl();
297 pipe.setReader(reader);
298 pipe.setWriter(writer);
300 bais = new ByteArrayInputStream(baos.toByteArray());
301 SAXReader saxReader = new SAXReader();
302 doc = saxReader.read(bais);
317 }catch(IOException ioe){
318 logger.error("Failed to close io streams with {}", ioe);
319 throw new WebApplicationException();
325 private void fillDocument(PersonNuxeo p, DocumentModel helloDoc) throws Exception {
326 if(p.getFirstName() != null){
327 helloDoc.setPropertyValue("dublincore:title", p.getFirstName() + " " + p.getLastName());
328 helloDoc.setPropertyValue("hello:firstName", p.getFirstName());
330 if(p.getLastName() != null){
331 helloDoc.setPropertyValue("hello:lastName", p.getLastName());
333 if(p.getStreet() != null){
334 helloDoc.setPropertyValue("hello:street", p.getStreet());
336 if(p.getCity() != null){
337 helloDoc.setPropertyValue("hello:city", p.getCity());
339 if(p.getState() != null){
340 helloDoc.setPropertyValue("hello:state", p.getState());
342 if(p.getZip() != null){
343 helloDoc.setPropertyValue("hello:zip", p.getZip());
345 if(p.getCountry() != null){
346 helloDoc.setPropertyValue("hello:country", p.getCountry());
350 private void verbosePerson(String msg, PersonNuxeo person) {
352 if(logger.isDebugEnabled()){
355 JAXBContext jc = JAXBContext.newInstance(
358 Marshaller m = jc.createMarshaller();
359 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
361 m.marshal(person, System.out);
368 private void verboseDublin(String msg, DublincoreNuxeo dubin) {
370 if(logger.isDebugEnabled()){
373 JAXBContext jc = JAXBContext.newInstance(
374 DublincoreNuxeo.class);
376 Marshaller m = jc.createMarshaller();
377 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
379 m.marshal(dubin, System.out);