hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r616704 - in /httpcomponents/httpclient/trunk/module-httpmime/src: main/java/org/apache/http/client/mime/ main/java/org/apache/http/client/mime/content/ test/java/org/apache/http/client/mime/
Date Wed, 30 Jan 2008 10:00:38 GMT
Author: olegk
Date: Wed Jan 30 02:00:36 2008
New Revision: 616704

URL: http://svn.apache.org/viewvc?rev=616704&view=rev
Log:
MIME multipart/form-data primitives (file and input stream bodies)

Added:
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/ContentBody.java
      - copied, changed from r616700, httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/ContentBody.java
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java
  (with props)
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java
  (with props)
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/StringBody.java
      - copied, changed from r616700, httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/StringBody.java
Removed:
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/ContentBody.java
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/StringBody.java
Modified:
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/FormBodyPart.java
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/MIME.java
    httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/client/mime/TestMultipartForm.java

Modified: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/FormBodyPart.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/FormBodyPart.java?rev=616704&r1=616703&r2=616704&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/FormBodyPart.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/FormBodyPart.java
Wed Jan 30 02:00:36 2008
@@ -31,6 +31,7 @@
 
 package org.apache.http.client.mime;
 
+import org.apache.http.client.mime.content.ContentBody;
 import org.apache.james.mime4j.field.Field;
 import org.apache.james.mime4j.message.BodyPart;
 import org.apache.james.mime4j.message.Header;

Modified: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/MIME.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/MIME.java?rev=616704&r1=616703&r2=616704&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/MIME.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/MIME.java
Wed Jan 30 02:00:36 2008
@@ -40,5 +40,6 @@
     public static final String CONTENT_DISPOSITION   = "Content-Disposition";
  
     public static final String ENC_8BIT              = "8bit";
+    public static final String ENC_BINARY            = "binary";
     
 }

Copied: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/ContentBody.java
(from r616700, httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/ContentBody.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/ContentBody.java?p2=httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/ContentBody.java&p1=httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/ContentBody.java&r1=616700&r2=616704&rev=616704&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/ContentBody.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/ContentBody.java
Wed Jan 30 02:00:36 2008
@@ -29,8 +29,9 @@
  *
  */
 
-package org.apache.http.client.mime;
+package org.apache.http.client.mime.content;
 
+import org.apache.http.client.mime.ContentDescriptor;
 import org.apache.james.mime4j.message.Body;
 
 public interface ContentBody extends Body, ContentDescriptor {

Added: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java?rev=616704&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java
(added)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java
Wed Jan 30 02:00:36 2008
@@ -0,0 +1,99 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.client.mime.content;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.nio.charset.Charset;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.http.client.mime.MIME;
+import org.apache.james.mime4j.message.AbstractBody;
+
+public class FileBody extends AbstractBody implements ContentBody {
+
+    private final File file;
+    
+    public FileBody(final File file) {
+        super();
+        if (file == null) {
+            throw new IllegalArgumentException("File may not be null");
+        }
+        this.file = file;
+    }
+    
+    public Reader getReader() throws IOException {
+        return new InputStreamReader(new FileInputStream(this.file));
+    }
+
+    public void writeTo(final OutputStream out) throws IOException {
+        if (out == null) {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        InputStream in = new FileInputStream(this.file);
+        try {
+            IOUtils.copy(in, out);
+        } finally {
+            in.close();
+        }
+    }
+
+    public String getTransferEncoding() {
+        return MIME.ENC_BINARY;
+    }
+
+    public Charset getCharset() {
+        return null;
+    }
+
+    public String getMimeType() {
+        return "application/octet-stream";
+    }
+    
+    public long getContentLength() {
+        return this.file.length();
+    }
+    
+    public String getFilename() {
+        return this.file.getName();
+    }
+    
+    public File getFile() {
+        return this.file;
+    }
+    
+}

Propchange: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/FileBody.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java?rev=616704&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java
(added)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java
Wed Jan 30 02:00:36 2008
@@ -0,0 +1,94 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.client.mime.content;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.nio.charset.Charset;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.http.client.mime.MIME;
+import org.apache.james.mime4j.message.AbstractBody;
+
+public class InputStreamBody extends AbstractBody implements ContentBody {
+
+    private final InputStream in;
+    private final String filename;
+    
+    public InputStreamBody(final InputStream in, final String filename) {
+        super();
+        if (in == null) {
+            throw new IllegalArgumentException("Input stream may not be null");
+        }
+        this.in = in;
+        this.filename = filename;
+    }
+    
+    public Reader getReader() throws IOException {
+        return new InputStreamReader(this.in);
+    }
+
+    public void writeTo(final OutputStream out) throws IOException {
+        if (out == null) {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        try {
+            IOUtils.copy(this.in, out);
+        } finally {
+            this.in.close();
+        }
+    }
+
+    public String getTransferEncoding() {
+        return MIME.ENC_BINARY;
+    }
+
+    public Charset getCharset() {
+        return null;
+    }
+
+    public String getMimeType() {
+        return "application/octet-stream";
+    }
+    
+    public long getContentLength() {
+        return -1;
+    }
+    
+    public String getFilename() {
+        return this.filename;
+    }
+    
+}

Propchange: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/InputStreamBody.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/StringBody.java
(from r616700, httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/StringBody.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/StringBody.java?p2=httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/StringBody.java&p1=httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/StringBody.java&r1=616700&r2=616704&rev=616704&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/StringBody.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/client/mime/content/StringBody.java
Wed Jan 30 02:00:36 2008
@@ -29,7 +29,7 @@
  *
  */
 
-package org.apache.http.client.mime;
+package org.apache.http.client.mime.content;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -40,6 +40,7 @@
 import java.nio.charset.Charset;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.http.client.mime.MIME;
 import org.apache.james.mime4j.message.AbstractBody;
 
 public class StringBody extends AbstractBody implements ContentBody {

Modified: httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/client/mime/TestMultipartForm.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/client/mime/TestMultipartForm.java?rev=616704&r1=616703&r2=616704&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/client/mime/TestMultipartForm.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/client/mime/TestMultipartForm.java
Wed Jan 30 02:00:36 2008
@@ -32,13 +32,20 @@
 package org.apache.http.client.mime;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
 import java.io.IOException;
+import java.io.Writer;
 import java.nio.charset.Charset;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.http.client.mime.content.FileBody;
+import org.apache.http.client.mime.content.InputStreamBody;
+import org.apache.http.client.mime.content.StringBody;
 import org.apache.james.mime4j.field.Field;
 import org.apache.james.mime4j.message.BodyPart;
 import org.apache.james.mime4j.message.Header;
@@ -162,6 +169,58 @@
             "Content-Transfer-Encoding: 8bit\r\n" +
             "\r\n" +
             "all kind of stuff\r\n" +
+            "--foo--\r\n" +
+            "\r\n";
+        String s = out.toString("US-ASCII");
+        assertEquals(expected, s);
+    }
+
+    public void testMultipartFormBinaryParts() throws Exception {
+        Message message = new Message();
+        Header header = new Header();
+        header.addField(
+                Field.parse("Content-Type: multipart/form-data; boundary=foo"));
+        message.setHeader(header);
+
+        File tmpfile = File.createTempFile("tmp", ".bin");
+        Writer writer = new FileWriter(tmpfile);
+        try {
+            writer.append("some random whatever");
+        } finally {
+            writer.close();
+        }
+        
+        Multipart multipart = new HttpMultipart();
+        multipart.setParent(message);
+        FormBodyPart p1 = new FormBodyPart(
+                "field1",
+                new FileBody(tmpfile));
+        FormBodyPart p2 = new FormBodyPart(
+                "field2",
+                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
+        
+        multipart.addBodyPart(p1);
+        multipart.addBodyPart(p2);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        multipart.writeTo(out);
+        out.close();
+        
+        String expected = "\r\n" + 
+            "--foo\r\n" +
+            "Content-Disposition: form-data; name=\"field1\"; " +
+                "filename=\"" + tmpfile.getName() + "\"\r\n" +
+            "Content-Type: application/octet-stream\r\n" +
+            "Content-Transfer-Encoding: binary\r\n" +
+            "\r\n" +
+            "some random whatever\r\n" +
+            "--foo\r\n" +
+            "Content-Disposition: form-data; name=\"field2\"; " +
+                "filename=\"file.tmp\"\r\n" +
+            "Content-Type: application/octet-stream\r\n" +
+            "Content-Transfer-Encoding: binary\r\n" +
+            "\r\n" +
+            "some random whatever\r\n" +
             "--foo--\r\n" +
             "\r\n";
         String s = out.toString("US-ASCII");



Mime
View raw message