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