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.core5.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.util.Collections;
34  import java.util.List;
35  import java.util.Set;
36  
37  import org.apache.hc.core5.function.Supplier;
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpEntity;
40  import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
41  import org.apache.hc.core5.http.io.entity.EmptyInputStream;
42  import org.apache.hc.core5.io.Closer;
43  
44  class IncomingHttpEntity implements HttpEntity {
45  
46      private final InputStream content;
47      private final long len;
48      private final boolean chunked;
49      private final Header contentType;
50      private final Header contentEncoding;
51  
52      IncomingHttpEntity(final InputStream content, final long len, final boolean chunked, final HeaderHeader.html#Header">Header contentType, final Header contentEncoding) {
53          this.content = content;
54          this.len = len;
55          this.chunked = chunked;
56          this.contentType = contentType;
57          this.contentEncoding = contentEncoding;
58      }
59  
60      @Override
61      public boolean isRepeatable() {
62          return false;
63      }
64  
65      @Override
66      public boolean isChunked() {
67          return chunked;
68      }
69  
70      @Override
71      public long getContentLength() {
72          return len;
73      }
74  
75      @Override
76      public String getContentType() {
77          return contentType != null ? contentType.getValue() : null;
78      }
79  
80      @Override
81      public String getContentEncoding() {
82          return contentEncoding != null ? contentEncoding.getValue() : null;
83      }
84  
85      @Override
86      public InputStream getContent() throws IOException, IllegalStateException {
87          return content;
88      }
89  
90      @Override
91      public boolean isStreaming() {
92          return content != null && content != EmptyInputStream.INSTANCE;
93      }
94  
95      @Override
96      public void writeTo(final OutputStream outStream) throws IOException {
97          AbstractHttpEntity.writeTo(this, outStream);
98      }
99  
100     @Override
101     public Supplier<List<? extends Header>> getTrailers() {
102         return null;
103     }
104 
105     @Override
106     public Set<String> getTrailerNames() {
107         return Collections.emptySet();
108     }
109 
110     @Override
111     public void close() throws IOException {
112         Closer.close(content);
113     }
114 
115     @Override
116     public String toString() {
117         final StringBuilder sb = new StringBuilder();
118         sb.append('[');
119         sb.append("Content-Type: ");
120         sb.append(getContentType());
121         sb.append(',');
122         sb.append("Content-Encoding: ");
123         sb.append(getContentEncoding());
124         sb.append(',');
125         final long len = getContentLength();
126         if (len >= 0) {
127             sb.append("Content-Length: ");
128             sb.append(len);
129             sb.append(',');
130         }
131         sb.append("Chunked: ");
132         sb.append(isChunked());
133         sb.append(']');
134         return sb.toString();
135     }
136 
137 }