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 org.apache.http.entity.mime.content.ContentBody;
31  
32  /**
33   * FormBodyPart class represents a content body that can be used as a part of multipart encoded
34   * entities. This class automatically populates the header with standard fields based on
35   * the content description of the enclosed body.
36   *
37   * @since 4.0
38   */
39  public class FormBodyPart {
40  
41      private final String name;
42      private final Header header;
43  
44      private final ContentBody body;
45  
46      public FormBodyPart(final String name, final ContentBody body) {
47          super();
48          if (name == null) {
49              throw new IllegalArgumentException("Name may not be null");
50          }
51          if (body == null) {
52              throw new IllegalArgumentException("Body may not be null");
53          }
54          this.name = name;
55          this.body = body;
56          this.header = new Header();
57  
58          generateContentDisp(body);
59          generateContentType(body);
60          generateTransferEncoding(body);
61      }
62  
63      public String getName() {
64          return this.name;
65      }
66  
67      public ContentBody getBody() {
68          return this.body;
69      }
70  
71      public Header getHeader() {
72          return this.header;
73      }
74  
75      public void addField(final String name, final String value) {
76          if (name == null) {
77              throw new IllegalArgumentException("Field name may not be null");
78          }
79          this.header.addField(new MinimalField(name, value));
80      }
81  
82      protected void generateContentDisp(final ContentBody body) {
83          StringBuilder buffer = new StringBuilder();
84          buffer.append("form-data; name=\"");
85          buffer.append(getName());
86          buffer.append("\"");
87          if (body.getFilename() != null) {
88              buffer.append("; filename=\"");
89              buffer.append(body.getFilename());
90              buffer.append("\"");
91          }
92          addField(MIME.CONTENT_DISPOSITION, buffer.toString());
93      }
94  
95      protected void generateContentType(final ContentBody body) {
96          StringBuilder buffer = new StringBuilder();
97          buffer.append(body.getMimeType()); // MimeType cannot be null
98          if (body.getCharset() != null) { // charset may legitimately be null
99              buffer.append("; charset=");
100             buffer.append(body.getCharset());
101         }
102         addField(MIME.CONTENT_TYPE, buffer.toString());
103     }
104 
105     protected void generateTransferEncoding(final ContentBody body) {
106         addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()); // TE cannot be null
107     }
108 
109 }