-
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.
Improve compatibility of the REST Client configuration
- Loading branch information
Showing
13 changed files
with
402 additions
and
126 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
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
87 changes: 0 additions & 87 deletions
87
...main/java/io/quarkus/restclient/config/RestClientNameFallbackConfigSourceInterceptor.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
...runtime/src/main/java/io/quarkus/restclient/config/RestClientNameFallbackInterceptor.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,77 @@ | ||
package io.quarkus.restclient.config; | ||
|
||
import static io.quarkus.restclient.config.AbstractRestClientConfigBuilder.indexOfRestClient; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import jakarta.annotation.Priority; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import io.smallrye.config.FallbackConfigSourceInterceptor; | ||
import io.smallrye.config.Priorities; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
|
||
/** | ||
* Fallbacks REST Client FQN to Simple Name and quoted config keys to unquoted | ||
* <p> | ||
* Ideally, this shouldn't be required. The old custom implementation allowed us to mix both FQN and Simple Name in a | ||
* merged configuration to use in the REST Client. The standard Config system does not support such a feature. If a | ||
* configuration supports multiple names, the user has to use the same name across all configuration sources. No other | ||
* Quarkus extension behaves this way because only the REST Client extension provides the custom code to make it work. | ||
* <p> | ||
* In the case of {@link RegisterRestClient#configKey()}, users either use quoted or unquoted configuration names for | ||
* single config key segments. Again, the Config system does not support such a feature (but could be implemented), so | ||
* the interceptor also fallbacks to unquoted configuration names, due to the <code>force</code> property added by | ||
* {@link AbstractRestClientConfigBuilder#configBuilder(SmallRyeConfigBuilder)}. | ||
*/ | ||
@Priority(Priorities.LIBRARY + 610) | ||
public class RestClientNameFallbackInterceptor extends FallbackConfigSourceInterceptor { | ||
public RestClientNameFallbackInterceptor(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
super(fallback(restClients, ignoreNames)); | ||
} | ||
|
||
private static Function<String, String> fallback(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
return new Function<String, String>() { | ||
@Override | ||
public String apply(final String name) { | ||
int indexOfRestClient = indexOfRestClient(name); | ||
if (indexOfRestClient != -1) { | ||
if (ignoreNames.contains(name)) { | ||
return name; | ||
} | ||
|
||
int endOfRestClient = indexOfRestClient + 1; | ||
for (RegisteredRestClient restClient : restClients) { | ||
if (name.length() > indexOfRestClient && name.charAt(indexOfRestClient) == '"') { | ||
String interfaceName = restClient.getFullName(); | ||
if (name.regionMatches(endOfRestClient, interfaceName, 0, interfaceName.length())) { | ||
if (name.length() > endOfRestClient + interfaceName.length() | ||
&& name.charAt(endOfRestClient + interfaceName.length()) == '"') { | ||
return "quarkus.rest-client." + restClient.getSimpleName() | ||
+ name.substring(endOfRestClient + interfaceName.length() + 1); | ||
} | ||
} | ||
|
||
String configKey = restClient.getConfigKey(); | ||
if (configKey == null || configKey.isEmpty() || restClient.isConfigKeySegments()) { | ||
continue; | ||
} | ||
int endOfConfigKey = endOfRestClient + configKey.length(); | ||
if (name.regionMatches(endOfRestClient, configKey, 0, configKey.length())) { | ||
if (name.length() > endOfConfigKey && name.charAt(endOfConfigKey) == '"') { | ||
return "quarkus.rest-client." + configKey + name.substring(endOfConfigKey + 1); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return name; | ||
} | ||
}; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...src/main/java/io/quarkus/restclient/config/RestClientNameUnquotedFallbackInterceptor.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,92 @@ | ||
package io.quarkus.restclient.config; | ||
|
||
import static io.quarkus.restclient.config.AbstractRestClientConfigBuilder.indexOfRestClient; | ||
import static io.smallrye.config.ProfileConfigSourceInterceptor.convertProfile; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import jakarta.annotation.Priority; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import io.smallrye.config.ConfigValue; | ||
import io.smallrye.config.FallbackConfigSourceInterceptor; | ||
import io.smallrye.config.NameIterator; | ||
import io.smallrye.config.Priorities; | ||
|
||
/** | ||
* Relocates unquoted config keys to quoted | ||
* <p> | ||
* In the case of {@link RegisterRestClient#configKey()}, users either use quoted or unquoted configuration names for | ||
* single config key segments. Again, the Config system does not support such a feature (but could be implemented), so | ||
* the interceptor also relocates to unquoted configuration names. | ||
* <p> | ||
* We need a double-way relocation / fallback mapping between unquoted and quoted because SmallRye Config will use the | ||
* first distict key it finds to populate {@link RestClientsConfig#clients()} in the list of property names. If quoted, | ||
* it will search for all quoted. If unquoted, it will search for all unquoted. We cannot be sure how the user sets the | ||
* configuration, especially considering that we may not be able to query the list directly if the config comes from a | ||
* source that does not support listing property names. | ||
*/ | ||
@Priority(Priorities.LIBRARY + 605) | ||
public class RestClientNameUnquotedFallbackInterceptor extends FallbackConfigSourceInterceptor { | ||
public RestClientNameUnquotedFallbackInterceptor(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
super(relocate(restClients, ignoreNames)); | ||
} | ||
|
||
private static Function<String, String> relocate(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
return new Function<String, String>() { | ||
@Override | ||
public String apply(final String name) { | ||
int indexOfRestClient = indexOfRestClient(name); | ||
if (indexOfRestClient != -1) { | ||
if (ignoreNames.contains(name)) { | ||
return name; | ||
} | ||
|
||
for (RegisteredRestClient restClient : restClients) { | ||
String configKey = restClient.getConfigKey(); | ||
if (configKey == null || configKey.isEmpty() || restClient.isConfigKeySegments()) { | ||
continue; | ||
} | ||
|
||
int endOfConfigKey = indexOfRestClient + configKey.length(); | ||
if (name.regionMatches(indexOfRestClient, configKey, 0, configKey.length())) { | ||
if (name.length() > endOfConfigKey && name.charAt(endOfConfigKey) == '.') { | ||
return "quarkus.rest-client.\"" + configKey + "\"" + name.substring(endOfConfigKey); | ||
} | ||
} | ||
} | ||
} | ||
return name; | ||
} | ||
}; | ||
} | ||
|
||
private static final Comparator<ConfigValue> CONFIG_SOURCE_COMPARATOR = new Comparator<ConfigValue>() { | ||
@Override | ||
public int compare(ConfigValue original, ConfigValue candidate) { | ||
int result = Integer.compare(original.getConfigSourceOrdinal(), candidate.getConfigSourceOrdinal()); | ||
if (result != 0) { | ||
return result; | ||
} | ||
result = Integer.compare(original.getConfigSourcePosition(), candidate.getConfigSourcePosition()) * -1; | ||
if (result != 0) { | ||
return result; | ||
} | ||
// If both properties are profiled, prioritize the one with the most specific profile. | ||
if (original.getName().charAt(0) == '%' && candidate.getName().charAt(0) == '%') { | ||
List<String> originalProfiles = convertProfile( | ||
new NameIterator(original.getName()).getNextSegment().substring(1)); | ||
List<String> candidateProfiles = convertProfile( | ||
new NameIterator(candidate.getName()).getNextSegment().substring(1)); | ||
return Integer.compare(originalProfiles.size(), candidateProfiles.size()) * -1; | ||
} | ||
return result; | ||
} | ||
}; | ||
} |
Oops, something went wrong.