From 40d66b7577eb4a5eb8f60fbd2c698d14e43d01de Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 30 Mar 2023 06:55:45 +0200 Subject: [PATCH] Take into account quarkus.resteasy.gzip.max-input in REST Client Fix https://github.com/quarkusio/quarkus/issues/12941 --- .../ClientUsingGzipCompressionTest.java | 62 +++++++++++++++++++ .../client-using-gzip-application.properties | 4 ++ .../ResteasyConfigurationMPConfig.java | 19 +++++- 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/compression/ClientUsingGzipCompressionTest.java create mode 100644 extensions/resteasy-classic/rest-client/deployment/src/test/resources/client-using-gzip-application.properties diff --git a/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/compression/ClientUsingGzipCompressionTest.java b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/compression/ClientUsingGzipCompressionTest.java new file mode 100644 index 0000000000000..12040820bf320 --- /dev/null +++ b/extensions/resteasy-classic/rest-client/deployment/src/test/java/io/quarkus/restclient/compression/ClientUsingGzipCompressionTest.java @@ -0,0 +1,62 @@ +package io.quarkus.restclient.compression; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.WebApplicationException; + +import org.apache.http.HttpStatus; +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.resteasy.annotations.GZIP; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; + +public class ClientUsingGzipCompressionTest { + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar + .addClasses(MyResource.class, MyClient.class)) + .withConfigurationResource("client-using-gzip-application.properties"); + + @RestClient + MyClient client; + + /** + * Test that covers the property `quarkus.resteasy.gzip.max-input`. + * Larger payloads than 10 bytes should return HTTP Request Too Large. + */ + @Test + public void testGzipMaxInput() { + WebApplicationException ex = Assertions.assertThrows(WebApplicationException.class, () -> client.gzip(new byte[11])); + assertEquals(HttpStatus.SC_REQUEST_TOO_LONG, ex.getResponse().getStatus()); + + // verify shorter message works fine + Assertions.assertEquals("Worked!", client.gzip(new byte[10])); + } + + @Path("/client") + @RegisterRestClient(configKey = "my-client") + public interface MyClient { + + @POST + @Path("/gzip") + String gzip(@GZIP byte[] message); + + } + + @Path("/client") + public class MyResource { + + @POST + @Path("/gzip") + public String gzip(@GZIP String message) { + return "Worked!"; + } + + } +} diff --git a/extensions/resteasy-classic/rest-client/deployment/src/test/resources/client-using-gzip-application.properties b/extensions/resteasy-classic/rest-client/deployment/src/test/resources/client-using-gzip-application.properties new file mode 100644 index 0000000000000..fd08bed841bd3 --- /dev/null +++ b/extensions/resteasy-classic/rest-client/deployment/src/test/resources/client-using-gzip-application.properties @@ -0,0 +1,4 @@ +quarkus.resteasy.gzip.enabled=true +quarkus.resteasy.gzip.max-input=10 + +quarkus.rest-client.my-client.url=http://localhost:${quarkus.http.test-port:8081} \ No newline at end of file diff --git a/extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/ResteasyConfigurationMPConfig.java b/extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/ResteasyConfigurationMPConfig.java index 1fe30ead48ec9..d11e7146f7093 100644 --- a/extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/ResteasyConfigurationMPConfig.java +++ b/extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/ResteasyConfigurationMPConfig.java @@ -2,10 +2,13 @@ import java.util.Collections; import java.util.HashSet; +import java.util.Map; +import java.util.Optional; import java.util.Set; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; +import org.jboss.resteasy.plugins.server.servlet.ResteasyContextParameters; import org.jboss.resteasy.spi.ResteasyConfiguration; /** @@ -13,12 +16,25 @@ * */ public class ResteasyConfigurationMPConfig implements ResteasyConfiguration { + + private static final Map RESTEASY_QUARKUS_MAPPING_PARAMS = Map.of( + ResteasyContextParameters.RESTEASY_GZIP_MAX_INPUT, "quarkus.resteasy.gzip.max-input"); + @Override public String getParameter(String name) { Config config = ConfigProvider.getConfig(); if (config == null) return null; - return config.getOptionalValue(name, String.class).orElse(null); + Optional value = Optional.empty(); + String mappedProperty = RESTEASY_QUARKUS_MAPPING_PARAMS.get(name); + if (mappedProperty != null) { + // try to use quarkus parameter + value = config.getOptionalValue(mappedProperty, String.class); + } + + // if the parameter name is not mapped or there is no value, use the parameter name as provided + return value.or(() -> config.getOptionalValue(name, String.class)) + .orElse(null); } @Override @@ -29,6 +45,7 @@ public Set getParameterNames() { HashSet set = new HashSet<>(); for (String name : config.getPropertyNames()) set.add(name); + set.addAll(RESTEASY_QUARKUS_MAPPING_PARAMS.keySet()); return set; }