1 package org.collectionspace.hello.services;
3 import org.collectionspace.hello.entity.Person;
5 import javax.ws.rs.Consumes;
6 import javax.ws.rs.GET;
7 import javax.ws.rs.POST;
8 import javax.ws.rs.PUT;
9 import javax.ws.rs.Path;
10 import javax.ws.rs.PathParam;
11 import javax.ws.rs.Produces;
12 import javax.ws.rs.WebApplicationException;
13 import javax.ws.rs.core.Response;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.atomic.AtomicInteger;
17 import javax.ws.rs.core.UriBuilder;
18 import javax.xml.bind.JAXBContext;
19 import javax.xml.bind.Marshaller;
22 @Consumes("application/xml")
23 @Produces("application/xml")
24 public class PersonResource {
26 private Map<Integer, Person> personDB = new ConcurrentHashMap<Integer, Person>();
27 private AtomicInteger idCounter = new AtomicInteger();
29 public PersonResource() {
33 public Response createPerson(Person p) {
34 p.setId(idCounter.incrementAndGet());
36 personDB.put(p.getId(), p);
37 verbose("create person", p);
38 UriBuilder path = UriBuilder.fromResource(PersonResource.class);
39 path.path("" + p.getId());
40 Response response = Response.created(path.build()).build();
46 public Person getPerson(@PathParam("id") int id) {
47 Person p = personDB.get(id);
49 Response response = Response.status(Response.Status.NOT_FOUND).entity(
50 "The requested ID was not found.").type("text/plain").build();
51 throw new WebApplicationException(response);
53 verbose("get person", p);
59 public Person updatePerson(@PathParam("id") int id, Person update) {
60 Person current = personDB.get(id);
61 if (current == null) {
62 throw new WebApplicationException(Response.Status.NOT_FOUND);
64 verbose("received person", update);
65 //todo: intelligent merge needed
66 current.setFirstName(update.getFirstName());
67 current.setLastName(update.getLastName());
68 current.setStreet(update.getStreet());
69 current.setState(update.getState());
70 current.setZip(update.getZip());
71 current.setCountry(update.getCountry());
72 current.setVersion(current.getVersion() + 1);
73 verbose("updated person", current);
77 private void verbose(String msg, Person p) {
79 System.out.println(msg);
80 JAXBContext jc = JAXBContext.newInstance(Person.class);
81 Marshaller m = jc.createMarshaller();
82 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
84 m.marshal(p, System.out);
85 } catch (Exception e) {
91 // @Consumes("application/xml")
92 // public Response createPerson(InputStream is) {
93 // Person p = readPerson(is);
94 // p.setId(idCounter.incrementAndGet());
96 // personDB.put(p.getId(), p);
98 // System.out.println("Created Person " + p.getId());
99 // outputPerson(System.out, p);
100 // } catch (IOException ioe) {
101 // throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
103 // return Response.created(URI.create("/persons/" + p.getId())).build();
109 // @Produces("application/xml")
110 // public StreamingOutput getPerson(@PathParam("id") int id) {
111 // final Person p = personDB.get(id);
113 // throw new WebApplicationException(Response.Status.NOT_FOUND);
115 // return new StreamingOutput() {
117 // public void write(OutputStream outputStream) throws IOException, WebApplicationException {
118 // outputPerson(outputStream, p);
125 // @Consumes("application/xml")
126 // @Produces("application/xml")
127 // public StreamingOutput updatePerson(@PathParam("id") int id, InputStream is) {
128 // Person update = readPerson(is);
129 // Person current = personDB.get(id);
130 // if (current == null) {
131 // throw new WebApplicationException(Response.Status.NOT_FOUND);
134 // current.setFirstName(update.getFirstName());
135 // current.setLastName(update.getLastName());
136 // current.setStreet(update.getStreet());
137 // current.setState(update.getState());
138 // current.setZip(update.getZip());
139 // current.setCountry(update.getCountry());
140 // current.setVersion(current.getVersion() + 1);
141 // final Person scurrent = current;
142 // return new StreamingOutput() {
144 // public void write(OutputStream outputStream) throws IOException, WebApplicationException {
145 // outputPerson(outputStream, scurrent);
150 // protected void outputPerson(OutputStream os, Person p) throws IOException {
151 // PrintStream writer = new PrintStream(os);
152 // writer.println("<Person id=\"" + p.getId() + "\" version=\"" + p.getVersion() + "\">");
153 // writer.println(" <first-name>" + p.getFirstName() + "</first-name>");
154 // writer.println(" <last-name>" + p.getLastName() + "</last-name>");
155 // writer.println(" <street>" + p.getStreet() + "</street>");
156 // writer.println(" <city>" + p.getCity() + "</city>");
157 // writer.println(" <state>" + p.getState() + "</state>");
158 // writer.println(" <zip>" + p.getZip() + "</zip>");
159 // writer.println(" <country>" + p.getCountry() + "</country>");
160 // writer.println("</Person>");
163 // protected Person readPerson(InputStream is) {
165 // DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
166 // Document doc = builder.parse(is);
167 // Element root = doc.getDocumentElement();
168 // Person p = new Person();
169 // if (root.getAttribute("id") != null && !root.getAttribute("id").trim().equals("")) {
170 // p.setId(Integer.valueOf(root.getAttribute("id")));
172 // if (root.getAttribute("version") != null && !root.getAttribute("version").trim().equals("")) {
173 // p.setVersion(Integer.valueOf(root.getAttribute("version")));
175 // NodeList nodes = root.getChildNodes();
176 // for (int i = 0; i < nodes.getLength(); i++) {
177 // Element element = (Element) nodes.item(i);
178 // if (element.getTagName().equals("first-name")) {
179 // p.setFirstName(element.getTextContent());
180 // } else if (element.getTagName().equals("last-name")) {
181 // p.setLastName(element.getTextContent());
182 // } else if (element.getTagName().equals("street")) {
183 // p.setStreet(element.getTextContent());
184 // } else if (element.getTagName().equals("city")) {
185 // p.setCity(element.getTextContent());
186 // } else if (element.getTagName().equals("state")) {
187 // p.setState(element.getTextContent());
188 // } else if (element.getTagName().equals("zip")) {
189 // p.setZip(element.getTextContent());
190 // } else if (element.getTagName().equals("country")) {
191 // p.setCountry(element.getTextContent());
195 // } catch (Exception e) {
196 // throw new WebApplicationException(e, Response.Status.BAD_REQUEST);