|
| 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 | + * As of Java Client 6.3.0, this can now be accomplished via the {@code DatabaseClientFactory} class and |
| 27 | + * {@code RemoveAcceptEncodingConfigurator}. |
| 28 | + */ |
| 29 | + public static void removeAcceptEncodingGzipHeader() { |
| 30 | + DatabaseClientFactory.addConfigurator(new OkHttpClientConfigurator() { |
| 31 | + @Override |
| 32 | + public void configure(OkHttpClient.Builder builder) { |
| 33 | + builder.addNetworkInterceptor(chain -> { |
| 34 | + Request newRequest = chain.request().newBuilder().removeHeader("Accept-Encoding").build(); |
| 35 | + return chain.proceed(newRequest); |
| 36 | + }); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | +} |
0 commit comments