Skip to content

Commit

Permalink
refactor httputil error stream handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Dec 16, 2017
1 parent f9cb281 commit aec394a
Showing 1 changed file with 29 additions and 17 deletions.
Expand Up @@ -8,6 +8,7 @@
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -93,20 +94,33 @@ private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
conn.connect();

statusCode = conn.getResponseCode();
String response;

try {
isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
} catch (Exception e) {
// ignore
}
try {
esr = new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8);
} catch (Exception e) {
// ignore
response = CharStreams.toString(isr);
} catch (IOException ex) {
/**
* according to https://docs.oracle.com/javase/7/docs/technotes/guides/net/http-keepalive.html,
* we should clean up the connection by reading the response body so that the connection
* could be reused.
*/
InputStream errorStream = conn.getErrorStream();

if (errorStream != null) {
esr = new InputStreamReader(errorStream, StandardCharsets.UTF_8);
try {
CharStreams.toString(esr);
} catch (IOException ioe) {
//ignore
}
}

throw ex;
}

if (statusCode == 200) {
String content = CharStreams.toString(isr);
return new HttpResponse<>(statusCode, serializeFunction.apply(content));
return new HttpResponse<>(statusCode, serializeFunction.apply(response));
}

if (statusCode == 304) {
Expand All @@ -117,20 +131,18 @@ private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
} finally {
if (isr != null) {
try {
CharStreams.toString(isr);
isr.close();
} catch (IOException e) {
} catch (IOException ex) {
// ignore
}
}

if (esr != null) {
try {
CharStreams.toString(esr);
esr.close();
} catch (Exception e) {
// ignore
}
try {
esr.close();
} catch (IOException ex) {
// ignore
}
}
}

Expand Down

0 comments on commit aec394a

Please sign in to comment.