1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.fluent;
28
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33
34 import org.apache.hc.client5.http.ClientProtocolException;
35 import org.apache.hc.client5.http.HttpResponseException;
36 import org.apache.hc.core5.http.ClassicHttpResponse;
37 import org.apache.hc.core5.http.ContentType;
38 import org.apache.hc.core5.http.HttpEntity;
39 import org.apache.hc.core5.http.HttpException;
40 import org.apache.hc.core5.http.HttpResponse;
41 import org.apache.hc.core5.http.HttpStatus;
42 import org.apache.hc.core5.http.io.HttpClientResponseHandler;
43 import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
44 import org.apache.hc.core5.http.io.entity.EntityUtils;
45
46
47
48
49
50
51 public class Response {
52
53 private final ClassicHttpResponse response;
54 private boolean consumed;
55
56 Response(final ClassicHttpResponse response) {
57 super();
58 this.response = response;
59 }
60
61 private void assertNotConsumed() {
62 if (this.consumed) {
63 throw new IllegalStateException("Response content has been already consumed");
64 }
65 }
66
67 private void dispose() {
68 if (this.consumed) {
69 return;
70 }
71 try {
72 final HttpEntity entity = this.response.getEntity();
73 if (entity != null) {
74 final InputStream content = entity.getContent();
75 if (content != null) {
76 content.close();
77 }
78 }
79 } catch (final Exception ignore) {
80 } finally {
81 this.consumed = true;
82 }
83 }
84
85
86
87
88 public void discardContent() {
89 dispose();
90 }
91
92
93
94
95 public <T> T handleResponse(final HttpClientResponseHandler<T> handler) throws IOException {
96 assertNotConsumed();
97 try {
98 return handler.handleResponse(this.response);
99 } catch (final HttpException ex) {
100 throw new ClientProtocolException(ex);
101 } finally {
102 dispose();
103 }
104 }
105
106 public Content returnContent() throws IOException {
107 return handleResponse(new ContentResponseHandler());
108 }
109
110 public HttpResponse returnResponse() throws IOException {
111 assertNotConsumed();
112 try {
113 final HttpEntity entity = this.response.getEntity();
114 if (entity != null) {
115 final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
116 EntityUtils.toByteArray(entity), ContentType.parse(entity.getContentType()));
117 this.response.setEntity(byteArrayEntity);
118 }
119 return this.response;
120 } finally {
121 this.consumed = true;
122 }
123 }
124
125 public void saveContent(final File file) throws IOException {
126 assertNotConsumed();
127 final int status = response.getCode();
128 if (status >= HttpStatus.SC_REDIRECTION) {
129 throw new HttpResponseException(status, response.getReasonPhrase());
130 }
131 try (FileOutputStream out = new FileOutputStream(file)) {
132 final HttpEntity entity = this.response.getEntity();
133 if (entity != null) {
134 entity.writeTo(out);
135 }
136 } finally {
137 this.consumed = true;
138 }
139 }
140
141 }