1 package org.collectionspace.hello.services;
3 import javax.ws.rs.Consumes;
4 import javax.ws.rs.GET;
5 import javax.ws.rs.POST;
6 import javax.ws.rs.PUT;
7 import javax.ws.rs.Path;
8 import javax.ws.rs.PathParam;
9 import javax.ws.rs.Produces;
10 import javax.ws.rs.WebApplicationException;
11 import javax.ws.rs.core.Response;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.atomic.AtomicLong;
15 import javax.ws.rs.core.UriBuilder;
16 import javax.xml.bind.JAXBContext;
17 import javax.xml.bind.Marshaller;
18 import org.collectionspace.hello.Person;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 @Consumes("application/xml")
24 @Produces("application/xml")
25 public class PersonResource {
27 final Logger logger = LoggerFactory.getLogger(PersonResource.class);
28 private Map<Long, Person> personDB = new ConcurrentHashMap<Long, Person>();
29 private AtomicLong idCounter = new AtomicLong();
31 public PersonResource() {
35 public Response createPerson(Person p) {
36 p.setId(idCounter.incrementAndGet());
38 personDB.put(p.getId(), p);
39 verbose("created person", p);
40 UriBuilder path = UriBuilder.fromResource(PersonResource.class);
41 path.path("" + p.getId());
42 Response response = Response.created(path.build()).build();
48 public Person getPerson(@PathParam("id") Long id) {
49 Person p = personDB.get(id);
51 Response response = Response.status(Response.Status.NOT_FOUND).entity(
52 "Get failed, the requested person ID:" + id + ": was not found.").type("text/plain").build();
53 throw new WebApplicationException(response);
55 verbose("get person", p);
61 public Person updatePerson(@PathParam("id") Long id, Person update) {
62 Person current = personDB.get(id);
63 if (current == null) {
64 Response response = Response.status(Response.Status.NOT_FOUND).entity(
65 "Update failed, the person ID:" + id + ": was not found.").type("text/plain").build();
66 throw new WebApplicationException(response);
68 verbose("update person input", update);
69 //todo: intelligent merge needed
70 current.setFirstName(update.getFirstName());
71 current.setLastName(update.getLastName());
72 current.setStreet(update.getStreet());
73 current.setState(update.getState());
74 current.setZip(update.getZip());
75 current.setCountry(update.getCountry());
76 current.setVersion(current.getVersion() + 1);
77 verbose("update person output", current);
81 private void verbose(String msg, Person p) {
83 System.out.println("PersonResource : " + msg);
84 JAXBContext jc = JAXBContext.newInstance(Person.class);
85 Marshaller m = jc.createMarshaller();
86 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
88 m.marshal(p, System.out);
89 } catch (Exception e) {
95 // @Consumes("application/xml")
96 // public Response createPerson(InputStream is) {
97 // Person p = readPerson(is);
98 // p.setId(idCounter.incrementAndGet());
100 // personDB.put(p.getId(), p);
102 // System.out.println("Created Person " + p.getId());
103 // outputPerson(System.out, p);
104 // } catch (IOException ioe) {
105 // throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
107 // return Response.created(URI.create("/persons/" + p.getId())).build();
113 // @Produces("application/xml")
114 // public StreamingOutput getPerson(@PathParam("id") int id) {
115 // final Person p = personDB.get(id);
117 // throw new WebApplicationException(Response.Status.NOT_FOUND);
119 // return new StreamingOutput() {
121 // public void write(OutputStream outputStream) throws IOException, WebApplicationException {
122 // outputPerson(outputStream, p);
129 // @Consumes("application/xml")
130 // @Produces("application/xml")
131 // public StreamingOutput updatePerson(@PathParam("id") int id, InputStream is) {
132 // Person update = readPerson(is);
133 // Person current = personDB.get(id);
134 // if (current == null) {
135 // throw new WebApplicationException(Response.Status.NOT_FOUND);
138 // current.setFirstName(update.getFirstName());
139 // current.setLastName(update.getLastName());
140 // current.setStreet(update.getStreet());
141 // current.setState(update.getState());
142 // current.setZip(update.getZip());
143 // current.setCountry(update.getCountry());
144 // current.setVersion(current.getVersion() + 1);
145 // final Person scurrent = current;
146 // return new StreamingOutput() {
148 // public void write(OutputStream outputStream) throws IOException, WebApplicationException {
149 // outputPerson(outputStream, scurrent);
154 // protected void outputPerson(OutputStream os, Person p) throws IOException {
155 // PrintStream writer = new PrintStream(os);
156 // writer.println("<Person id=\"" + p.getId() + "\" version=\"" + p.getVersion() + "\">");
157 // writer.println(" <first-name>" + p.getFirstName() + "</first-name>");
158 // writer.println(" <last-name>" + p.getLastName() + "</last-name>");
159 // writer.println(" <street>" + p.getStreet() + "</street>");
160 // writer.println(" <city>" + p.getCity() + "</city>");
161 // writer.println(" <state>" + p.getState() + "</state>");
162 // writer.println(" <zip>" + p.getZip() + "</zip>");
163 // writer.println(" <country>" + p.getCountry() + "</country>");
164 // writer.println("</Person>");
167 // protected Person readPerson(InputStream is) {
169 // DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
170 // Document doc = builder.parse(is);
171 // Element root = doc.getDocumentElement();
172 // Person p = new Person();
173 // if (root.getAttribute("id") != null && !root.getAttribute("id").trim().equals("")) {
174 // p.setId(Integer.valueOf(root.getAttribute("id")));
176 // if (root.getAttribute("version") != null && !root.getAttribute("version").trim().equals("")) {
177 // p.setVersion(Integer.valueOf(root.getAttribute("version")));
179 // NodeList nodes = root.getChildNodes();
180 // for (int i = 0; i < nodes.getLength(); i++) {
181 // Element element = (Element) nodes.item(i);
182 // if (element.getTagName().equals("first-name")) {
183 // p.setFirstName(element.getTextContent());
184 // } else if (element.getTagName().equals("last-name")) {
185 // p.setLastName(element.getTextContent());
186 // } else if (element.getTagName().equals("street")) {
187 // p.setStreet(element.getTextContent());
188 // } else if (element.getTagName().equals("city")) {
189 // p.setCity(element.getTextContent());
190 // } else if (element.getTagName().equals("state")) {
191 // p.setState(element.getTextContent());
192 // } else if (element.getTagName().equals("zip")) {
193 // p.setZip(element.getTextContent());
194 // } else if (element.getTagName().equals("country")) {
195 // p.setCountry(element.getTextContent());
199 // } catch (Exception e) {
200 // throw new WebApplicationException(e, Response.Status.BAD_REQUEST);