-
Notifications
You must be signed in to change notification settings - Fork 73
DEVEXP-562 Can now prevent Accept-Encoding=gzip header being used #1593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...c/main/java/com/marklogic/client/extra/okhttpclient/RemoveAcceptEncodingConfigurator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.marklogic.client.extra.okhttpclient; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
|
||
/** | ||
* Can be used with {@code DatabaseClientFactory.addConfigurator} to remove the "Accept-Encoding=gzip" request header | ||
* that the underlying OkHttp library adds by default. This is useful in a scenario where many small HTTP responses | ||
* are expected to be returned by MarkLogic, and thus the costs of gzipping the responses may outweigh the benefits. | ||
* | ||
* @since 6.3.0 | ||
*/ | ||
public class RemoveAcceptEncodingConfigurator implements OkHttpClientConfigurator { | ||
|
||
@Override | ||
public void configure(OkHttpClient.Builder builder) { | ||
builder.addNetworkInterceptor(chain -> { | ||
Request newRequest = chain.request().newBuilder().removeHeader("Accept-Encoding").build(); | ||
return chain.proceed(newRequest); | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...client-api/src/test/java/com/marklogic/client/test/extra/DisableGzippedResponsesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.marklogic.client.test.extra; | ||
|
||
import com.marklogic.client.DatabaseClientFactory; | ||
import com.marklogic.client.extra.okhttpclient.OkHttpClientConfigurator; | ||
import com.marklogic.client.test.Common; | ||
import com.marklogic.client.test.junit5.RequiresML11; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
@ExtendWith(RequiresML11.class) | ||
public class DisableGzippedResponsesTest { | ||
|
||
@AfterEach | ||
void afterEach() { | ||
DatabaseClientFactory.removeConfigurators(); | ||
} | ||
|
||
@Test | ||
void test() { | ||
// Add a DatabaseClientFactory configurator that uses an OkHttp interceptor to capture the value of the | ||
// Content-Encoding response header. That is the easiest way to verify whether the Accept-Encoding:gzip request | ||
// header is being sent or not. | ||
TestInterceptor testInterceptor = new TestInterceptor(); | ||
DatabaseClientFactory.addConfigurator((OkHttpClientConfigurator) builder -> builder.addNetworkInterceptor(testInterceptor)); | ||
|
||
|
||
final String testUri = "/optic/test/musician1.json"; | ||
|
||
Common.newClient().newJSONDocumentManager().read(testUri); | ||
assertEquals("gzip", testInterceptor.contentEncoding, "MarkLogic 11 now supports the Accept-Encoding:gzip " + | ||
"header. The OkHttp library used by the Java Client includes this request header by default. So we " + | ||
"expect for responses from the REST API to be gzipped, which is indicated by the Content-Encoding " + | ||
"response header having a value of 'gzip'."); | ||
|
||
Common.newClientBuilder().withGzippedResponsesDisabled().build() | ||
.newJSONDocumentManager().read(testUri); | ||
assertNull(testInterceptor.contentEncoding, "When a DatabaseClient is constructed with gzipped responses " + | ||
"disabled, the Accept-Encoding header added automatically by OkHttp should be removed before the request " + | ||
"is sent to MarkLogic. This prevents MarkLogic from gzipping the response, which is indicated by the " + | ||
"HTTP response not having a 'Content-Encoding' header."); | ||
} | ||
|
||
/** | ||
* Used to capture the value of the Content-Encoding response header. | ||
*/ | ||
private static class TestInterceptor implements Interceptor { | ||
|
||
String contentEncoding; | ||
|
||
@NotNull | ||
@Override | ||
public Response intercept(@NotNull Chain chain) throws IOException { | ||
Response response = chain.proceed(chain.request()); | ||
contentEncoding = response.header("Content-Encoding"); | ||
return response; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.