Commits

Sam Adams  committed e640130

Refactoring deposit client

  • Participants
  • Parent commits 66e62aa

Comments (0)

Files changed (6)

 
     <modelVersion>4.0.0</modelVersion>
 
-    <groupId>net.quixote</groupId>
-    <artifactId>quixote-client</artifactId>
+    <artifactId>deposit-client</artifactId>
     <version>0.1-SNAPSHOT</version>
 
-    <name>Quixote Deposit Client</name>
+    <name>Chempound Deposit Client</name>
 
     <dependencies>
 
         <dependency>
+            <groupId>uk.ac.cam.ch.wwmm.chempound</groupId>
+            <artifactId>chempound-api</artifactId>
+            <version>0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
             <groupId>org.swordapp</groupId>
             <artifactId>sword-client</artifactId>
             <version>2.0-SNAPSHOT</version>
         </dependency>
 
         <dependency>
-            <groupId>uk.ac.cam.ch.wwmm.chempound.crystallography</groupId>
-            <artifactId>crystallography-importer</artifactId>
-            <version>0.1-SNAPSHOT</version>
-        </dependency>
-        <!--<dependency>-->
-            <!--<groupId>uk.ac.cam.ch.wwmm.chempound.compchem</groupId>-->
-            <!--<artifactId>compchem-importer</artifactId>-->
-            <!--<version>0.1-SNAPSHOT</version>-->
-        <!--</dependency>-->
-
-        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <version>[1.6.1]</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.0.3</version>
+        </dependency>
+
     </dependencies>
 
     <build>
                     </descriptorRefs>
                     <archive>
                         <manifest>
-                            <mainClass>uk.ac.cam.ch.wwmm.chempound.deposit.DepositCif</mainClass>
+                            <mainClass>net.quixote.utils.DepositCif</mainClass>
                         </manifest>
                     </archive>
                 </configuration>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>exec-maven-plugin</artifactId>
                 <configuration>
-                    <mainClass>uk.ac.cam.ch.wwmm.chempound.deposit.DepositCif</mainClass>
+                    <mainClass>net.quixote.utils.DepositCif</mainClass>
                 </configuration>
             </plugin>
 
         </plugins>
     </build>
 
+    <repositories>
+        <repository>
+            <id>ucc-repo</id>
+            <url>https://maven.ch.cam.ac.uk/m2repo</url>
+        </repository>
+    </repositories>
+
 </project>

File src/main/java/uk/ac/cam/ch/wwmm/chempound/deposit/DepositCif.java

-package uk.ac.cam.ch.wwmm.chempound.deposit;
-
-import com.hp.hpl.jena.rdf.model.*;
-import com.hp.hpl.jena.rdf.model.Statement;
-import com.hp.hpl.jena.xmloutput.RDFXMLWriterI;
-import com.hp.hpl.jena.xmloutput.impl.Abbreviated;
-import nu.xom.ParsingException;
-import org.apache.abdera.Abdera;
-import org.apache.abdera.model.Document;
-import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOUtils;
-import org.swordapp.client.*;
-import org.xmlcml.cif.CIF;
-import org.xmlcml.cif.CIFException;
-import uk.ac.cam.ch.wwmm.chempound.content.DefaultContentLoadRequest;
-import uk.ac.cam.ch.wwmm.chempound.crystal.importer.CrystalStructureImporter;
-import uk.ac.cam.ch.wwmm.chempound.crystal.importer.utils.CifIO;
-import uk.ac.cam.ch.wwmm.chempound.rdf.DCTerms;
-import uk.ac.cam.ch.wwmm.chempound.storage.AggregatedResource;
-
-import javax.annotation.Resource;
-import java.io.*;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
-
-/**
- * @author sea36
- */
-public class DepositCif {
-
-
-    public static void main(String[] args) throws SWORDClientException, SWORDError, ProtocolViolationException, IOException, CIFException, ParsingException {
-
-        if (args.length != 2) {
-            System.err.println("Two arguments required:");
-            System.err.println("  <SWORD Endpoint> <CIF File>");
-            System.exit(1);
-        }
-
-        String swordEndpoint = args[0];
-        String cifFile = args[1];
-
-        File file = new File(cifFile);
-        if (!file.isFile()) {
-            System.err.println("File not found: "+file);
-            System.exit(1);
-        }
-
-        System.err.println();
-        System.err.println("**");
-        System.err.println("** Processing CIF");
-        System.err.println("**");
-        System.err.println();
-
-        Deposit deposit = prepareDeposit(file);
-
-        System.err.println();
-        System.err.println("**");
-        System.err.println("** Executing SWORD deposit");
-        System.err.println("**");
-        System.err.println();
-
-        SWORDClient client = new SWORDClient();
-        DepositReceipt receipt = client.deposit(swordEndpoint, deposit);
-
-        if (receipt == null) {
-            System.err.println();
-            System.err.println("**                             **");
-            System.err.println("** DEPOSIT FAILED : NO RECEIPT **");
-            System.err.println("**                             **");
-            System.err.println();
-            System.exit(1);
-        }
-
-        System.err.println("**");
-        System.err.println("** Deposit complete");
-        System.err.println("**   status: "+receipt.getStatusCode());
-        System.err.println("**");
-
-    }
-
-    private static Deposit prepareDeposit(File file) throws CIFException, IOException, ParsingException {
-        CIF cif = CifIO.readCif(file, "UTF-8");
-
-        String id = getId(file);
-        CrystalStructureImporter importer = new CrystalStructureImporter();
-        DefaultContentLoadRequest request = importer.generateDepositRequest(cif, id);
-
-        return createDeposit(request);
-    }
-
-    private static String getId(File file) {
-        String filename = file.getName();
-        String ext = FilenameUtils.getExtension(filename).toLowerCase();
-        if ("cif".equals(ext) || "txt".equals(ext)) {
-            int l = filename.length();
-            filename.substring(0, l-4);
-        }
-        return filename;
-    }
-
-
-    private static Deposit createDeposit(DefaultContentLoadRequest request) throws IOException {
-        Deposit deposit = new Deposit();
-
-        EntryPart entryPart = new EntryPart();
-        String title = "Crystal Structure";
-
-        if (!request.getMetadataModel().isEmpty()) {
-            attachMetaData(request, entryPart);
-            Statement s = request.getMetadataModel().getResource("").getRequiredProperty(DCTerms.title);
-            if (s != null) {
-                title = s.getString();
-            }
-        }
-        entryPart.getEntry().setTitle(title);
-        deposit.setEntryPart(entryPart);
-
-        byte[] zip = createZipFile(request);
-
-        deposit.setFile(new ByteArrayInputStream(zip));
-        deposit.setMimeType("application/zip");
-        deposit.setFilename("deposit.zip");
-        deposit.setMd5(DigestUtils.md5Hex(zip));
-        deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP);
-
-        return deposit;
-    }
-
-    private static byte[] createZipFile(DefaultContentLoadRequest request) throws IOException {
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        ZipOutputStream out = new ZipOutputStream(buffer);
-        for (AggregatedResource resource : request.getAggregatedResources()) {
-            ZipEntry zipEntry = new ZipEntry(resource.getUri().toString());
-            out.putNextEntry(zipEntry);
-            InputStream in = resource.openInputStream();
-            IOUtils.copy(in, out);
-            IOUtils.closeQuietly(in);
-        }
-        out.close();
-
-        return buffer.toByteArray();
-    }
-
-    private static void attachMetaData(DefaultContentLoadRequest request, EntryPart entryPart) {
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-
-        RDFXMLWriterI w = new Abbreviated();
-        w.setProperty("allowBadURIs", true);
-        w.write(request.getMetadataModel(), buffer, null);
-
-        Abdera abdera = new Abdera();
-        Document doc = abdera.getParser().parse(new ByteArrayInputStream(buffer.toByteArray()));
-        entryPart.getEntry().addExtension(doc.getRoot());
-    }
-
-}

File src/main/java/uk/ac/cam/ch/wwmm/chempound/deposit/DepositClient.java

+package uk.ac.cam.ch.wwmm.chempound.deposit;
+
+import org.swordapp.client.*;
+import uk.ac.cam.ch.wwmm.chempound.content.ContentLoadRequest;
+
+import java.io.IOException;
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public abstract class DepositClient {
+
+    private URI swordEndpoint;
+
+    public URI getSwordEndpoint() {
+        return swordEndpoint;
+    }
+
+    public void setSwordEndpoint(URI swordEndpoint) {
+        this.swordEndpoint = swordEndpoint;
+    }
+
+    public void setSwordEndpoint(String swordEndpoint) {
+        this.swordEndpoint = URI.create(swordEndpoint);
+    }
+
+
+    public DepositReceipt executeDeposit() throws SWORDClientException, SWORDError, ProtocolViolationException, IOException {
+
+        SWORDClient client = new SWORDClient();
+        Deposit deposit = prepareSwordDeposit();
+        DepositReceipt receipt = client.deposit(swordEndpoint.toString(), deposit);
+
+        return receipt;
+    }
+
+    private Deposit prepareSwordDeposit() throws IOException {
+        ContentLoadRequest depositRequest = getDepositRequest();
+        return SwordUtil.createSwordDeposit(depositRequest);
+    }
+
+    protected abstract ContentLoadRequest getDepositRequest();
+
+}

File src/main/java/uk/ac/cam/ch/wwmm/chempound/deposit/DepositCollection.java

+package uk.ac.cam.ch.wwmm.chempound.deposit;
+
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.vocabulary.RDF;
+import com.hp.hpl.jena.xmloutput.RDFXMLWriterI;
+import com.hp.hpl.jena.xmloutput.impl.Abbreviated;
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Document;
+import org.swordapp.client.Deposit;
+import org.swordapp.client.DepositReceipt;
+import org.swordapp.client.EntryPart;
+import org.swordapp.client.SWORDClient;
+import uk.ac.cam.ch.wwmm.chempound.content.ContentLoadRequest;
+import uk.ac.cam.ch.wwmm.chempound.content.DefaultContentLoadRequest;
+import uk.ac.cam.ch.wwmm.chempound.rdf.CPTerms;
+import uk.ac.cam.ch.wwmm.chempound.rdf.DCTerms;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+/**
+ * @author sea36
+ */
+public class DepositCollection extends DepositClient {
+
+    private String id;
+    private String title;
+
+    public void setCollectionId(String id) {
+        this.id = id;
+    }
+
+    public void setCollectionTitle(String title) {
+        this.title = title;
+    }
+
+    @Override
+    protected ContentLoadRequest getDepositRequest() {
+        ContentLoadRequest request = new DefaultContentLoadRequest();
+        request.setSlug(id);
+        request.getResourceMetadata().setTitle(title);
+        request.getResourceMetadata().addProperty(RDF.type, CPTerms.Collection);
+        return request;
+    }
+
+
+    public static void main(String[] args) throws Exception {
+
+        if (args.length != 3) {
+            System.err.println("Two arguments required:");
+            System.err.println("  <SWORD Endpoint> <Collection> <Title>");
+            System.exit(1);
+        }
+
+        String swordEndpoint = args[0];
+        String id = args[1];
+        String title = args[2];
+
+        System.err.println();
+        System.err.println("**");
+        System.err.println("** Executing SWORD deposit");
+        System.err.println("**");
+        System.err.println();
+
+        DepositCollection client = new DepositCollection();
+        client.setSwordEndpoint(swordEndpoint);
+        client.setCollectionId(id);
+        client.setCollectionTitle(title);
+        DepositReceipt receipt = client.executeDeposit();
+
+        if (receipt == null) {
+            System.err.println();
+            System.err.println("**                             **");
+            System.err.println("** DEPOSIT FAILED : NO RECEIPT **");
+            System.err.println("**                             **");
+            System.err.println();
+            System.exit(1);
+        }
+
+        System.err.println("**");
+        System.err.println("** Deposit complete");
+        System.err.println("**   status: "+receipt.getStatusCode());
+        System.err.println("**");
+
+    }
+
+}

File src/main/java/uk/ac/cam/ch/wwmm/chempound/deposit/SwordUtil.java

+package uk.ac.cam.ch.wwmm.chempound.deposit;
+
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.vocabulary.DC;
+import com.hp.hpl.jena.vocabulary.RDFS;
+import com.hp.hpl.jena.xmloutput.RDFXMLWriterI;
+import com.hp.hpl.jena.xmloutput.impl.Abbreviated;
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Document;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.IOUtils;
+import org.swordapp.client.Deposit;
+import org.swordapp.client.EntryPart;
+import org.swordapp.client.UriRegistry;
+import uk.ac.cam.ch.wwmm.chempound.content.ContentLoadRequest;
+import uk.ac.cam.ch.wwmm.chempound.rdf.DCTerms;
+import uk.ac.cam.ch.wwmm.chempound.storage.DepositResource;
+import uk.ac.cam.ch.wwmm.chempound.storage.LocalResource;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * @author Sam Adams
+ */
+public class SwordUtil {
+
+    public static Deposit createSwordDeposit(ContentLoadRequest request) throws IOException {
+        Deposit deposit = new Deposit();
+
+        EntryPart entryPart = new EntryPart();
+
+        String title = "untitled";
+        if (!request.getMetadataModel().isEmpty()) {
+            attachMetaData(request, entryPart);
+            Statement s = getTitleStatement(request);
+            if (s != null) {
+                if (s.getObject().isLiteral()) {
+                    title = s.getString();
+                }
+            }
+        }
+        entryPart.getEntry().setTitle(title);
+        deposit.setEntryPart(entryPart);
+
+        deposit.setSlug(request.getSlug());
+        byte[] zip = createZipFile(request);
+        if (zip != null) {
+            deposit.setFile(new ByteArrayInputStream(zip));
+            deposit.setMimeType("application/zip");
+            deposit.setFilename("deposit.zip");
+            deposit.setMd5(DigestUtils.md5Hex(zip));
+            deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP);
+        }
+
+        return deposit;
+    }
+
+    private static Statement getTitleStatement(ContentLoadRequest request) {
+        Statement s = request.getResourceMetadata().getProperty(DCTerms.title);
+        if (s == null) {
+            s = request.getResourceMetadata().getProperty(DC.title);
+        }
+        if (s == null) {
+            s = request.getResourceMetadata().getProperty(RDFS.label);
+        }
+        return s;
+    }
+
+    private static byte[] createZipFile(ContentLoadRequest request) throws IOException {
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        ZipOutputStream out = new ZipOutputStream(buffer);
+        int n = 0;
+        for (DepositResource resource : request.getAggregatedResources()) {
+            if (resource instanceof LocalResource) {
+                ZipEntry zipEntry = new ZipEntry(resource.getUri().toString());
+                out.putNextEntry(zipEntry);
+                InputStream in = resource.openInputStream();
+                IOUtils.copy(in, out);
+                IOUtils.closeQuietly(in);
+                n++;
+            }
+        }
+        out.close();
+        return n == 0 ? null : buffer.toByteArray();
+    }
+
+    private static void attachMetaData(ContentLoadRequest request, EntryPart entryPart) {
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+
+        RDFXMLWriterI w = new Abbreviated();
+        w.setProperty("allowBadURIs", true);
+        w.write(request.getMetadataModel(), buffer, null);
+
+        Abdera abdera = new Abdera();
+        Document doc = abdera.getParser().parse(new ByteArrayInputStream(buffer.toByteArray()));
+        entryPart.getEntry().addExtension(doc.getRoot());
+    }
+
+}

File src/main/resources/log4j.properties

 log4j.appender.A.layout.ConversionPattern = %-4r [%t] %-5p %c %x - %m%n
 log4j.appender.A.target = System.err
 
-log4j.logger.uk.ac.cam.ch.wwmm.chempound.webapp = DEBUG
-