Refactored HttpMime ContentBody classes to use ContentType at construction time; added MultipartEntityBuilder

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1398094 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-10-14 14:55:11 +00:00
parent ea62c7fb5e
commit 84d4b9e2ac
12 changed files with 451 additions and 81 deletions

View File

@ -31,6 +31,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import org.apache.http.HttpEntity;
@ -70,10 +71,6 @@ public class EntityBuilder {
return new EntityBuilder();
}
public String getText() {
return text;
}
private void clearContent() {
this.text = null;
this.binary = null;
@ -83,6 +80,10 @@ public class EntityBuilder {
this.file = null;
}
public String getText() {
return text;
}
public EntityBuilder setText(final String text) {
clearContent();
this.text = text;
@ -119,6 +120,10 @@ public class EntityBuilder {
return this;
}
public EntityBuilder setParameters(final NameValuePair... parameters) {
return setParameters(Arrays.asList(parameters));
}
public Serializable getSerializable() {
return serializable;
}

View File

@ -123,6 +123,13 @@ public class MultipartEntity implements HttpEntity {
return buffer.toString();
}
/**
* @since 4.3
*/
protected HttpMultipart getMultipart() {
return multipart;
}
public void addPart(final FormBodyPart bodyPart) {
this.multipart.addBodyPart(bodyPart);
this.dirty = true;

View File

@ -0,0 +1,156 @@
/*
* ====================================================================
* 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.entity.mime;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
/**
* @since 4.3
*/
public class MultipartEntityBuilder {
private boolean lax;
private String boundary;
private Charset charset;
private List<FormBodyPart> bodyParts;
public static MultipartEntityBuilder create() {
return new MultipartEntityBuilder();
}
MultipartEntityBuilder() {
super();
}
public MultipartEntityBuilder setLaxMode() {
this.lax = true;
return this;
}
public MultipartEntityBuilder setStrictMode() {
this.lax = false;
return this;
}
public MultipartEntityBuilder setBoundary(final String boundary) {
this.boundary = boundary;
return this;
}
public MultipartEntityBuilder setCharset(final Charset charset) {
this.charset = charset;
return this;
}
public MultipartEntityBuilder addTextBody(
final String name, final String text, final ContentType contentType) {
if (name == null) {
throw new IllegalArgumentException("Name may not be null");
}
if (text == null) {
throw new IllegalArgumentException("Text may not be null");
}
if (this.bodyParts == null) {
this.bodyParts = new ArrayList<FormBodyPart>();
}
this.bodyParts.add(new FormBodyPart(name, new StringBody(text, contentType)));
return this;
}
public MultipartEntityBuilder addTextBody(
final String name, final String text) {
return addTextBody(name, text, ContentType.DEFAULT_TEXT);
}
public MultipartEntityBuilder addBinaryBody(
final String name, final byte[] b, final ContentType contentType, final String filename) {
if (this.bodyParts == null) {
this.bodyParts = new ArrayList<FormBodyPart>();
}
this.bodyParts.add(new FormBodyPart(name, new ByteArrayBody(b, contentType, filename)));
return this;
}
public MultipartEntityBuilder addBinaryBody(
final String name, final byte[] b) {
return addBinaryBody(name, b, ContentType.DEFAULT_BINARY, null);
}
public MultipartEntityBuilder addBinaryBody(
final String name, final File file, final ContentType contentType, final String filename) {
if (this.bodyParts == null) {
this.bodyParts = new ArrayList<FormBodyPart>();
}
this.bodyParts.add(
new FormBodyPart(name, new FileBody(file, contentType, filename)));
return this;
}
public MultipartEntityBuilder addBinaryBody(
final String name, final File file) {
return addBinaryBody(name, file, ContentType.DEFAULT_BINARY, null);
}
public MultipartEntityBuilder addBinaryBody(
final String name, final InputStream stream, final ContentType contentType,
final String filename) {
if (this.bodyParts == null) {
this.bodyParts = new ArrayList<FormBodyPart>();
}
this.bodyParts.add(
new FormBodyPart(name, new InputStreamBody(stream, contentType, filename)));
return this;
}
public MultipartEntityBuilder addBinaryBody(final String name, final InputStream stream) {
return addBinaryBody(name, stream, ContentType.DEFAULT_BINARY, null);
}
public MultipartEntity build() {
MultipartEntity e = new MultipartEntity(
this.lax ? HttpMultipartMode.BROWSER_COMPATIBLE : HttpMultipartMode.STRICT,
this.boundary, this.charset);
if (this.bodyParts != null) {
for (FormBodyPart bp: this.bodyParts) {
e.addPart(bp);
}
}
return e;
}
}

View File

@ -27,42 +27,71 @@
package org.apache.http.entity.mime.content;
import java.nio.charset.Charset;
import org.apache.http.entity.ContentType;
/**
*
* @since 4.0
*/
public abstract class AbstractContentBody implements ContentBody {
private final String mimeType;
private final String mediaType;
private final String subType;
private final ContentType contentType;
public AbstractContentBody(final String mimeType) {
/**
* @since 4.3
*/
public AbstractContentBody(final ContentType contentType) {
super();
if (mimeType == null) {
throw new IllegalArgumentException("MIME type may not be null");
}
this.mimeType = mimeType;
int i = mimeType.indexOf('/');
if (i != -1) {
this.mediaType = mimeType.substring(0, i);
this.subType = mimeType.substring(i + 1);
} else {
this.mediaType = mimeType;
this.subType = null;
if (contentType == null) {
throw new IllegalArgumentException("Content type may not be null");
}
this.contentType = contentType;
}
/**
* @deprecated (4.3) use {@link AbstractContentBody#AbstractContentBody(ContentType)}
*/
@Deprecated
public AbstractContentBody(final String mimeType) {
this(ContentType.parse(mimeType));
}
/**
* @since 4.3
*/
public ContentType getContentType() {
return this.contentType;
}
public String getMimeType() {
return this.mimeType;
return this.contentType.getMimeType();
}
public String getMediaType() {
return this.mediaType;
String mimeType = this.contentType.getMimeType();
int i = mimeType.indexOf('/');
if (i != -1) {
return mimeType.substring(0, i);
} else {
return mimeType;
}
}
public String getSubType() {
return this.subType;
String mimeType = this.contentType.getMimeType();
int i = mimeType.indexOf('/');
if (i != -1) {
return mimeType.substring(i + 1);
} else {
return null;
}
}
public String getCharset() {
Charset charset = this.contentType.getCharset();
return charset != null ? charset.name() : null;
}
}

View File

@ -29,11 +29,15 @@ package org.apache.http.entity.mime.content;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MIME;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.AbstractContentBody;
/**
* Body part that is built using a byte array containing a file.
* Binary body part backed by a byte array.
*
* @see MultipartEntityBuilder
*
* @since 4.1
*/
@ -55,9 +59,19 @@ public class ByteArrayBody extends AbstractContentBody {
* @param data The contents of the file contained in this part.
* @param mimeType The mime type of the file contained in this part.
* @param filename The name of the file contained in this part.
*
* @deprecated (4.3) use {@link ByteArrayBody#ByteArrayBody(byte[], ContentType, String)}
* or {@link MultipartEntityBuilder}
*/
public ByteArrayBody(final byte[] data, final String mimeType, final String filename) {
super(mimeType);
this(data, ContentType.create(mimeType), filename);
}
/**
* @since 4.3
*/
public ByteArrayBody(final byte[] data, final ContentType contentType, final String filename) {
super(contentType);
if (data == null) {
throw new IllegalArgumentException("byte[] may not be null");
}

View File

@ -33,9 +33,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MIME;
import org.apache.http.entity.mime.MultipartEntityBuilder;
/**
* Binary body part backed by a file.
*
* @see MultipartEntityBuilder
*
* @since 4.0
*/
@ -43,42 +48,64 @@ public class FileBody extends AbstractContentBody {
private final File file;
private final String filename;
private final String charset;
/**
* @since 4.1
*
* @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType, String)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public FileBody(final File file,
final String filename,
final String mimeType,
final String charset) {
super(mimeType);
if (file == null) {
throw new IllegalArgumentException("File may not be null");
}
this.file = file;
if (filename != null)
this.filename = filename;
else
this.filename = file.getName();
this.charset = charset;
this(file, ContentType.create(mimeType, charset), filename);
}
/**
* @since 4.1
*
* @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public FileBody(final File file,
final String mimeType,
final String charset) {
this(file, null, mimeType, charset);
}
/**
* @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public FileBody(final File file, final String mimeType) {
this(file, mimeType, null);
this(file, ContentType.create(mimeType), null);
}
public FileBody(final File file) {
this(file, "application/octet-stream");
this(file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null);
}
/**
* @since 4.3
*/
public FileBody(final File file, final ContentType contentType, final String filename) {
super(contentType);
if (file == null) {
throw new IllegalArgumentException("File may not be null");
}
this.file = file;
this.filename = filename;
}
/**
* @since 4.3
*/
public FileBody(final File file, final ContentType contentType) {
this(file, contentType, null);
}
public InputStream getInputStream() throws IOException {
@ -106,10 +133,6 @@ public class FileBody extends AbstractContentBody {
return MIME.ENC_BINARY;
}
public String getCharset() {
return charset;
}
public long getContentLength() {
return this.file.length();
}

View File

@ -31,9 +31,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MIME;
import org.apache.http.entity.mime.MultipartEntityBuilder;
/**
* Binary body part backed by an input stream.
*
* @see MultipartEntityBuilder
*
* @since 4.0
*/
@ -42,8 +47,25 @@ public class InputStreamBody extends AbstractContentBody {
private final InputStream in;
private final String filename;
/**
* @since 4.1
*
* @deprecated (4.3) use {@link InputStreamBody#InputStreamBody(InputStream, ContentType,
* String)} or {@link MultipartEntityBuilder}
*/
public InputStreamBody(final InputStream in, final String mimeType, final String filename) {
super(mimeType);
this(in, ContentType.create(mimeType), filename);
}
public InputStreamBody(final InputStream in, final String filename) {
this(in, ContentType.DEFAULT_BINARY, filename);
}
/**
* @since 4.3
*/
public InputStreamBody(final InputStream in, final ContentType contentType, final String filename) {
super(contentType);
if (in == null) {
throw new IllegalArgumentException("Input stream may not be null");
}
@ -51,8 +73,11 @@ public class InputStreamBody extends AbstractContentBody {
this.filename = filename;
}
public InputStreamBody(final InputStream in, final String filename) {
this(in, "application/octet-stream", filename);
/**
* @since 4.3
*/
public InputStreamBody(final InputStream in, final ContentType contentType) {
this(in, contentType, null);
}
public InputStream getInputStream() {
@ -79,10 +104,6 @@ public class InputStreamBody extends AbstractContentBody {
return MIME.ENC_BINARY;
}
public String getCharset() {
return null;
}
public long getContentLength() {
return -1;
}

View File

@ -35,21 +35,31 @@ import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import org.apache.http.Consts;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MIME;
import org.apache.http.entity.mime.MultipartEntityBuilder;
/**
* Text body part backed by a String.
*
* @see MultipartEntityBuilder
*
* @since 4.0
*/
public class StringBody extends AbstractContentBody {
private final byte[] content;
private final Charset charset;
/**
* @since 4.1
*
* @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public static StringBody create(
final String text,
final String mimeType,
@ -63,7 +73,11 @@ public class StringBody extends AbstractContentBody {
/**
* @since 4.1
*
* @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public static StringBody create(
final String text, final Charset charset) throws IllegalArgumentException {
return create(text, null, charset);
@ -71,7 +85,11 @@ public class StringBody extends AbstractContentBody {
/**
* @since 4.1
*
* @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public static StringBody create(final String text) throws IllegalArgumentException {
return create(text, null, null);
}
@ -84,20 +102,16 @@ public class StringBody extends AbstractContentBody {
* @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used
* @throws UnsupportedEncodingException
* @throws IllegalArgumentException if the {@code text} parameter is null
*
* @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public StringBody(
final String text,
final String mimeType,
Charset charset) throws UnsupportedEncodingException {
super(mimeType);
if (text == null) {
throw new IllegalArgumentException("Text may not be null");
}
if (charset == null) {
charset = Charset.forName("US-ASCII");
}
this.content = text.getBytes(charset.name());
this.charset = charset;
this(text, ContentType.create(mimeType, charset));
}
/**
@ -108,7 +122,11 @@ public class StringBody extends AbstractContentBody {
* @param charset the character set, may be {@code null}, in which case the US-ASCII charset is used
* @throws UnsupportedEncodingException
* @throws IllegalArgumentException if the {@code text} parameter is null
*
* @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public StringBody(final String text, final Charset charset) throws UnsupportedEncodingException {
this(text, "text/plain", charset);
}
@ -121,15 +139,35 @@ public class StringBody extends AbstractContentBody {
* @param text to be used for the body, not {@code null}
* @throws UnsupportedEncodingException
* @throws IllegalArgumentException if the {@code text} parameter is null
*
* @deprecated (4.3) use {@link StringBody#StringBody(String, ContentType)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public StringBody(final String text) throws UnsupportedEncodingException {
this(text, "text/plain", null);
this(text, "text/plain", Consts.ASCII);
}
/**
* @since 4.3
*/
public StringBody(final String text, final ContentType contentType) {
super(contentType);
Charset charset = contentType.getCharset();
String csname = charset != null ? charset.name() : Consts.ASCII.name();
try {
this.content = text.getBytes(csname);
} catch (UnsupportedEncodingException ex) {
// Should never happen
throw new UnsupportedCharsetException(csname);
}
}
public Reader getReader() {
Charset charset = getContentType().getCharset();
return new InputStreamReader(
new ByteArrayInputStream(this.content),
this.charset);
charset != null ? charset : Consts.ASCII);
}
public void writeTo(final OutputStream out) throws IOException {
@ -149,10 +187,6 @@ public class StringBody extends AbstractContentBody {
return MIME.ENC_8BIT;
}
public String getCharset() {
return this.charset.name();
}
public long getContentLength() {
return this.content.length;
}

View File

@ -29,6 +29,7 @@ package org.apache.http.entity.mime;
import java.io.ByteArrayInputStream;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.junit.Assert;
@ -38,10 +39,10 @@ public class TestMultipartContentBody {
@Test
public void testStringBody() throws Exception {
StringBody b1 = new StringBody("text");
StringBody b1 = new StringBody("text", ContentType.DEFAULT_TEXT);
Assert.assertEquals(4, b1.getContentLength());
Assert.assertEquals("US-ASCII", b1.getCharset());
Assert.assertEquals("ISO-8859-1", b1.getCharset());
Assert.assertNull(b1.getFilename());
Assert.assertEquals("text/plain", b1.getMimeType());
@ -50,7 +51,8 @@ public class TestMultipartContentBody {
Assert.assertEquals(MIME.ENC_8BIT, b1.getTransferEncoding());
StringBody b2 = new StringBody("more text", "text/other", MIME.DEFAULT_CHARSET);
StringBody b2 = new StringBody("more text",
ContentType.create("text/other", MIME.DEFAULT_CHARSET));
Assert.assertEquals(9, b2.getContentLength());
Assert.assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getCharset());
@ -77,7 +79,7 @@ public class TestMultipartContentBody {
Assert.assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
InputStreamBody b2 = new InputStreamBody(
new ByteArrayInputStream(stuff), "some/stuff", "stuff");
new ByteArrayInputStream(stuff), ContentType.create("some/stuff"), "stuff");
Assert.assertEquals(-1, b2.getContentLength());
Assert.assertNull(b2.getCharset());
Assert.assertEquals("stuff", b2.getFilename());

View File

@ -0,0 +1,75 @@
/*
* ====================================================================
* 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.entity.mime;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.List;
import org.apache.http.Consts;
import org.junit.Assert;
import org.junit.Test;
public class TestMultipartEntityBuilder {
@Test
public void testBasics() throws Exception {
MultipartEntity entity = MultipartEntityBuilder.create().build();
Assert.assertNotNull(entity);
Assert.assertEquals(HttpMultipartMode.STRICT, entity.getMultipart().getMode());
Assert.assertEquals(0, entity.getMultipart().getBodyParts().size());
}
@Test
public void testMultipartOptions() throws Exception {
MultipartEntity entity = MultipartEntityBuilder.create()
.setBoundary("blah-blah")
.setCharset(Consts.UTF_8)
.setLaxMode()
.build();
Assert.assertNotNull(entity);
Assert.assertEquals(HttpMultipartMode.BROWSER_COMPATIBLE, entity.getMultipart().getMode());
Assert.assertEquals("blah-blah", entity.getMultipart().getBoundary());
Assert.assertEquals(Consts.UTF_8, entity.getMultipart().getCharset());
}
@Test
public void testAddBodyParts() throws Exception {
MultipartEntity entity = MultipartEntityBuilder.create()
.addTextBody("p1", "stuff")
.addBinaryBody("p2", new File("stuff"))
.addBinaryBody("p3", new byte[] {})
.addBinaryBody("p4", new ByteArrayInputStream(new byte[] {}))
.build();
Assert.assertNotNull(entity);
List<FormBodyPart> bodyParts = entity.getMultipart().getBodyParts();
Assert.assertNotNull(bodyParts);
Assert.assertEquals(4, bodyParts.size());
}
}

View File

@ -34,6 +34,8 @@ import java.io.FileWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import org.apache.http.Consts;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.HttpMultipart;
import org.apache.http.entity.mime.HttpMultipartMode;
@ -50,13 +52,14 @@ public class TestMultipartForm {
HttpMultipart multipart = new HttpMultipart("form-data", "foo");
FormBodyPart p1 = new FormBodyPart(
"field1",
new StringBody("this stuff"));
new StringBody("this stuff", ContentType.DEFAULT_TEXT));
FormBodyPart p2 = new FormBodyPart(
"field2",
new StringBody("that stuff", Charset.forName("UTF-8")));
new StringBody("that stuff", ContentType.create(
ContentType.TEXT_PLAIN.getMimeType(), Consts.UTF_8)));
FormBodyPart p3 = new FormBodyPart(
"field3",
new StringBody("all kind of stuff"));
new StringBody("all kind of stuff", ContentType.DEFAULT_TEXT));
multipart.addBodyPart(p1);
multipart.addBodyPart(p2);
@ -69,7 +72,7 @@ public class TestMultipartForm {
String expected =
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field1\"\r\n" +
"Content-Type: text/plain; charset=US-ASCII\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"this stuff\r\n" +
@ -81,7 +84,7 @@ public class TestMultipartForm {
"that stuff\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field3\"\r\n" +
"Content-Type: text/plain; charset=US-ASCII\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"all kind of stuff\r\n" +
@ -157,7 +160,7 @@ public class TestMultipartForm {
new FileBody(tmpfile));
FormBodyPart p2 = new FormBodyPart(
"field2",
new FileBody(tmpfile, "test-file", "text/plain", "ANSI_X3.4-1968"));
new FileBody(tmpfile, ContentType.create("text/plain", "ANSI_X3.4-1968"), "test-file"));
FormBodyPart p3 = new FormBodyPart(
"field3",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
@ -181,7 +184,7 @@ public class TestMultipartForm {
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"; " +
"filename=\"test-file\"\r\n" +
"Content-Type: text/plain; charset=ANSI_X3.4-1968\r\n" +
"Content-Type: text/plain; charset=US-ASCII\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
@ -277,10 +280,10 @@ public class TestMultipartForm {
HttpMultipart multipart = new HttpMultipart("form-data", "foo");
FormBodyPart p1 = new FormBodyPart(
"field1",
new StringBody(s1, Charset.forName("ISO-8859-1")));
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1"))));
FormBodyPart p2 = new FormBodyPart(
"field2",
new StringBody(s2, Charset.forName("KOI8-R")));
new StringBody(s2, ContentType.create("text/plain", Charset.forName("KOI8-R"))));
multipart.addBodyPart(p1);
multipart.addBodyPart(p2);

View File

@ -34,6 +34,7 @@ import java.nio.charset.Charset;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.NameValuePair;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
@ -95,8 +96,8 @@ public class TestMultipartFormHttpEntity {
@Test
public void testRepeatable() throws Exception {
MultipartEntity entity = new MultipartEntity();
entity.addPart("p1", new StringBody("blah blah"));
entity.addPart("p2", new StringBody("yada yada"));
entity.addPart("p1", new StringBody("blah blah", ContentType.DEFAULT_TEXT));
entity.addPart("p2", new StringBody("yada yada", ContentType.DEFAULT_TEXT));
Assert.assertTrue(entity.isRepeatable());
Assert.assertFalse(entity.isChunked());
Assert.assertFalse(entity.isStreaming());
@ -127,9 +128,9 @@ public class TestMultipartFormHttpEntity {
public void testNonRepeatable() throws Exception {
MultipartEntity entity = new MultipartEntity();
entity.addPart("p1", new InputStreamBody(
new ByteArrayInputStream("blah blah".getBytes()), null));
new ByteArrayInputStream("blah blah".getBytes()), ContentType.DEFAULT_BINARY));
entity.addPart("p2", new InputStreamBody(
new ByteArrayInputStream("yada yada".getBytes()), null));
new ByteArrayInputStream("yada yada".getBytes()), ContentType.DEFAULT_BINARY));
Assert.assertFalse(entity.isRepeatable());
Assert.assertTrue(entity.isChunked());
Assert.assertTrue(entity.isStreaming());