-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32684 from Sgitario/26981
Support decoding of messages GZIP-compressed in REST Client Reactive
- Loading branch information
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
72 changes: 72 additions & 0 deletions
72
...est/java/io/quarkus/rest/client/reactive/jackson/test/ClientUsingGzipCompressionTest.java
This file contains 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,72 @@ | ||
package io.quarkus.rest.client.reactive.jackson.test; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
|
||
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.http.Compressed; | ||
|
||
public class ClientUsingGzipCompressionTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyResource.class, Message.class, MyClient.class)) | ||
.withConfigurationResource("client-using-gzip-application.properties"); | ||
|
||
@RestClient | ||
MyClient client; | ||
|
||
@Test | ||
public void testClientSupportCompressedMessagesWithGzip() { | ||
Message actual = client.receiveCompressed(); | ||
Assertions.assertEquals(1, actual.id); | ||
} | ||
|
||
@Test | ||
public void testClientStillWorksWhenMessageIsUncompressed() { | ||
Message actual = client.receiveUncompressed(); | ||
Assertions.assertEquals(1, actual.id); | ||
} | ||
|
||
@Path("/client") | ||
@RegisterRestClient(configKey = "my-client") | ||
public interface MyClient { | ||
|
||
// This header is used to reproduce the issue: it will force the server to produce the payload with gzip compression | ||
@ClientHeaderParam(name = "Accept-Encoding", value = "gzip") | ||
@GET | ||
@Path("/message") | ||
Message receiveCompressed(); | ||
|
||
@GET | ||
@Path("/message") | ||
Message receiveUncompressed(); | ||
|
||
} | ||
|
||
@Path("/client") | ||
public static class MyResource { | ||
|
||
@Compressed | ||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Path("/message") | ||
public String receive() { | ||
return "{\"id\": 1}"; | ||
} | ||
|
||
} | ||
|
||
public static class Message { | ||
public int id; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...t-reactive-jackson/deployment/src/test/resources/client-using-gzip-application.properties
This file contains 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,3 @@ | ||
quarkus.http.enable-compression=true | ||
|
||
quarkus.rest-client.my-client.url=http://localhost:${quarkus.http.test-port:8081} |
This file contains 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 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
41 changes: 41 additions & 0 deletions
41
...n/java/org/jboss/resteasy/reactive/client/interceptors/ClientGZIPDecodingInterceptor.java
This file contains 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,41 @@ | ||
package org.jboss.resteasy.reactive.client.interceptors; | ||
|
||
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_ENCODING; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.ext.ReaderInterceptor; | ||
import jakarta.ws.rs.ext.ReaderInterceptorContext; | ||
|
||
/** | ||
* Implementation based on {@see org.jboss.resteasy.plugins.interceptors.GZIPDecodingInterceptor}. | ||
*/ | ||
public class ClientGZIPDecodingInterceptor implements ReaderInterceptor { | ||
|
||
private static final String GZIP = "gzip"; | ||
|
||
@Override | ||
public Object aroundReadFrom(ReaderInterceptorContext context) | ||
throws IOException, WebApplicationException { | ||
Object encoding = context.getHeaders().getFirst(CONTENT_ENCODING); | ||
if (encoding != null && encoding.toString().equalsIgnoreCase(GZIP)) { | ||
InputStream old = context.getInputStream(); | ||
GZIPInputStream is = new GZIPInputStream(old); | ||
context.setInputStream(is); | ||
|
||
Object response; | ||
try { | ||
response = context.proceed(); | ||
} finally { | ||
context.setInputStream(old); | ||
} | ||
|
||
return response; | ||
} else { | ||
return context.proceed(); | ||
} | ||
} | ||
} |