Commits

Sam Adams committed cf2cf95

Developing deposit ATs

Comments (0)

Files changed (2)

chempound-client/src/main/java/net/chempound/client/ChempoundClient.java

 import com.hp.hpl.jena.vocabulary.RDF;
 import net.chempound.rdf.CPTerms;
 import net.chempound.rdf.RdfIO;
+import net.chempound.storage.LocalResource;
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Document;
+import org.apache.commons.io.IOUtils;
 import org.swordapp.client.*;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 /**
  * @author Sam Adams
     }
 
     public CreateCollectionResponse createCollection(final CreateCollectionRequest request) throws Exception {
-        final DepositReceipt receipt = swordClient.deposit(request.getURI().toString(), mapCreateCollectionRequest(request));
+        final DepositReceipt receipt = swordClient.deposit(normaliseUri(request.getURI()), mapCreateCollectionRequest(request));
         return mapCreateCollectionResponse(receipt);
     }
 
     public DepositItemResponse depositItem(final DepositItemRequest request) throws Exception {
-        final DepositReceipt receipt = swordClient.deposit(request.getURI().toString(), mapDepositItemRequest(request));
+        final DepositReceipt receipt = swordClient.deposit(normaliseUri(request.getURI()), mapDepositItemRequest(request));
         return mapDepositItemResponse(receipt);
     }
 
+    private String normaliseUri(final URI uri) {
+        final String s = uri.toString();
+        return s.endsWith("/") ? s : s + '/';
+    }
 
     private Deposit mapCreateCollectionRequest(final CreateCollectionRequest request) {
         final EntryPart entryPart = new EntryPart();
         return new CreateCollectionResponse(URI.create(receipt.getSplashPageIRI().toString()), null);
     }
 
+    private Deposit mapDepositItemRequest(final DepositItemRequest request) {
 
-    private Deposit mapDepositItemRequest(final DepositItemRequest request) {
-        final EntryPart entryPart = new EntryPart();
-        if (request.getTitle() != null) {
-            entryPart.getEntry().setTitle(request.getTitle());
+        final DepositFactory depositFactory = new DepositFactory();
+
+        final Deposit deposit;
+        if (request.hasResources()) {
+
+            final String filename = "deposit.zip";
+            final String mimetype = "application/zip";
+            final String packaging = UriRegistry.PACKAGE_SIMPLE_ZIP;
+            final InputStream file = createZipFile(request);
+            
+            if (request.hasMetadata()) {
+                deposit = depositFactory.newMultipart(createEntryPart(request), file, filename, mimetype, packaging);
+            } else {
+                deposit = depositFactory.newBinaryOnly(file, filename, mimetype, packaging);
+            }
+        } else {
+            deposit = depositFactory.newMetadataOnly(createEntryPart(request));
         }
 
-        final Deposit deposit = new DepositFactory().newMetadataOnly(entryPart);
         if (request.getSlug() != null) {
             deposit.setSlug(request.getSlug());
         }
         return deposit;
     }
 
+    private static InputStream createZipFile(final DepositItemRequest request) {
+        try {
+            final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+            final ZipOutputStream out = new ZipOutputStream(buffer);
+            for (final LocalResource resource : request.getResources()) {
+                final ZipEntry zipEntry = new ZipEntry(resource.getUri().toString());
+                out.putNextEntry(zipEntry);
+
+                final InputStream in = resource.openInputStream();
+                IOUtils.copy(in, out);
+                IOUtils.closeQuietly(in);
+            }
+            out.close();
+
+            return new ByteArrayInputStream(buffer.toByteArray());
+        } catch (IOException e) {
+            throw new RuntimeException("Error writing zip file", e);
+        }
+    }
+
+    private static EntryPart createEntryPart(final DepositItemRequest request) {
+        final EntryPart entryPart = new EntryPart();
+        if (request.getTitle() != null) {
+            entryPart.getEntry().setTitle(request.getTitle());
+        }
+        return entryPart;
+    }
+
     private DepositItemResponse mapDepositItemResponse(final DepositReceipt receipt) {
         return new DepositItemResponse(URI.create(receipt.getSplashPageIRI().toString()));
     }
 
-
     private void attachMetadata(final EntryPart entryPart, final Model metadataModel) {
         final byte[] bytes = RdfIO.writeRdf(metadataModel);
         final Document document = Abdera.getNewParser().parse(new ByteArrayInputStream(bytes));

chempound-client/src/main/java/net/chempound/client/DepositItemRequest.java

 package net.chempound.client;
 
+import net.chempound.storage.LocalResource;
+
 import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author Sam Adams
  */
 public class DepositItemRequest {
 
+    private final List<LocalResource> resources = new ArrayList<LocalResource>();
+
     private URI uri;
     private String title;
     private String slug;
         return this;
     }
 
+    public void addResource(final LocalResource resource) {
+        resources.add(resource);
+    }
+
+    public boolean hasMetadata() {
+        return title != null;
+    }
+
+    public boolean hasResources() {
+        return !resources.isEmpty();
+    }
+
+    List<LocalResource> getResources() {
+        return resources;
+    }
 }