-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Adds gzip handler to JDK http client based on header #35225
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,8 @@ public class JdkClientHttpRequestFactory implements ClientHttpRequestFactory { | |
|
||
private @Nullable Duration readTimeout; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Create |
||
|
||
private boolean compressionEnabled; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Optional |
||
|
||
/** | ||
* Create a new instance of the {@code JdkClientHttpRequestFactory} | ||
|
@@ -96,10 +98,18 @@ public void setReadTimeout(Duration readTimeout) { | |
this.readTimeout = readTimeout; | ||
} | ||
|
||
/** | ||
* Sets custom {@link BodyHandler} that can handle gzip encoded {@link HttpClient}'s response. | ||
* @param compressionEnabled to enable compression by default for all {@link HttpClient}'s requests. | ||
*/ | ||
public void setCompressionEnabled(boolean compressionEnabled) { | ||
this.compressionEnabled = compressionEnabled; | ||
} | ||
|
||
|
||
@Override | ||
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { | ||
return new JdkClientHttpRequest(this.httpClient, uri, httpMethod, this.executor, this.readTimeout); | ||
return new JdkClientHttpRequest(this.httpClient, uri, httpMethod, this.executor, this.readTimeout, this.compressionEnabled); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: With above suggestion, inject |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,14 @@ | |
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.zip.DeflaterOutputStream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
|
@@ -106,6 +112,26 @@ else if(request.getTarget().startsWith("/header/")) { | |
String headerName = request.getTarget().replace("/header/",""); | ||
return new MockResponse.Builder().body(headerName + ":" + request.getHeaders().get(headerName)).code(200).build(); | ||
} | ||
else if(request.getTarget().startsWith("/compress/")) { | ||
String encoding = request.getTarget().replace("/compress/",""); | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
if (encoding.equals("gzip")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use improved switch? |
||
try(GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) { | ||
gzipOutputStream.write("Test Payload".getBytes()); | ||
gzipOutputStream.flush(); | ||
} | ||
} | ||
else if(encoding.equals("deflate")) { | ||
try(DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream)) { | ||
deflaterOutputStream.write("Test Payload".getBytes()); | ||
deflaterOutputStream.flush(); | ||
} | ||
} else { | ||
byteArrayOutputStream.write("Test Payload".getBytes()); | ||
} | ||
return new MockResponse.Builder().body(byteArrayOutputStream.toString(StandardCharsets.ISO_8859_1)) | ||
.code(200).setHeader(HttpHeaders.CONTENT_ENCODING, encoding).build(); | ||
} | ||
return new MockResponse.Builder().code(404).build(); | ||
} | ||
catch (Throwable ex) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this Handler a parameter ? This will make Workarounds easier