View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.http.client;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.apache.http.Header;
23  import org.apache.http.HttpEntity;
24  import org.apache.http.client.methods.CloseableHttpResponse;
25  import org.apache.http.util.EntityUtils;
26  
27  import org.springframework.http.HttpHeaders;
28  
29  /**
30   * {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
31   * Apache HttpComponents HttpClient to execute requests.
32   *
33   * <p>Created via the {@link HttpComponentsClientHttpRequest}.
34   *
35   * <p><b>NOTE:</b> Requires Apache HttpComponents 4.3 or higher, as of Spring 4.0.
36   *
37   * @author Oleg Kalnichevski
38   * @author Arjen Poutsma
39   * @since 3.1
40   * @see HttpComponentsClientHttpRequest#execute()
41   */
42  final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse {
43  
44  	private final CloseableHttpResponse httpResponse;
45  
46  	private HttpHeaders headers;
47  
48  
49  	HttpComponentsClientHttpResponse(CloseableHttpResponse httpResponse) {
50  		this.httpResponse = httpResponse;
51  	}
52  
53  
54  	@Override
55  	public int getRawStatusCode() throws IOException {
56  		return this.httpResponse.getStatusLine().getStatusCode();
57  	}
58  
59  	@Override
60  	public String getStatusText() throws IOException {
61  		return this.httpResponse.getStatusLine().getReasonPhrase();
62  	}
63  
64  	@Override
65  	public HttpHeaders getHeaders() {
66  		if (this.headers == null) {
67  			this.headers = new HttpHeaders();
68  			for (Header header : this.httpResponse.getAllHeaders()) {
69  				this.headers.add(header.getName(), header.getValue());
70  			}
71  		}
72  		return this.headers;
73  	}
74  
75  	@Override
76  	public InputStream getBody() throws IOException {
77  		HttpEntity entity = this.httpResponse.getEntity();
78  		return (entity != null ? entity.getContent() : null);
79  	}
80  
81  	@Override
82  	public void close() {
83          // Release underlying connection back to the connection manager
84          try {
85              try {
86                  // Attempt to keep connection alive by consuming its remaining content
87                  EntityUtils.consume(this.httpResponse.getEntity());
88              }
89  			finally {
90                  this.httpResponse.close();
91              }
92          }
93          catch (IOException ex) {
94  			// Ignore exception on close...
95          }
96  	}
97  
98  }