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.ArrayList;
31  import java.util.List;
32  
33  import org.apache.hc.core5.http.ContentType;
34  import org.apache.hc.core5.http.NameValuePair;
35  import org.apache.hc.core5.http.message.BasicNameValuePair;
36  import org.apache.hc.core5.util.Args;
37  import org.apache.hc.core5.util.Asserts;
38  
39  /**
40   * Builder for individual {@link org.apache.hc.client5.http.entity.mime.FormBodyPart}s.
41   *
42   * @since 4.4
43   */
44  public class FormBodyPartBuilder {
45  
46      private String name;
47      private ContentBody body;
48      private final Header header;
49  
50      public static FormBodyPartBuilder create(final String name, final ContentBody body) {
51          return new FormBodyPartBuilder(name, body);
52      }
53  
54      public static FormBodyPartBuilder create() {
55          return new FormBodyPartBuilder();
56      }
57  
58      FormBodyPartBuilder(final String name, final ContentBody body) {
59          this();
60          this.name = name;
61          this.body = body;
62      }
63  
64      FormBodyPartBuilder() {
65          this.header = new Header();
66      }
67  
68      public FormBodyPartBuilder setName(final String name) {
69          this.name = name;
70          return this;
71      }
72  
73      public FormBodyPartBuilder setBody(final ContentBody body) {
74          this.body = body;
75          return this;
76      }
77  
78      /**
79       * @since 4.6
80       */
81      public FormBodyPartBuilder addField(final String name, final String value, final List<NameValuePair> parameters) {
82          Args.notNull(name, "Field name");
83          this.header.addField(new MimeField(name, value, parameters));
84          return this;
85      }
86  
87      public FormBodyPartBuilder addField(final String name, final String value) {
88          Args.notNull(name, "Field name");
89          this.header.addField(new MimeField(name, value));
90          return this;
91      }
92  
93      public FormBodyPartBuilder setField(final String name, final String value) {
94          Args.notNull(name, "Field name");
95          this.header.setField(new MimeField(name, value));
96          return this;
97      }
98  
99      public FormBodyPartBuilder removeFields(final String name) {
100         Args.notNull(name, "Field name");
101         this.header.removeFields(name);
102         return this;
103     }
104 
105     public FormBodyPart build() {
106         Asserts.notBlank(this.name, "Name");
107         Asserts.notNull(this.body, "Content body");
108         final Header headerCopy = new Header();
109         final List<MimeField> fields = this.header.getFields();
110         for (final MimeField field: fields) {
111             headerCopy.addField(field);
112         }
113         if (headerCopy.getField(MimeConsts.CONTENT_DISPOSITION) == null) {
114             final List<NameValuePair> fieldParameters = new ArrayList<>();
115             fieldParameters.add(new BasicNameValuePair(MimeConsts.FIELD_PARAM_NAME, this.name));
116             if (this.body.getFilename() != null) {
117                 fieldParameters.add(new BasicNameValuePair(MimeConsts.FIELD_PARAM_FILENAME, this.body.getFilename()));
118             }
119             headerCopy.addField(new MimeField(MimeConsts.CONTENT_DISPOSITION, "form-data", fieldParameters));
120         }
121         if (headerCopy.getField(MimeConsts.CONTENT_TYPE) == null) {
122             final ContentType contentType;
123             if (body instanceof AbstractContentBody) {
124                 contentType = ((AbstractContentBody) body).getContentType();
125             } else {
126                 contentType = null;
127             }
128             if (contentType != null) {
129                 headerCopy.addField(new MimeField(MimeConsts.CONTENT_TYPE, contentType.toString()));
130             } else {
131                 final StringBuilder buffer = new StringBuilder();
132                 buffer.append(this.body.getMimeType()); // MimeType cannot be null
133                 if (this.body.getCharset() != null) { // charset may legitimately be null
134                     buffer.append("; charset=");
135                     buffer.append(this.body.getCharset());
136                 }
137                 headerCopy.addField(new MimeField(MimeConsts.CONTENT_TYPE, buffer.toString()));
138             }
139         }
140         return new FormBodyPart(this.name, this.body, headerCopy);
141     }
142 
143 }