Skip to content

Commit

Permalink
Adjust REST Client Reactive config
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Mar 11, 2024
1 parent e75d910 commit 849761c
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public static Optional<String> findConfiguredScope(Config config, ClassInfo rest
String.format(QUARKUS_REST_SCOPE_FORMAT, restClientInterface.simpleName()),
String.class);
}
if (scopeConfig.isEmpty()) { // "global" rest-config property as a fallback
scopeConfig = config.getOptionalValue(GLOBAL_REST_SCOPE_FORMAT, String.class);
}
return scopeConfig;
}

public static Optional<String> getDefaultScope(Config config) {
return config.getOptionalValue(GLOBAL_REST_SCOPE_FORMAT, String.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,24 @@ public class RestClientConfig {

/**
* Proxy username.
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> proxyUser;

/**
* Proxy password.
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> proxyPassword;

/**
* Hosts to access without proxy
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> nonProxyHosts;
Expand Down Expand Up @@ -207,16 +207,16 @@ public class RestClientConfig {

/**
* The maximum number of redirection a request can follow.
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<Integer> maxRedirects;

/**
* The HTTP headers that should be applied to all requests of the rest client.
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Map<String, String> headers;
Expand All @@ -225,24 +225,24 @@ public class RestClientConfig {
* Set to true to share the HTTP client between REST clients.
* There can be multiple shared clients distinguished by <em>name</em>, when no specific name is set,
* the name <code>__vertx.DEFAULT</code> is used.
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<Boolean> shared;

/**
* Set the HTTP client name, used when the client is shared, otherwise ignored.
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> name;

/**
* Configure the HTTP user-agent header to use.
*
* This property is applicable to reactive REST clients only.
* <p>
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> userAgent;
Expand All @@ -256,7 +256,7 @@ public class RestClientConfig {
/**
* The max HTTP chunk size (8096 bytes by default).
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
@ConfigDocDefault("8K")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public class RestClientFallbackConfigSourceInterceptor extends FallbackConfigSou
GLOBAL_PROPERTIES = new HashMap<>();
GLOBAL_PROPERTIES.put("quarkus.rest-client.multipart-post-encoder-mode",
"quarkus.rest.client.multipart-post-encoder-mode");
GLOBAL_PROPERTIES.put("quarkus.rest-client.disable-smart-produces",
"quarkus.rest-client-reactive.disable-smart-produces");

GLOBAL_PROPERTIES_INVERSE = inverseMap(GLOBAL_PROPERTIES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ public class RestClientsConfig {
@Deprecated
private final Map<String, RestClientConfig> configs = new ConcurrentHashMap<>();

/**
* By default, REST Client Reactive uses text/plain content type for String values
* and application/json for everything else.
* <p>
* MicroProfile Rest Client spec requires the implementations to always default to application/json.
* This build item disables the "smart" behavior of RESTEasy Reactive to comply to the spec.
* <p>
* This property is applicable to reactive REST clients only.
*/
@ConfigItem(defaultValue = "false")
public Optional<Boolean> disableSmartProduces;

/**
* Mode in which the form data are encoded. Possible values are `HTML5`, `RFC1738` and `RFC3986`.
* The modes are described in the
Expand All @@ -58,7 +46,7 @@ public class RestClientsConfig {
* <p>
* By default, Rest Client Reactive uses RFC1738.
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> multipartPostEncoderMode;
Expand All @@ -77,7 +65,7 @@ public class RestClientsConfig {
* <p>
* Can be overwritten by client-specific settings.
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> proxyUser;
Expand All @@ -87,7 +75,7 @@ public class RestClientsConfig {
* <p>
* Can be overwritten by client-specific settings.
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> proxyPassword;
Expand All @@ -98,7 +86,7 @@ public class RestClientsConfig {
* <p>
* Can be overwritten by client-specific settings.
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> nonProxyHosts;
Expand Down Expand Up @@ -127,7 +115,7 @@ public class RestClientsConfig {
* If true, the REST clients will not provide additional contextual information (like REST client class and method
* names) when exception occurs during a client invocation.
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem(defaultValue = "false")
public boolean disableContextualErrorMessages;
Expand All @@ -137,7 +125,7 @@ public class RestClientsConfig {
* <p>
* Can be overwritten by client-specific settings.
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<String> userAgent;
Expand Down Expand Up @@ -186,7 +174,7 @@ public class RestClientsConfig {
* <p>
* Can be overwritten by client-specific settings.
* <p>
* This property is applicable to reactive REST clients only.
* This property is not applicable to the RESTEasy Client.
*/
@ConfigItem
public Optional<Integer> maxRedirects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public void testIterateNames() {
iterator = interceptor.iterateNames(new TestContext(Arrays.asList(
"prefix/mp-rest/url",
"a.b.c/mp-rest/url",
"quarkus.rest.client.multipart-post-encoder-mode",
"quarkus.rest-client-reactive.disable-smart-produces")));
"quarkus.rest.client.multipart-post-encoder-mode")));

assertThat(iteratorToCollection(iterator)).containsOnly(
"prefix/mp-rest/url",
Expand All @@ -106,10 +105,7 @@ public void testIterateNames() {
"quarkus.rest-client.\"a.b.c\".url",

"quarkus.rest.client.multipart-post-encoder-mode",
"quarkus.rest-client.multipart-post-encoder-mode",

"quarkus.rest-client-reactive.disable-smart-produces",
"quarkus.rest-client.disable-smart-produces");
"quarkus.rest-client.multipart-post-encoder-mode");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,20 @@ private ScopeInfo computeDefaultScope(Capabilities capabilities, Config config,
ClassInfo classInfo = entry.getValue();
Optional<String> scopeConfig = RestClientConfigUtils.findConfiguredScope(config, classInfo, configKey);

Optional<String> configuredGlobalDefaultScope = RestClientConfigUtils.getDefaultScope(config);
BuiltinScope globalDefaultScope;

if (configuredGlobalDefaultScope.isPresent()) {
globalDefaultScope = builtinScopeFromName(DotName.createSimple(configuredGlobalDefaultScope.get()));
if (globalDefaultScope == null) {
log.warnf("Unable to map the global REST client scope: '%s' to a scope. Using @Dependent",
configuredGlobalDefaultScope.get());
globalDefaultScope = BuiltinScope.DEPENDENT;
}
} else {
globalDefaultScope = BuiltinScope.DEPENDENT;
}

if (scopeConfig.isPresent()) {
final DotName scope = DotName.createSimple(scopeConfig.get());
final BuiltinScope builtinScope = builtinScopeFromName(scope);
Expand All @@ -415,9 +429,8 @@ private ScopeInfo computeDefaultScope(Capabilities capabilities, Config config,

if (scopeToUse == null) {
log.warn(String.format(
"Unsupported default scope %s provided for rest client %s. Defaulting to @Dependent.",
"Unsupported default scope %s provided for REST client %s. Defaulting to @Dependent.",
scope, entry.getKey()));
scopeToUse = BuiltinScope.DEPENDENT.getInfo();
}
} else {
final Set<DotName> annotations = classInfo.annotationsMap().keySet();
Expand All @@ -435,7 +448,7 @@ private ScopeInfo computeDefaultScope(Capabilities capabilities, Config config,
}

// Initialize a default @Dependent scope as per the spec
return scopeToUse != null ? scopeToUse : BuiltinScope.DEPENDENT.getInfo();
return scopeToUse != null ? scopeToUse : globalDefaultScope.getInfo();
}

private String getAnnotationParameter(ClassInfo classInfo, String parameterName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void shouldRespond() {
@Test
void checkGlobalConfigValues() {
// global properties:
assertThat(configRoot.disableSmartProduces.get()).isTrue();
assertThat(configRoot.multipartPostEncoderMode.get()).isEqualTo("HTML5");
assertThat(configRoot.disableContextualErrorMessages).isTrue();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# Legacy reactive rest client configs
quarkus.rest-client-reactive.disable-smart-produces=true
quarkus.rest-client-reactive.scope=InvalidScope
quarkus.rest-client-reactive.provider-autodiscovery=false

# Global configs
quarkus.rest-client.multipart-post-encoder-mode=HTML5
quarkus.rest-client.disable-contextual-error-messages=true
quarkus.rest-client.disable-smart-produces=true

# Global configs that can be overridden by client-specific configs
quarkus.rest-client.scope=Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class RestClientReactiveProcessor {
private static final DotName SESSION_SCOPED = DotName.createSimple(SessionScoped.class.getName());
private static final DotName KOTLIN_METADATA_ANNOTATION = DotName.createSimple("kotlin.Metadata");

private static final String DISABLE_SMART_PRODUCES_QUARKUS = "quarkus.rest-client.disable-smart-produces";
private static final String ENABLE_COMPRESSION = "quarkus.http.enable-compression";
private static final String KOTLIN_INTERFACE_DEFAULT_IMPL_SUFFIX = "$DefaultImpls";

Expand Down Expand Up @@ -163,9 +162,7 @@ void setUpDefaultMediaType(BuildProducer<RestClientDefaultConsumesBuildItem> con
RestClientReactiveConfig config) {
consumes.produce(new RestClientDefaultConsumesBuildItem(MediaType.APPLICATION_JSON, 10));
produces.produce(new RestClientDefaultProducesBuildItem(MediaType.APPLICATION_JSON, 10));
Config mpConfig = ConfigProvider.getConfig();
Optional<Boolean> disableSmartProducesConfig = mpConfig.getOptionalValue(DISABLE_SMART_PRODUCES_QUARKUS, Boolean.class);
if (config.disableSmartProduces || disableSmartProducesConfig.orElse(false)) {
if (config.disableSmartProduces) {
disableSmartProduces.produce(new RestClientDisableSmartDefaultProduces());
}
}
Expand Down Expand Up @@ -462,7 +459,7 @@ void addRestClientBeans(Capabilities capabilities,
key -> configKeys.put(jaxrsInterface.name().toString(), key));

final ScopeInfo scope = computeDefaultScope(capabilities, ConfigProvider.getConfig(), jaxrsInterface,
configKey, clientConfig);
configKey);
// add a scope annotation, e.g. @Singleton
classCreator.addAnnotation(scope.getDotName().toString());
classCreator.addAnnotation(RestClient.class);
Expand Down Expand Up @@ -810,16 +807,21 @@ private Optional<String> getConfigKey(AnnotationInstance registerRestClientAnnot

private ScopeInfo computeDefaultScope(Capabilities capabilities, Config config,
ClassInfo restClientInterface,
Optional<String> configKey,
RestClientReactiveConfig mpClientConfig) {
Optional<String> configKey) {
ScopeInfo scopeToUse = null;

Optional<String> scopeConfig = RestClientConfigUtils.findConfiguredScope(config, restClientInterface, configKey);

BuiltinScope globalDefaultScope = BuiltinScope.from(DotName.createSimple(mpClientConfig.scope));
if (globalDefaultScope == null) {
log.warnv("Unable to map the global rest client scope: '{0}' to a scope. Using @ApplicationScoped",
mpClientConfig.scope);
Optional<String> configuredGlobalDefaultScope = RestClientConfigUtils.getDefaultScope(config);
BuiltinScope globalDefaultScope;
if (configuredGlobalDefaultScope.isPresent()) {
globalDefaultScope = builtinScopeFromName(DotName.createSimple(configuredGlobalDefaultScope.get()));
if (globalDefaultScope == null) {
log.warnf("Unable to map the global REST client scope: '%s' to a scope. Using @ApplicationScoped",
configuredGlobalDefaultScope.get());
globalDefaultScope = BuiltinScope.APPLICATION;
}
} else {
globalDefaultScope = BuiltinScope.APPLICATION;
}

Expand All @@ -835,9 +837,8 @@ private ScopeInfo computeDefaultScope(Capabilities capabilities, Config config,
}

if (scopeToUse == null) {
log.warnf("Unsupported default scope {} provided for rest client {}. Defaulting to {}",
log.warnf("Unsupported default scope %s provided for REST client %s. Defaulting to %s",
scope, restClientInterface.name(), globalDefaultScope.getName());
scopeToUse = globalDefaultScope.getInfo();
}
} else {
final Set<DotName> annotations = restClientInterface.annotationsMap().keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void shouldRespond() {
@Test
void checkGlobalConfigValues() {
// global properties:
assertThat(configRoot.disableSmartProduces.get()).isTrue();
assertThat(configRoot.multipartPostEncoderMode.get()).isEqualTo("HTML5");
assertThat(configRoot.disableContextualErrorMessages).isTrue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class LegacyConfigurationTest {

@Test
void configurationShouldBeLoaded() {
assertThat(configRoot.disableSmartProduces).isPresent();
assertThat(configRoot.disableSmartProduces.get()).isTrue();
assertThat(configRoot.multipartPostEncoderMode).isPresent();
assertThat(configRoot.multipartPostEncoderMode.get()).isEqualTo("RFC3986");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# Legacy reactive rest client configs
quarkus.rest-client-reactive.disable-smart-produces=true
quarkus.rest-client-reactive.scope=InvalidScope
quarkus.rest-client-reactive.provider-autodiscovery=false

# Global configs
quarkus.rest-client.multipart-post-encoder-mode=HTML5
quarkus.rest-client.disable-contextual-error-messages=true
quarkus.rest-client.disable-smart-produces=true

# Global configs that can be overridden by client-specific configs
quarkus.rest-client.scope=Singleton
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
quarkus.rest-client-reactive.scope=Dependent
quarkus.rest-client-reactive.disable-smart-produces=true
quarkus.rest-client.scope=Dependent
quarkus.rest.client.max-redirects=4
quarkus.rest.client.multipart-post-encoder-mode=RFC3986

Expand Down
Loading

0 comments on commit 849761c

Please sign in to comment.