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.http.entity.mime;
29  
30  import java.util.List;
31  
32  import org.apache.http.entity.ContentType;
33  import org.apache.http.entity.mime.content.AbstractContentBody;
34  import org.apache.http.entity.mime.content.ContentBody;
35  import org.apache.http.util.Args;
36  import org.apache.http.util.Asserts;
37  
38  /**
39   * Builder for individual {@link org.apache.http.entity.mime.FormBodyPart}s.
40   *
41   * @since 4.4
42   */
43  public class FormBodyPartBuilder {
44  
45      private String name;
46      private ContentBody body;
47      private final Header header;
48  
49      public static FormBodyPartBuilder create(final String name, final ContentBody body) {
50          return new FormBodyPartBuilder(name, body);
51      }
52  
53      public static FormBodyPartBuilder create() {
54          return new FormBodyPartBuilder();
55      }
56  
57      FormBodyPartBuilder(final String name, final ContentBody body) {
58          this();
59          this.name = name;
60          this.body = body;
61      }
62  
63      FormBodyPartBuilder() {
64          this.header = new Header();
65      }
66  
67      public FormBodyPartBuilder setName(final String name) {
68          this.name = name;
69          return this;
70      }
71  
72      public FormBodyPartBuilder setBody(final ContentBody body) {
73          this.body = body;
74          return this;
75      }
76  
77      public FormBodyPartBuilder addField(final String name, final String value) {
78          Args.notNull(name, "Field name");
79          this.header.addField(new MinimalField(name, value));
80          return this;
81      }
82  
83      public FormBodyPartBuilder setField(final String name, final String value) {
84          Args.notNull(name, "Field name");
85          this.header.setField(new MinimalField(name, value));
86          return this;
87      }
88  
89      public FormBodyPartBuilder removeFields(final String name) {
90          Args.notNull(name, "Field name");
91          this.header.removeFields(name);
92          return this;
93      }
94  
95      public FormBodyPart build() {
96          Asserts.notBlank(this.name, "Name");
97          Asserts.notNull(this.body, "Content body");
98          final Headereader.html#Header">Header headerCopy = new Header();
99          final List<MinimalField> fields = this.header.getFields();
100         for (final MinimalField field: fields) {
101             headerCopy.addField(field);
102         }
103         if (headerCopy.getField(MIME.CONTENT_DISPOSITION) == null) {
104             final StringBuilder buffer = new StringBuilder();
105             buffer.append("form-data; name=\"");
106             buffer.append(encodeForHeader(this.name));
107             buffer.append("\"");
108             if (this.body.getFilename() != null) {
109                 buffer.append("; filename=\"");
110                 buffer.append(encodeForHeader(this.body.getFilename()));
111                 buffer.append("\"");
112             }
113             headerCopy.addField(new MinimalField(MIME.CONTENT_DISPOSITION, buffer.toString()));
114         }
115         if (headerCopy.getField(MIME.CONTENT_TYPE) == null) {
116             final ContentType contentType;
117             if (body instanceof AbstractContentBody) {
118                 contentType = ((AbstractContentBody) body).getContentType();
119             } else {
120                 contentType = null;
121             }
122             if (contentType != null) {
123                 headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, contentType.toString()));
124             } else {
125                 final StringBuilder buffer = new StringBuilder();
126                 buffer.append(this.body.getMimeType()); // MimeType cannot be null
127                 if (this.body.getCharset() != null) { // charset may legitimately be null
128                     buffer.append("; charset=");
129                     buffer.append(this.body.getCharset());
130                 }
131                 headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, buffer.toString()));
132             }
133         }
134         if (headerCopy.getField(MIME.CONTENT_TRANSFER_ENC) == null) {
135             // TE cannot be null
136             headerCopy.addField(new MinimalField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()));
137         }
138         return new FormBodyPart(this.name, this.body, headerCopy);
139     }
140 
141     private static String encodeForHeader(final String headerName) {
142         if (headerName == null) {
143             return null;
144         }
145         final StringBuilder sb = new StringBuilder();
146         for (int i = 0; i < headerName.length(); i++) {
147             final char x = headerName.charAt(i);
148             if (x == '"' || x == '\\' || x == '\r') {
149                 sb.append("\\");
150             }
151             sb.append(x);
152         }
153         return sb.toString();
154     }
155 
156 }