Commits

Sam Adams committed 543bc72

Upgrade httpclient

  • Participants
  • Parent commits 940dcfa

Comments (0)

Files changed (5)

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

 package net.chempound.client;
 
+import org.apache.http.entity.mime.FormBodyPart;
 import org.apache.http.entity.mime.MIME;
 import org.apache.http.entity.mime.content.ContentBody;
-import org.apache.james.mime4j.descriptor.ContentDescriptor;
-import org.apache.james.mime4j.message.BodyPart;
-import org.apache.james.mime4j.message.Header;
 
 /**
  * @author Sam Adams
  */
-public class AttachmentBodyPart extends BodyPart {
+public class AttachmentBodyPart extends FormBodyPart {
 
-    private final String name;
     private final String md5;
 
     public AttachmentBodyPart(final String name, final ContentBody body, final String md5) {
-        super();
+        super(name, body);
         if (name == null) {
             throw new IllegalArgumentException("Name may not be null");
         }
         if (body == null) {
             throw new IllegalArgumentException("Body may not be null");
         }
-        this.name = name;
         this.md5 = md5;
-
-        final Header header = new Header();
-        setHeader(header);
-        setBody(body);
-
-        generateContentDisp(body);
-        generateContentType(body);
-        generateTransferEncoding(body);
     }
 
-    public String getName() {
-        return this.name;
-    }
-
+    @Override
     protected void generateContentDisp(final ContentBody body) {
         final StringBuilder buffer = new StringBuilder();
         buffer.append("form-data; name=\"");
         }
     }
 
-    protected void generateContentType(final ContentDescriptor desc) {
-        if (desc.getMimeType() != null) {
-            final StringBuilder buffer = new StringBuilder();
-            buffer.append(desc.getMimeType());
-            if (desc.getCharset() != null) {
-                buffer.append("; charset=");
-                buffer.append(desc.getCharset());
-            }
-            addField(MIME.CONTENT_TYPE, buffer.toString());
-        }
-    }
-
-    protected void generateTransferEncoding(final ContentDescriptor desc) {
-        if (desc.getTransferEncoding() != null) {
-            addField(MIME.CONTENT_TRANSFER_ENC, desc.getTransferEncoding());
-        }
-    }
-
-    private void addField(final String name, final String value) {
-        getHeader().addField(new MinimalField(name, value));
-    }
-
-
 }

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() {
-        final 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

 import org.apache.http.entity.mime.content.ContentBody;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.protocol.HTTP;
-import org.apache.james.mime4j.field.Fields;
-import org.apache.james.mime4j.message.Message;
 
 import java.io.IOException;
 import java.io.InputStream;
         "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
             .toCharArray();
 
-    private final Message message;
     private final HttpMultipart multipart;
     private final Header contentType;
 
     private volatile boolean dirty; // used to decide whether to recalculate length
 
     public MultipartRelatedEntity(
-            final HttpMultipartMode mode,
-            final String boundary,
+            HttpMultipartMode mode,
+            String boundary,
             final Charset charset) {
         super();
-        this.multipart = new HttpMultipart("related");
+        if (boundary == null) {
+            boundary = generateBoundary();
+        }
+        if (mode == null) {
+            mode = HttpMultipartMode.STRICT;
+        }
+        this.multipart = new HttpMultipart("related", charset, boundary, mode);
         this.contentType = new BasicHeader(
-                HTTP.CONTENT_TYPE,
-                generateContentType(boundary, charset));
+            HTTP.CONTENT_TYPE,
+            generateContentType(boundary, charset));
         this.dirty = true;
-
-        this.message = new Message();
-        final org.apache.james.mime4j.message.Header header =
-          new org.apache.james.mime4j.message.Header();
-        this.message.setHeader(header);
-        this.multipart.setParent(message);
-        this.multipart.setMode(mode == null ? HttpMultipartMode.STRICT : mode);
-        this.message.getHeader().addField(Fields.contentType(this.contentType.getValue()));
     }
 
     public MultipartRelatedEntity(final HttpMultipartMode mode) {
         return buffer.toString();
     }
 
+    protected String generateBoundary() {
+        final StringBuilder buffer = new StringBuilder();
+        final Random rand = new Random();
+        final int count = rand.nextInt(11) + 30; // a random size from 30 to 40
+        for (int i = 0; i < count; i++) {
+            buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
+        }
+        return buffer.toString();
+    }
+
     public void addPart(final String name, final ContentBody contentBody) {
         addPart(new AttachmentBodyPart(name, contentBody, null));
     }
 
-    public void addPart(final AttachmentBodyPart bodyPart) {
-        this.multipart.addBodyPart(bodyPart);
+    public void addPart(final AttachmentBodyPart part) {
+        this.multipart.addBodyPart(part);
         this.dirty = true;
     }
 
         final List<?> parts = this.multipart.getBodyParts();
         for (final Iterator<?> it = parts.iterator(); it.hasNext();) {
             final AttachmentBodyPart part = (AttachmentBodyPart) it.next();
-            final ContentBody body = (ContentBody) part.getBody();
+            final ContentBody body = part.getBody();
             if (body.getContentLength() < 0) {
                 return false;
             }

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

 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.mime.Field;
 import org.apache.http.entity.mime.HttpMultipartMode;
 import org.apache.http.entity.mime.content.ContentBody;
 import org.apache.http.impl.client.DefaultHttpClient;
         final MultipartRelatedEntity entity = new MultipartRelatedEntity(HttpMultipartMode.STRICT);
         entity.addPart("atom", createContentBody(entry));
         final AttachmentBodyPart bodyPart = new AttachmentBodyPart("payload", createPayloadBody(deposit), null);
-        bodyPart.getHeader().addField(new MinimalField(PACKAGING, PACKAGING_SIMPLE_ZIP));
+        bodyPart.getHeader().addField(new Field(PACKAGING, PACKAGING_SIMPLE_ZIP));
         entity.addPart(bodyPart);
 
         request.setEntity(entity);

File client-utils/src/main/java/org/apache/http/entity/mime/Field.java

+package org.apache.http.entity.mime;
+
+/**
+ * @author Sam Adams
+ */
+public class Field extends MinimalField {
+
+    public Field(final String name, final String value) {
+        super(name, value);
+    }
+
+}