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

Properly convert MemorySize to RESTEasy configuration #39828

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package io.quarkus.resteasy.runtime.standalone;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

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;

import io.quarkus.runtime.configuration.MemorySize;

/**
* Some resteasy components use this class for configuration. This bridges MP Config to 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");
private static final Map<String, Function<Config, Optional<String>>> RESTEASY_QUARKUS_MAPPING_PARAMS = Map.of(
ResteasyContextParameters.RESTEASY_GZIP_MAX_INPUT, ResteasyConfigurationMPConfig::getGzipMaxInput);

@Override
public String getParameter(String name) {
Config config = ConfigProvider.getConfig();
if (config == null)
if (config == null) {
return 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);
Function<Config, Optional<String>> mappingFunction = RESTEASY_QUARKUS_MAPPING_PARAMS.get(name);
if (mappingFunction != null) {
// try to use Quarkus configuration
value = mappingFunction.apply(config);
}

// if the parameter name is not mapped or there is no value, use the parameter name as provided
Expand All @@ -40,12 +44,15 @@ public String getParameter(String name) {
@Override
public Set<String> getParameterNames() {
Config config = ConfigProvider.getConfig();
if (config == null)
return Collections.EMPTY_SET;
if (config == null) {
return Set.of();
}
HashSet<String> set = new HashSet<>();
for (String name : config.getPropertyNames())
for (String name : config.getPropertyNames()) {
set.add(name);
}
set.addAll(RESTEASY_QUARKUS_MAPPING_PARAMS.keySet());

return set;
}

Expand All @@ -58,4 +65,14 @@ public String getInitParameter(String name) {
public Set<String> getInitParameterNames() {
return getParameterNames();
}

private static Optional<String> getGzipMaxInput(Config config) {
Optional<MemorySize> rawValue = config.getOptionalValue("quarkus.resteasy.gzip.max-input", MemorySize.class);

if (rawValue.isEmpty()) {
return Optional.empty();
}

return Optional.of(Long.toString(rawValue.get().asLongValue()));
}
}
Loading