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