]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4c41048907f51ef1f2fbb1d3a8e22c8f6cdb43cc
[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.Path;
7 import javax.ws.rs.PathParam;
8 import javax.ws.rs.Produces;
9 import javax.ws.rs.WebApplicationException;
10 import javax.ws.rs.core.Response;
11 import java.util.Map;
12 import java.util.UUID;
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.Identifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 @Path("/identifiers")
23 @Consumes("application/xml")
24 @Produces("application/xml")
25 public class IdentifierResource {
26
27     final Logger logger = LoggerFactory.getLogger(IdentifierResource.class);
28     private Map<Long, Identifier> idDB = new ConcurrentHashMap<Long, Identifier>();
29     private AtomicLong idCounter = new AtomicLong();
30
31     public IdentifierResource() {
32     }
33
34     @POST
35     public Response createIdentifier(Identifier id) {
36         if (id.getNamespace() == null) {
37             id.setNamespace("edu.berkeley");
38         }
39         id.setId(idCounter.incrementAndGet());
40         id.setVersion(1);
41         UUID uuid = UUID.nameUUIDFromBytes(id.getNamespace().getBytes());
42         id.setValue(uuid.toString());
43         idDB.put(id.getId(), id);
44         verbose("created Id", id);
45         UriBuilder path = UriBuilder.fromResource(IdentifierResource.class);
46         path.path("" + id.getId());
47         Response response = Response.created(path.build()).build();
48         return response;
49     }
50
51     @GET
52     @Path("{id}")
53     public Identifier getIdentifier(@PathParam("id") Long id) {
54         Identifier i = idDB.get(id);
55         if (i == null) {
56             Response response = Response.status(Response.Status.NOT_FOUND).entity(
57                     "The requested ID was not found.").type("text/plain").build();
58             throw new WebApplicationException(response);
59         }
60         verbose("get Id", i);
61         return i;
62     }
63
64     private void verbose(String msg, Identifier id) {
65         try {
66             System.out.println("IdentifierResource : " + msg);
67             JAXBContext jc = JAXBContext.newInstance(Identifier.class);
68             Marshaller m = jc.createMarshaller();
69             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
70                     Boolean.TRUE);
71             m.marshal(id, System.out);
72
73         } catch (Exception e) {
74             e.printStackTrace();
75         }
76     }
77 }