This is an automated email from the ASF dual-hosted git repository. elsloo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git
commit 57158d6cc31995167f46f583941f65f01e090f1a Author: Jesse Rivas <jesse_ri...@comcast.com> AuthorDate: Wed Mar 7 14:51:36 2018 -0700 Added 'Accept-Encoding: gzip' headers to outgoing TR requests. Also decoded gzip encoded responses. --- .../traffic_router/core/util/Fetcher.java | 39 +++++++++++++--------- .../core/util/PeriodicResourceUpdater.java | 21 +++++++++++- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java index 65d77f7..6a6ded4 100644 --- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java +++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java @@ -27,6 +27,7 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; +import java.util.zip.GZIPInputStream; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -102,6 +103,7 @@ public class Fetcher { http.setInstanceFollowRedirects(false); http.setRequestMethod(method); http.setAllowUserInteraction(true); + http.addRequestProperty("Accept-Encoding", "gzip"); for (final String key : requestProps.keySet()) { http.addRequestProperty(key, requestProps.get(key)); @@ -143,14 +145,7 @@ public class Fetcher { } final StringBuilder sb = new StringBuilder(); - - try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { - String input; - - while ((input = in.readLine()) != null) { - sb.append(input); - } - } + createStringBuilderFromResponse(sb, connection); return sb.toString(); } finally { @@ -173,13 +168,7 @@ public class Fetcher { return status; } - try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { - String input; - - while ((input = in.readLine()) != null) { - stringBuilder.append(input); - } - } + createStringBuilderFromResponse(stringBuilder, connection); return status; } finally { @@ -210,4 +199,24 @@ public class Fetcher { result = 31 * result + (requestProps != null ? requestProps.hashCode() : 0); return result; } + + public void createStringBuilderFromResponse (final StringBuilder sb, final HttpURLConnection connection) throws IOException { + if ("gzip".equals(connection.getContentEncoding())) { + final GZIPInputStream zippedInputStream = new GZIPInputStream(connection.getInputStream()); + final BufferedReader r = new BufferedReader(new InputStreamReader(zippedInputStream)); + String input; + while((input = r.readLine()) != null) { + sb.append(input); + } + } + else { + try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String input; + + while ((input = in.readLine()) != null) { + sb.append(input); + } + } + } + } } diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java index ed1de75..9ce224f 100644 --- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java +++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java @@ -15,12 +15,14 @@ package com.comcast.cdn.traffic_control.traffic_router.core.util; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.StringReader; import java.net.URI; import java.net.URISyntaxException; @@ -29,6 +31,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import java.util.zip.GZIPInputStream; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; @@ -133,6 +136,7 @@ public class PeriodicResourceUpdater { if (!hasBeenLoaded || needsUpdating(existingDB)) { final Request request = getRequest(urls.nextUrl()); if (request != null) { + request.getHeaders().add("Accept-Encoding", "gzip"); asyncHttpClient.executeRequest(request, new UpdateHandler(request)); // AsyncHandlers are NOT thread safe; one instance per request return true; } @@ -256,7 +260,22 @@ public class PeriodicResourceUpdater { return code; } - updateDatabase(response.getResponseBody()); + final String responseBody; + if ("gzip".equals(response.getHeader("Content-Encoding"))) { + final StringBuilder stringBuilder = new StringBuilder(); + final GZIPInputStream zippedInputStream = new GZIPInputStream(response.getResponseBodyAsStream()); + final BufferedReader r = new BufferedReader(new InputStreamReader(zippedInputStream)); + String line; + while((line = r.readLine()) != null) { + stringBuilder.append(line); + } + responseBody = stringBuilder.toString(); + } + else { + responseBody = response.getResponseBody(); + } + + updateDatabase(responseBody); return code; } -- To stop receiving notification emails like this one, please contact els...@apache.org.