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() {
35 public Response createIdentifier(Identifier id) {
36 if (id.getNamespace() == null) {
37 id.setNamespace("edu.berkeley");
39 id.setId(idCounter.incrementAndGet());
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();
53 public Identifier getIdentifier(@PathParam("id") Long id) {
54 Identifier i = idDB.get(id);
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);
64 private void verbose(String msg, Identifier id) {
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,
71 m.marshal(id, System.out);
73 } catch (Exception e) {