]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-14, created new resource - Identifier. service providers use jaxb as well...
authorSanjay Dalal <sanjay.dalal@berkeley.edu>
Thu, 5 Mar 2009 23:31:21 +0000 (23:31 +0000)
committerSanjay Dalal <sanjay.dalal@berkeley.edu>
Thu, 5 Mar 2009 23:31:21 +0000 (23:31 +0000)
19 files changed:
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/pom.xml
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java [new file with mode: 0644]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierProxy.java [new file with mode: 0644]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/PersonClient.java
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/PersonProxy.java
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/IdentifierServiceTest.java [new file with mode: 0644]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/PersonServiceTest.java [moved from sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/PersonServiceTest.java with 89% similarity]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldJaxb/pom.xml
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldJaxb/src/main/resources/hello.xsd [new file with mode: 0644]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldJaxb/src/main/resources/person.xsd [deleted file]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/nbactions.xml
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/pom.xml
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/entity/Person.java [deleted file]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/services/HelloworldApplication.java
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java [new file with mode: 0644]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/services/PersonResource.java
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/test/java/org/collectionspace/hello/test/IdentifierResourceTest.java [new file with mode: 0644]
sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/test/java/org/collectionspace/hello/test/PersonResourceTest.java
sandbox/sanjay/prototypes/restws/HelloWorld/pom.xml

index 080bb2401f736a82cfb639d4cbaad04d3947fc16..24500e86cd79af54d7b12d29ecd1e45196e52254 100644 (file)
@@ -6,14 +6,14 @@
         <version>0.1</version>\r
     </parent>\r
     <modelVersion>4.0.0</modelVersion>\r
-    <groupId>org.collectionspace.services.hello</groupId>\r
+    <groupId>org.collectionspace.hello.services</groupId>\r
     <artifactId>helloworld-client</artifactId>\r
     <packaging>jar</packaging>\r
     <version>0.1</version>\r
     <name>Helloworld Client</name>\r
     <dependencies>\r
         <dependency>\r
-            <groupId>org.collectionspace.services.hello</groupId>\r
+            <groupId>org.collectionspace.hello.services</groupId>\r
             <artifactId>helloworld-jaxb</artifactId>\r
             <version>0.1</version>\r
         </dependency>\r
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java
new file mode 100644 (file)
index 0000000..a7b12f9
--- /dev/null
@@ -0,0 +1,64 @@
+package org.collectionspace.hello.client;
+
+import javax.ws.rs.core.Response;
+
+import org.collectionspace.hello.Identifier;
+import org.jboss.resteasy.client.ProxyFactory;
+import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
+import org.jboss.resteasy.client.ClientResponse;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
+
+/**
+ * A IdentifierClient.
+
+ * @version $Revision:$
+ */
+public class IdentifierClient {
+
+    /**
+     *
+     */
+    private static final IdentifierClient instance = new IdentifierClient();
+    /**
+     *
+     */
+    private IdentifierProxy identifierProxy;
+
+    /**
+     *
+     * Create a new IdentifierClient.
+     *
+     */
+    private IdentifierClient() {
+        ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
+        RegisterBuiltin.register(factory);
+        identifierProxy = ProxyFactory.create(IdentifierProxy.class, "http://localhost:8080/helloworld/cspace");
+    }
+
+    /**
+     * FIXME Comment this
+     *
+     * @return
+     */
+    public static IdentifierClient getInstance() {
+        return instance;
+    }
+
+    /**
+     * @param id
+     * @return
+     * @see org.collectionspace.hello.client.IdentifierProxy#getIdentifier(java.lang.Long)
+     */
+    public ClientResponse<Identifier> getIdentifier(Long id) {
+        return identifierProxy.getIdentifier(id);
+    }
+
+    /**
+     * @param identifier
+     * @return
+     * @see org.collectionspace.hello.client.IdentifierProxy#createIdentifier(org.collectionspace.hello.client.entity.Identifier)
+     */
+    public ClientResponse<Response> createIdentifier(Identifier identifier) {
+        return identifierProxy.createIdentifier(identifier);
+    }
+}
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierProxy.java b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierProxy.java
new file mode 100644 (file)
index 0000000..125aab4
--- /dev/null
@@ -0,0 +1,32 @@
+package org.collectionspace.hello.client;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+
+import org.collectionspace.hello.Identifier;
+import org.jboss.resteasy.client.ClientResponse;
+
+/**
+ * @version $Revision:$
+ */
+@Path("/identifiers/")
+@Produces({"application/xml"})
+@Consumes({"application/xml"})
+public interface IdentifierProxy {
+
+    /**
+     * @param id
+     * @return
+     */
+    @GET
+    @Path("/{id}")
+    ClientResponse<Identifier> getIdentifier(@PathParam("id") Long id);
+
+    @POST
+    ClientResponse<Response> createIdentifier(Identifier so);
+}
\ No newline at end of file
index 3f05d6a74205cdd7a32242b221284ca5c35533a6..8f8653c06528341e5fd07c3ae4a71cf916e464e6 100644 (file)
@@ -3,6 +3,7 @@ package org.collectionspace.hello.client;
 
 import javax.ws.rs.core.Response;
 
+import org.collectionspace.hello.Person;
 import org.jboss.resteasy.client.ProxyFactory;
 import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
 import org.jboss.resteasy.client.ClientResponse;
index 668c1f7fca3faf37c7aeab132b34db74b1e2faa1..69c762a605e36f8a949c0876d9befb54fcc891d2 100644 (file)
@@ -1,7 +1,6 @@
 package org.collectionspace.hello.client;
 
 import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
@@ -10,6 +9,7 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
 
+import org.collectionspace.hello.Person;
 import org.jboss.resteasy.client.ClientResponse;
 
 /**
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/IdentifierServiceTest.java b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/IdentifierServiceTest.java
new file mode 100644 (file)
index 0000000..f050329
--- /dev/null
@@ -0,0 +1,60 @@
+package org.collectionspace.hello.client.test;
+
+import org.collectionspace.hello.client.*;
+import java.util.ArrayList;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import org.collectionspace.hello.Identifier;
+import org.jboss.resteasy.client.ClientResponse;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * A IdentifierServiceTest.
+ * 
+ * @version $Revision:$
+ */
+public class IdentifierServiceTest {
+
+    private IdentifierClient identifierClient = IdentifierClient.getInstance();
+    private Long id = 0L;
+
+    @Test
+    public void createIdentifier() {
+        Identifier identifier = new Identifier();
+        identifier.setNamespace("org.bnhm");
+        ClientResponse<Response> res = identifierClient.createIdentifier(identifier);
+        Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
+        id = extractId(res);
+    }
+
+    @Test(dependsOnMethods = {"createIdentifier"})
+    public void getIdentifier() {
+        Identifier i = identifierClient.getIdentifier(id).getEntity();
+        verbose("received with get", i);
+    }
+
+    private Long extractId(ClientResponse<Response> res) {
+        MultivaluedMap mvm = res.getMetadata();
+        String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
+        String[] segments = uri.split("/");
+        System.out.println("id=" + segments[segments.length - 1]);
+        return Long.valueOf(segments[segments.length - 1]);
+    }
+
+    private void verbose(String msg, Identifier p) {
+        try {
+            System.out.println(msg);
+            JAXBContext jc = JAXBContext.newInstance(Identifier.class);
+            Marshaller m = jc.createMarshaller();
+            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
+                    Boolean.TRUE);
+            m.marshal(p, System.out);
+            //m.marshal(new JAXBElement(new QName("uri", "local"), Identifier.class, p), System.out);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}
@@ -1,13 +1,12 @@
-package org.collectionspace.hello.client;
+package org.collectionspace.hello.client.test;
 
 import java.util.ArrayList;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
 import javax.xml.bind.Marshaller;
-import javax.xml.namespace.QName;
-import org.collectionspace.hello.client.*;
+import org.collectionspace.hello.Person;
+import org.collectionspace.hello.client.PersonClient;
 import org.jboss.resteasy.client.ClientResponse;
 import org.testng.Assert;
 import org.testng.annotations.Test;
@@ -65,7 +64,8 @@ public class PersonServiceTest {
             Marshaller m = jc.createMarshaller();
             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                     Boolean.TRUE);
-            m.marshal(new JAXBElement(new QName("uri", "local"), Person.class, p), System.out);
+            m.marshal(p, System.out);
+            //m.marshal(new JAXBElement(new QName("uri", "local"), Person.class, p), System.out);
         } catch (Exception e) {
             e.printStackTrace();
         }
index f73ba8dd25a047c56cf61f1a02882cc6a0edd8fa..8f0e2a1029362ea05ed3c31aaf05ac4ebcb64df0 100644 (file)
@@ -1,15 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project>
-   <parent>
-      <artifactId>helloworld</artifactId>
-      <groupId>org.collectionspace.hello.services</groupId>
-      <version>0.1</version>
-   </parent>
+    <parent>
+        <artifactId>helloworld</artifactId>
+        <groupId>org.collectionspace.hello.services</groupId>
+        <version>0.1</version>
+    </parent>
     <modelVersion>4.0.0</modelVersion>
-    <groupId>org.collectionspace.services.hello</groupId>
+    <groupId>org.collectionspace.hello.services</groupId>
     <artifactId>helloworld-jaxb</artifactId>
-    <name>Helloworld Client JAXB</name>
     <version>0.1</version>
+    <name>Helloworld Client JAXB</name>
     <dependencies>
         <dependency>
             <groupId>com.sun.xml.bind</groupId>
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldJaxb/src/main/resources/hello.xsd b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldJaxb/src/main/resources/hello.xsd
new file mode 100644 (file)
index 0000000..1b0a3b8
--- /dev/null
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<xs:schema 
+  xmlns:xs="http://www.w3.org/2001/XMLSchema"
+  xmlns:ns="http://collectionspace.org/hello"
+  xmlns="http://collectionspace.org/hello"
+  targetNamespace="http://collectionspace.org/hello"
+  version="0.1"
+>
+
+<!-- avoid XmlRootElement nightnmare, see http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html-->
+    <xs:element name="person">
+        <xs:complexType>
+
+            <xs:sequence>
+                <xs:element name="firstName" type="xs:string"
+                                               minOccurs="1" />
+                <xs:element name="lastName" type="xs:string"
+                                               minOccurs="1" />
+                <xs:element name="street" type="xs:string"
+                                               minOccurs="1" />
+                <xs:element name="city" type="xs:string"
+                                               minOccurs="1" />
+                <xs:element name="state" type="xs:string"
+                                               minOccurs="1" />
+                <xs:element name="zip" type="xs:string"
+                                               minOccurs="1" />
+                <xs:element name="country" type="xs:string"
+                                               minOccurs="1" />
+            </xs:sequence>
+            <xs:attribute name="id" type="xs:long" />
+            <xs:attribute name="version" type="xs:int" />
+
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="identifier">
+        <xs:complexType>
+
+            <xs:sequence>
+                <xs:element name="namespace" type="xs:string"
+                                               minOccurs="1" />
+                <xs:element name="value" type="xs:string"
+                                               minOccurs="0" />
+            </xs:sequence>
+            <xs:attribute name="id" type="xs:long" />
+            <xs:attribute name="version" type="xs:int" />
+
+        </xs:complexType>
+    </xs:element>
+</xs:schema>
+
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldJaxb/src/main/resources/person.xsd b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldJaxb/src/main/resources/person.xsd
deleted file mode 100644 (file)
index 319a722..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<xs:schema version="1.0"
-       xmlns:re="http://www.collectionspace.org/services/hello"
-       xmlns:xs="http://www.w3.org/2001/XMLSchema">
-
-    <xs:element name="person" type="person" />
-
-    <xs:complexType name="person">
-
-        <xs:sequence>
-            <xs:element name="firstName" type="xs:string"
-                                               minOccurs="1" />
-            <xs:element name="lastName" type="xs:string"
-                                               minOccurs="1" />
-            <xs:element name="street" type="xs:string"
-                                               minOccurs="1" />
-            <xs:element name="city" type="xs:string"
-                                               minOccurs="1" />
-            <xs:element name="state" type="xs:string"
-                                               minOccurs="1" />
-            <xs:element name="zip" type="xs:string"
-                                               minOccurs="1" />
-            <xs:element name="country" type="xs:string"
-                                               minOccurs="1" />
-        </xs:sequence>
-        <xs:attribute name="id" type="xs:long" />
-        <xs:attribute name="version" type="xs:int" />
-
-    </xs:complexType>
-
-</xs:schema>
-
index c0f982061b4cad83b72ac830d2cf331c8c6a82d4..9d30c239abd6ef11d33f1639b45ba172edecb3cf 100644 (file)
@@ -36,6 +36,9 @@
                 <goal>clean</goal>\r
                 <goal>install</goal>\r
             </goals>\r
+            <properties>\r
+                <maven.test.skip>true</maven.test.skip>\r
+            </properties>\r
         </action>\r
         <action>\r
             <actionName>run</actionName>\r
index ca7743721f8a715ce85523314e52c869f1342e46..b9e0b3da43a819ded81be9d2e2b0e7c4ad37ee4d 100644 (file)
@@ -6,7 +6,7 @@
         <version>0.1</version>\r
     </parent>\r
     <modelVersion>4.0.0</modelVersion>\r
-    <groupId>org.collectionspace.prototype</groupId>\r
+    <groupId>org.collectionspace.hello.services</groupId>\r
     <artifactId>helloworld-service</artifactId>\r
     <packaging>war</packaging>\r
     <version>0.1</version>\r
     </repositories>\r
 \r
     <dependencies>\r
+        <dependency>\r
+            <groupId>org.collectionspace.hello.services</groupId>\r
+            <artifactId>helloworld-jaxb</artifactId>\r
+            <version>0.1</version>\r
+        </dependency>\r
         <dependency>\r
             <groupId>org.jboss.resteasy</groupId>\r
             <artifactId>resteasy-jaxrs</artifactId>\r
                 <groupId>org.apache.maven.plugins</groupId>\r
                 <artifactId>maven-surefire-plugin</artifactId>\r
                 <configuration>\r
-                    <skip>true</skip>\r
                 </configuration>\r
                 <executions>\r
                     <execution>\r
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/entity/Person.java b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/entity/Person.java
deleted file mode 100644 (file)
index bf4fc50..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-package org.collectionspace.hello.entity;
-
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlAttribute;
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlType;
-import javax.xml.bind.Unmarshaller;
-import javax.xml.bind.Unmarshaller.Listener;
-import javax.xml.bind.annotation.XmlElement;
-
-@XmlRootElement
-@XmlAccessorType(XmlAccessType.FIELD)
-@XmlType(name = "person", propOrder = {
-    "firstName",
-    "lastName",
-    "street",
-    "city",
-    "state",
-    "zip",
-    "country"
-})
-public class Person extends Listener {
-
-    @XmlAttribute
-    private int id;
-    @XmlAttribute
-    private int version = 1;
-    private String firstName;
-    private String lastName;
-    private String street;
-    private String city;
-    private String state;
-    private String zip;
-    private String country;
-
-    public int getId() {
-        return id;
-    }
-
-    public void setId(int id) {
-        this.id = id;
-    }
-
-    /**
-     * @return the version
-     */
-    public int getVersion() {
-        return version;
-    }
-
-    /**
-     * @param version the version to set
-     */
-    public void setVersion(int version) {
-        this.version = version;
-    }
-
-    public String getFirstName() {
-        return firstName;
-    }
-
-    public void setFirstName(String firstName) {
-        this.firstName = firstName;
-    }
-
-    public String getLastName() {
-        return lastName;
-    }
-
-    public void setLastName(String lastName) {
-        this.lastName = lastName;
-    }
-
-    public String getStreet() {
-        return street;
-    }
-
-    public void setStreet(String street) {
-        this.street = street;
-    }
-
-    public String getCity() {
-        return city;
-    }
-
-    public void setCity(String city) {
-        this.city = city;
-    }
-
-    public String getState() {
-        return state;
-    }
-
-    public void setState(String state) {
-        this.state = state;
-    }
-
-    public String getZip() {
-        return zip;
-    }
-
-    public void setZip(String zip) {
-        this.zip = zip;
-    }
-
-    public String getCountry() {
-        return country;
-    }
-
-    public void setCountry(String country) {
-        this.country = country;
-    }
-
-    /**
-     * JAXB Callback method used to reassociate the item with the owning contact.
-     * JAXB doesn't seem to read this method from a super class and it must
-     * therefore be placed on any subclass.
-     *
-     * @param unmarshaller the JAXB {@link Unmarshaller}.
-     * @param parent the owning {@link Contact} instance.
-     */
-    public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
-        super.afterUnmarshal(unmarshaller, parent);
-    }
-}
index 0021b9ae5ddefb9ca56423891a8cea0ad29bf62b..b7b7acdc0c421359fca89811a547aacaa81427d4 100644 (file)
@@ -5,20 +5,22 @@ import java.util.HashSet;
 import java.util.Set;
 
 public class HelloworldApplication extends Application {
-   private Set<Object> singletons = new HashSet<Object>();
-   private Set<Class<?>> empty = new HashSet<Class<?>>();
 
-   public HelloworldApplication() {
-      singletons.add(new PersonResource());
-   }
+    private Set<Object> singletons = new HashSet<Object>();
+    private Set<Class<?>> empty = new HashSet<Class<?>>();
 
-   @Override
-   public Set<Class<?>> getClasses() {
-      return empty;
-   }
+    public HelloworldApplication() {
+        singletons.add(new PersonResource());
+        singletons.add(new IdentifierResource());
+    }
 
-   @Override
-   public Set<Object> getSingletons() {
-      return singletons;
-   }
+    @Override
+    public Set<Class<?>> getClasses() {
+        return empty;
+    }
+
+    @Override
+    public Set<Object> getSingletons() {
+        return singletons;
+    }
 }
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java
new file mode 100644 (file)
index 0000000..de9a905
--- /dev/null
@@ -0,0 +1,74 @@
+package org.collectionspace.hello.services;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.ws.rs.core.UriBuilder;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import org.collectionspace.hello.Identifier;
+
+@Path("/identifiers")
+@Consumes("application/xml")
+@Produces("application/xml")
+public class IdentifierResource {
+
+    private Map<Long, Identifier> idDB = new ConcurrentHashMap<Long, Identifier>();
+    private AtomicLong idCounter = new AtomicLong();
+
+    public IdentifierResource() {
+    }
+
+    @POST
+    public Response createIdentifier(Identifier id) {
+        if (id.getNamespace() == null) {
+            id.setNamespace("edu.berkeley");
+        }
+        id.setId(idCounter.incrementAndGet());
+        id.setVersion(1);
+        UUID uuid = UUID.nameUUIDFromBytes(id.getNamespace().getBytes());
+        id.setValue(uuid.toString());
+        idDB.put(id.getId(), id);
+        verbose("create Id", id);
+        UriBuilder path = UriBuilder.fromResource(IdentifierResource.class);
+        path.path("" + id.getId());
+        Response response = Response.created(path.build()).build();
+        return response;
+    }
+
+    @GET
+    @Path("{id}")
+    public Identifier getId(@PathParam("id") Long id) {
+        Identifier i = idDB.get(id);
+        if (i == null) {
+            Response response = Response.status(Response.Status.NOT_FOUND).entity(
+                    "The requested ID was not found.").type("text/plain").build();
+            throw new WebApplicationException(response);
+        }
+        verbose("get Id", i);
+        return i;
+    }
+
+    private void verbose(String msg, Identifier id) {
+        try {
+            System.out.println(msg);
+            JAXBContext jc = JAXBContext.newInstance(Identifier.class);
+            Marshaller m = jc.createMarshaller();
+            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
+                    Boolean.TRUE);
+            m.marshal(id, System.out);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}
index 7afccdf92c8b8f5b077e7a8cbf5bb4a58abf1682..783c395446359ca4984f402b4558ce0e16e402f5 100644 (file)
@@ -1,7 +1,5 @@
 package org.collectionspace.hello.services;
 
-import org.collectionspace.hello.entity.Person;
-
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -13,18 +11,21 @@ import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
 import javax.ws.rs.core.UriBuilder;
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
 import javax.xml.bind.Marshaller;
+import javax.xml.namespace.QName;
+import org.collectionspace.hello.Person;
 
 @Path("/persons")
 @Consumes("application/xml")
 @Produces("application/xml")
 public class PersonResource {
 
-    private Map<Integer, Person> personDB = new ConcurrentHashMap<Integer, Person>();
-    private AtomicInteger idCounter = new AtomicInteger();
+    private Map<Long, Person> personDB = new ConcurrentHashMap<Long, Person>();
+    private AtomicLong idCounter = new AtomicLong();
 
     public PersonResource() {
     }
@@ -43,7 +44,7 @@ public class PersonResource {
 
     @GET
     @Path("{id}")
-    public Person getPerson(@PathParam("id") int id) {
+    public Person getPerson(@PathParam("id") Long id) {
         Person p = personDB.get(id);
         if (p == null) {
             Response response = Response.status(Response.Status.NOT_FOUND).entity(
@@ -56,12 +57,12 @@ public class PersonResource {
 
     @PUT
     @Path("{id}")
-    public Person updatePerson(@PathParam("id") int id, Person update) {
+    public Person updatePerson(@PathParam("id") Long id, Person update) {
         Person current = personDB.get(id);
         if (current == null) {
             throw new WebApplicationException(Response.Status.NOT_FOUND);
         }
-        verbose("received person", update);
+        verbose("update input", update);
         //todo: intelligent merge needed
         current.setFirstName(update.getFirstName());
         current.setLastName(update.getLastName());
@@ -70,7 +71,7 @@ public class PersonResource {
         current.setZip(update.getZip());
         current.setCountry(update.getCountry());
         current.setVersion(current.getVersion() + 1);
-        verbose("updated person", current);
+        verbose("update output", current);
         return current;
     }
 
diff --git a/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/test/java/org/collectionspace/hello/test/IdentifierResourceTest.java b/sandbox/sanjay/prototypes/restws/HelloWorld/HelloWorldService/src/test/java/org/collectionspace/hello/test/IdentifierResourceTest.java
new file mode 100644 (file)
index 0000000..ded21d4
--- /dev/null
@@ -0,0 +1,59 @@
+package org.collectionspace.hello.test;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * @version $Revision: 1 $
+ */
+public class IdentifierResourceTest {
+
+    @Test
+    public void testIdentifierResource() throws Exception {
+        System.out.println("*** Create a new Identifier ***");
+        // Create a new object
+        String newIdentifier = "<ns2:identifier xmlns:ns2=\"http://collectionspace.org/hello\">" +
+                "<namespace>edu.stanford</namespace>" +
+                "</ns2:identifier>";
+
+        URL postUrl = new URL("http://localhost:8080/helloworld/cspace/identifiers");
+        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
+        connection.setDoOutput(true);
+        connection.setInstanceFollowRedirects(false);
+        connection.setRequestMethod("POST");
+        connection.setRequestProperty("Content-Type", "application/xml");
+        OutputStream os = connection.getOutputStream();
+        os.write(newIdentifier.getBytes());
+        os.flush();
+        Assert.assertEquals(HttpURLConnection.HTTP_CREATED, connection.getResponseCode());
+        String createdUrl = connection.getHeaderField("Location");
+        System.out.println("Location: " + createdUrl);
+        connection.disconnect();
+
+
+        // Get the new object
+        System.out.println("*** GET Created Identifier **");
+        URL getUrl = new URL(createdUrl);
+        connection = (HttpURLConnection) getUrl.openConnection();
+        connection.setRequestMethod("GET");
+        System.out.println("Content-Type: " + connection.getContentType());
+
+        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+
+        String line = reader.readLine();
+        while (line != null) {
+            System.out.println(line);
+            line = reader.readLine();
+        }
+        Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
+        connection.disconnect();
+
+        connection.disconnect();
+    }
+}
index 7757b15dd586162c58684d1ec17de85df71c9279..51b4dffc48c609fd96eaa8696d3198f8b4e22bb7 100644 (file)
@@ -14,7 +14,6 @@ import java.net.URL;
 
 
 /**
- * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
  * @version $Revision: 1 $
  */
 public class PersonResourceTest
@@ -24,7 +23,7 @@ public class PersonResourceTest
    {
       System.out.println("*** Create a new Person ***");
       // Create a new object
-      String newPerson = "<person>"
+      String newPerson = "<ns2:person xmlns:ns2=\"http://collectionspace.org/hello\">"
               + "<firstName>John</firstName>"
               + "<lastName>Doe</lastName>"
               + "<street>2195 Hearst Ave</street>"
@@ -32,7 +31,7 @@ public class PersonResourceTest
               + "<state>CA</state>"
               + "<zip>94504</zip>"
               + "<country>USA</country>"
-              + "</person>";
+              + "</ns2:person>";
 
       URL postUrl = new URL("http://localhost:8080/helloworld/cspace/persons");
       HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
@@ -68,7 +67,7 @@ public class PersonResourceTest
       Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
       connection.disconnect();
 
-      String updatePerson = "<person>"
+      String updatePerson = "<ns2:person xmlns:ns2=\"http://collectionspace.org/hello\">"
               + "<firstName>Jane</firstName>"
               + "<lastName>Doe</lastName>"
               + "<street>1 University Ave</street>"
@@ -76,7 +75,7 @@ public class PersonResourceTest
               + "<state>CA</state>"
               + "<zip>94504</zip>"
               + "<country>USA</country>"
-              + "</person>";
+              + "</ns2:person>";
       connection = (HttpURLConnection) getUrl.openConnection();
       connection.setDoOutput(true);
       connection.setRequestMethod("PUT");
index b26a1d350baf0302f1fbe242926500209ab17f5a..a058af929dfe30ca5a2723791564eb7736c86b91 100644 (file)
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
-   <modelVersion>4.0.0</modelVersion>
-   <groupId>org.collectionspace.hello.services</groupId>
-   <version>0.1</version>
-   <artifactId>helloworld</artifactId>
-   <packaging>pom</packaging>
-   <name>HelloWorld</name>
-   <modules>
-      <module>HelloWorldService</module>
-      <module>HelloWorldJaxb</module>
-      <module>HelloWorldClient</module>
-   </modules>
-   <repositories>
-      <repository>
-         <id>jboss</id>
-         <url>http://repository.jboss.org/maven2</url>
-      </repository>
-      <repository>
-         <id>sun</id>
-         <url>http://download.java.net/maven/2</url>
-      </repository>
-      <repository>
-         <id>java.net</id>
-         <name>java.net Maven Repository</name>
-         <url>
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.collectionspace.hello.services</groupId>
+    <version>0.1</version>
+    <artifactId>helloworld</artifactId>
+    <packaging>pom</packaging>
+    <name>HelloWorld</name>
+    <modules>
+        <module>HelloWorldJaxb</module>
+        <module>HelloWorldService</module>
+        <module>HelloWorldClient</module>
+    </modules>
+    <repositories>
+        <repository>
+            <id>jboss</id>
+            <url>http://repository.jboss.org/maven2</url>
+        </repository>
+        <repository>
+            <id>sun</id>
+            <url>http://download.java.net/maven/2</url>
+        </repository>
+        <repository>
+            <id>java.net</id>
+            <name>java.net Maven Repository</name>
+            <url>
             https://maven-repository.dev.java.net/nonav/repository
-         </url>
-         <layout>legacy</layout>
-      </repository>
-      <repository>
-         <id>maven2-repository.dev.java.net</id>
-         <name>Java.net Maven 2 Repository</name>
-         <url>http://download.java.net/maven/2</url>
-      </repository>
-   </repositories>
-   <pluginRepositories>
+            </url>
+            <layout>legacy</layout>
+        </repository>
+        <repository>
+            <id>maven2-repository.dev.java.net</id>
+            <name>Java.net Maven 2 Repository</name>
+            <url>http://download.java.net/maven/2</url>
+        </repository>
+    </repositories>
+    <pluginRepositories>
 
-      <pluginRepository>
-         <id>java.net</id>
-         <name>java.net Maven Repository</name>
-         <url>
+        <pluginRepository>
+            <id>java.net</id>
+            <name>java.net Maven Repository</name>
+            <url>
             https://maven-repository.dev.java.net/nonav/repository
-         </url>
-         <layout>legacy</layout>
-      </pluginRepository>
-      <pluginRepository>
-         <id>maven2-repository.dev.java.net</id>
-         <name>Java.net Maven 2 Repository</name>
-         <url>http://download.java.net/maven/2</url>
-      </pluginRepository>
-   </pluginRepositories>
+            </url>
+            <layout>legacy</layout>
+        </pluginRepository>
+        <pluginRepository>
+            <id>maven2-repository.dev.java.net</id>
+            <name>Java.net Maven 2 Repository</name>
+            <url>http://download.java.net/maven/2</url>
+        </pluginRepository>
+    </pluginRepositories>
 
-   <build>
-      <pluginManagement>
-         <plugins>
-            <plugin>
-               <groupId>org.apache.maven.plugins</groupId>
-               <artifactId>maven-compiler-plugin</artifactId>
-               <configuration>
-                  <source>1.5</source>
-                  <target>1.5</target>
-               </configuration>
-            </plugin>
-            <plugin>
-               <groupId>org.apache.maven.plugins</groupId>
-               <artifactId>maven-site-plugin</artifactId>
-               <configuration>
-                  <unzipCommand>
+    <build>
+        <pluginManagement>
+            <plugins>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-compiler-plugin</artifactId>
+                    <configuration>
+                        <source>1.5</source>
+                        <target>1.5</target>
+                    </configuration>
+                </plugin>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-site-plugin</artifactId>
+                    <configuration>
+                        <unzipCommand>
                      /usr/bin/unzip -o &gt; err.txt
-                  </unzipCommand>
-               </configuration>
-            </plugin>
-            <plugin>
-               <groupId>org.apache.maven.plugins</groupId>
-               <artifactId>maven-war-plugin</artifactId>
-               <version>2.0.1</version>
-               <configuration>
+                        </unzipCommand>
+                    </configuration>
+                </plugin>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-war-plugin</artifactId>
+                    <version>2.0.1</version>
+                    <configuration>
 
-                  <warSourceExcludes>
+                        <warSourceExcludes>
                      WEB-INF/lib/*.jar
-                  </warSourceExcludes>
-                  <archive>
-                     <manifest>
-                        <addClasspath>true</addClasspath>
+                        </warSourceExcludes>
+                        <archive>
+                            <manifest>
+                                <addClasspath>true</addClasspath>
 
-                     </manifest>
-                  </archive>
-               </configuration>
-            </plugin>
-            <plugin>
-               <groupId>org.jvnet.jaxb2.maven2</groupId>
-               <artifactId>maven-jaxb2-plugin</artifactId>
-               <executions>
-                  <execution>
-                     <goals>
-                        <goal>generate</goal>
-                     </goals>
-                  </execution>
-               </executions>
-               <configuration>
-                  <args>
-                     <arg>-XtoString</arg>
-                     <arg>-Xinject-listener-code</arg>
+                            </manifest>
+                        </archive>
+                    </configuration>
+                </plugin>
+                <plugin>
+                    <groupId>org.jvnet.jaxb2.maven2</groupId>
+                    <artifactId>maven-jaxb2-plugin</artifactId>
+                    <executions>
+                        <execution>
+                            <goals>
+                                <goal>generate</goal>
+                            </goals>
+                        </execution>
+                    </executions>
+                    <configuration>
+                        <args>
+                            <arg>-XtoString</arg>
+                            <arg>-Xinject-listener-code</arg>
 
                      <!-- <arg>-Xcollection-setter-injector</arg>
                                 <arg>-Xfluent-api</arg> -->
-                  </args>
-                  <plugins>
-                     <plugin>
-                        <groupId>
+                        </args>
+                        <plugins>
+                            <plugin>
+                                <groupId>
                            org.jvnet.jaxb2_commons
-                        </groupId>
-                        <artifactId>basic</artifactId>
-                        <version>0.4.1</version>
-                     </plugin>
-                     <plugin>
-                        <groupId>
+                                </groupId>
+                                <artifactId>basic</artifactId>
+                                <version>0.4.1</version>
+                            </plugin>
+                            <plugin>
+                                <groupId>
                            org.jvnet.jaxb2-commons
-                        </groupId>
-                        <artifactId>
+                                </groupId>
+                                <artifactId>
                            property-listener-injector
-                        </artifactId>
-                        <version>1.0</version>
-                     </plugin>
+                                </artifactId>
+                                <version>1.0</version>
+                            </plugin>
                      <!--
                             <plugin>
                                <groupId>
                                <version>2.0.1</version>
                             </plugin>
                              -->
-                  </plugins>
-                  <generatePackage>
-                     org.collectionspace.hello.client
-                  </generatePackage>
-               </configuration>
-            </plugin>
-         </plugins>
-      </pluginManagement>
-   </build>
-   <dependencyManagement>
-      <dependencies>
-         <dependency>
-            <groupId>net.java.dev.jaxb2-commons</groupId>
-            <artifactId>jaxb-fluent-api</artifactId>
-            <version>2.0.1</version>
-         </dependency>
-         <dependency>
-            <groupId>org.testng</groupId>
-            <artifactId>testng</artifactId>
-            <version>5.6</version>
-            <scope>test</scope>
-            <classifier>jdk15</classifier>
-         </dependency>
-         <dependency>
-            <groupId>org.jboss.resteasy</groupId>
-            <artifactId>jaxrs-api</artifactId>
-            <version>1.0.2.GA</version>
-         </dependency>
-         <dependency>
-            <groupId>net.java.dev.jaxb2-commons</groupId>
-            <artifactId>jaxb-fluent-api</artifactId>
-            <version>2.0.1</version>
-         </dependency>
-         <dependency>
-            <groupId>org.jvnet.jaxb2-commons</groupId>
-            <artifactId>property-listener-injector</artifactId>
-            <version>1.0</version>
-         </dependency>
-         <dependency>
-            <groupId>org.jvnet.jaxb2_commons</groupId>
-            <artifactId>runtime</artifactId>
-            <version>0.4.1</version>
-         </dependency>
-         <dependency>
-            <groupId>org.jboss.resteasy</groupId>
-            <artifactId>resteasy-jaxrs</artifactId>
-            <version>1.0.2.GA</version>
-         </dependency>
-         <dependency>
-            <groupId>commons-httpclient</groupId>
-            <artifactId>commons-httpclient</artifactId>
-            <version>3.1</version>
-         </dependency>
-         <dependency>
-            <groupId>com.sun.xml.bind</groupId>
-            <artifactId>jaxb-impl</artifactId>
-            <version>2.1.7</version>
-         </dependency>
-         <dependency>
-            <groupId>org.testng</groupId>
-            <artifactId>testng</artifactId>
-            <version>5.6</version>
-            <scope>test</scope>
-            <classifier>jdk15</classifier>
-         </dependency>
-         <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-api</artifactId>
-            <version>1.5.2</version>
-         </dependency>
-         <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-log4j12</artifactId>
-            <version>1.5.2</version>
-         </dependency>
-         <dependency>
-            <groupId>mysql</groupId>
-            <artifactId>mysql-connector-java</artifactId>
-            <version>5.1.5</version>
-            <scope>test</scope>
-         </dependency>
-         <dependency>
-            <groupId>com.jgoodies</groupId>
-            <artifactId>binding</artifactId>
-            <version>2.0.2</version>
-         </dependency>
-         <dependency>
-            <groupId>com.jgoodies</groupId>
-            <artifactId>forms</artifactId>
-            <version>1.2.0</version>
-         </dependency>
-      </dependencies>
-   </dependencyManagement>
+                        </plugins>
+                        <generatePackage>
+                     org.collectionspace.hello
+                        </generatePackage>
+                    </configuration>
+                </plugin>
+            </plugins>
+        </pluginManagement>
+    </build>
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>net.java.dev.jaxb2-commons</groupId>
+                <artifactId>jaxb-fluent-api</artifactId>
+                <version>2.0.1</version>
+            </dependency>
+            <dependency>
+                <groupId>org.testng</groupId>
+                <artifactId>testng</artifactId>
+                <version>5.6</version>
+                <scope>test</scope>
+                <classifier>jdk15</classifier>
+            </dependency>
+            <dependency>
+                <groupId>org.jboss.resteasy</groupId>
+                <artifactId>jaxrs-api</artifactId>
+                <version>1.0.2.GA</version>
+            </dependency>
+            <dependency>
+                <groupId>net.java.dev.jaxb2-commons</groupId>
+                <artifactId>jaxb-fluent-api</artifactId>
+                <version>2.0.1</version>
+            </dependency>
+            <dependency>
+                <groupId>org.jvnet.jaxb2-commons</groupId>
+                <artifactId>property-listener-injector</artifactId>
+                <version>1.0</version>
+            </dependency>
+            <dependency>
+                <groupId>org.jvnet.jaxb2_commons</groupId>
+                <artifactId>runtime</artifactId>
+                <version>0.4.1</version>
+            </dependency>
+            <dependency>
+                <groupId>org.jboss.resteasy</groupId>
+                <artifactId>resteasy-jaxrs</artifactId>
+                <version>1.0.2.GA</version>
+            </dependency>
+            <dependency>
+                <groupId>commons-httpclient</groupId>
+                <artifactId>commons-httpclient</artifactId>
+                <version>3.1</version>
+            </dependency>
+            <dependency>
+                <groupId>com.sun.xml.bind</groupId>
+                <artifactId>jaxb-impl</artifactId>
+                <version>2.1.7</version>
+            </dependency>
+            <dependency>
+                <groupId>org.testng</groupId>
+                <artifactId>testng</artifactId>
+                <version>5.6</version>
+                <scope>test</scope>
+                <classifier>jdk15</classifier>
+            </dependency>
+            <dependency>
+                <groupId>org.slf4j</groupId>
+                <artifactId>slf4j-api</artifactId>
+                <version>1.5.2</version>
+            </dependency>
+            <dependency>
+                <groupId>org.slf4j</groupId>
+                <artifactId>slf4j-log4j12</artifactId>
+                <version>1.5.2</version>
+            </dependency>
+            <dependency>
+                <groupId>mysql</groupId>
+                <artifactId>mysql-connector-java</artifactId>
+                <version>5.1.5</version>
+                <scope>test</scope>
+            </dependency>
+            <dependency>
+                <groupId>com.jgoodies</groupId>
+                <artifactId>binding</artifactId>
+                <version>2.0.2</version>
+            </dependency>
+            <dependency>
+                <groupId>com.jgoodies</groupId>
+                <artifactId>forms</artifactId>
+                <version>1.2.0</version>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
 </project>