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.http.entity;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  
34  import org.apache.http.Header;
35  import org.apache.http.HttpEntity;
36  import org.apache.http.util.Args;
37  
38  /**
39   * Base class for wrapping entities.
40   * Keeps a {@link #wrappedEntity wrappedEntity} and delegates all
41   * calls to it. Implementations of wrapping entities can derive
42   * from this class and need to override only those methods that
43   * should not be delegated to the wrapped entity.
44   *
45   * @since 4.0
46   */
47  public class HttpEntityWrapper implements HttpEntity {
48  
49      /** The wrapped entity. */
50      protected HttpEntity wrappedEntity;
51  
52      /**
53       * Creates a new entity wrapper.
54       *
55       * @param wrappedEntity the entity to wrap.
56       */
57      public HttpEntityWrapper(final HttpEntity wrappedEntity) {
58          super();
59          this.wrappedEntity = Args.notNull(wrappedEntity, "Wrapped entity");
60      }
61  
62      @Override
63      public boolean isRepeatable() {
64          return wrappedEntity.isRepeatable();
65      }
66  
67      @Override
68      public boolean isChunked() {
69          return wrappedEntity.isChunked();
70      }
71  
72      @Override
73      public long getContentLength() {
74          return wrappedEntity.getContentLength();
75      }
76  
77      @Override
78      public Header getContentType() {
79          return wrappedEntity.getContentType();
80      }
81  
82      @Override
83      public Header 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     /**
105      * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;
106      * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources.
107      */
108     @Override
109     @Deprecated
110     public void consumeContent() throws IOException {
111         wrappedEntity.consumeContent();
112     }
113 
114 }