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