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.io.entity;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.util.Arrays;
34  import java.util.LinkedHashSet;
35  import java.util.List;
36  import java.util.Set;
37  
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpEntity;
40  import org.apache.hc.core5.function.Supplier;
41  import org.apache.hc.core5.util.Args;
42  
43  /**
44   * Wrapping entity that also includes trailers.
45   *
46   * @since 5.0
47   */
48  public class HttpEntityWithTrailers implements HttpEntity {
49  
50      private final HttpEntity wrappedEntity;
51      private final List<Header> trailers;
52  
53      /**
54       * Creates a new entity wrapper.
55       */
56      public HttpEntityWithTrailers(final HttpEntity wrappedEntity, final Header... trailers) {
57          super();
58          this.wrappedEntity = Args.notNull(wrappedEntity, "Wrapped entity");
59          this.trailers = Arrays.asList(trailers);
60      }
61  
62      @Override
63      public boolean isRepeatable() {
64          return wrappedEntity.isRepeatable();
65      }
66  
67      @Override
68      public boolean isChunked() {
69          return true;
70      }
71  
72      @Override
73      public long getContentLength() {
74          return wrappedEntity.getContentLength();
75      }
76  
77      @Override
78      public String getContentType() {
79          return wrappedEntity.getContentType();
80      }
81  
82      @Override
83      public String getContentEncoding() {
84          return wrappedEntity.getContentEncoding();
85      }
86  
87      @Override
88      public InputStream getContent()
89          throws IOException {
90          return wrappedEntity.getContent();
91      }
92  
93      @Override
94      public void writeTo(final OutputStream outStream)
95          throws IOException {
96          wrappedEntity.writeTo(outStream);
97      }
98  
99      @Override
100     public boolean isStreaming() {
101         return wrappedEntity.isStreaming();
102     }
103 
104     @Override
105     public Supplier<List<? extends Header>> getTrailers() {
106         return new Supplier<List<? extends Header>>() {
107             @Override
108             public List<? extends Header> get() {
109                 return trailers;
110             }
111         };
112     }
113 
114     @Override
115     public Set<String> getTrailerNames() {
116         final Set<String> names = new LinkedHashSet<>();
117         for (final Header trailer: trailers) {
118             names.add(trailer.getName());
119         }
120         return names;
121     }
122 
123     @Override
124     public void close() throws IOException {
125         wrappedEntity.close();
126     }
127 
128 }