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 import org.apache.hc.core5.http.io.support.ClassicResponseBuilder;
46
47
48
49
50
51
52 public class Response {
53
54 private final ClassicHttpResponse response;
55 private boolean consumed;
56
57 Response(final ClassicHttpResponse response) {
58 super();
59 this.response = response;
60 }
61
62 private void assertNotConsumed() {
63 if (this.consumed) {
64 throw new IllegalStateException("Response content has been already consumed");
65 }
66 }
67
68 private void dispose() throws IOException {
69 if (this.consumed) {
70 return;
71 }
72 try {
73 final HttpEntity entity = this.response.getEntity();
74 if (entity != null) {
75 final InputStream content = entity.getContent();
76 if (content != null) {
77 content.close();
78 }
79 }
80 } finally {
81 this.consumed = true;
82 this.response.close();
83 }
84 }
85
86
87
88
89 public void discardContent() {
90 try {
91 dispose();
92 } catch (final Exception ignore) {
93 }
94 }
95
96
97
98
99 public <T> T handleResponse(final HttpClientResponseHandler<T> handler) throws IOException {
100 assertNotConsumed();
101 try {
102 return handler.handleResponse(this.response);
103 } catch (final HttpException ex) {
104 throw new ClientProtocolException(ex);
105 } finally {
106 dispose();
107 }
108 }
109
110 public Content returnContent() throws IOException {
111 return handleResponse(new ContentResponseHandler());
112 }
113
114 public HttpResponse returnResponse() throws IOException {
115 assertNotConsumed();
116 try {
117 final HttpEntity entity = this.response.getEntity();
118 return ClassicResponseBuilder.copy(response)
119 .setEntity(entity != null ?
120 new ByteArrayEntity(
121 EntityUtils.toByteArray(entity),
122 ContentType.parse(entity.getContentType()))
123 : null)
124 .build();
125 } finally {
126 this.consumed = true;
127 this.response.close();
128 }
129 }
130
131 public void saveContent(final File file) throws IOException {
132 assertNotConsumed();
133 try {
134 final int status = response.getCode();
135 if (status >= HttpStatus.SC_REDIRECTION) {
136 throw new HttpResponseException(status, response.getReasonPhrase());
137 }
138 try (FileOutputStream out = new FileOutputStream(file)) {
139 final HttpEntity entity = this.response.getEntity();
140 if (entity != null) {
141 entity.writeTo(out);
142 }
143 }
144 } finally {
145 this.consumed = true;
146 this.response.close();
147 }
148 }
149
150 }