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.http.client.fluent;
28
29 import java.io.Closeable;
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34
35 import org.apache.http.HttpEntity;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.StatusLine;
38 import org.apache.http.client.ClientProtocolException;
39 import org.apache.http.client.HttpResponseException;
40 import org.apache.http.client.ResponseHandler;
41 import org.apache.http.entity.ByteArrayEntity;
42 import org.apache.http.entity.ContentType;
43 import org.apache.http.util.EntityUtils;
44
45 public class Response {
46
47 private final HttpResponse response;
48 private boolean consumed;
49
50 Response(final HttpResponse response) {
51 super();
52 this.response = response;
53 }
54
55 private void assertNotConsumed() {
56 if (this.consumed) {
57 throw new IllegalStateException("Response content has been already consumed");
58 }
59 }
60
61 private void dispose() {
62 if (this.consumed) {
63 return;
64 }
65 try {
66 final HttpEntity entity = this.response.getEntity();
67 final InputStream content = entity.getContent();
68 if (content != null) {
69 content.close();
70 }
71 } catch (final Exception ignore) {
72 } finally {
73 this.consumed = true;
74 }
75 }
76
77
78
79
80 public void discardContent() {
81 dispose();
82 }
83
84
85
86
87 public <T> T handleResponse(
88 final ResponseHandler<T> handler) throws ClientProtocolException, IOException {
89 assertNotConsumed();
90 try {
91 return handler.handleResponse(this.response);
92 } finally {
93 dispose();
94 }
95 }
96
97 public Content returnContent() throws ClientProtocolException, IOException {
98 return handleResponse(new ContentResponseHandler());
99 }
100
101 public HttpResponse returnResponse() throws IOException {
102 assertNotConsumed();
103 try {
104 final HttpEntity entity = this.response.getEntity();
105 if (entity != null) {
106 final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
107 EntityUtils.toByteArray(entity));
108 final ContentType contentType = ContentType.getOrDefault(entity);
109 byteArrayEntity.setContentType(contentType.toString());
110 this.response.setEntity(byteArrayEntity);
111 }
112 return this.response;
113 } finally {
114 this.consumed = true;
115 }
116 }
117
118 public void saveContent(final File file) throws IOException {
119 assertNotConsumed();
120 try {
121 final StatusLine statusLine = response.getStatusLine();
122 if (statusLine.getStatusCode() >= 300) {
123 throw new HttpResponseException(statusLine.getStatusCode(),
124 statusLine.getReasonPhrase());
125 }
126 final FileOutputStream out = new FileOutputStream(file);
127 try {
128 final HttpEntity entity = this.response.getEntity();
129 if (entity != null) {
130 entity.writeTo(out);
131 }
132 } finally {
133 out.close();
134 }
135 } finally {
136 this.consumed = true;
137 if (this.response instanceof Closeable) {
138 ((Closeable) this.response).close();
139 }
140 }
141 }
142
143 }