Skip to content

Commit 25d24be

Browse files
committed
add input stream wrapper
1 parent 45f8142 commit 25d24be

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,23 @@ final class Apache5HttpResponse extends LowLevelHttpResponse {
3131
private final HttpUriRequestBase request;
3232
private final ClassicHttpResponse response;
3333
private final Header[] allHeaders;
34+
private final HttpEntity entity;
35+
private final Apache5ResponseContent content;
3436

3537
Apache5HttpResponse(HttpUriRequestBase request, ClassicHttpResponse response) {
3638
this.request = request;
3739
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;
3951
}
4052

4153
@Override
@@ -45,28 +57,21 @@ public int getStatusCode() {
4557

4658
@Override
4759
public InputStream getContent() throws IOException {
48-
HttpEntity entity = response.getEntity();
49-
return entity == null ? null : entity.getContent();
60+
return content;
5061
}
5162

5263
@Override
5364
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;
5966
}
6067

6168
@Override
6269
public long getContentLength() {
63-
HttpEntity entity = response.getEntity();
6470
return entity == null ? -1 : entity.getContentLength();
6571
}
6672

6773
@Override
6874
public String getContentType() {
69-
HttpEntity entity = response.getEntity();
7075
return entity == null ? null : entity.getContentType();
7176
}
7277

@@ -102,17 +107,14 @@ public String getHeaderValue(int index) {
102107
/** Aborts execution of the request. */
103108
@Override
104109
public void disconnect() {
105-
close();
106-
}
107-
108-
public void close() {
109110
request.abort();
110111
try {
111-
response.close();
112+
// this call also close the response
113+
content.close();
112114
} catch (IOException e) {
113115
// the close() method contract won't allow us to declare a thrown exception. Here we just log
114116
// 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);
116118
}
117119
}
118120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.google.api.client.http.apache.v5;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import org.apache.hc.core5.http.ClassicHttpResponse;
6+
7+
public class Apache5ResponseContent extends InputStream {
8+
private final ClassicHttpResponse response;
9+
private final InputStream wrappedStream;
10+
11+
public Apache5ResponseContent(InputStream wrappedStream, ClassicHttpResponse response) {
12+
this.response = response;
13+
this.wrappedStream = wrappedStream;
14+
}
15+
16+
@Override
17+
public int read() throws IOException {
18+
return wrappedStream.read();
19+
}
20+
21+
@Override
22+
public void reset() throws IOException {
23+
wrappedStream.reset();
24+
}
25+
26+
@Override
27+
public void mark(int readLimit) {
28+
wrappedStream.mark(readLimit);
29+
}
30+
31+
@Override
32+
public void close() throws IOException {
33+
wrappedStream.close();
34+
response.close();
35+
}
36+
}

google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void testRequest(HttpTransport transport) throws IOException {
9292
System.out.println("Project display name: " + project.getDisplayName());
9393

9494
// temp test to confirm re-usage of client works with several consecutive requests
95-
projects.get("projects/" + PROJECT_ID).execute();
95+
Project p = projects.get("projects/" + PROJECT_ID).execute();
9696
projects.get("projects/" + PROJECT_ID).execute();
9797
projects.get("projects/" + PROJECT_ID).execute();
9898
projects.get("projects/" + PROJECT_ID).execute();

0 commit comments

Comments
 (0)