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