Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id ACDBC900B for ; Tue, 23 Dec 2014 19:03:46 +0000 (UTC) Received: (qmail 98811 invoked by uid 500); 23 Dec 2014 19:03:46 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 98771 invoked by uid 500); 23 Dec 2014 19:03:46 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 98747 invoked by uid 99); 23 Dec 2014 19:03:46 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 Dec 2014 19:03:46 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id 3B09EAC092E for ; Tue, 23 Dec 2014 19:03:46 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1647648 - in /httpcomponents/httpclient/trunk/httpmime/src: main/java-deprecated/org/apache/http/entity/mime/ main/java/org/apache/http/entity/mime/ main/java/org/apache/http/entity/mime/content/ test/java/org/apache/http/entity/mime/ Date: Tue, 23 Dec 2014 19:03:45 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20141223190346.3B09EAC092E@hades.apache.org> Author: olegk Date: Tue Dec 23 19:03:45 2014 New Revision: 1647648 URL: http://svn.apache.org/r1647648 Log: Added builder class for FormBodyPart instances; API improvements in MultipartEntityBuilder Added: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java (with props) httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java (with props) Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java-deprecated/org/apache/http/entity/mime/HttpMultipart.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpRFC6532Multipart.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntityBuilder.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartEntityBuilder.java httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java-deprecated/org/apache/http/entity/mime/HttpMultipart.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java-deprecated/org/apache/http/entity/mime/HttpMultipart.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java-deprecated/org/apache/http/entity/mime/HttpMultipart.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java-deprecated/org/apache/http/entity/mime/HttpMultipart.java Tue Dec 23 19:03:45 2014 @@ -48,6 +48,8 @@ public class HttpMultipart extends Abstr private final HttpMultipartMode mode; private final List parts; + private final String subType; + /** * Creates an instance with the specified settings. * @@ -61,7 +63,8 @@ public class HttpMultipart extends Abstr 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(); } @@ -123,4 +126,16 @@ public class HttpMultipart extends Abstr this.parts.add(part); } + public String getSubType() { + return this.subType; + } + + public Charset getCharset() { + return this.charset; + } + + public String getBoundary() { + return this.boundary; + } + } Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java Tue Dec 23 19:03:45 2014 @@ -93,50 +93,34 @@ abstract class AbstractMultipartForm { 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 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); Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPart.java Tue Dec 23 19:03:45 2014 @@ -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 class FormBodyPart { 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 @@ public class FormBodyPart { 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 @@ public class FormBodyPart { } } + /** + * @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 } Added: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java?rev=1647648&view=auto ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java (added) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java Tue Dec 23 19:03:45 2014 @@ -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 + * . + * + */ + +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 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); + } + +} Propchange: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/FormBodyPartBuilder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java Tue Dec 23 19:03:45 2014 @@ -43,11 +43,10 @@ class HttpBrowserCompatibleMultipart ext private final List parts; public HttpBrowserCompatibleMultipart( - final String subType, final Charset charset, final String boundary, final List parts) { - super(subType, charset, boundary); + super(charset, boundary); this.parts = parts; } Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpRFC6532Multipart.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpRFC6532Multipart.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpRFC6532Multipart.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpRFC6532Multipart.java Tue Dec 23 19:03:45 2014 @@ -44,11 +44,10 @@ class HttpRFC6532Multipart extends Abstr private final List parts; public HttpRFC6532Multipart( - final String subType, final Charset charset, final String boundary, final List parts) { - super(subType, charset, boundary); + super(charset, boundary); this.parts = parts; } Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java Tue Dec 23 19:03:45 2014 @@ -44,11 +44,10 @@ class HttpStrictMultipart extends Abstra private final List parts; public HttpStrictMultipart( - final String subType, final Charset charset, final String boundary, final List parts) { - super(subType, charset, boundary); + super(charset, boundary); this.parts = parts; } Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntityBuilder.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntityBuilder.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntityBuilder.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntityBuilder.java Tue Dec 23 19:03:45 2014 @@ -36,12 +36,14 @@ import java.util.List; 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 class MultipartEntityBuilder { } MultipartEntityBuilder() { - super(); } public MultipartEntityBuilder setMode(final HttpMultipartMode mode) { @@ -99,7 +100,16 @@ public class MultipartEntityBuilder { */ 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 class MultipartEntityBuilder { 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 class MultipartEntityBuilder { 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 @@ public class MultipartEntityBuilder { } MultipartFormEntity buildEntity() { - final String st = subType != null ? subType : DEFAULT_SUBTYPE; - final Charset cs = charset; - final String b = boundary != null ? boundary : generateBoundary(); - final List bps = bodyParts != null ? new ArrayList(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 paramsList = new ArrayList(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 bodyPartsCopy = bodyParts != null ? new ArrayList(bodyParts) : Collections.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() { Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java Tue Dec 23 19:03:45 2014 @@ -29,6 +29,7 @@ package org.apache.http.entity.mime; 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 Htt 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; } Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java Tue Dec 23 19:03:45 2014 @@ -103,7 +103,7 @@ public class FileBody extends AbstractCo * @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 { Added: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java?rev=1647648&view=auto ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java (added) +++ httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java Tue Dec 23 19:03:45 2014 @@ -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 + * . + * + */ + +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 expected, final List 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()); + } + } + +} Propchange: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestFormBodyPartBuilder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartEntityBuilder.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartEntityBuilder.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartEntityBuilder.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartEntityBuilder.java Tue Dec 23 19:03:45 2014 @@ -32,6 +32,9 @@ import java.io.File; 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 class TestMultipartEntityBuilder .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 class TestMultipartEntityBuilder 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 bodyParts = entity.getMultipart().getBodyParts(); @@ -72,4 +75,52 @@ public class TestMultipartEntityBuilder 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()); + } + } Modified: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java?rev=1647648&r1=1647647&r2=1647648&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java (original) +++ httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java Tue Dec 23 19:03:45 2014 @@ -57,17 +57,17 @@ public class TestMultipartForm { @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 class TestMultipartForm { @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 class TestMultipartForm { 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 class TestMultipartForm { 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 class TestMultipartForm { 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 class TestMultipartForm { } @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 class TestMultipartForm { 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();