@@ -31,11 +31,23 @@ final class Apache5HttpResponse extends LowLevelHttpResponse {
31
31
private final HttpUriRequestBase request ;
32
32
private final ClassicHttpResponse response ;
33
33
private final Header [] allHeaders ;
34
+ private final HttpEntity entity ;
35
+ private final Apache5ResponseContent content ;
34
36
35
37
Apache5HttpResponse (HttpUriRequestBase request , ClassicHttpResponse response ) {
36
38
this .request = request ;
37
39
this .response = response ;
38
- allHeaders = response .getHeaders ();
40
+ this .allHeaders = response .getHeaders ();
41
+ this .entity = response .getEntity ();
42
+ Apache5ResponseContent contentValue = null ;
43
+ if (entity != null ) {
44
+ try {
45
+ contentValue = new Apache5ResponseContent (entity .getContent (), response );
46
+ } catch (IOException ex ) {
47
+ LOGGER .log (Level .SEVERE , "Error when obtaining content from the response" , ex );
48
+ }
49
+ }
50
+ this .content = contentValue ;
39
51
}
40
52
41
53
@ Override
@@ -45,28 +57,21 @@ public int getStatusCode() {
45
57
46
58
@ Override
47
59
public InputStream getContent () throws IOException {
48
- HttpEntity entity = response .getEntity ();
49
- return entity == null ? null : entity .getContent ();
60
+ return content ;
50
61
}
51
62
52
63
@ Override
53
64
public String getContentEncoding () {
54
- HttpEntity entity = response .getEntity ();
55
- if (entity != null ) {
56
- return entity .getContentEncoding ();
57
- }
58
- return null ;
65
+ return entity != null ? entity .getContentEncoding () : null ;
59
66
}
60
67
61
68
@ Override
62
69
public long getContentLength () {
63
- HttpEntity entity = response .getEntity ();
64
70
return entity == null ? -1 : entity .getContentLength ();
65
71
}
66
72
67
73
@ Override
68
74
public String getContentType () {
69
- HttpEntity entity = response .getEntity ();
70
75
return entity == null ? null : entity .getContentType ();
71
76
}
72
77
@@ -102,17 +107,14 @@ public String getHeaderValue(int index) {
102
107
/** Aborts execution of the request. */
103
108
@ Override
104
109
public void disconnect () {
105
- close ();
106
- }
107
-
108
- public void close () {
109
110
request .abort ();
110
111
try {
111
- response .close ();
112
+ // this call also close the response
113
+ content .close ();
112
114
} catch (IOException e ) {
113
115
// the close() method contract won't allow us to declare a thrown exception. Here we just log
114
116
// the error
115
- LOGGER .log (Level .SEVERE , "Error occurred when closing the response " , e );
117
+ LOGGER .log (Level .SEVERE , "Error occurred when closing the content " , e );
116
118
}
117
119
}
118
120
}
0 commit comments