1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36
37
38
39
40
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
84 try {
85 try {
86
87 EntityUtils.consume(this.httpResponse.getEntity());
88 }
89 finally {
90 this.httpResponse.close();
91 }
92 }
93 catch (IOException ex) {
94
95 }
96 }
97
98 }