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