]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7afccdf92c8b8f5b077e7a8cbf5bb4a58abf1682
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.services;
2
3 import org.collectionspace.hello.entity.Person;
4
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;
14 import java.util.Map;
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;
20
21 @Path("/persons")
22 @Consumes("application/xml")
23 @Produces("application/xml")
24 public class PersonResource {
25
26     private Map<Integer, Person> personDB = new ConcurrentHashMap<Integer, Person>();
27     private AtomicInteger idCounter = new AtomicInteger();
28
29     public PersonResource() {
30     }
31
32     @POST
33     public Response createPerson(Person p) {
34         p.setId(idCounter.incrementAndGet());
35         p.setVersion(1);
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();
41         return response;
42     }
43
44     @GET
45     @Path("{id}")
46     public Person getPerson(@PathParam("id") int id) {
47         Person p = personDB.get(id);
48         if (p == null) {
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);
52         }
53         verbose("get person", p);
54         return p;
55     }
56
57     @PUT
58     @Path("{id}")
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);
63         }
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);
74         return current;
75     }
76
77     private void verbose(String msg, Person p) {
78         try {
79             System.out.println(msg);
80             JAXBContext jc = JAXBContext.newInstance(Person.class);
81             Marshaller m = jc.createMarshaller();
82             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
83                     Boolean.TRUE);
84             m.marshal(p, System.out);
85         } catch (Exception e) {
86             e.printStackTrace();
87         }
88     }
89
90 //    @POST
91 //    @Consumes("application/xml")
92 //    public Response createPerson(InputStream is) {
93 //        Person p = readPerson(is);
94 //        p.setId(idCounter.incrementAndGet());
95 //        p.setVersion(1);
96 //        personDB.put(p.getId(), p);
97 //        try {
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);
102 //        }
103 //        return Response.created(URI.create("/persons/" + p.getId())).build();
104 //
105 //    }
106 //
107 //    @GET
108 //    @Path("{id}")
109 //    @Produces("application/xml")
110 //    public StreamingOutput getPerson(@PathParam("id") int id) {
111 //        final Person p = personDB.get(id);
112 //        if (p == null) {
113 //            throw new WebApplicationException(Response.Status.NOT_FOUND);
114 //        }
115 //        return new StreamingOutput() {
116 //
117 //            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
118 //                outputPerson(outputStream, p);
119 //            }
120 //        };
121 //    }
122 //
123 //    @PUT
124 //    @Path("{id}")
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);
132 //        }
133 //
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() {
143 //
144 //            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
145 //                outputPerson(outputStream, scurrent);
146 //            }
147 //        };
148 //    }
149 //
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>");
161 //    }
162 //
163 //    protected Person readPerson(InputStream is) {
164 //        try {
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")));
171 //            }
172 //            if (root.getAttribute("version") != null && !root.getAttribute("version").trim().equals("")) {
173 //                p.setVersion(Integer.valueOf(root.getAttribute("version")));
174 //            }
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());
192 //                }
193 //            }
194 //            return p;
195 //        } catch (Exception e) {
196 //            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
197 //        }
198 //    }
199 }