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.annotation.NotThreadSafe;
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  @NotThreadSafe
48  public class HttpEntityWrapper implements HttpEntity {
49  
50      /** The wrapped entity. */
51      protected HttpEntity wrappedEntity;
52  
53      /**
54       * Creates a new entity wrapper.
55       *
56       * @param wrapped   the entity to wrap, not null
57       * @throws IllegalArgumentException if wrapped is null
58       */
59      public HttpEntityWrapper(HttpEntity wrapped) {
60          super();
61  
62          if (wrapped == null) {
63              throw new IllegalArgumentException
64                  ("wrapped entity must not be null");
65          }
66          wrappedEntity = wrapped;
67  
68      } // constructor
69  
70  
71      public boolean isRepeatable() {
72          return wrappedEntity.isRepeatable();
73      }
74  
75      public boolean isChunked() {
76          return wrappedEntity.isChunked();
77      }
78  
79      public long getContentLength() {
80          return wrappedEntity.getContentLength();
81      }
82  
83      public Header getContentType() {
84          return wrappedEntity.getContentType();
85      }
86  
87      public Header getContentEncoding() {
88          return wrappedEntity.getContentEncoding();
89      }
90  
91      public InputStream getContent()
92          throws IOException {
93          return wrappedEntity.getContent();
94      }
95  
96      public void writeTo(OutputStream outstream)
97          throws IOException {
98          wrappedEntity.writeTo(outstream);
99      }
100 
101     public boolean isStreaming() {
102         return wrappedEntity.isStreaming();
103     }
104 
105     /**
106      * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;
107      * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources.
108      */
109     @Deprecated
110     public void consumeContent() throws IOException {
111         wrappedEntity.consumeContent();
112     }
113 
114 }