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      @Override
60      public boolean isRepeatable() {
61          return this.contentLength != -1;
62      }
63  
64      @Override
65      public boolean isChunked() {
66          return !isRepeatable();
67      }
68  
69      @Override
70      public boolean isStreaming() {
71          return !isRepeatable();
72      }
73  
74      @Override
75      public long getContentLength() {
76          return this.contentLength;
77      }
78  
79      @Override
80      public Header getContentType() {
81          return this.contentType;
82      }
83  
84      @Override
85      public Header getContentEncoding() {
86          return null;
87      }
88  
89      @Override
90      public void consumeContent()
91          throws IOException, UnsupportedOperationException{
92          if (isStreaming()) {
93              throw new UnsupportedOperationException(
94                      "Streaming entity does not implement #consumeContent()");
95          }
96      }
97  
98      @Override
99      public InputStream getContent() throws IOException {
100         throw new UnsupportedOperationException(
101                     "Multipart form entity does not implement #getContent()");
102     }
103 
104     @Override
105     public void writeTo(final OutputStream outstream) throws IOException {
106         this.multipart.writeTo(outstream);
107     }
108 
109 }