Added builder class for FormBodyPart instances; API improvements in MultipartEntityBuilder

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1647648 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-12-23 19:03:45 +00:00
parent f114594c6f
commit 978c67d3da
13 changed files with 507 additions and 106 deletions

View File

@ -48,6 +48,8 @@ public class HttpMultipart extends AbstractMultipartForm {
private final HttpMultipartMode mode;
private final List<FormBodyPart> parts;
private final String subType;
/**
* Creates an instance with the specified settings.
*
@ -61,7 +63,8 @@ public class HttpMultipart extends AbstractMultipartForm {
public HttpMultipart(
final String subType, final Charset charset, final String boundary,
final HttpMultipartMode mode) {
super(subType, charset, boundary);
super(charset, boundary);
this.subType = subType;
this.mode = mode;
this.parts = new ArrayList<FormBodyPart>();
}
@ -123,4 +126,16 @@ public void addBodyPart(final FormBodyPart part) {
this.parts.add(part);
}
public String getSubType() {
return this.subType;
}
public Charset getCharset() {
return this.charset;
}
public String getBoundary() {
return this.boundary;
}
}

View File

@ -93,50 +93,34 @@ protected static void writeField(
private static final ByteArrayBuffer CR_LF = encode(MIME.DEFAULT_CHARSET, "\r\n");
private static final ByteArrayBuffer TWO_DASHES = encode(MIME.DEFAULT_CHARSET, "--");
private final String subType;
protected final Charset charset;
private final String boundary;
final Charset charset;
final String boundary;
/**
* Creates an instance with the specified settings.
*
* @param subType MIME subtype - must not be {@code null}
* @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
* @throws IllegalArgumentException if charset is null or boundary is null
*/
public AbstractMultipartForm(final String subType, final Charset charset, final String boundary) {
public AbstractMultipartForm(final Charset charset, final String boundary) {
super();
Args.notNull(subType, "Multipart subtype");
Args.notNull(boundary, "Multipart boundary");
this.subType = subType;
this.charset = charset != null ? charset : MIME.DEFAULT_CHARSET;
this.boundary = boundary;
}
public AbstractMultipartForm(final String subType, final String boundary) {
this(subType, null, boundary);
}
public String getSubType() {
return this.subType;
}
public Charset getCharset() {
return this.charset;
public AbstractMultipartForm(final String boundary) {
this(null, boundary);
}
public abstract List<FormBodyPart> getBodyParts();
public String getBoundary() {
return this.boundary;
}
void doWriteTo(
final OutputStream out,
final boolean writeContent) throws IOException {
final ByteArrayBuffer boundaryEncoded = encode(this.charset, getBoundary());
final ByteArrayBuffer boundaryEncoded = encode(this.charset, this.boundary);
for (final FormBodyPart part: getBodyParts()) {
writeBytes(TWO_DASHES, out);
writeBytes(boundaryEncoded, out);

View File

@ -43,9 +43,21 @@ public class FormBodyPart {
private final String name;
private final Header header;
private final ContentBody body;
FormBodyPart(final String name, final ContentBody body, final Header header) {
super();
Args.notNull(name, "Name");
Args.notNull(body, "Body");
this.name = name;
this.body = body;
this.header = header != null ? header : new Header();
}
/**
* @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
*/
@Deprecated
public FormBodyPart(final String name, final ContentBody body) {
super();
Args.notNull(name, "Name");
@ -76,6 +88,10 @@ public void addField(final String name, final String value) {
this.header.addField(new MinimalField(name, value));
}
/**
* @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
*/
@Deprecated
protected void generateContentDisp(final ContentBody body) {
final StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
@ -89,6 +105,10 @@ protected void generateContentDisp(final ContentBody body) {
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
/**
* @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
*/
@Deprecated
protected void generateContentType(final ContentBody body) {
final ContentType contentType;
if (body instanceof AbstractContentBody) {
@ -109,6 +129,10 @@ protected void generateContentType(final ContentBody body) {
}
}
/**
* @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
*/
@Deprecated
protected void generateTransferEncoding(final ContentBody body) {
addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()); // TE cannot be null
}

View File

@ -0,0 +1,141 @@
/*
* ====================================================================
* 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.util.List;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.content.AbstractContentBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.util.Args;
import org.apache.http.util.Asserts;
/**
* Builder for individual {@link org.apache.http.entity.mime.FormBodyPart}s.
*
* @since 4.4
*/
public class FormBodyPartBuilder {
private String name;
private ContentBody body;
private final Header header;
public static FormBodyPartBuilder create(final String name, final ContentBody body) {
return new FormBodyPartBuilder(name, body);
}
public static FormBodyPartBuilder create() {
return new FormBodyPartBuilder();
}
FormBodyPartBuilder(final String name, final ContentBody body) {
this();
this.name = name;
this.body = body;
}
FormBodyPartBuilder() {
this.header = new Header();
}
public FormBodyPartBuilder setName(final String name) {
this.name = name;
return this;
}
public FormBodyPartBuilder setBody(final ContentBody body) {
this.body = body;
return this;
}
public FormBodyPartBuilder addField(final String name, final String value) {
Args.notNull(name, "Field name");
this.header.addField(new MinimalField(name, value));
return this;
}
public FormBodyPartBuilder setField(final String name, final String value) {
Args.notNull(name, "Field name");
this.header.setField(new MinimalField(name, value));
return this;
}
public FormBodyPartBuilder removeFields(final String name) {
Args.notNull(name, "Field name");
this.header.removeFields(name);
return this;
}
public FormBodyPart build() {
Asserts.notBlank(this.name, "Name");
Asserts.notNull(this.body, "Content body");
final Header headerCopy = new Header();
final List<MinimalField> fields = this.header.getFields();
for (MinimalField field: fields) {
headerCopy.addField(field);
}
if (headerCopy.getField(MIME.CONTENT_DISPOSITION) == null) {
final StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
buffer.append(this.name);
buffer.append("\"");
if (this.body.getFilename() != null) {
buffer.append("; filename=\"");
buffer.append(this.body.getFilename());
buffer.append("\"");
}
headerCopy.addField(new MinimalField(MIME.CONTENT_DISPOSITION, buffer.toString()));
}
if (headerCopy.getField(MIME.CONTENT_TYPE) == null) {
final ContentType contentType;
if (body instanceof AbstractContentBody) {
contentType = ((AbstractContentBody) body).getContentType();
} else {
contentType = null;
}
if (contentType != null) {
headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, contentType.toString()));
} else {
final StringBuilder buffer = new StringBuilder();
buffer.append(this.body.getMimeType()); // MimeType cannot be null
if (this.body.getCharset() != null) { // charset may legitimately be null
buffer.append("; charset=");
buffer.append(this.body.getCharset());
}
headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, buffer.toString()));
}
}
if (headerCopy.getField(MIME.CONTENT_TRANSFER_ENC) == null) {
// TE cannot be null
headerCopy.addField(new MinimalField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()));
}
return new FormBodyPart(this.name, this.body, headerCopy);
}
}

View File

@ -43,11 +43,10 @@ class HttpBrowserCompatibleMultipart extends AbstractMultipartForm {
private final List<FormBodyPart> parts;
public HttpBrowserCompatibleMultipart(
final String subType,
final Charset charset,
final String boundary,
final List<FormBodyPart> parts) {
super(subType, charset, boundary);
super(charset, boundary);
this.parts = parts;
}

View File

@ -44,11 +44,10 @@ class HttpRFC6532Multipart extends AbstractMultipartForm {
private final List<FormBodyPart> parts;
public HttpRFC6532Multipart(
final String subType,
final Charset charset,
final String boundary,
final List<FormBodyPart> parts) {
super(subType, charset, boundary);
super(charset, boundary);
this.parts = parts;
}

View File

@ -44,11 +44,10 @@ class HttpStrictMultipart extends AbstractMultipartForm {
private final List<FormBodyPart> parts;
public HttpStrictMultipart(
final String subType,
final Charset charset,
final String boundary,
final List<FormBodyPart> parts) {
super(subType, charset, boundary);
super(charset, boundary);
this.parts = parts;
}

View File

@ -36,12 +36,14 @@
import java.util.Random;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.Args;
/**
@ -60,7 +62,7 @@ public class MultipartEntityBuilder {
private final static String DEFAULT_SUBTYPE = "form-data";
private String subType = DEFAULT_SUBTYPE;
private ContentType contentType;
private HttpMultipartMode mode = HttpMultipartMode.STRICT;
private String boundary = null;
private Charset charset = null;
@ -71,7 +73,6 @@ public static MultipartEntityBuilder create() {
}
MultipartEntityBuilder() {
super();
}
public MultipartEntityBuilder setMode(final HttpMultipartMode mode) {
@ -99,7 +100,16 @@ public MultipartEntityBuilder setBoundary(final String boundary) {
*/
public MultipartEntityBuilder setMimeSubtype(final String subType) {
Args.notBlank(subType, "MIME subtype");
this.subType = subType;
this.contentType = ContentType.create("multipart/" + subType);
return this;
}
/**
* @since 4.4
*/
public MultipartEntityBuilder seContentType(final ContentType contentType) {
Args.notNull(contentType, "Content type");
this.contentType = contentType;
return this;
}
@ -125,7 +135,7 @@ public MultipartEntityBuilder addPart(final FormBodyPart bodyPart) {
public MultipartEntityBuilder addPart(final String name, final ContentBody contentBody) {
Args.notNull(name, "Name");
Args.notNull(contentBody, "Content body");
return addPart(new FormBodyPart(name, contentBody));
return addPart(FormBodyPartBuilder.create(name, contentBody).build());
}
public MultipartEntityBuilder addTextBody(
@ -168,20 +178,6 @@ public MultipartEntityBuilder addBinaryBody(final String name, final InputStream
return addBinaryBody(name, stream, ContentType.DEFAULT_BINARY, null);
}
private String generateContentType(
final String boundary,
final String subType,
final Charset charset) {
final StringBuilder buffer = new StringBuilder();
buffer.append("multipart/").append(subType).append("; boundary=");
buffer.append(boundary);
if (charset != null) {
buffer.append("; charset=");
buffer.append(charset.name());
}
return buffer.toString();
}
private String generateBoundary() {
final StringBuilder buffer = new StringBuilder();
final Random rand = new Random();
@ -193,24 +189,41 @@ private String generateBoundary() {
}
MultipartFormEntity buildEntity() {
final String st = subType != null ? subType : DEFAULT_SUBTYPE;
final Charset cs = charset;
final String b = boundary != null ? boundary : generateBoundary();
final List<FormBodyPart> bps = bodyParts != null ? new ArrayList<FormBodyPart>(bodyParts) :
String boundaryCopy = boundary;
if (boundaryCopy == null && contentType != null) {
boundaryCopy = contentType.getParameter("boundary");
}
if (boundaryCopy == null) {
boundaryCopy = generateBoundary();
}
Charset charsetCopy = charset;
if (charsetCopy == null && contentType != null) {
charsetCopy = contentType.getCharset();
}
final List<NameValuePair> paramsList = new ArrayList<NameValuePair>(2);
paramsList.add(new BasicNameValuePair("boundary", boundaryCopy));
if (charsetCopy != null) {
paramsList.add(new BasicNameValuePair("charset", charsetCopy.name()));
}
final NameValuePair[] params = paramsList.toArray(new NameValuePair[paramsList.size()]);
final ContentType contentTypeCopy = contentType != null ?
contentType.withParameters(params) :
ContentType.create("multipart/" + DEFAULT_SUBTYPE, params);
final List<FormBodyPart> bodyPartsCopy = bodyParts != null ? new ArrayList<FormBodyPart>(bodyParts) :
Collections.<FormBodyPart>emptyList();
final HttpMultipartMode m = mode != null ? mode : HttpMultipartMode.STRICT;
final HttpMultipartMode modeCopy = mode != null ? mode : HttpMultipartMode.STRICT;
final AbstractMultipartForm form;
switch (m) {
switch (modeCopy) {
case BROWSER_COMPATIBLE:
form = new HttpBrowserCompatibleMultipart(st, cs, b, bps);
form = new HttpBrowserCompatibleMultipart(charsetCopy, boundaryCopy, bodyPartsCopy);
break;
case RFC6532:
form = new HttpRFC6532Multipart(st, cs, b, bps);
form = new HttpRFC6532Multipart(charsetCopy, boundaryCopy, bodyPartsCopy);
break;
default:
form = new HttpStrictMultipart(st, cs, b, bps);
form = new HttpStrictMultipart(charsetCopy, boundaryCopy, bodyPartsCopy);
}
return new MultipartFormEntity(form, generateContentType(b, st, cs), form.getTotalLength());
return new MultipartFormEntity(form, contentTypeCopy, form.getTotalLength());
}
public HttpEntity build() {

View File

@ -29,6 +29,7 @@
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
@ -44,11 +45,11 @@ class MultipartFormEntity implements HttpEntity {
MultipartFormEntity(
final AbstractMultipartForm multipart,
final String contentType,
final ContentType contentType,
final long contentLength) {
super();
this.multipart = multipart;
this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType);
this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType.toString());
this.contentLength = contentLength;
}

View File

@ -103,7 +103,7 @@ public FileBody(final File file, final ContentType contentType, final String fil
* @since 4.3
*/
public FileBody(final File file, final ContentType contentType) {
this(file, contentType, null);
this(file, contentType, file != null ? file.getName() : null);
}
public InputStream getInputStream() throws IOException {

View File

@ -0,0 +1,175 @@
/*
* ====================================================================
* 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.util.Arrays;
import java.util.List;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.junit.Assert;
import org.junit.Test;
public class TestFormBodyPartBuilder {
@Test
public void testBuildBodyPartBasics() throws Exception {
final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
final FormBodyPart bodyPart = FormBodyPartBuilder.create()
.setName("blah")
.setBody(stringBody)
.build();
Assert.assertNotNull(bodyPart);
Assert.assertEquals("blah", bodyPart.getName());
Assert.assertEquals(stringBody, bodyPart.getBody());
final Header header = bodyPart.getHeader();
Assert.assertNotNull(header);
assertFields(Arrays.asList(
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
new MinimalField("Content-Transfer-Encoding", "8bit")),
header.getFields());
}
@Test
public void testBuildBodyPartMultipleBuilds() throws Exception {
final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
final FormBodyPartBuilder builder = FormBodyPartBuilder.create();
final FormBodyPart bodyPart1 = builder
.setName("blah")
.setBody(stringBody)
.build();
Assert.assertNotNull(bodyPart1);
Assert.assertEquals("blah", bodyPart1.getName());
Assert.assertEquals(stringBody, bodyPart1.getBody());
final Header header1 = bodyPart1.getHeader();
Assert.assertNotNull(header1);
assertFields(Arrays.asList(
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
new MinimalField("Content-Transfer-Encoding", "8bit")),
header1.getFields());
final FileBody fileBody = new FileBody(new File("/path/stuff.bin"), ContentType.DEFAULT_BINARY);
final FormBodyPart bodyPart2 = builder
.setName("yada")
.setBody(fileBody)
.build();
Assert.assertNotNull(bodyPart2);
Assert.assertEquals("yada", bodyPart2.getName());
Assert.assertEquals(fileBody, bodyPart2.getBody());
final Header header2 = bodyPart2.getHeader();
Assert.assertNotNull(header2);
assertFields(Arrays.asList(
new MinimalField("Content-Disposition", "form-data; name=\"yada\"; filename=\"stuff.bin\""),
new MinimalField("Content-Type", "application/octet-stream"),
new MinimalField("Content-Transfer-Encoding", "binary")),
header2.getFields());
}
@Test
public void testBuildBodyPartCustomHeaders() throws Exception {
final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
final FormBodyPartBuilder builder = FormBodyPartBuilder.create("blah", stringBody);
final FormBodyPart bodyPart1 = builder
.addField("header1", "blah")
.addField("header3", "blah")
.addField("header3", "blah")
.addField("header3", "blah")
.addField("header3", "blah")
.addField("header3", "blah")
.build();
Assert.assertNotNull(bodyPart1);
final Header header1 = bodyPart1.getHeader();
Assert.assertNotNull(header1);
assertFields(Arrays.asList(
new MinimalField("header1", "blah"),
new MinimalField("header3", "blah"),
new MinimalField("header3", "blah"),
new MinimalField("header3", "blah"),
new MinimalField("header3", "blah"),
new MinimalField("header3", "blah"),
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
new MinimalField("Content-Transfer-Encoding", "8bit")),
header1.getFields());
final FormBodyPart bodyPart2 = builder
.setField("header2", "yada")
.removeFields("header3")
.build();
Assert.assertNotNull(bodyPart2);
final Header header2 = bodyPart2.getHeader();
Assert.assertNotNull(header2);
assertFields(Arrays.asList(
new MinimalField("header1", "blah"),
new MinimalField("header2", "yada"),
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
new MinimalField("Content-Transfer-Encoding", "8bit")),
header2.getFields());
final FormBodyPart bodyPart3 = builder
.addField("Content-Disposition", "disposition stuff")
.addField("Content-Type", "type stuff")
.addField("Content-Transfer-Encoding", "encoding stuff")
.build();
Assert.assertNotNull(bodyPart3);
final Header header3 = bodyPart3.getHeader();
Assert.assertNotNull(header3);
assertFields(Arrays.asList(
new MinimalField("header1", "blah"),
new MinimalField("header2", "yada"),
new MinimalField("Content-Disposition", "disposition stuff"),
new MinimalField("Content-Type", "type stuff"),
new MinimalField("Content-Transfer-Encoding", "encoding stuff")),
header3.getFields());
}
private static void assertFields(final List<MinimalField> expected, final List<MinimalField> result) {
Assert.assertNotNull(result);
Assert.assertEquals(expected.size(), result.size());
for (int i = 0; i < expected.size(); i++) {
final MinimalField expectedField = expected.get(i);
final MinimalField resultField = result.get(i);
Assert.assertNotNull(resultField);
Assert.assertEquals(expectedField.getName(), resultField.getName());
Assert.assertEquals(expectedField.getBody(), resultField.getBody());
}
}
}

View File

@ -32,6 +32,9 @@
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Assert;
import org.junit.Test;
@ -54,8 +57,8 @@ public void testMultipartOptions() throws Exception {
.buildEntity();
Assert.assertNotNull(entity);
Assert.assertTrue(entity.getMultipart() instanceof HttpBrowserCompatibleMultipart);
Assert.assertEquals("blah-blah", entity.getMultipart().getBoundary());
Assert.assertEquals(Consts.UTF_8, entity.getMultipart().getCharset());
Assert.assertEquals("blah-blah", entity.getMultipart().boundary);
Assert.assertEquals(Consts.UTF_8, entity.getMultipart().charset);
}
@Test
@ -63,8 +66,8 @@ public void testAddBodyParts() throws Exception {
final MultipartFormEntity entity = MultipartEntityBuilder.create()
.addTextBody("p1", "stuff")
.addBinaryBody("p2", new File("stuff"))
.addBinaryBody("p3", new byte[] {})
.addBinaryBody("p4", new ByteArrayInputStream(new byte[] {}))
.addBinaryBody("p3", new byte[]{})
.addBinaryBody("p4", new ByteArrayInputStream(new byte[]{}))
.buildEntity();
Assert.assertNotNull(entity);
final List<FormBodyPart> bodyParts = entity.getMultipart().getBodyParts();
@ -72,4 +75,52 @@ public void testAddBodyParts() throws Exception {
Assert.assertEquals(4, bodyParts.size());
}
@Test
public void testMultipartCustomContentType() throws Exception {
final MultipartFormEntity entity = MultipartEntityBuilder.create()
.seContentType(ContentType.APPLICATION_XML)
.setBoundary("blah-blah")
.setCharset(Consts.UTF_8)
.setLaxMode()
.buildEntity();
Assert.assertNotNull(entity);
final Header contentType = entity.getContentType();
Assert.assertNotNull(contentType);
Assert.assertEquals("application/xml; boundary=blah-blah; charset=UTF-8", contentType.getValue());
}
@Test
public void testMultipartContentTypeParameter() throws Exception {
final MultipartFormEntity entity = MultipartEntityBuilder.create()
.seContentType(ContentType.MULTIPART_FORM_DATA.withParameters(
new BasicNameValuePair("boundary", "yada-yada"),
new BasicNameValuePair("charset", "ascii")))
.buildEntity();
Assert.assertNotNull(entity);
final Header contentType = entity.getContentType();
Assert.assertNotNull(contentType);
Assert.assertEquals("multipart/form-data; boundary=yada-yada; charset=US-ASCII",
contentType.getValue());
Assert.assertEquals("yada-yada", entity.getMultipart().boundary);
Assert.assertEquals(Consts.ASCII, entity.getMultipart().charset);
}
@Test
public void testMultipartCustomContentTypeParameterOverrides() throws Exception {
final MultipartFormEntity entity = MultipartEntityBuilder.create()
.seContentType(ContentType.MULTIPART_FORM_DATA.withParameters(
new BasicNameValuePair("boundary", "yada-yada"),
new BasicNameValuePair("charset", "ascii"),
new BasicNameValuePair("my", "stuff")))
.setBoundary("blah-blah")
.setCharset(Consts.UTF_8)
.setLaxMode()
.buildEntity();
Assert.assertNotNull(entity);
final Header contentType = entity.getContentType();
Assert.assertNotNull(contentType);
Assert.assertEquals("multipart/form-data; boundary=blah-blah; charset=UTF-8; my=stuff",
contentType.getValue());
}
}

View File

@ -57,17 +57,17 @@ public void cleanup() {
@Test
public void testMultipartFormStringParts() throws Exception {
final FormBodyPart p1 = new FormBodyPart(
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new StringBody("this stuff", ContentType.DEFAULT_TEXT));
final FormBodyPart p2 = new FormBodyPart(
new StringBody("this stuff", ContentType.DEFAULT_TEXT)).build();
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new StringBody("that stuff", ContentType.create(
ContentType.TEXT_PLAIN.getMimeType(), Consts.UTF_8)));
final FormBodyPart p3 = new FormBodyPart(
ContentType.TEXT_PLAIN.getMimeType(), Consts.UTF_8))).build();
final FormBodyPart p3 = FormBodyPartBuilder.create(
"field3",
new StringBody("all kind of stuff", ContentType.DEFAULT_TEXT));
final HttpStrictMultipart multipart = new HttpStrictMultipart("form-data", null, "foo",
new StringBody("all kind of stuff", ContentType.DEFAULT_TEXT)).build();
final HttpStrictMultipart multipart = new HttpStrictMultipart(null, "foo",
Arrays.asList(p1, p2, p3));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -101,13 +101,13 @@ public void testMultipartFormStringParts() throws Exception {
@Test
public void testMultipartFormCustomContentType() throws Exception {
final FormBodyPart p1 = new FormBodyPart(
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new StringBody("this stuff", ContentType.DEFAULT_TEXT));
final FormBodyPart p2 = new FormBodyPart(
new StringBody("this stuff", ContentType.DEFAULT_TEXT)).build();
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new StringBody("that stuff", ContentType.parse("stuff/plain; param=value")));
final HttpStrictMultipart multipart = new HttpStrictMultipart("form-data", null, "foo",
new StringBody("that stuff", ContentType.parse("stuff/plain; param=value"))).build();
final HttpStrictMultipart multipart = new HttpStrictMultipart(null, "foo",
Arrays.asList(p1, p2));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -143,14 +143,14 @@ public void testMultipartFormBinaryParts() throws Exception {
writer.close();
}
final FormBodyPart p1 = new FormBodyPart(
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new FileBody(tmpfile));
new FileBody(tmpfile)).build();
@SuppressWarnings("resource")
final FormBodyPart p2 = new FormBodyPart(
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
final HttpStrictMultipart multipart = new HttpStrictMultipart("form-data", null, "foo",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp")).build();
final HttpStrictMultipart multipart = new HttpStrictMultipart(null, "foo",
Arrays.asList(p1, p2));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -188,17 +188,17 @@ public void testMultipartFormStrict() throws Exception {
writer.close();
}
final FormBodyPart p1 = new FormBodyPart(
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new FileBody(tmpfile));
final FormBodyPart p2 = new FormBodyPart(
new FileBody(tmpfile)).build();
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new FileBody(tmpfile, ContentType.create("text/plain", "ANSI_X3.4-1968"), "test-file"));
new FileBody(tmpfile, ContentType.create("text/plain", "ANSI_X3.4-1968"), "test-file")).build();
@SuppressWarnings("resource")
final FormBodyPart p3 = new FormBodyPart(
final FormBodyPart p3 = FormBodyPartBuilder.create(
"field3",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
final HttpStrictMultipart multipart = new HttpStrictMultipart("form-data", null, "foo",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp")).build();
final HttpStrictMultipart multipart = new HttpStrictMultipart(null, "foo",
Arrays.asList(p1, p2, p3));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -243,17 +243,17 @@ public void testMultipartFormRFC6532() throws Exception {
writer.close();
}
final FormBodyPart p1 = new FormBodyPart(
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1\u0414",
new FileBody(tmpfile));
final FormBodyPart p2 = new FormBodyPart(
new FileBody(tmpfile)).build();
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new FileBody(tmpfile, ContentType.create("text/plain", "ANSI_X3.4-1968"), "test-file"));
new FileBody(tmpfile, ContentType.create("text/plain", "ANSI_X3.4-1968"), "test-file")).build();
@SuppressWarnings("resource")
final FormBodyPart p3 = new FormBodyPart(
final FormBodyPart p3 = FormBodyPartBuilder.create(
"field3",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
final HttpRFC6532Multipart multipart = new HttpRFC6532Multipart("form-data", null, "foo",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp")).build();
final HttpRFC6532Multipart multipart = new HttpRFC6532Multipart(null, "foo",
Arrays.asList(p1, p2, p3));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -321,15 +321,15 @@ public void testMultipartFormBrowserCompatibleNonASCIIHeaders() throws Exception
}
@SuppressWarnings("resource")
final FormBodyPart p1 = new FormBodyPart(
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp"));
new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp")).build();
@SuppressWarnings("resource")
final FormBodyPart p2 = new FormBodyPart(
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new InputStreamBody(new FileInputStream(tmpfile), s2 + ".tmp"));
new InputStreamBody(new FileInputStream(tmpfile), s2 + ".tmp")).build();
final HttpBrowserCompatibleMultipart multipart = new HttpBrowserCompatibleMultipart(
"form-data", Consts.UTF_8, "foo",
Consts.UTF_8, "foo",
Arrays.asList(p1, p2));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -360,13 +360,13 @@ public void testMultipartFormStringPartsMultiCharsets() throws Exception {
final String s1 = constructString(SWISS_GERMAN_HELLO);
final String s2 = constructString(RUSSIAN_HELLO);
final FormBodyPart p1 = new FormBodyPart(
final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1"))));
final FormBodyPart p2 = new FormBodyPart(
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1")))).build();
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new StringBody(s2, ContentType.create("text/plain", Charset.forName("KOI8-R"))));
final HttpStrictMultipart multipart = new HttpStrictMultipart("form-data", null, "foo",
new StringBody(s2, ContentType.create("text/plain", Charset.forName("KOI8-R")))).build();
final HttpStrictMultipart multipart = new HttpStrictMultipart(null, "foo",
Arrays.asList(p1, p2));
final ByteArrayOutputStream out1 = new ByteArrayOutputStream();