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

AlwaysPrint feature #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Here is a configuration example with a proxy for intranet addresses, and another
network:
proxy:
enable: true # allows disabling auto-config; enabled by default
alwaysPrint: false #allow the info log showing the proxy used every time a call is made

# explicit list of proxy servers with settings
servers:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>com.orange.common</groupId>
<artifactId>spring-boot-autoconfigure-proxy</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<name>spring-boot-autoconfigure-proxy</name>
<description>Spring Boot AutoConfigure Proxy</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ public int hashCode() {

private final List<ProxyEntry> proxies;

private final boolean alwaysPrint;

private Map<SchemeAndHost, List<Proxy>> hostname2Proxies = new HashMap<>();

private MultiProxySelector(List<ProxyEntry> proxies) {
private MultiProxySelector(List<ProxyEntry> proxies, boolean alwaysPrint) {
this.proxies = proxies;
this.alwaysPrint = alwaysPrint;
}

@Override
Expand Down Expand Up @@ -126,7 +129,13 @@ public List<Proxy> select(URI uri) {
throw new IllegalArgumentException("protocol = " + protocol + " host = " + host);
}

return hostname2Proxies.computeIfAbsent(new SchemeAndHost(protocol, host), this::doGetProxies);
SchemeAndHost schemeAndHost = new SchemeAndHost(protocol, host);
List<Proxy> proxiesList =
hostname2Proxies.computeIfAbsent(schemeAndHost, this::doGetProxies);
if (alwaysPrint) {
LOGGER.info("Proxies for [{}] : {}", schemeAndHost, proxiesList);
}
return proxiesList;
}

private List<Proxy> doGetProxies(SchemeAndHost schemeAndHost) {
Expand All @@ -151,7 +160,8 @@ public String toString() {
'}';
}

static MultiProxySelector build(List<NetworkProxyProperties.ProxyServerConfig> proxies) {
static MultiProxySelector build(List<NetworkProxyProperties.ProxyServerConfig> proxies,
boolean alwaysPrint) {
List<ProxyEntry> proxyEntries = new ArrayList<>();
for (int i = 0; i < proxies.size(); i++) {
NetworkProxyProperties.ProxyServerConfig cfg = proxies.get(i);
Expand Down Expand Up @@ -190,6 +200,6 @@ static MultiProxySelector build(List<NetworkProxyProperties.ProxyServerConfig> p
proxyEntries.add(new ProxyEntry(cfg, proxy, positiveMatchers, negativeMatchers));
}

return new MultiProxySelector(proxyEntries);
return new MultiProxySelector(proxyEntries, alwaysPrint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void setupProxyConfiguration() {
LOGGER.info("Configuring proxies from Spring Boot configuration");

// install proxy selector
ProxySelector.setDefault(MultiProxySelector.build(properties.getServers()));
ProxySelector.setDefault(MultiProxySelector.build(properties.getServers(),
properties.isAlwaysPrint()));

// set password authentication for every proxy that need one
for (NetworkProxyProperties.ProxyServerConfig cfg : properties.getServers()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class NetworkProxyProperties implements Validator {
*/
private boolean enabled = true;

/**
* Whether to enable the print of the proxy used every time.
*/
private boolean alwaysPrint = false;

/**
* Explicit network proxy servers configuration
*/
Expand All @@ -43,10 +48,19 @@ public void setServers(List<ProxyServerConfig> servers) {
this.servers = servers;
}

public boolean isAlwaysPrint() {
return alwaysPrint;
}

public void setAlwaysPrint(boolean alwaysPrint) {
this.alwaysPrint = alwaysPrint;
}

@Override
public String toString() {
return "NetworkProxyProperties{" +
"enabled=" + enabled +
", alwaysPrint= " +alwaysPrint +
", servers=" + servers +
'}';
}
Expand Down