Commits

Sam Adams committed 0936eb1

Added deposit URI discovery

Comments (0)

Files changed (3)

client-utils/src/main/java/net/chempound/client/SwordClient.java

         return getServiceDocument(url, true);
     }
 
-    public SwordService getServiceDocument(final URI url, final boolean discover) throws Exception {
+    public SwordService getServiceDocument(final URI url, final boolean discoveryEnabled) throws Exception {
         final HttpResponse response = httpClient.execute(new HttpGet(url));
         try {
             if (SC_OK == response.getStatusLine().getStatusCode()) {
-                return getServiceDocument(discover, response);
+                return getServiceDocument(discoveryEnabled, response);
             } else {
                 throw new RuntimeException("Error loading repository page: "+response.getStatusLine());
             }
         }
     }
 
-    public DepositReceipt deposit(final URI uri, final DepositRequest deposit) throws IOException {
-        final HttpPost request = createDepositRequest(uri, deposit);
+    public DepositReceipt deposit(final URI uri, final DepositRequest deposit) throws Exception {
+        return deposit(uri, deposit, true);
+    }
 
+    public DepositReceipt deposit(final URI uri, final DepositRequest deposit, final boolean discoveryEnabled) throws Exception {
+        final URI depositUri = discoveryEnabled ? discoverDepositUri(uri) : uri;
+
+        final HttpPost request = createDepositRequest(depositUri, deposit);
         final HttpResponse response = httpClient.execute(request);
         try {
             if (response.getStatusLine().getStatusCode() == SC_CREATED) {
         return null;
     }
 
+    private URI discoverDepositUri(final URI uri) throws IOException, SAXException, ParsingException {
+        final HttpGet request = new HttpGet(uri);
+        request.addHeader("Accept", "text/html");
+        final HttpResponse response = httpClient.execute(request);
+        try {
+            if (response.getStatusLine().getStatusCode() == SC_OK) {
+                final Document doc = readHtml(response);
+                final Nodes nodes = doc.query("/h:html/h:head/h:link[@rel='http://purl.org/net/sword/terms/deposit']/@href", HTML_CONTEXT);
+                return nodes.size() == 0 ? uri : URI.create(nodes.get(0).getValue());
+            }
+        } finally {
+            if (response.getEntity() != null) {
+                response.getEntity().consumeContent();
+            }
+        }
+        return uri;
+    }
+
     private boolean isServiceDocument(final Document doc) {
         final Element root = doc.getRootElement();
         return ATOMPUB_NAMESPACE.equals(root.getNamespaceURI()) && "service".equals(root.getLocalName());

client-utils/src/main/java/net/chempound/client/atom/AtomPub.java

 /**
  * @author Sam Adams
  */
-public class AtomPub
-{
+public class AtomPub {
 
     public static final String ATOMPUB_NAMESPACE = "http://www.w3.org/2007/app";
 

client-utils/src/test/java/net/chempound/client/SwordClientTest.java

         assertEquals(URI.create("http://localhost:8181/sword/collection/foobar/"), receipt.getAlternateIRI());
     }
 
+    @Test
+    public void testDepositByDiscovery() throws Exception {
+        final HttpResponse response1 = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "Okay"));
+        response1.setEntity(new StringEntity(
+                "<html>" +
+                "<head><link rel='http://purl.org/net/sword/terms/deposit' href='http://localhost:8181/sword/collection/'></head>" +
+                "<body><h1>Collection</h1></body>" +
+                "</html>"
+        ));
+
+        final HttpResponse response2 = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_CREATED, "Created"));
+        response2.setEntity(new StringEntity(
+                "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:sword='http://purl.org/net/sword/terms/'>" +
+                "<link rel='alternate' href='http://localhost:8181/content/foobar/'/>" +
+                "</entry>"));
+
+        when(httpClient.execute(argThat(matchesGetUri(URI.create("http://localhost:8181/content/")))))
+                .thenReturn(response1);
+
+        when(httpClient.execute(argThat(matchesPostUri(URI.create("http://localhost:8181/sword/collection/")))))
+                .thenReturn(response2);
+
+        final DepositRequest depositRequest = new DefaultDepositRequest();
+        depositRequest.setTitle("--title--");
+        depositRequest.getMetadataModel().getResource("").addProperty(RDF.type, CPTerms.Collection);
+        depositRequest.addResource(new InMemoryResource("foo.txt", "foobar"));
+
+        final DepositReceipt receipt = client.deposit(URI.create("http://localhost:8181/content/"), depositRequest);
+        assertEquals(URI.create("http://localhost:8181/content/foobar/"), receipt.getAlternateIRI());
+    }
+
 
     private Matcher<HttpUriRequest> matchesGetUri(final URI uri) {
         return new ArgumentMatcher<HttpUriRequest>() {