|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +package org.apache.http.entity.mime; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.nio.charset.Charset; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Random; |
| 37 | + |
| 38 | +import org.apache.http.HttpEntity; |
| 39 | +import org.apache.http.NameValuePair; |
| 40 | +import org.apache.http.entity.ContentType; |
| 41 | +import org.apache.http.entity.mime.content.ByteArrayBody; |
| 42 | +import org.apache.http.entity.mime.content.ContentBody; |
| 43 | +import org.apache.http.entity.mime.content.FileBody; |
| 44 | +import org.apache.http.entity.mime.content.InputStreamBody; |
| 45 | +import org.apache.http.entity.mime.content.StringBody; |
| 46 | +import org.apache.http.message.BasicNameValuePair; |
| 47 | +import org.apache.http.util.Args; |
| 48 | +import org.apiguardian.api.API; |
| 49 | + |
| 50 | +/** |
| 51 | + * Builder for multipart {@link HttpEntity}s. |
| 52 | + * |
| 53 | + * Note: the code was taken from |
| 54 | + * <a href="https://github.com/apache/httpcomponents-client/blob/54900db4653d7f207477e6ee40135b88e9bcf832/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntityBuilder.java">MultipartEntityBuilder 4.5.14</a> |
| 55 | + * |
| 56 | + */ |
| 57 | +@API(status = API.Status.INTERNAL, since = "5.6.4") |
| 58 | +public class MultipartEntityBuilder2 { |
| 59 | + |
| 60 | + /** |
| 61 | + * The pool of ASCII chars to be used for generating a multipart boundary. |
| 62 | + */ |
| 63 | + private final static char[] MULTIPART_CHARS = |
| 64 | + "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 65 | + .toCharArray(); |
| 66 | + |
| 67 | + private final static String DEFAULT_SUBTYPE = "form-data"; |
| 68 | + |
| 69 | + private ContentType contentType; |
| 70 | + private HttpMultipartMode mode = HttpMultipartMode.STRICT; |
| 71 | + private String boundary = null; |
| 72 | + private Charset charset = null; |
| 73 | + private List<FormBodyPart> bodyParts = null; |
| 74 | + |
| 75 | + public static MultipartEntityBuilder2 create() { |
| 76 | + return new MultipartEntityBuilder2(); |
| 77 | + } |
| 78 | + |
| 79 | + MultipartEntityBuilder2() { |
| 80 | + } |
| 81 | + |
| 82 | + public MultipartEntityBuilder2 setMode(final HttpMultipartMode mode) { |
| 83 | + this.mode = mode; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public MultipartEntityBuilder2 setLaxMode() { |
| 88 | + this.mode = HttpMultipartMode.BROWSER_COMPATIBLE; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public MultipartEntityBuilder2 setStrictMode() { |
| 93 | + this.mode = HttpMultipartMode.STRICT; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public MultipartEntityBuilder2 setBoundary(final String boundary) { |
| 98 | + this.boundary = boundary; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @since 4.4 |
| 104 | + */ |
| 105 | + public MultipartEntityBuilder2 setMimeSubtype(final String subType) { |
| 106 | + Args.notBlank(subType, "MIME subtype"); |
| 107 | + this.contentType = ContentType.create("multipart/" + subType); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @since 4.4 |
| 113 | + * |
| 114 | + * @deprecated (4.5) Use {@link #setContentType(org.apache.http.entity.ContentType)}. |
| 115 | + */ |
| 116 | + @Deprecated |
| 117 | + public MultipartEntityBuilder2 seContentType(final ContentType contentType) { |
| 118 | + return setContentType(contentType); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @since 4.5 |
| 123 | + */ |
| 124 | + public MultipartEntityBuilder2 setContentType(final ContentType contentType) { |
| 125 | + Args.notNull(contentType, "Content type"); |
| 126 | + this.contentType = contentType; |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + public MultipartEntityBuilder2 setCharset(final Charset charset) { |
| 131 | + this.charset = charset; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @since 4.4 |
| 137 | + */ |
| 138 | + public MultipartEntityBuilder2 addPart(final FormBodyPart bodyPart) { |
| 139 | + if (bodyPart == null) { |
| 140 | + return this; |
| 141 | + } |
| 142 | + if (this.bodyParts == null) { |
| 143 | + this.bodyParts = new ArrayList<FormBodyPart>(); |
| 144 | + } |
| 145 | + this.bodyParts.add(bodyPart); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + public MultipartEntityBuilder2 addPart(final String name, final ContentBody contentBody) { |
| 150 | + Args.notNull(name, "Name"); |
| 151 | + Args.notNull(contentBody, "Content body"); |
| 152 | + return addPart(FormBodyPartBuilder.create(name, contentBody).build()); |
| 153 | + } |
| 154 | + |
| 155 | + public MultipartEntityBuilder2 addTextBody( |
| 156 | + final String name, final String text, final ContentType contentType) { |
| 157 | + return addPart(name, new StringBody(text, contentType)); |
| 158 | + } |
| 159 | + |
| 160 | + public MultipartEntityBuilder2 addTextBody( |
| 161 | + final String name, final String text) { |
| 162 | + return addTextBody(name, text, ContentType.DEFAULT_TEXT); |
| 163 | + } |
| 164 | + |
| 165 | + public MultipartEntityBuilder2 addBinaryBody( |
| 166 | + final String name, final byte[] b, final ContentType contentType, final String filename) { |
| 167 | + return addPart(name, new ByteArrayBody(b, contentType, filename)); |
| 168 | + } |
| 169 | + |
| 170 | + public MultipartEntityBuilder2 addBinaryBody( |
| 171 | + final String name, final byte[] b) { |
| 172 | + return addBinaryBody(name, b, ContentType.DEFAULT_BINARY, null); |
| 173 | + } |
| 174 | + |
| 175 | + public MultipartEntityBuilder2 addBinaryBody( |
| 176 | + final String name, final File file, final ContentType contentType, final String filename) { |
| 177 | + return addPart(name, new FileBody(file, contentType, filename)); |
| 178 | + } |
| 179 | + |
| 180 | + public MultipartEntityBuilder2 addBinaryBody( |
| 181 | + final String name, final File file) { |
| 182 | + return addBinaryBody(name, file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null); |
| 183 | + } |
| 184 | + |
| 185 | + public MultipartEntityBuilder2 addBinaryBody( |
| 186 | + final String name, final InputStream stream, final ContentType contentType, |
| 187 | + final String filename) { |
| 188 | + return addPart(name, new InputStreamBody(stream, contentType, filename)); |
| 189 | + } |
| 190 | + |
| 191 | + public MultipartEntityBuilder2 addBinaryBody(final String name, final InputStream stream) { |
| 192 | + return addBinaryBody(name, stream, ContentType.DEFAULT_BINARY, null); |
| 193 | + } |
| 194 | + |
| 195 | + private static String generateBoundary() { |
| 196 | + final StringBuilder buffer = new StringBuilder(); |
| 197 | + final Random rand = new Random(); |
| 198 | + final int count = rand.nextInt(11) + 30; // a random size from 30 to 40 |
| 199 | + for (int i = 0; i < count; i++) { |
| 200 | + buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]); |
| 201 | + } |
| 202 | + return buffer.toString(); |
| 203 | + } |
| 204 | + |
| 205 | + MultipartFormEntity buildEntity() { |
| 206 | + String boundaryCopy = boundary; |
| 207 | + if (boundaryCopy == null && contentType != null) { |
| 208 | + boundaryCopy = contentType.getParameter("boundary"); |
| 209 | + } |
| 210 | + if (boundaryCopy == null) { |
| 211 | + boundaryCopy = generateBoundary(); |
| 212 | + } |
| 213 | + Charset charsetCopy = charset; |
| 214 | + if (charsetCopy == null && contentType != null) { |
| 215 | + charsetCopy = contentType.getCharset(); |
| 216 | + } |
| 217 | + // JMeter update: charset is no longer explicitly added |
| 218 | + // See https://github.com/apache/httpcomponents-client/pull/556 |
| 219 | + final NameValuePair[] params = new NameValuePair[]{new BasicNameValuePair("boundary", boundaryCopy)}; |
| 220 | + final ContentType contentTypeCopy = contentType != null ? |
| 221 | + contentType.withParameters(params) : |
| 222 | + ContentType.create("multipart/" + DEFAULT_SUBTYPE, params); |
| 223 | + final List<FormBodyPart> bodyPartsCopy = bodyParts != null ? new ArrayList<>(bodyParts) : |
| 224 | + Collections.emptyList(); |
| 225 | + final HttpMultipartMode modeCopy = mode != null ? mode : HttpMultipartMode.STRICT; |
| 226 | + final AbstractMultipartForm form; |
| 227 | + switch (modeCopy) { |
| 228 | + case BROWSER_COMPATIBLE: |
| 229 | + form = new HttpBrowserCompatibleMultipart(charsetCopy, boundaryCopy, bodyPartsCopy); |
| 230 | + break; |
| 231 | + case RFC6532: |
| 232 | + form = new HttpRFC6532Multipart(charsetCopy, boundaryCopy, bodyPartsCopy); |
| 233 | + break; |
| 234 | + default: |
| 235 | + form = new HttpStrictMultipart(charsetCopy, boundaryCopy, bodyPartsCopy); |
| 236 | + } |
| 237 | + return new MultipartFormEntity(form, contentTypeCopy, form.getTotalLength()); |
| 238 | + } |
| 239 | + |
| 240 | + public HttpEntity build() { |
| 241 | + return buildEntity(); |
| 242 | + } |
| 243 | + |
| 244 | +} |
0 commit comments