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 final InputStream content = entity.getContent();
74 if (content != null) {
75 content.close();
76 }
77 } catch (final Exception ignore) {
78 } finally {
79 this.consumed = true;
80 }
81 }
82
83
84
85
86 public void discardContent() {
87 dispose();
88 }
89
90
91
92
93 public <T> T handleResponse(final HttpClientResponseHandler<T> handler) throws IOException {
94 assertNotConsumed();
95 try {
96 return handler.handleResponse(this.response);
97 } catch (final HttpException ex) {
98 throw new ClientProtocolException(ex);
99 } finally {
100 dispose();
101 }
102 }
103
104 public Content returnContent() throws IOException {
105 return handleResponse(new ContentResponseHandler());
106 }
107
108 public HttpResponse returnResponse() throws IOException {
109 assertNotConsumed();
110 try {
111 final HttpEntity entity = this.response.getEntity();
112 if (entity != null) {
113 final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
114 EntityUtils.toByteArray(entity), ContentType.parse(entity.getContentType()));
115 this.response.setEntity(byteArrayEntity);
116 }
117 return this.response;
118 } finally {
119 this.consumed = true;
120 }
121 }
122
123 public void saveContent(final File file) throws IOException {
124 assertNotConsumed();
125 final int status = response.getCode();
126 if (status >= HttpStatus.SC_REDIRECTION) {
127 throw new HttpResponseException(status, response.getReasonPhrase());
128 }
129 try (FileOutputStream out = new FileOutputStream(file)) {
130 final HttpEntity entity = this.response.getEntity();
131 if (entity != null) {
132 entity.writeTo(out);
133 }
134 } finally {
135 this.consumed = true;
136
137 }
138 }
139
140 }