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.ContentType;
31  import org.apache.http.entity.mime.content.AbstractContentBody;
32  import org.apache.http.entity.mime.content.ContentBody;
33  import org.apache.http.util.Args;
34  
35  /**
36   * FormBodyPart class represents a content body that can be used as a part of multipart encoded
37   * entities. This class automatically populates the header with standard fields based on
38   * the content description of the enclosed body.
39   *
40   * @since 4.0
41   */
42  public class FormBodyPart {
43  
44      private final String name;
45      private final Header header;
46      private final ContentBody body;
47  
48      FormBodyPart(final String name, final ContentBody body, final Header header) {
49          super();
50          Args.notNull(name, "Name");
51          Args.notNull(body, "Body");
52          this.name = name;
53          this.body = body;
54          this.header = header != null ? header : new Header();
55      }
56  
57      /**
58       * @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
59       */
60      @Deprecated
61      public FormBodyPart(final String name, final ContentBody body) {
62          super();
63          Args.notNull(name, "Name");
64          Args.notNull(body, "Body");
65          this.name = name;
66          this.body = body;
67          this.header = new Header();
68  
69          generateContentDisp(body);
70          generateContentType(body);
71          generateTransferEncoding(body);
72      }
73  
74      public String getName() {
75          return this.name;
76      }
77  
78      public ContentBody getBody() {
79          return this.body;
80      }
81  
82      public Header getHeader() {
83          return this.header;
84      }
85  
86      public void addField(final String name, final String value) {
87          Args.notNull(name, "Field name");
88          this.header.addField(new MinimalField(name, value));
89      }
90  
91      /**
92       * @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
93       */
94      @Deprecated
95      protected void generateContentDisp(final ContentBody body) {
96          final StringBuilder buffer = new StringBuilder();
97          buffer.append("form-data; name=\"");
98          buffer.append(getName());
99          buffer.append("\"");
100         if (body.getFilename() != null) {
101             buffer.append("; filename=\"");
102             buffer.append(body.getFilename());
103             buffer.append("\"");
104         }
105         addField(MIME.CONTENT_DISPOSITION, buffer.toString());
106     }
107 
108     /**
109      * @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
110      */
111     @Deprecated
112     protected void generateContentType(final ContentBody body) {
113         final ContentType contentType;
114         if (body instanceof AbstractContentBody) {
115             contentType = ((AbstractContentBody) body).getContentType();
116         } else {
117             contentType = null;
118         }
119         if (contentType != null) {
120             addField(MIME.CONTENT_TYPE, contentType.toString());
121         } else {
122             final StringBuilder buffer = new StringBuilder();
123             buffer.append(body.getMimeType()); // MimeType cannot be null
124             if (body.getCharset() != null) { // charset may legitimately be null
125                 buffer.append("; charset=");
126                 buffer.append(body.getCharset());
127             }
128             addField(MIME.CONTENT_TYPE, buffer.toString());
129         }
130     }
131 
132     /**
133      * @deprecated (4.4) use {@link org.apache.http.entity.mime.FormBodyPartBuilder}.
134      */
135     @Deprecated
136     protected void generateTransferEncoding(final ContentBody body) {
137         addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()); // TE cannot be null
138     }
139 
140 }