Sam Adams avatar Sam Adams committed f7d757d

Extract interface for ChempoundClient

Comments (0)

Files changed (2)

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

 package net.chempound.client;
 
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-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 class ChempoundClient {
+public interface ChempoundClient {
 
-    private final SWORDClient swordClient;
+    CreateCollectionResponse createCollection(CreateCollectionRequest request) throws Exception;
 
-    public ChempoundClient() {
-        this(new SWORDClient());
-    }
-
-    public ChempoundClient(final SWORDClient swordClient) {
-        this.swordClient = swordClient;
-    }
-
-    public CreateCollectionResponse createCollection(final CreateCollectionRequest request) throws Exception {
-        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(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();
-        if (request.getTitle() != null) {
-            entryPart.getEntry().setTitle(request.getTitle());
-        }
-
-        final Model metadataModel = ModelFactory.createDefaultModel();
-        metadataModel.getResource("").addProperty(RDF.type, CPTerms.Collection);
-
-        attachMetadata(entryPart, metadataModel);
-
-        final Deposit deposit = new DepositFactory().newMetadataOnly(entryPart);
-        if (request.getSlug() != null) {
-            deposit.setSlug(request.getSlug());
-        }
-        return deposit;
-    }
-
-    private CreateCollectionResponse mapCreateCollectionResponse(final DepositReceipt receipt) {
-        return new CreateCollectionResponse(URI.create(receipt.getSplashPageIRI().toString()), null);
-    }
-
-    private Deposit mapDepositItemRequest(final DepositItemRequest request) {
-
-        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));
-        }
-
-        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));
-        entryPart.getEntry().addExtension(document.getRoot());
-    }
+    DepositItemResponse depositItem(DepositItemRequest request) throws Exception;
 
 }

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

+package net.chempound.client;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+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 class DefaultChempoundClient implements ChempoundClient {
+
+    private final SWORDClient swordClient;
+
+    public DefaultChempoundClient() {
+        this(new SWORDClient());
+    }
+
+    public DefaultChempoundClient(final SWORDClient swordClient) {
+        this.swordClient = swordClient;
+    }
+
+    @Override
+    public CreateCollectionResponse createCollection(final CreateCollectionRequest request) throws Exception {
+        final DepositReceipt receipt = swordClient.deposit(normaliseUri(request.getURI()), mapCreateCollectionRequest(request));
+        return mapCreateCollectionResponse(receipt);
+    }
+
+    @Override
+    public DepositItemResponse depositItem(final DepositItemRequest request) throws Exception {
+        final DepositReceipt receipt = swordClient.deposit(normaliseUri(request.getURI()), mapDepositItemRequest(request));
+        return mapDepositItemResponse(receipt);
+    }
+
+    private static String normaliseUri(final URI uri) {
+        final String s = uri.toString();
+        return s.endsWith("/") ? s : s + '/';
+    }
+
+    private static Deposit mapCreateCollectionRequest(final CreateCollectionRequest request) {
+        final EntryPart entryPart = new EntryPart();
+        if (request.getTitle() != null) {
+            entryPart.getEntry().setTitle(request.getTitle());
+        }
+
+        final Model metadataModel = ModelFactory.createDefaultModel();
+        metadataModel.getResource("").addProperty(RDF.type, CPTerms.Collection);
+
+        attachMetadata(entryPart, metadataModel);
+
+        final Deposit deposit = new DepositFactory().newMetadataOnly(entryPart);
+        if (request.getSlug() != null) {
+            deposit.setSlug(request.getSlug());
+        }
+        return deposit;
+    }
+
+    private static CreateCollectionResponse mapCreateCollectionResponse(final DepositReceipt receipt) {
+        return new CreateCollectionResponse(URI.create(receipt.getSplashPageIRI().toString()), null);
+    }
+
+    private static Deposit mapDepositItemRequest(final DepositItemRequest request) {
+
+        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));
+        }
+
+        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 static DepositItemResponse mapDepositItemResponse(final DepositReceipt receipt) {
+        return new DepositItemResponse(URI.create(receipt.getSplashPageIRI().toString()));
+    }
+
+    private static void attachMetadata(final EntryPart entryPart, final Model metadataModel) {
+        final byte[] bytes = RdfIO.writeRdf(metadataModel);
+        final Document document = Abdera.getNewParser().parse(new ByteArrayInputStream(bytes));
+        entryPart.getEntry().addExtension(document.getRoot());
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.