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.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;
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;
23 @Consumes("application/xml")
24 @Produces("application/xml")
25 public class IdentifierResource {
27 final Logger logger = LoggerFactory.getLogger(IdentifierResource.class);
28 private Map<Long, Identifier> idDB = new ConcurrentHashMap<Long, Identifier>();
29 private AtomicLong idCounter = new AtomicLong();
31 public IdentifierResource() {
36 public Response createIdentifier(Identifier id) {
37 if (id.getNamespace() == null) {
38 id.setNamespace("edu.berkeley");
40 id.setId(idCounter.incrementAndGet());
42 UUID uuid = UUID.nameUUIDFromBytes(id.getNamespace().getBytes());
43 id.setValue(uuid.toString());
44 idDB.put(id.getId(), id);
45 verbose("created Id", id);
46 UriBuilder path = UriBuilder.fromResource(IdentifierResource.class);
47 path.path("" + id.getId());
48 Response response = Response.created(path.build()).build();
54 public Identifier getIdentifier(@PathParam("id") Long id) {
55 Identifier i = idDB.get(id);
57 Response response = Response.status(Response.Status.NOT_FOUND).entity(
58 "The requested ID was not found.").type("text/plain").build();
59 throw new WebApplicationException(response);
65 private void verbose(String msg, Identifier id) {
67 System.out.println("IdentifierResource : " + msg);
68 JAXBContext jc = JAXBContext.newInstance(Identifier.class);
69 Marshaller m = jc.createMarshaller();
70 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
72 m.marshal(id, System.out);
74 } catch (Exception e) {