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