View Javadoc
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.hc.client5.http.entity.mime;
29  
30  import java.util.List;
31  
32  import org.apache.hc.core5.http.ContentType;
33  import org.apache.hc.core5.http.NameValuePair;
34  import org.apache.hc.core5.util.Args;
35  import org.apache.hc.core5.util.Asserts;
36  
37  /**
38   * Builder for individual {@link MultipartPart}s.
39   *
40   * @since 4.4
41   */
42  public class MultipartPartBuilder {
43  
44      private ContentBody body;
45      private final Header header;
46  
47      public static MultipartPartBuilder create(final ContentBody body) {
48          return new MultipartPartBuilder(body);
49      }
50  
51      public static MultipartPartBuilder create() {
52          return new MultipartPartBuilder();
53      }
54  
55      MultipartPartBuilder(final ContentBody body) {
56          this();
57          this.body = body;
58      }
59  
60      MultipartPartBuilder() {
61          this.header = new Header();
62      }
63  
64      public MultipartPartBuilder setBody(final ContentBody body) {
65          this.body = body;
66          return this;
67      }
68  
69      public MultipartPartBuilder addHeader(final String name, final String value, final List<NameValuePair> parameters) {
70          Args.notNull(name, "Header name");
71          this.header.addField(new MimeField(name, value, parameters));
72          return this;
73      }
74  
75      public MultipartPartBuilder addHeader(final String name, final String value) {
76          Args.notNull(name, "Header name");
77          this.header.addField(new MimeField(name, value));
78          return this;
79      }
80  
81      public MultipartPartBuilder setHeader(final String name, final String value) {
82          Args.notNull(name, "Header name");
83          this.header.setField(new MimeField(name, value));
84          return this;
85      }
86  
87      public MultipartPartBuilder removeHeaders(final String name) {
88          Args.notNull(name, "Header name");
89          this.header.removeFields(name);
90          return this;
91      }
92  
93      public MultipartPart build() {
94          Asserts.notNull(this.body, "Content body");
95          final Header headerCopy = new Header();
96          final List<MimeField> fields = this.header.getFields();
97          for (final MimeField field: fields) {
98              headerCopy.addField(field);
99          }
100         if (headerCopy.getField(MimeConsts.CONTENT_TYPE) == null) {
101             final ContentType contentType;
102             if (body instanceof AbstractContentBody) {
103                 contentType = ((AbstractContentBody) body).getContentType();
104             } else {
105                 contentType = null;
106             }
107             if (contentType != null) {
108                 headerCopy.addField(new MimeField(MimeConsts.CONTENT_TYPE, contentType.toString()));
109             } else {
110                 final StringBuilder buffer = new StringBuilder();
111                 buffer.append(this.body.getMimeType()); // MimeType cannot be null
112                 if (this.body.getCharset() != null) { // charset may legitimately be null
113                     buffer.append("; charset=");
114                     buffer.append(this.body.getCharset());
115                 }
116                 headerCopy.addField(new MimeField(MimeConsts.CONTENT_TYPE, buffer.toString()));
117             }
118         }
119         return new MultipartPart(this.body, headerCopy);
120     }
121 }