Skip to content

Commit 92951c3

Browse files
committed
Added example of removing the Accept-Encoding header
This is for customers that do not want responses to be gzip-encoded.
1 parent e05f32f commit 92951c3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

examples/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ dependencies {
1212
}
1313
implementation project(':marklogic-client-api')
1414

15+
// Forcing usage of 3.4.0 instead of 3.2.0 to address vulnerability - https://security.snyk.io/vuln/SNYK-JAVA-COMSQUAREUPOKIO-5820002
16+
implementation 'com.squareup.okio:okio:3.4.0'
17+
1518
// The 'api' configuration is used so that the test configuration in marklogic-client-api doesn't have to declare
1619
// all of these dependencies. This library project won't otherwise be depended on by anything else as it's not
1720
// setup for publishing.
18-
api 'com.squareup.okhttp3:okhttp:4.10.0'
21+
api 'com.squareup.okhttp3:okhttp:4.11.0'
1922
api 'io.github.rburgst:okhttp-digest:2.7'
2023
api 'org.slf4j:slf4j-api:1.7.36'
2124
api 'com.fasterxml.jackson.core:jackson-databind:2.14.3'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.marklogic.client.example.cookbook;
2+
3+
import com.marklogic.client.DatabaseClientFactory;
4+
import com.marklogic.client.extra.okhttpclient.OkHttpClientConfigurator;
5+
import okhttp3.OkHttpClient;
6+
import okhttp3.Request;
7+
8+
/**
9+
* Provides examples of configuring the underlying OkHttp client for various use cases.
10+
*/
11+
public class ConfigureOkHttp {
12+
13+
/**
14+
* OkHttp 4.x includes a header of "Accept-Encoding=gzip" by default. The following shows how to use an OkHttp
15+
* interceptor to remove this header from every request. By doing this via a configurator passed to
16+
* {@code DatabaseClientFactory}, every {@code DatabaseClient} created via the factory will inherit this
17+
* interceptor.
18+
*
19+
* Note that while the Accept-Encoding header can contain multiple values - see
20+
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding for examples - the MarkLogic Java
21+
* Client does not set this header and OkHttp only sets it to "gzip" for every request. Thus, removing the
22+
* header should not result in losing any other values besides "gzip" for the header. You are free to
23+
* customize this as you wish though; this is primarily intended as an example for how to customize OkHttp
24+
* when using the MarkLogic Java Client.
25+
*/
26+
public static void removeAcceptEncodingGzipHeader() {
27+
DatabaseClientFactory.addConfigurator(new OkHttpClientConfigurator() {
28+
@Override
29+
public void configure(OkHttpClient.Builder builder) {
30+
builder.addNetworkInterceptor(chain -> {
31+
Request newRequest = chain.request().newBuilder().removeHeader("Accept-Encoding").build();
32+
return chain.proceed(newRequest);
33+
});
34+
}
35+
});
36+
}
37+
}

0 commit comments

Comments
 (0)