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      public void close() throws IOException {
62          if (this.connHolder != null) {
63              this.connHolder.abortConnection();
64          }
65      }
66  
67      public StatusLine getStatusLine() {
68          return original.getStatusLine();
69      }
70  
71      public void setStatusLine(final StatusLine statusline) {
72          original.setStatusLine(statusline);
73      }
74  
75      public void setStatusLine(final ProtocolVersion ver, final int code) {
76          original.setStatusLine(ver, code);
77      }
78  
79      public void setStatusLine(final ProtocolVersion ver, final int code, final String reason) {
80          original.setStatusLine(ver, code, reason);
81      }
82  
83      public void setStatusCode(final int code) throws IllegalStateException {
84          original.setStatusCode(code);
85      }
86  
87      public void setReasonPhrase(final String reason) throws IllegalStateException {
88          original.setReasonPhrase(reason);
89      }
90  
91      public HttpEntity getEntity() {
92          return original.getEntity();
93      }
94  
95      public void setEntity(final HttpEntity entity) {
96          original.setEntity(entity);
97      }
98  
99      public Locale getLocale() {
100         return original.getLocale();
101     }
102 
103     public void setLocale(final Locale loc) {
104         original.setLocale(loc);
105     }
106 
107     public ProtocolVersion getProtocolVersion() {
108         return original.getProtocolVersion();
109     }
110 
111     public boolean containsHeader(final String name) {
112         return original.containsHeader(name);
113     }
114 
115     public Header[] getHeaders(final String name) {
116         return original.getHeaders(name);
117     }
118 
119     public Header getFirstHeader(final String name) {
120         return original.getFirstHeader(name);
121     }
122 
123     public Header getLastHeader(final String name) {
124         return original.getLastHeader(name);
125     }
126 
127     public Header[] getAllHeaders() {
128         return original.getAllHeaders();
129     }
130 
131     public void addHeader(final Header header) {
132         original.addHeader(header);
133     }
134 
135     public void addHeader(final String name, final String value) {
136         original.addHeader(name, value);
137     }
138 
139     public void setHeader(final Header header) {
140         original.setHeader(header);
141     }
142 
143     public void setHeader(final String name, final String value) {
144         original.setHeader(name, value);
145     }
146 
147     public void setHeaders(final Header[] headers) {
148         original.setHeaders(headers);
149     }
150 
151     public void removeHeader(final Header header) {
152         original.removeHeader(header);
153     }
154 
155     public void removeHeaders(final String name) {
156         original.removeHeaders(name);
157     }
158 
159     public HeaderIterator headerIterator() {
160         return original.headerIterator();
161     }
162 
163     public HeaderIterator headerIterator(final String name) {
164         return original.headerIterator(name);
165     }
166 
167     @Deprecated
168     public HttpParams getParams() {
169         return original.getParams();
170     }
171 
172     @Deprecated
173     public void setParams(final HttpParams params) {
174         original.setParams(params);
175     }
176 
177     @Override
178     public String toString() {
179         final StringBuilder sb = new StringBuilder("HttpResponseProxy{");
180         sb.append(original);
181         sb.append('}');
182         return sb.toString();
183     }
184 
185 }