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.hc.client5.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  import java.util.List;
36  import java.util.Set;
37  
38  import org.apache.hc.core5.function.Supplier;
39  import org.apache.hc.core5.http.ContentTooLongException;
40  import org.apache.hc.core5.http.ContentType;
41  import org.apache.hc.core5.http.Header;
42  import org.apache.hc.core5.http.HttpEntity;
43  
44  class MultipartFormEntity implements HttpEntity {
45  
46      private final AbstractMultipartFormat multipart;
47      private final ContentType contentType;
48      private final long contentLength;
49  
50      MultipartFormEntity(
51              final AbstractMultipartFormat multipart,
52              final ContentType contentType,
53              final long contentLength) {
54          super();
55          this.multipart = multipart;
56          this.contentType = contentType;
57          this.contentLength = contentLength;
58      }
59  
60      AbstractMultipartFormat 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 String getContentType() {
86          return this.contentType != null ? this.contentType.toString() : null;
87      }
88  
89      @Override
90      public String getContentEncoding() {
91          return null;
92      }
93  
94      @Override
95      public InputStream getContent() throws IOException {
96          if (this.contentLength < 0) {
97              throw new ContentTooLongException("Content length is unknown");
98          } else if (this.contentLength > 25 * 1024) {
99              throw new ContentTooLongException("Content length is too long: " + this.contentLength);
100         }
101         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
102         writeTo(outStream);
103         outStream.flush();
104         return new ByteArrayInputStream(outStream.toByteArray());
105     }
106 
107     @Override
108     public void writeTo(final OutputStream outStream) throws IOException {
109         this.multipart.writeTo(outStream);
110     }
111 
112     @Override
113     public Supplier<List<? extends Header>> getTrailers() {
114         return null;
115     }
116 
117     @Override
118     public Set<String> getTrailerNames() {
119         return null;
120     }
121 
122     @Override
123     public void close() throws IOException {
124     }
125 
126 }