]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
945b87e7da565a4bb990a45362e90931a17a10fa
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.services;
2
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;
12 import java.util.Map;
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;
21
22 @Path("/persons")
23 @Consumes("application/xml")
24 @Produces("application/xml")
25 public class PersonResource {
26
27     final Logger logger = LoggerFactory.getLogger(PersonResource.class);
28     private Map<Long, Person> personDB = new ConcurrentHashMap<Long, Person>();
29     private AtomicLong idCounter = new AtomicLong();
30
31     public PersonResource() {
32     }
33
34     @POST
35     public Response createPerson(Person p) {
36         p.setId(idCounter.incrementAndGet());
37         p.setVersion(1);
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();
43         return response;
44     }
45
46     @GET
47     @Path("{id}")
48     public Person getPerson(@PathParam("id") Long id) {
49         Person p = personDB.get(id);
50         if (p == null) {
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);
54         }
55         verbose("get person", p);
56         return p;
57     }
58
59     @PUT
60     @Path("{id}")
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);
67         }
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);
78         return current;
79     }
80
81     private void verbose(String msg, Person p) {
82         try {
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,
87                     Boolean.TRUE);
88             m.marshal(p, System.out);
89         } catch (Exception e) {
90             e.printStackTrace();
91         }
92     }
93
94 //    @POST
95 //    @Consumes("application/xml")
96 //    public Response createPerson(InputStream is) {
97 //        Person p = readPerson(is);
98 //        p.setId(idCounter.incrementAndGet());
99 //        p.setVersion(1);
100 //        personDB.put(p.getId(), p);
101 //        try {
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);
106 //        }
107 //        return Response.created(URI.create("/persons/" + p.getId())).build();
108 //
109 //    }
110 //
111 //    @GET
112 //    @Path("{id}")
113 //    @Produces("application/xml")
114 //    public StreamingOutput getPerson(@PathParam("id") int id) {
115 //        final Person p = personDB.get(id);
116 //        if (p == null) {
117 //            throw new WebApplicationException(Response.Status.NOT_FOUND);
118 //        }
119 //        return new StreamingOutput() {
120 //
121 //            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
122 //                outputPerson(outputStream, p);
123 //            }
124 //        };
125 //    }
126 //
127 //    @PUT
128 //    @Path("{id}")
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);
136 //        }
137 //
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() {
147 //
148 //            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
149 //                outputPerson(outputStream, scurrent);
150 //            }
151 //        };
152 //    }
153 //
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>");
165 //    }
166 //
167 //    protected Person readPerson(InputStream is) {
168 //        try {
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")));
175 //            }
176 //            if (root.getAttribute("version") != null && !root.getAttribute("version").trim().equals("")) {
177 //                p.setVersion(Integer.valueOf(root.getAttribute("version")));
178 //            }
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());
196 //                }
197 //            }
198 //            return p;
199 //        } catch (Exception e) {
200 //            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
201 //        }
202 //    }
203 }