Commits

Sam Adams committed 932d060

Fixed client multipart encoding

Comments (0)

Files changed (2)

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

             final String boundary,
             final Charset charset) {
         super();
-        this.multipart = new HttpMultipart("related", charset, boundary == null ? generateBoundary() : boundary, mode == null ? HttpMultipartMode.STRICT : mode);
+        final String bdry = boundary == null ? generateBoundary() : boundary;
+        this.multipart = new HttpMultipart("related", charset, bdry, mode == null ? HttpMultipartMode.STRICT : mode);
         this.contentType = new BasicHeader(
             HTTP.CONTENT_TYPE,
-            generateContentType(boundary, charset));
+            generateContentType(bdry, charset));
         this.dirty = true;
     }
 

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

 import org.apache.http.entity.mime.HttpMultipartMode;
 import org.apache.http.entity.mime.content.ContentBody;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
 import org.xml.sax.SAXException;
 import org.xml.sax.XMLReader;
 import org.xml.sax.helpers.XMLReaderFactory;
                 throw new IOException(response.getStatusLine().toString());
             }
         } finally {
-            if (response.getEntity() != null) {
-                response.getEntity().consumeContent();
-            }
+            closeQuietly(response);
         }
     }
 
         return new ByteArrayBody(bytes, "application/atom+xml", "deposit.atom", "UTF-8");
     }
 
-    private ContentBody createPayloadBody(final DepositRequest deposit) throws IOException {
+    private static ContentBody createPayloadBody(final DepositRequest deposit) throws IOException {
         return new ByteArrayBody(createZipFile(deposit), "application/zip", "deposit.zip");
     }
 
         return writeDocument(element.getDocument() == null ? new Document(element) : element.getDocument());
     }
 
-    private byte[] writeDocument(final Document document) throws IOException {
+    private static byte[] writeDocument(final Document document) throws IOException {
         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         final Serializer ser = new Serializer(buffer);
         ser.write(document);
         return buffer.toByteArray();
     }
 
-    private SwordEntry createSwordEntry(final DepositRequest deposit) {
+    private static SwordEntry createSwordEntry(final DepositRequest deposit) {
         final SwordEntry entry = new SwordEntry();
         entry.setTitle(deposit.getTitle());
 
                 return nodes.size() == 0 ? uri : URI.create(nodes.get(0).getValue());
             }
         } finally {
-            if (response.getEntity() != null) {
-                response.getEntity().consumeContent();
-            }
+            closeQuietly(response);
         }
         return uri;
     }
 
-    private boolean isServiceDocument(final Document doc) {
+    private static boolean isServiceDocument(final Document doc) {
         final Element root = doc.getRootElement();
         return ATOMPUB_NAMESPACE.equals(root.getNamespaceURI()) && "service".equals(root.getLocalName());
     }
 
-    private URI findServiceDocumentUrl(final Document doc) {
+    private static URI findServiceDocumentUrl(final Document doc) {
         final Nodes nodes = doc.query("//h:link[@rel='http://purl.org/net/sword/terms/service-document' or @rel='sword']/@href", HTML_CONTEXT);
         return nodes.size() == 0 ? null : URI.create(nodes.get(0).getValue());
     }
 
-    private Document readHtml(final HttpResponse response) throws SAXException, ParsingException, IOException {
+    private static Document readHtml(final HttpResponse response) throws SAXException, ParsingException, IOException {
         final byte[] bytes = IOUtils.toByteArray(response.getEntity().getContent());
         try {
             final XMLReader parser = XMLReaderFactory.createXMLReader();
         }
     }
 
-    private Document readSwordDocument(final HttpResponse response) throws IOException {
+    private static Document readSwordDocument(final HttpResponse response) throws IOException {
         try {
             final byte[] bytes = IOUtils.toByteArray(response.getEntity().getContent());
             final XMLReader parser = XMLReaderFactory.createXMLReader();
     private static void closeQuietly(final HttpResponse response) {
         if (response.getEntity() != null) {
             try {
-                response.getEntity().consumeContent();
+                EntityUtils.consume(response.getEntity());
             } catch (IOException e) {
+                // ignore
             }
         }
     }