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.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  
36  import org.apache.http.ContentTooLongException;
37  import org.apache.http.Header;
38  import org.apache.http.HttpEntity;
39  import org.apache.http.entity.ContentType;
40  import org.apache.http.message.BasicHeader;
41  import org.apache.http.protocol.HTTP;
42  
43  @SuppressWarnings("deprecation")
44  class MultipartFormEntity implements HttpEntity {
45  
46      private final AbstractMultipartForm multipart;
47      private final Header contentType;
48      private final long contentLength;
49  
50      MultipartFormEntity(
51              final AbstractMultipartForm multipart,
52              final ContentType contentType,
53              final long contentLength) {
54          super();
55          this.multipart = multipart;
56          this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType.toString());
57          this.contentLength = contentLength;
58      }
59  
60      AbstractMultipartForm getMultipart() {
61          return this.multipart;
62      }
63  
64      @Override
65      public boolean isRepeatable() {
66          return this.contentLength != -1;
67      }
68  
69      @Override
70      public boolean isChunked() {
71          return !isRepeatable();
72      }
73  
74      @Override
75      public boolean isStreaming() {
76          return !isRepeatable();
77      }
78  
79      @Override
80      public long getContentLength() {
81          return this.contentLength;
82      }
83  
84      @Override
85      public Header getContentType() {
86          return this.contentType;
87      }
88  
89      @Override
90      public Header getContentEncoding() {
91          return null;
92      }
93  
94      @Override
95      public void consumeContent() {
96      }
97  
98      @Override
99      public InputStream getContent() throws IOException {
100         if (this.contentLength < 0) {
101             throw new ContentTooLongException("Content length is unknown");
102         } else if (this.contentLength > 25 * 1024) {
103             throw new ContentTooLongException("Content length is too long: " + this.contentLength);
104         }
105         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
106         writeTo(outStream);
107         outStream.flush();
108         return new ByteArrayInputStream(outStream.toByteArray());
109     }
110 
111     @Override
112     public void writeTo(final OutputStream outStream) throws IOException {
113         this.multipart.writeTo(outStream);
114     }
115 
116 }