Skip to content

Commit d51b590

Browse files
author
Karl Rieb
committed
Add methods to allow HttpRequestor subclasses to more easily intercept HTTP responses (e.g. for logging, analytics, etc).
1 parent ae0d005 commit d51b590

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/main/java/com/dropbox/core/http/OkHttp3Requestor.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,25 @@ public OkHttpClient getClient() {
9090
*/
9191
protected void configureRequest(Request.Builder request) { }
9292

93+
/**
94+
* Called before returning {@link Response} from a request.
95+
*
96+
* <p> This method should be used by subclasses to add any logging, analytics, or cleanup
97+
* necessary.
98+
*
99+
* <p> Do not consume the response body in this method.
100+
*
101+
* @param response OkHttp response
102+
*/
103+
protected void interceptResponse(okhttp3.Response response) { }
104+
93105
@Override
94106
public Response doGet(String url, Iterable<Header> headers) throws IOException {
95107
Request.Builder builder = new Request.Builder().get().url(url);
96108
toOkHttpHeaders(headers, builder);
97109
configureRequest(builder);
98110
okhttp3.Response response = client.newCall(builder.build()).execute();
111+
interceptResponse(response);
99112
Map<String, List<String>> responseHeaders = fromOkHttpHeaders(response.headers());
100113
return new Response(response.code(), response.body().byteStream(), responseHeaders);
101114
}
@@ -138,7 +151,7 @@ private static Map<String, List<String>> fromOkHttpHeaders(Headers headers) {
138151
* the {@link java.io.OutputStream} from an Okio {@link Buffer} that is connected to an OkHttp
139152
* {@link RequestBody}. Calling {@link #finish()} will execute the request with OkHttp
140153
*/
141-
private static class BufferUploader extends HttpRequestor.Uploader {
154+
private class BufferUploader extends HttpRequestor.Uploader {
142155
private final Call call;
143156
private final Buffer requestBuffer;
144157

@@ -161,6 +174,7 @@ public void abort() {
161174
@Override
162175
public Response finish() throws IOException {
163176
okhttp3.Response response = call.execute();
177+
interceptResponse(response);
164178
Map<String, List<String>> responseHeaders = fromOkHttpHeaders(response.headers());
165179
return new Response(response.code(), response.body().byteStream(), responseHeaders);
166180
}

src/main/java/com/dropbox/core/http/OkHttpRequestor.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,25 @@ public OkHttpClient getClient() {
9393
*/
9494
protected void configureRequest(Request.Builder request) { }
9595

96+
/**
97+
* Called before returning {@link Response} from a request.
98+
*
99+
* <p> This method should be used by subclasses to add any logging, analytics, or cleanup
100+
* necessary.
101+
*
102+
* <p> Do not consume the response body in this method.
103+
*
104+
* @param response OkHttp response
105+
*/
106+
protected void interceptResponse(com.squareup.okhttp.Response response) { }
107+
96108
@Override
97109
public Response doGet(String url, Iterable<Header> headers) throws IOException {
98110
Request.Builder builder = new Request.Builder().get().url(url);
99111
toOkHttpHeaders(headers, builder);
100112
configureRequest(builder);
101113
com.squareup.okhttp.Response response = client.newCall(builder.build()).execute();
114+
interceptResponse(response);
102115
Map<String, List<String>> responseHeaders = fromOkHttpHeaders(response.headers());
103116
return new Response(response.code(), response.body().byteStream(), responseHeaders);
104117
}
@@ -141,7 +154,7 @@ private static Map<String, List<String>> fromOkHttpHeaders(Headers headers) {
141154
* the {@link java.io.OutputStream} from an Okio {@link Buffer} that is connected to an OkHttp
142155
* {@link RequestBody}. Calling {@link #finish()} will execute the request with OkHttp
143156
*/
144-
private static class BufferUploader extends HttpRequestor.Uploader {
157+
private class BufferUploader extends HttpRequestor.Uploader {
145158
private final Call call;
146159
private final Buffer requestBuffer;
147160

@@ -164,6 +177,7 @@ public void abort() {
164177
@Override
165178
public Response finish() throws IOException {
166179
com.squareup.okhttp.Response response = call.execute();
180+
interceptResponse(response);
167181
Map<String, List<String>> responseHeaders = fromOkHttpHeaders(response.headers());
168182
return new Response(response.code(), response.body().byteStream(), responseHeaders);
169183
}

src/main/java/com/dropbox/core/http/StandardHttpRequestor.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ public StandardHttpRequestor(Config config) {
4545
this.config = config;
4646
}
4747

48-
private static Response toResponse(HttpURLConnection conn) throws IOException {
48+
private Response toResponse(HttpURLConnection conn) throws IOException {
4949
int responseCode = conn.getResponseCode();
5050
InputStream bodyStream;
5151
if (responseCode >= 400 || responseCode == -1) {
5252
bodyStream = conn.getErrorStream();
5353
} else {
5454
bodyStream = conn.getInputStream();
5555
}
56+
interceptResponse(conn);
5657
return new Response(responseCode, bodyStream, conn.getHeaderFields());
5758
}
5859

@@ -103,7 +104,26 @@ protected void configureConnection(HttpsURLConnection conn) throws IOException {
103104
*/
104105
protected void configure(HttpURLConnection conn) throws IOException { }
105106

106-
private static class Uploader extends HttpRequestor.Uploader {
107+
/**
108+
* Called before returning {@link Response} from a request.
109+
*
110+
* <p> This method should be used by subclasses to add any logging, analytics, or cleanup
111+
* necessary. Note that the connection response code and response streams will already be
112+
* fetched before calling this method. This means any {@link IOException} from reading the
113+
* response should already have occurred before this method is called.
114+
*
115+
* <p> Do not consume the response or error streams in this method.
116+
*
117+
* @param response OkHttp response
118+
*/
119+
protected void interceptResponse(HttpURLConnection conn) throws IOException { }
120+
121+
private static OutputStream getOutputStream(HttpURLConnection conn) throws IOException {
122+
conn.setDoOutput(true);
123+
return conn.getOutputStream();
124+
}
125+
126+
private class Uploader extends HttpRequestor.Uploader {
107127
private /*@Nullable*/ HttpURLConnection conn;
108128

109129
public Uploader(HttpURLConnection conn)
@@ -113,12 +133,6 @@ public Uploader(HttpURLConnection conn)
113133
this.conn = conn;
114134
}
115135

116-
private static OutputStream getOutputStream(HttpURLConnection conn)
117-
throws IOException {
118-
conn.setDoOutput(true);
119-
return conn.getOutputStream();
120-
}
121-
122136
@Override
123137
public void abort() {
124138
if (conn == null) {

0 commit comments

Comments
 (0)