Skip to content

Commit 0532750

Browse files
committed
Proxy Registry: fix a few glaring issues
1 parent 1babe31 commit 0532750

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

extensions/proxy-registry/runtime/src/main/java/io/quarkus/proxy/config/ProxyConfigurationRegistry.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Map.Entry;
88
import java.util.Optional;
99
import java.util.OptionalInt;
10+
import java.util.StringJoiner;
1011
import java.util.function.Supplier;
1112
import java.util.stream.Collectors;
1213

@@ -32,19 +33,13 @@ public Optional<NamedProxyConfig> getProxyConfig(Optional<String> name, NoneRetu
3233
}
3334
final String plainName = name.get();
3435
if (ProxyConfig.NO_PROXY.equals(plainName)) {
35-
switch (noneBehavior) {
36-
case NONE_INSTANCE: {
37-
return None.INSTANCE;
38-
}
39-
case EMPTY: {
40-
return Optional.empty();
41-
}
42-
default:
43-
throw new IllegalArgumentException("Unexpected value: " + noneBehavior);
44-
}
36+
return switch (noneBehavior) {
37+
case NONE_INSTANCE -> None.INSTANCE;
38+
case EMPTY -> Optional.empty();
39+
};
4540
}
4641
final Optional<NamedProxyConfig> namedProxyConfig = namedProxyConfigs.getOrDefault(plainName, Optional.empty());
47-
if (namedProxyConfig == null) {
42+
if (namedProxyConfig.isEmpty()) {
4843
throw new IllegalStateException("Proxy configuration with name " + plainName + " was requested but "
4944
+ "quarkus.proxy.\"" + plainName + "\".host is not defined");
5045
}
@@ -62,11 +57,13 @@ public static Optional<UsernamePassword> getUsernamePassword(NamedProxyConfig co
6257
String username = creds.get(cpConfig.usernameKey());
6358
String password = creds.get(cpConfig.passwordKey());
6459
if (username == null || password == null) {
65-
final String badKeys = Map.of(cpConfig.usernameKey(), username, cpConfig.passwordKey(), password).entrySet()
66-
.stream()
67-
.filter(en -> en.getValue() == null)
68-
.map(Entry::getKey)
69-
.collect(Collectors.joining(" and "));
60+
StringJoiner badKeys = new StringJoiner(" and ");
61+
if (username == null) {
62+
badKeys.add(cpConfig.usernameKey());
63+
}
64+
if (password == null) {
65+
badKeys.add(cpConfig.passwordKey());
66+
}
7067
throw new IllegalStateException(
7168
"Could not retrieve " + badKeys + " from credential bucket " + cpConfig.name().get());
7269
}
@@ -97,19 +94,20 @@ static CredentialsProvider lookupCredentialsProvider(String name) {
9794

9895
static Optional<NamedProxyConfig> assertValid(String prefix, NamedProxyConfig config) {
9996
if (config.host().isEmpty()) {
100-
String badValues = Map.<String, Optional<?>> of(
97+
String badValues = Map.of(
10198
"port", config.port().isPresent() ? Optional.of(config.port().getAsInt()) : Optional.empty(),
10299
"username", config.username(),
103100
"password", config.password(),
104-
"nonProxyHosts", config.nonProxyHosts(),
105-
"proxyConnectTimeout", config.proxyConnectTimeout())
106-
.entrySet().stream()
101+
"non-proxy-hosts", config.nonProxyHosts(),
102+
"proxy-connect-timeout", config.proxyConnectTimeout())
103+
.entrySet()
104+
.stream()
107105
.filter(en -> en.getValue().isPresent())
108106
.map(en -> prefix + en.getKey())
109107
.collect(Collectors.joining(", "));
110108
if (!badValues.isEmpty()) {
111109
throw new IllegalStateException(
112-
"If " + prefix + ".host is not set, then all of " + badValues + " must not be set too");
110+
"If " + prefix + ".host is not set, then all of " + badValues + " must not be set either");
113111
}
114112
return Optional.empty();
115113
} else {
@@ -126,6 +124,9 @@ static Optional<NamedProxyConfig> assertValid(String prefix, NamedProxyConfig co
126124
}
127125
}
128126

127+
/**
128+
* This method may only be called by {@code ProxyConfigProcessor} and must not be called elsewhere.
129+
*/
129130
public void init(ProxyConfig proxyConfig) {
130131
final HashMap<String, Optional<NamedProxyConfig>> m = new HashMap<>();
131132
for (Entry<String, NamedProxyConfig> en : proxyConfig.namedProxyConfigs().entrySet()) {
@@ -145,6 +146,9 @@ public void init(ProxyConfig proxyConfig) {
145146

146147
}
147148

149+
/**
150+
* This method may only be called by {@code ProxyConfigProcessor} and must not be called elsewhere.
151+
*/
148152
public Supplier<ProxyConfigurationRegistry> getSupplier() {
149153
return new Supplier<ProxyConfigurationRegistry>() {
150154
@Override

0 commit comments

Comments
 (0)