Commits

Anonymous committed f26a751

Extract Deposit Request and Receipt factories from ChempoundCollectionManager

Comments (0)

Files changed (4)

chempound-webapp/src/main/java/net/chempound/webapp/sword/impl/ChempoundCollectionManager.java

 package net.chempound.webapp.sword.impl;
 
-import com.hp.hpl.jena.rdf.model.Model;
 import net.chempound.Chempound;
 import net.chempound.bean.ItemInfo;
 import net.chempound.config.ContentUri;
 import net.chempound.config.SwordUri;
-import net.chempound.content.DefaultDepositRequest;
 import net.chempound.content.DepositRequest;
 import net.chempound.content.DepositResponse;
-import net.chempound.rdf.RdfIO;
-import net.chempound.rdf.ore.AggregatedResource;
-import net.chempound.storage.InMemoryResource;
-import net.chempound.util.MimeType;
 import org.apache.abdera.i18n.iri.IRI;
-import org.apache.abdera.model.Element;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
 import org.apache.abdera.parser.stax.FOMEntry;
 import org.apache.abdera.parser.stax.FOMFeed;
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.swordapp.server.*;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import javax.xml.namespace.QName;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
 
 /**
  * @author sea36
 @Singleton
 public class ChempoundCollectionManager implements CollectionListManager, CollectionDepositManager {
 
-    private static final Logger LOG = LoggerFactory.getLogger(ChempoundCollectionManager.class);
-
-    private static final String DEPOSIT_BASE_URI = "";
-
     private final Chempound chempound;
+    private final DepositRequestFactory depositRequestFactory;
+    private final DepositReceiptFactory depositReceiptFactory;
 
     private final URI swordUri;
     private final URI contentUri;
 
     @Inject
-    public ChempoundCollectionManager(final Chempound chempound, @SwordUri final URI swordUri, @ContentUri final URI contentUri) {
+    public ChempoundCollectionManager(final Chempound chempound, final DepositRequestFactory depositRequestFactory, final DepositReceiptFactory depositReceiptFactory, @SwordUri final URI swordUri, @ContentUri final URI contentUri) {
         this.chempound = chempound;
+        this.depositRequestFactory = depositRequestFactory;
+        this.depositReceiptFactory = depositReceiptFactory;
         this.swordUri = swordUri;
         this.contentUri = contentUri;
     }
 
     @Override
     public DepositReceipt createNew(final String collectionURI, final Deposit deposit, final AuthCredentials auth, final SwordConfiguration config) throws SwordError, SwordServerException, SwordAuthException {
-        final DepositRequest depositRequest = new DefaultDepositRequest();
-        depositRequest.setSlug(deposit.getSlug());
-
-        final SwordEntry entry = deposit.getSwordEntry();
-        if (entry != null) {
-            handleDepositEntry(depositRequest, entry);
-        }
-
-        final boolean packaged;
-        if (!deposit.isEntryOnly()) {
-            packaged = handleDepositPayload(deposit, depositRequest);
-        } else {
-            packaged = false;
-        }
-
-        final URI depositBase = swordUri.resolve("collection/");
-        final URI targetPath = depositBase.relativize(URI.create(collectionURI));
-        final URI target = contentUri.resolve(targetPath);
-        LOG.info("SWORD deposit target: {}", target);
-        depositRequest.addCollection(target);
+        final DepositRequest depositRequest = depositRequestFactory.createDepositRequest(deposit, collectionURI);
+        final boolean packaged = depositRequestFactory.isPackaged(deposit);
 
         final DepositResponse response;
         try {
         final URI uri = response.getUri();
         final URI relative = contentUri.relativize(uri);
 
-        return createDepositReceipt(packaged, swordUri, response, uri, relative);
+        return depositReceiptFactory.createDepositReceipt(packaged, swordUri, response, uri, relative);
     }
 
-    private DepositReceipt createDepositReceipt(final boolean packaged, final URI swordBase, final DepositResponse response, final URI uri, final URI relative) {
-        final DepositReceipt receipt = new DepositReceipt();
-
-        final URI swordEdit = swordBase.resolve("edit/");
-        final URI swordEditMedia = swordBase.resolve("edit-media/");
-        final URI swordStatement = swordBase.resolve("statement/");
-
-        receipt.setEditIRI(new IRI(swordEdit.resolve(relative)));
-        receipt.addEditMediaIRI(new IRI(swordEditMedia.resolve(relative)));
-        receipt.setStatementURI("application/rdf+xml", swordStatement.resolve(relative).toString());
-
-        receipt.setTreatment("Deposited.");
-        receipt.setSplashUri(uri.toString());
-
-        if (packaged) {
-            for (final AggregatedResource resource : response.getResources()) {
-                MimeType mimeType = resource.getFormat();
-                if (mimeType == null) {
-                    mimeType = MimeType.APPLICATION_OCTET_STREAM;
-                }
-                receipt.addDerivedResource(resource.getURI(), mimeType.getName());
-            }
-        }
-        return receipt;
-    }
-
-    private boolean handleDepositPayload(final Deposit deposit, final DepositRequest depositRequest) throws SwordServerException, SwordError {
-        if (deposit.getPackaging() == null || UriRegistry.PACKAGE_BINARY.equals(deposit.getPackaging())) {
-            LOG.info("Deposit with BINARY packaging");
-            processBinaryDeposit(deposit, depositRequest);
-            return false;
-        } else if (UriRegistry.PACKAGE_SIMPLE_ZIP.equals(deposit.getPackaging())) {
-            LOG.info("Deposit with SIMPLE_ZIP packaging");
-            processZipDeposit(deposit, depositRequest);
-            return true;
-        } else {
-            throw new SwordError(UriRegistry.ERROR_CONTENT);
-        }
-    }
-
-    private void handleDepositEntry(final DepositRequest depositRequest, final SwordEntry entry) throws SwordServerException {
-        extractRdfMetadata(depositRequest, entry);
-
-        if (entry.getTitle() != null) {
-            depositRequest.setTitle(entry.getTitle());
-        }
-        // TODO handle DC elements
-    }
-
-    private void extractRdfMetadata(final DepositRequest depositRequest, final SwordEntry entry) throws SwordServerException {
-        final Element rdf = entry.getEntry().getExtension(QName.valueOf("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF"));
-        if (rdf != null) {
-            final byte[] bytes = writeXmlToByteArray(rdf);
-            readXmlFromByteArray(depositRequest.getMetadataModel(), bytes);
-        }
-    }
-
-    private void readXmlFromByteArray(final Model model, final byte[] bytes) {
-        RdfIO.readRdfXmlWithEmptyBaseUri(model, new ByteArrayInputStream(bytes), DEPOSIT_BASE_URI);
-    }
-
-    private byte[] writeXmlToByteArray(final Element rdf) throws SwordServerException {
-        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        try {
-            rdf.writeTo(buffer);
-        } catch (IOException e) {
-            throw new SwordServerException("Error reading RDF", e);
-        }
-        return buffer.toByteArray();
-    }
-
-
-    private void processBinaryDeposit(final Deposit deposit, final DepositRequest depositRequest) throws SwordServerException {
-        LOG.info("Processing binary deposit payload");
-        final String filename = deposit.getFilename();
-        final byte[] bytes;
-        try {
-            bytes = IOUtils.toByteArray(deposit.getInputStream());
-        } catch (IOException e) {
-            throw new SwordServerException("Error reading content", e);
-        }
-        LOG.info("Adding resource '" + filename + "' to deposit");
-        final MimeType mimeType = deposit.getMimeType() == null ? null : new MimeType(deposit.getMimeType());
-        final InMemoryResource resource = new InMemoryResource(filename, mimeType, bytes);
-        depositRequest.addResource(resource);
-    }
-
-    private void processZipDeposit(final Deposit deposit, final DepositRequest depositRequest) throws SwordServerException {
-        LOG.info("Processing zip deposit payload");
-        final ZipInputStream in = new ZipInputStream(deposit.getInputStream());
-        try {
-            for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
-                final String filename = entry.getName();
-                final byte[] bytes = IOUtils.toByteArray(in);
-                final MimeType mimeType = depositRequest.getMetadataModel().getResource(filename).as(AggregatedResource.class).getFormat();
-                LOG.info("Adding resource '" + filename + "' [" + mimeType + "] to deposit");
-                final InMemoryResource resource = new InMemoryResource(filename, mimeType, bytes);
-                depositRequest.addResource(resource);
-            }
-        } catch (IOException e) {
-            throw new SwordServerException("Error reading content", e);
-        }
-    }
-
-
     @Override
     public Feed listCollectionContents(final IRI collectionIRI, final AuthCredentials auth, final SwordConfiguration config) throws SwordServerException, SwordAuthException {
         final Feed feed;
         final Entry entry = new FOMEntry();
         entry.setId(item.getUri().toString());
         entry.setTitle(item.getTitle());
+        entry.addLink(item.getUri().toString(), "edit");
         return entry;
     }
 

chempound-webapp/src/main/java/net/chempound/webapp/sword/impl/DepositReceiptFactory.java

+package net.chempound.webapp.sword.impl;
+
+import net.chempound.content.DepositResponse;
+import net.chempound.rdf.ore.AggregatedResource;
+import net.chempound.util.MimeType;
+import org.apache.abdera.i18n.iri.IRI;
+import org.swordapp.server.DepositReceipt;
+
+import java.net.URI;
+
+public class DepositReceiptFactory {
+
+    public DepositReceipt createDepositReceipt(final boolean packaged, final URI swordBase, final DepositResponse response, final URI uri, final URI relative) {
+        final DepositReceipt receipt = new DepositReceipt();
+
+        final URI swordEdit = swordBase.resolve("edit/");
+        final URI swordEditMedia = swordBase.resolve("edit-media/");
+        final URI swordStatement = swordBase.resolve("statement/");
+
+        receipt.setEditIRI(new IRI(swordEdit.resolve(relative)));
+        receipt.addEditMediaIRI(new IRI(swordEditMedia.resolve(relative)));
+        receipt.setStatementURI("application/rdf+xml", swordStatement.resolve(relative).toString());
+
+        receipt.setTreatment("Deposited.");
+        receipt.setSplashUri(uri.toString());
+
+        if (packaged) {
+            for (final AggregatedResource resource : response.getResources()) {
+                MimeType mimeType = resource.getFormat();
+                if (mimeType == null) {
+                    mimeType = MimeType.APPLICATION_OCTET_STREAM;
+                }
+                receipt.addDerivedResource(resource.getURI(), mimeType.getName());
+            }
+        }
+        return receipt;
+    }
+}

chempound-webapp/src/main/java/net/chempound/webapp/sword/impl/DepositRequestFactory.java

+package net.chempound.webapp.sword.impl;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import net.chempound.config.ContentUri;
+import net.chempound.config.SwordUri;
+import net.chempound.content.DefaultDepositRequest;
+import net.chempound.content.DepositRequest;
+import net.chempound.rdf.RdfIO;
+import net.chempound.rdf.ore.AggregatedResource;
+import net.chempound.storage.InMemoryResource;
+import net.chempound.util.MimeType;
+import org.apache.abdera.model.Element;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.swordapp.server.*;
+
+import javax.inject.Inject;
+import javax.xml.namespace.QName;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class DepositRequestFactory {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DepositRequestFactory.class);
+
+    private static final String DEPOSIT_BASE_URI = "";
+
+    private final URI swordUri;
+    private final URI contentUri;
+
+    @Inject
+    public DepositRequestFactory(@SwordUri final URI swordUri, @ContentUri final URI contentUri) {
+        this.swordUri = swordUri;
+        this.contentUri = contentUri;
+    }
+
+    public DepositRequest createDepositRequest(final Deposit deposit, final String collectionURI) throws SwordServerException, SwordError {
+        final DepositRequest depositRequest = new DefaultDepositRequest();
+        depositRequest.setSlug(deposit.getSlug());
+
+        final SwordEntry entry = deposit.getSwordEntry();
+        if (entry != null) {
+            handleDepositEntry(depositRequest, entry);
+        }
+
+        if (!deposit.isEntryOnly()) {
+            handleDepositPayload(deposit, depositRequest);
+        }
+
+        final URI depositBase = swordUri.resolve("collection/");
+        final URI targetPath = depositBase.relativize(URI.create(collectionURI));
+        final URI target = contentUri.resolve(targetPath);
+        LOG.info("SWORD deposit target: {}", target);
+        depositRequest.addCollection(target);
+
+        return depositRequest;
+    }
+
+    public boolean isPackaged(final Deposit deposit) {
+        return deposit.getPackaging() != null && UriRegistry.PACKAGE_SIMPLE_ZIP.equals(deposit.getPackaging());
+    }
+
+    private boolean handleDepositPayload(final Deposit deposit, final DepositRequest depositRequest) throws SwordServerException, SwordError {
+        if (deposit.getPackaging() == null || UriRegistry.PACKAGE_BINARY.equals(deposit.getPackaging())) {
+            LOG.info("Deposit with BINARY packaging");
+            processBinaryDeposit(deposit, depositRequest);
+            return false;
+        } else if (UriRegistry.PACKAGE_SIMPLE_ZIP.equals(deposit.getPackaging())) {
+            LOG.info("Deposit with SIMPLE_ZIP packaging");
+            processZipDeposit(deposit, depositRequest);
+            return true;
+        } else {
+            throw new SwordError(UriRegistry.ERROR_CONTENT);
+        }
+    }
+
+    private void handleDepositEntry(final DepositRequest depositRequest, final SwordEntry entry) throws SwordServerException {
+        extractRdfMetadata(depositRequest, entry);
+
+        if (entry.getTitle() != null) {
+            depositRequest.setTitle(entry.getTitle());
+        }
+        // TODO handle DC elements
+    }
+
+    private void extractRdfMetadata(final DepositRequest depositRequest, final SwordEntry entry) throws SwordServerException {
+        final Element rdf = entry.getEntry().getExtension(QName.valueOf("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF"));
+        if (rdf != null) {
+            final byte[] bytes = writeXmlToByteArray(rdf);
+            readXmlFromByteArray(depositRequest.getMetadataModel(), bytes);
+        }
+    }
+
+    private void readXmlFromByteArray(final Model model, final byte[] bytes) {
+        RdfIO.readRdfXmlWithEmptyBaseUri(model, new ByteArrayInputStream(bytes), DEPOSIT_BASE_URI);
+    }
+
+    private byte[] writeXmlToByteArray(final Element rdf) throws SwordServerException {
+        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        try {
+            rdf.writeTo(buffer);
+        } catch (IOException e) {
+            throw new SwordServerException("Error reading RDF", e);
+        }
+        return buffer.toByteArray();
+    }
+
+
+    private void processBinaryDeposit(final Deposit deposit, final DepositRequest depositRequest) throws SwordServerException {
+        LOG.info("Processing binary deposit payload");
+        final String filename = deposit.getFilename();
+        final byte[] bytes;
+        try {
+            bytes = IOUtils.toByteArray(deposit.getInputStream());
+        } catch (IOException e) {
+            throw new SwordServerException("Error reading content", e);
+        }
+        LOG.info("Adding resource '" + filename + "' to deposit");
+        final MimeType mimeType = deposit.getMimeType() == null ? null : new MimeType(deposit.getMimeType());
+        final InMemoryResource resource = new InMemoryResource(filename, mimeType, bytes);
+        depositRequest.addResource(resource);
+    }
+
+    private void processZipDeposit(final Deposit deposit, final DepositRequest depositRequest) throws SwordServerException {
+        LOG.info("Processing zip deposit payload");
+        final ZipInputStream in = new ZipInputStream(deposit.getInputStream());
+        try {
+            for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
+                final String filename = entry.getName();
+                final byte[] bytes = IOUtils.toByteArray(in);
+                final MimeType mimeType = depositRequest.getMetadataModel().getResource(filename).as(AggregatedResource.class).getFormat();
+                LOG.info("Adding resource '" + filename + "' [" + mimeType + "] to deposit");
+                final InMemoryResource resource = new InMemoryResource(filename, mimeType, bytes);
+                depositRequest.addResource(resource);
+            }
+        } catch (IOException e) {
+            throw new SwordServerException("Error reading content", e);
+        }
+    }
+
+
+}

chempound-webapp/src/test/java/net/chempound/webapp/sword/impl/ChempoundCollectionManagerTest.java

     private static final URI COLLECTION_URI = URI.create("http://repo.example.com/content/foo/");
 
     private Chempound chempound;
+    private DepositRequestFactory depositRequestFactory;
+    private DepositReceiptFactory depositReceiptFactory;
 
     @Before
     public void setUp() throws Exception {
         chempound = mock(Chempound.class);
+
+        depositRequestFactory = new DepositRequestFactory(SWORD_URI, CONTENT_URI);
+        depositReceiptFactory = new DepositReceiptFactory();
     }
 
     @Test
         when(chempound.listCollectionItems(COLLECTION_URI))
                 .thenReturn(Collections.<ItemInfo>emptyList());
 
-        ChempoundCollectionManager manager = new ChempoundCollectionManager(chempound, SWORD_URI, CONTENT_URI);
+        ChempoundCollectionManager manager = new ChempoundCollectionManager(chempound, depositRequestFactory, depositReceiptFactory, SWORD_URI, CONTENT_URI);
         Feed feed = manager.listCollectionContents(new IRI("http://repo.example.com/sword/collection/foo/"), null, null);
         assertEquals(0, feed.getEntries().size());
     }
                    new ItemInfoImpl(URI.create("http://repo.example.com/content/foo/c/"), null, null, null, null)
                 ));
 
-        ChempoundCollectionManager manager = new ChempoundCollectionManager(chempound, SWORD_URI, CONTENT_URI);
+        ChempoundCollectionManager manager = new ChempoundCollectionManager(chempound, depositRequestFactory, depositReceiptFactory, SWORD_URI, CONTENT_URI);
         Feed feed = manager.listCollectionContents(new IRI("http://repo.example.com/sword/collection/foo/"), null, null);
         assertEquals(3, feed.getEntries().size());
     }
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.