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.InputStream;
32  import java.io.OutputStream;
33  import java.nio.charset.Charset;
34  import java.util.Random;
35  
36  import org.apache.http.Header;
37  import org.apache.http.HttpEntity;
38  import org.apache.http.entity.mime.content.ContentBody;
39  
40  /**
41   * Multipart/form coded HTTP entity consisting of multiple body parts.
42   *
43   * @since 4.0
44   *
45   * @deprecated 4.3 Use {@link MultipartEntityBuilder}.
46   */
47  @Deprecated
48  public class MultipartEntity implements HttpEntity {
49  
50      /**
51       * The pool of ASCII chars to be used for generating a multipart boundary.
52       */
53      private final static char[] MULTIPART_CHARS =
54          "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
55              .toCharArray();
56  
57      private final MultipartEntityBuilder builder;
58      private volatile MultipartFormEntity entity;
59  
60      /**
61       * Creates an instance using the specified parameters
62       * @param mode the mode to use, may be {@code null}, in which case {@link HttpMultipartMode#STRICT} is used
63       * @param boundary the boundary string, may be {@code null}, in which case {@link #generateBoundary()} is invoked to create the string
64       * @param charset the character set to use, may be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
65       */
66      public MultipartEntity(
67              final HttpMultipartMode mode,
68              final String boundary,
69              final Charset charset) {
70          super();
71          this.builder = new MultipartEntityBuilder()
72                  .setMode(mode)
73                  .setCharset(charset != null ? charset : MIME.DEFAULT_CHARSET)
74                  .setBoundary(boundary);
75          this.entity = null;
76      }
77  
78      /**
79       * Creates an instance using the specified {@link HttpMultipartMode} mode.
80       * Boundary and charset are set to {@code null}.
81       * @param mode the desired mode
82       */
83      public MultipartEntity(final HttpMultipartMode mode) {
84          this(mode, null, null);
85      }
86  
87      /**
88       * Creates an instance using mode {@link HttpMultipartMode#STRICT}
89       */
90      public MultipartEntity() {
91          this(HttpMultipartMode.STRICT, null, null);
92      }
93  
94      protected String generateContentType(
95              final String boundary,
96              final Charset charset) {
97          final StringBuilder buffer = new StringBuilder();
98          buffer.append("multipart/form-data; boundary=");
99          buffer.append(boundary);
100         if (charset != null) {
101             buffer.append("; charset=");
102             buffer.append(charset.name());
103         }
104         return buffer.toString();
105     }
106 
107     protected String generateBoundary() {
108         final StringBuilder buffer = new StringBuilder();
109         final Random rand = new Random();
110         final int count = rand.nextInt(11) + 30; // a random size from 30 to 40
111         for (int i = 0; i < count; i++) {
112             buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
113         }
114         return buffer.toString();
115     }
116 
117     private MultipartFormEntity getEntity() {
118         if (this.entity == null) {
119             this.entity = this.builder.buildEntity();
120         }
121         return this.entity;
122     }
123 
124     public void addPart(final FormBodyPart bodyPart) {
125         this.builder.addPart(bodyPart);
126         this.entity = null;
127     }
128 
129     public void addPart(final String name, final ContentBody contentBody) {
130         addPart(new FormBodyPart(name, contentBody));
131     }
132 
133     @Override
134     public boolean isRepeatable() {
135         return getEntity().isRepeatable();
136     }
137 
138     @Override
139     public boolean isChunked() {
140         return getEntity().isChunked();
141     }
142 
143     @Override
144     public boolean isStreaming() {
145         return getEntity().isStreaming();
146     }
147 
148     @Override
149     public long getContentLength() {
150         return getEntity().getContentLength();
151     }
152 
153     @Override
154     public Header getContentType() {
155         return getEntity().getContentType();
156     }
157 
158     @Override
159     public Header getContentEncoding() {
160         return getEntity().getContentEncoding();
161     }
162 
163     @Override
164     public void consumeContent()
165         throws IOException, UnsupportedOperationException{
166         if (isStreaming()) {
167             throw new UnsupportedOperationException(
168                     "Streaming entity does not implement #consumeContent()");
169         }
170     }
171 
172     @Override
173     public InputStream getContent() throws IOException, UnsupportedOperationException {
174         throw new UnsupportedOperationException(
175                     "Multipart form entity does not implement #getContent()");
176     }
177 
178     @Override
179     public void writeTo(final OutputStream outStream) throws IOException {
180         getEntity().writeTo(outStream);
181     }
182 
183 }