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      public HttpEntityWrapper(final HttpEntity wrappedEntity) {
56          super();
57          this.wrappedEntity = Args.notNull(wrappedEntity, "Wrapped entity");
58      } // constructor
59  
60      @Override
61      public boolean isRepeatable() {
62          return wrappedEntity.isRepeatable();
63      }
64  
65      @Override
66      public boolean isChunked() {
67          return wrappedEntity.isChunked();
68      }
69  
70      @Override
71      public long getContentLength() {
72          return wrappedEntity.getContentLength();
73      }
74  
75      @Override
76      public Header getContentType() {
77          return wrappedEntity.getContentType();
78      }
79  
80      @Override
81      public Header getContentEncoding() {
82          return wrappedEntity.getContentEncoding();
83      }
84  
85      @Override
86      public InputStream getContent()
87          throws IOException {
88          return wrappedEntity.getContent();
89      }
90  
91      @Override
92      public void writeTo(final OutputStream outstream)
93          throws IOException {
94          wrappedEntity.writeTo(outstream);
95      }
96  
97      @Override
98      public boolean isStreaming() {
99          return wrappedEntity.isStreaming();
100     }
101 
102     /**
103      * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;
104      * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources.
105      */
106     @Override
107     @Deprecated
108     public void consumeContent() throws IOException {
109         wrappedEntity.consumeContent();
110     }
111 
112 }