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