Commits

Sam Adams committed c5f3b93

Fix to SwordClient package deposit

  • Participants
  • Parent commits 5c6e7c3

Comments (0)

Files changed (7)

File client-cli/src/main/java/net/chempound/client/cli/command/DepositCommand.java

 package net.chempound.client.cli.command;
 
 import net.chempound.client.DepositBuilder;
+import net.chempound.client.SwordClient;
 import net.chempound.client.cli.options.DepositOptions;
 import net.chempound.client.cli.options.GlobalOptions;
+import net.chempound.client.sword.DepositReceipt;
 import net.chempound.storage.LocalFileResource;
 import net.chempound.storage.LocalResource;
 import org.apache.commons.io.FilenameUtils;
 import org.swordapp.client.Deposit;
-import org.swordapp.client.DepositReceipt;
 import org.swordapp.client.SWORDClient;
 
 import java.io.File;
+import java.net.URI;
 
 /**
  * @author Sam Adams
 public class DepositCommand implements Command<DepositOptions> {
 
     protected final DepositOptions depositOptions;
+    protected final SwordClient swordClient;
 
     public DepositCommand() {
         this(new DepositOptions());
     }
 
     protected DepositCommand(final DepositOptions depositOptions) {
+        this(depositOptions, new SwordClient());
+    }
+
+    protected DepositCommand(final DepositOptions depositOptions, final SwordClient swordClient) {
         this.depositOptions = depositOptions;
+        this.swordClient = swordClient;
     }
 
     @Override
         setTitle(depositBuilder);
         setSlug(depositBuilder);
 
-        final SWORDClient swordClient = new SWORDClient();
         final String url = globalOptions.repository;
         try {
-            final Deposit deposit = depositBuilder.build();
-            final DepositReceipt receipt = swordClient.deposit(url, deposit);
-
+            final DepositReceipt depositReceipt = swordClient.deposit(URI.create(url), depositBuilder.buildDepositRequest());
         } catch (Exception e) {
             e.printStackTrace();
         }

File client-cli/src/test/java/net/chempound/client/cli/ChempoundClientCLIIntegrationTest.java

                 new DefaultChempoundModule(),
                 new DefaultChempoundWebModule(),
                 new ChempoundConfigurationModule(configuration)
-
         );
 
         component = injector.getInstance(Component.class);
 
     @After
     public void tearDown() throws Exception {
-        if (tmpDir != null) {
-            FileUtils.deleteQuietly(tmpDir);
-        }
+        FileUtils.deleteQuietly(tmpDir);
     }
 
     @Test

File client-utils/src/main/java/net/chempound/client/AttachmentBodyPart.java

 import org.apache.james.mime4j.descriptor.ContentDescriptor;
 import org.apache.james.mime4j.message.BodyPart;
 import org.apache.james.mime4j.message.Header;
-import org.apache.james.mime4j.parser.Field;
-import org.apache.james.mime4j.util.ByteSequence;
-import org.apache.james.mime4j.util.ContentUtil;
 
 /**
  * @author Sam Adams
 
     protected void generateContentDisp(final ContentBody body) {
         StringBuilder buffer = new StringBuilder();
-        buffer.append("attachment; name=\"");
+        buffer.append("form-data; name=\"");
         buffer.append(getName());
         buffer.append("\"");
         if (body.getFilename() != null) {
     }
 
 
-    static class MinimalField implements Field {
-
-        private final String name;
-        private final String value;
-
-        private ByteSequence raw; // cache, recreated on demand
-
-        MinimalField(final String name, final String value) {
-            super();
-            this.name = name;
-            this.value = value;
-            this.raw = null;
-        }
-
-        public String getName() {
-            return this.name;
-        }
-
-        public String getBody() {
-            return this.value;
-        }
-
-        public ByteSequence getRaw() {
-            if (this.raw == null) {
-                this.raw = ContentUtil.encode(toString());
-            }
-            return this.raw;
-        }
-
-        @Override
-        public String toString() {
-            StringBuilder buffer = new StringBuilder();
-            buffer.append(this.name);
-            buffer.append(": ");
-            buffer.append(this.value);
-            return buffer.toString();
-        }
-
-    }
-
 }

File client-utils/src/main/java/net/chempound/client/DepositBuilder.java

         depositRequest.addResource(resource);
     }
 
+    public DepositRequest buildDepositRequest() {
+        return depositRequest;
+    }
+
     public Deposit build() throws Exception {
         return SwordUtil.createSwordDeposit(depositRequest);
     }

File client-utils/src/main/java/net/chempound/client/MinimalField.java

+package net.chempound.client;
+
+import org.apache.james.mime4j.parser.Field;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+/**
+* @author Sam Adams
+*/
+public class MinimalField implements Field {
+
+    private final String name;
+    private final String value;
+
+    private ByteSequence raw; // cache, recreated on demand
+
+    MinimalField(final String name, final String value) {
+        super();
+        this.name = name;
+        this.value = value;
+        this.raw = null;
+    }
+
+    public String getName() {
+        return this.name;
+    }
+
+    public String getBody() {
+        return this.value;
+    }
+
+    public ByteSequence getRaw() {
+        if (this.raw == null) {
+            this.raw = ContentUtil.encode(toString());
+        }
+        return this.raw;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder buffer = new StringBuilder();
+        buffer.append(this.name);
+        buffer.append(": ");
+        buffer.append(this.value);
+        return buffer.toString();
+    }
+
+}

File client-utils/src/main/java/net/chempound/client/MultipartRelatedEntity.java

 package net.chempound.client;
 
+import com.sun.xml.internal.ws.api.message.Attachment;
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.entity.mime.HttpMultipart;
     }
 
     public void addPart(final String name, final ContentBody contentBody) {
-        this.multipart.addBodyPart(new AttachmentBodyPart(name, contentBody, null));
+        addPart(new AttachmentBodyPart(name, contentBody, null));
+    }
+
+    public void addPart(final AttachmentBodyPart bodyPart) {
+        this.multipart.addBodyPart(bodyPart);
         this.dirty = true;
     }
 

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

 import net.chempound.storage.LocalResource;
 import net.sf.atomxom.AtomBuilder;
 import nu.xom.*;
+import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpResponse;
 
     public DepositReceipt deposit(final URI uri, final DepositRequest deposit, final boolean discoveryEnabled) throws Exception {
         final URI depositUri = discoveryEnabled ? discoverDepositUri(uri) : uri;
-        System.err.println("deposit to: "+depositUri);
 
         final HttpPost request = createDepositRequest(depositUri, deposit);
         final HttpResponse response = httpClient.execute(request);
 
         final MultipartRelatedEntity entity = new MultipartRelatedEntity(HttpMultipartMode.STRICT);
         entity.addPart("atom", createContentBody(entry));
-        entity.addPart("payload", createPayloadBody(deposit));
+        final AttachmentBodyPart bodyPart = new AttachmentBodyPart("payload", createPayloadBody(deposit), null);
+        bodyPart.getHeader().addField(new MinimalField(PACKAGING, PACKAGING_SIMPLE_ZIP));
+        entity.addPart(bodyPart);
 
-        request.setHeader(PACKAGING, PACKAGING_SIMPLE_ZIP);
         request.setEntity(entity);
     }