]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9ddf5965aaa78e0af4b115aa1534af8d333d6aed
[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
19 import org.collectionspace.hello.DomainIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 @Path("/domainidentifiers")
24 @Consumes("application/xml")
25 @Produces("application/xml")
26 public class DomainIdentifierResource extends CollectionSpaceResource {
27
28     final Logger logger = LoggerFactory.getLogger(DomainIdentifierResource.class);
29     private Map<String, DomainIdentifier> idDB = new ConcurrentHashMap<String, DomainIdentifier>();
30
31     public DomainIdentifierResource() {
32         // do nothing
33     }
34
35     @POST
36     public Response createIdentifier(DomainIdentifier id) {
37         DomainIdentifier newId = id;
38         
39         if (newId.getDsid() == null) {
40                 newId.setDsid("org.collectionspace");
41         }
42         newId.setDsid(newId.getDsid() + "." + System.currentTimeMillis());
43         idDB.put(newId.getDsid(), newId);
44         
45         verbose("createIdentifier: ", newId);
46         UriBuilder path = UriBuilder.fromResource(DomainIdentifierResource.class);
47         path.path("" + newId.getDsid());
48         Response response = Response.created(path.build()).build();
49         
50         return response;
51     }
52
53     @GET
54     @Path("{id}")
55     public DomainIdentifier getIdentifier(@PathParam("id") String id) {
56         DomainIdentifier result = idDB.get(id);
57         if (result == null) {
58             Response response = Response.status(Response.Status.NOT_FOUND).entity(
59                     "The requested DomainIdentifier was not found.").type("text/plain").build();
60             throw new WebApplicationException(response);
61         }
62         verbose("getIdentifier: ", result);
63         
64         return result;
65     }
66
67     private void verbose(String msg, DomainIdentifier id) {
68         try {
69             System.out.println("DomainIdentifierResource: " + msg);
70             JAXBContext jc = JAXBContext.newInstance(DomainIdentifier.class);
71             Marshaller m = jc.createMarshaller();
72             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
73                     Boolean.TRUE);
74             m.marshal(id, System.out);
75
76         } catch (Exception e) {
77             e.printStackTrace();
78         }
79     }
80 }