Skip to content
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

Take into account quarkus.resteasy.gzip.max-input in classic REST Client #32232

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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!";
}

}
}
Original file line number Diff line number Diff line change
@@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@

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;

/**
* Some resteasy components use this class for configuration. This bridges MP Config to ResteasyConfiguration
*
*/
public class ResteasyConfigurationMPConfig implements ResteasyConfiguration {

private static final Map<String, String> 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<String> 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
Expand All @@ -29,6 +45,7 @@ public Set<String> getParameterNames() {
HashSet<String> set = new HashSet<>();
for (String name : config.getPropertyNames())
set.add(name);
set.addAll(RESTEASY_QUARKUS_MAPPING_PARAMS.keySet());
return set;
}

Expand Down