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      /**
52       * Creates an instance with the specified settings.
53       *
54       * @param subType MIME subtype - must not be {@code null}
55       * @param charset the character set to use. May be {@code null},
56       *  in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
57       * @param boundary to use  - must not be {@code null}
58       * @param mode the mode to use
59       * @throws IllegalArgumentException if charset is null or boundary is null
60       */
61      public HttpMultipart(
62              final String subType, final Charset charset, final String boundary,
63              final HttpMultipartMode mode) {
64          super(subType, charset, boundary);
65          this.mode = mode;
66          this.parts = new ArrayList<FormBodyPart>();
67      }
68  
69      /**
70       * Creates an instance with the specified settings.
71       * Mode is set to {@link HttpMultipartMode#STRICT}
72       *
73       * @param subType MIME subtype - must not be {@code null}
74       * @param charset the character set to use. May be {@code null},
75       *   in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
76       * @param boundary to use  - must not be {@code null}
77       * @throws IllegalArgumentException if charset is null or boundary is null
78       */
79      public HttpMultipart(final String subType, final Charset charset, final String boundary) {
80          this(subType, charset, boundary, HttpMultipartMode.STRICT);
81      }
82  
83      public HttpMultipart(final String subType, final String boundary) {
84          this(subType, null, boundary);
85      }
86  
87      public HttpMultipartMode getMode() {
88          return this.mode;
89      }
90  
91      @Override
92      protected void formatMultipartHeader(
93              final FormBodyPart part, final OutputStream out) throws IOException {
94          final Header header = part.getHeader();
95          switch (this.mode) {
96              case BROWSER_COMPATIBLE:
97                  // For browser-compatible, only write Content-Disposition
98                  // Use content charset
99                  final MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION);
100                 writeField(cd, this.charset, out);
101                 final String filename = part.getBody().getFilename();
102                 if (filename != null) {
103                     final MinimalField ct = header.getField(MIME.CONTENT_TYPE);
104                     writeField(ct, this.charset, out);
105                 }
106                 break;
107             default:
108                 for (final MinimalField field: header) {
109                     writeField(field, out);
110                 }
111         }
112     }
113 
114     @Override
115     public List<FormBodyPart> getBodyParts() {
116         return this.parts;
117     }
118 
119     public void addBodyPart(final FormBodyPart part) {
120         if (part == null) {
121             return;
122         }
123         this.parts.add(part);
124     }
125 
126 }