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