Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Unterrainer committed Jun 28, 2021
2 parents 62fc305 + f9d5cfb commit 20951bb
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<modelVersion>4.0.0</modelVersion>
<artifactId>rest-client</artifactId>
<version>0.0.9</version>
<version>0.0.10</version>
<name>RestClient</name>
<packaging>jar</packaging>

Expand All @@ -33,5 +33,10 @@
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.17.2</version>
</dependency>
</dependencies>
</project>
13 changes: 13 additions & 0 deletions src/main/java/info/unterrainer/commons/restclient/BaseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ protected enum Retry {
protected Map<String, String> headers = new HashMap<>();
protected Map<String, String> parameters = new HashMap<>();
protected Retry retry = Retry.ONCE;
protected boolean isGZipped;

/**
* Adds the header {@code "Content-Encoding": "gzip"} which triggers
* GZIP-compression within our RestClient.
*
* @return a {@link BaseBuilder} to provide a fluent interface.
*/
@SuppressWarnings("unchecked")
public R gzip() {
addHeader("Content-Encoding", "gzip");
return (R) this;
}

/**
* Add a string to an URL.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package info.unterrainer.commons.restclient;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.GzipSink;
import okio.GzipSource;
import okio.Okio;

public class GzipInterceptor implements Interceptor {
@Override
public Response intercept(final Interceptor.Chain chain) throws IOException {

Request originalRequest = chain.request();

if (acceptGzipAlreadySetAndNoContentToZip(originalRequest))
return chain.proceed(originalRequest);

Builder newRequestBuilder = originalRequest.newBuilder().addHeader("Accept-Encoding", "gzip");
if (contentToZip(originalRequest))
newRequestBuilder.method(originalRequest.method(), gzip(originalRequest.body()));

// Make the call.
Response response = chain.proceed(newRequestBuilder.build());

if (isGzipped(response))
return unzip(response);
else
return response;
}

private boolean acceptGzipAlreadySetAndNoContentToZip(final Request originalRequest) {
return originalRequest.header("Accept-Encoding") == "gzip"
&& (originalRequest.body() == null || originalRequest.header("Content-Encoding") != "gzip");
}

private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override
public MediaType contentType() {
return body.contentType();
}

@Override
public long contentLength() {
return -1; // We don't know the compressed length in advance!
}

@Override
public void writeTo(final BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}

private Response unzip(final Response response) throws IOException {

if (response.body() == null)
return response;

GzipSource gzipSource = new GzipSource(response.body().source());
String bodyString = Okio.buffer(gzipSource).readUtf8();

ResponseBody responseBody = ResponseBody.create(response.body().contentType(), bodyString);

Headers strippedHeaders = response.headers()
.newBuilder()
.removeAll("Content-Encoding")
.removeAll("Content-Length")
.build();
return response.newBuilder().headers(strippedHeaders).body(responseBody).message(response.message()).build();

}

private Boolean isGzipped(final Response response) {
return response.header("Content-Encoding") != null && response.header("Content-Encoding").equals("gzip");
}

private boolean contentToZip(final Request originalRequest) {
return originalRequest.body() != null && originalRequest.header("Content-Encoding") == "gzip";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package info.unterrainer.commons.restclient;

public class Information {
public static final String name = "REST-Client";
public static final String buildTime = "2021-06-28T07:57:53Z";
public static final String pomVersion = "0.0.10";
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public RestClient(final JsonMapper jsonMapper, final String userName, final Stri
.connectTimeout(connectTimeoutInMillis, TimeUnit.MILLISECONDS)
.readTimeout(readTimeoutInMillis, TimeUnit.MILLISECONDS)
.writeTimeout(writeTimeoutInMillis, TimeUnit.MILLISECONDS)
.addInterceptor(new GzipInterceptor())
.followRedirects(true);
if (userName != null || password != null)
c.authenticator((route, response) -> {
Expand Down

0 comments on commit 20951bb

Please sign in to comment.