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.io.IOException;
31  import java.io.OutputStream;
32  import java.nio.charset.Charset;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   * HttpMultipart represents a collection of MIME multipart encoded content bodies. This class is
38   * capable of operating either in the strict (RFC 822, RFC 2045, RFC 2046 compliant) or
39   * the browser compatible modes.
40   *
41   * @since 4.0
42   *
43   * @deprecated  (4.3) Use {@link MultipartEntityBuilder}.
44   */
45   @Deprecated
46  public class HttpMultipart extends AbstractMultipartForm {
47  
48      private final HttpMultipartMode mode;
49      private final List<FormBodyPart> parts;
50  
51      private final String subType;
52  
53      /**
54       * Creates an instance with the specified settings.
55       *
56       * @param subType MIME subtype - must not be {@code null}
57       * @param charset the character set to use. May be {@code null},
58       *  in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
59       * @param boundary to use  - must not be {@code null}
60       * @param mode the mode to use
61       * @throws IllegalArgumentException if charset is null or boundary is null
62       */
63      public HttpMultipart(
64              final String subType, final Charset charset, final String boundary,
65              final HttpMultipartMode mode) {
66          super(charset, boundary);
67          this.subType = subType;
68          this.mode = mode;
69          this.parts = new ArrayList<FormBodyPart>();
70      }
71  
72      /**
73       * Creates an instance with the specified settings.
74       * Mode is set to {@link HttpMultipartMode#STRICT}
75       *
76       * @param subType MIME subtype - must not be {@code null}
77       * @param charset the character set to use. May be {@code null},
78       *   in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
79       * @param boundary to use  - must not be {@code null}
80       * @throws IllegalArgumentException if charset is null or boundary is null
81       */
82      public HttpMultipart(final String subType, final Charset charset, final String boundary) {
83          this(subType, charset, boundary, HttpMultipartMode.STRICT);
84      }
85  
86      public HttpMultipart(final String subType, final String boundary) {
87          this(subType, null, boundary);
88      }
89  
90      public HttpMultipartMode getMode() {
91          return this.mode;
92      }
93  
94      @Override
95      protected void formatMultipartHeader(
96              final FormBodyPart part, final OutputStream out) throws IOException {
97          final Header header = part.getHeader();
98          switch (this.mode) {
99              case BROWSER_COMPATIBLE:
100                 // For browser-compatible, only write Content-Disposition
101                 // Use content charset
102                 final MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION);
103                 writeField(cd, this.charset, out);
104                 final String filename = part.getBody().getFilename();
105                 if (filename != null) {
106                     final MinimalField ct = header.getField(MIME.CONTENT_TYPE);
107                     writeField(ct, this.charset, out);
108                 }
109                 break;
110             default:
111                 for (final MinimalField field: header) {
112                     writeField(field, out);
113                 }
114         }
115     }
116 
117     @Override
118     public List<FormBodyPart> getBodyParts() {
119         return this.parts;
120     }
121 
122     public void addBodyPart(final FormBodyPart part) {
123         if (part == null) {
124             return;
125         }
126         this.parts.add(part);
127     }
128 
129     public String getSubType() {
130         return this.subType;
131     }
132 
133     public Charset getCharset() {
134         return this.charset;
135     }
136 
137     public String getBoundary() {
138         return this.boundary;
139     }
140 
141 }