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.List;
34  import java.util.Set;
35  
36  import org.apache.hc.core5.http.Header;
37  import org.apache.hc.core5.http.HttpEntity;
38  import org.apache.hc.core5.function.Supplier;
39  import org.apache.hc.core5.util.Args;
40  
41  /**
42   * Base class for wrapping entities.
43   * Keeps a {@link #wrappedEntity wrappedEntity} and delegates all
44   * calls to it. Implementations of wrapping entities can derive
45   * from this class and need to override only those methods that
46   * should not be delegated to the wrapped entity.
47   *
48   * @since 4.0
49   */
50  public class HttpEntityWrapper implements HttpEntity {
51  
52      /** The wrapped entity. */
53      private final HttpEntity wrappedEntity;
54  
55      /**
56       * Creates a new entity wrapper.
57       *
58       * @param wrappedEntity the entity to wrap.
59       */
60      public HttpEntityWrapper(final HttpEntity wrappedEntity) {
61          super();
62          this.wrappedEntity = Args.notNull(wrappedEntity, "Wrapped entity");
63      }
64  
65      @Override
66      public boolean isRepeatable() {
67          return wrappedEntity.isRepeatable();
68      }
69  
70      @Override
71      public boolean isChunked() {
72          return wrappedEntity.isChunked();
73      }
74  
75      @Override
76      public long getContentLength() {
77          return wrappedEntity.getContentLength();
78      }
79  
80      @Override
81      public String getContentType() {
82          return wrappedEntity.getContentType();
83      }
84  
85      @Override
86      public String getContentEncoding() {
87          return wrappedEntity.getContentEncoding();
88      }
89  
90      @Override
91      public InputStream getContent()
92          throws IOException {
93          return wrappedEntity.getContent();
94      }
95  
96      @Override
97      public void writeTo(final OutputStream outStream)
98          throws IOException {
99          wrappedEntity.writeTo(outStream);
100     }
101 
102     @Override
103     public boolean isStreaming() {
104         return wrappedEntity.isStreaming();
105     }
106 
107     @Override
108     public Supplier<List<? extends Header>> getTrailers() {
109         return wrappedEntity.getTrailers();
110     }
111 
112     @Override
113     public Set<String> getTrailerNames() {
114         return wrappedEntity.getTrailerNames();
115     }
116 
117     @Override
118     public void close() throws IOException {
119         wrappedEntity.close();
120     }
121 
122     @Override
123     public String toString() {
124         return super.toString() + "[" + wrappedEntity + "]";
125     }
126 
127 }