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.impl.execchain;
29  
30  import java.io.IOException;
31  import java.util.Locale;
32  
33  import org.apache.http.Header;
34  import org.apache.http.HeaderIterator;
35  import org.apache.http.HttpEntity;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.ProtocolVersion;
38  import org.apache.http.StatusLine;
39  import org.apache.http.client.methods.CloseableHttpResponse;
40  import org.apache.http.params.HttpParams;
41  
42  /**
43   * A proxy class for {@link org.apache.http.HttpResponse} that can be used to release client connection
44   * associated with the original response.
45   *
46   * @since 4.3
47   */
48  class HttpResponseProxy implements CloseableHttpResponse {
49  
50      private final HttpResponse original;
51      private final ConnectionHolder connHolder;
52  
53      public HttpResponseProxy(final HttpResponse original, final ConnectionHolder connHolder) {
54          this.original = original;
55          this.connHolder = connHolder;
56          ResponseEntityProxy.enchance(original, connHolder);
57      }
58  
59      @Override
60      public void close() throws IOException {
61          if (this.connHolder != null) {
62              this.connHolder.close();
63          }
64      }
65  
66      @Override
67      public StatusLine getStatusLine() {
68          return original.getStatusLine();
69      }
70  
71      @Override
72      public void setStatusLine(final StatusLine statusline) {
73          original.setStatusLine(statusline);
74      }
75  
76      @Override
77      public void setStatusLine(final ProtocolVersion ver, final int code) {
78          original.setStatusLine(ver, code);
79      }
80  
81      @Override
82      public void setStatusLine(final ProtocolVersion ver, final int code, final String reason) {
83          original.setStatusLine(ver, code, reason);
84      }
85  
86      @Override
87      public void setStatusCode(final int code) throws IllegalStateException {
88          original.setStatusCode(code);
89      }
90  
91      @Override
92      public void setReasonPhrase(final String reason) throws IllegalStateException {
93          original.setReasonPhrase(reason);
94      }
95  
96      @Override
97      public HttpEntity getEntity() {
98          return original.getEntity();
99      }
100 
101     @Override
102     public void setEntity(final HttpEntity entity) {
103         original.setEntity(entity);
104     }
105 
106     @Override
107     public Locale getLocale() {
108         return original.getLocale();
109     }
110 
111     @Override
112     public void setLocale(final Locale loc) {
113         original.setLocale(loc);
114     }
115 
116     @Override
117     public ProtocolVersion getProtocolVersion() {
118         return original.getProtocolVersion();
119     }
120 
121     @Override
122     public boolean containsHeader(final String name) {
123         return original.containsHeader(name);
124     }
125 
126     @Override
127     public Header[] getHeaders(final String name) {
128         return original.getHeaders(name);
129     }
130 
131     @Override
132     public Header getFirstHeader(final String name) {
133         return original.getFirstHeader(name);
134     }
135 
136     @Override
137     public Header getLastHeader(final String name) {
138         return original.getLastHeader(name);
139     }
140 
141     @Override
142     public Header[] getAllHeaders() {
143         return original.getAllHeaders();
144     }
145 
146     @Override
147     public void addHeader(final Header header) {
148         original.addHeader(header);
149     }
150 
151     @Override
152     public void addHeader(final String name, final String value) {
153         original.addHeader(name, value);
154     }
155 
156     @Override
157     public void setHeader(final Header header) {
158         original.setHeader(header);
159     }
160 
161     @Override
162     public void setHeader(final String name, final String value) {
163         original.setHeader(name, value);
164     }
165 
166     @Override
167     public void setHeaders(final Header[] headers) {
168         original.setHeaders(headers);
169     }
170 
171     @Override
172     public void removeHeader(final Header header) {
173         original.removeHeader(header);
174     }
175 
176     @Override
177     public void removeHeaders(final String name) {
178         original.removeHeaders(name);
179     }
180 
181     @Override
182     public HeaderIterator headerIterator() {
183         return original.headerIterator();
184     }
185 
186     @Override
187     public HeaderIterator headerIterator(final String name) {
188         return original.headerIterator(name);
189     }
190 
191     @Override
192     public HttpParams getParams() {
193         return original.getParams();
194     }
195 
196     @Override
197     public void setParams(final HttpParams params) {
198         original.setParams(params);
199     }
200 
201     @Override
202     public String toString() {
203         final StringBuilder sb = new StringBuilder("HttpResponseProxy{");
204         sb.append(original);
205         sb.append('}');
206         return sb.toString();
207     }
208 
209 }