MIME multipart/form-data primitives (file and input stream bodies)

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@616704 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2008-01-30 10:00:36 +00:00
parent 8328d00926
commit dc8f6d4a50
7 changed files with 258 additions and 2 deletions

View File

@ -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;

View File

@ -40,5 +40,6 @@ public final class MIME {
public static final String CONTENT_DISPOSITION = "Content-Disposition";
public static final String ENC_8BIT = "8bit";
public static final String ENC_BINARY = "binary";
}

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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.io.UnsupportedEncodingException;
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 {

View File

@ -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;
@ -168,4 +175,56 @@ public class TestMultipartForm extends TestCase {
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");
assertEquals(expected, s);
}
}