Skip to content

Commit

Permalink
Refactor environment variable logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benkropf committed Apr 13, 2019
1 parent 436d2f0 commit 5a45ba0
Showing 1 changed file with 34 additions and 73 deletions.
107 changes: 34 additions & 73 deletions aws-java-sdk-core/src/main/java/com/amazonaws/ClientConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,43 +621,20 @@ private String getSystemProperty(String property) {
/**
* Returns the value for the given environment variable.
*/
private String getEnvironmentVariable(String variable) {
return System.getenv(variable);
private String getEnvironmentVariable(String environmentVariable) {
return System.getenv(environmentVariable);
}

/**
* Returns a URL object for the given environment variable, if can be constructed, null otherwise.
* Returns the value for the given environment variable if its set, otherwise returns
* the lowercase version of variable.
*/
private URL getUrlFromEnvironmentVariable(String variable) {
try {
return new URL(getEnvironmentVariable(variable));
} catch (MalformedURLException e) {
return null;
}
}

/**
* Returns a URL object for the environment variable HTTPS_PROXY or https_proxy if it is set, null otherwise.
*/
private URL getHttpsProxyUrlFromEnvironment() {
if (getUrlFromEnvironmentVariable("HTTPS_PROXY") != null) {
return getUrlFromEnvironmentVariable("HTTPS_PROXY");
} else if (getUrlFromEnvironmentVariable("https_proxy") != null) {
return getUrlFromEnvironmentVariable("https_proxy");
}
return null;
}

/**
* Returns the a URL object for the environment variable HTTP_PROXY or http_proxy if it is set, null otherwise.
*/
private URL getHttpProxyUrlFromEnvironment() {
if (getUrlFromEnvironmentVariable("HTTP_PROXY") != null) {
return getUrlFromEnvironmentVariable("HTTP_PROXY");
} else if (getUrlFromEnvironmentVariable("http_proxy") != null) {
return getUrlFromEnvironmentVariable("http_proxy");
private String getEnvironmentVariableCaseInsensitive(String environmentVariable) {
if (getEnvironmentVariable(environmentVariable) != null) {
return getEnvironmentVariable(environmentVariable);
} else {
return getEnvironmentVariable(environmentVariable.toLowerCase());
}
return null;
}

/**
Expand Down Expand Up @@ -707,16 +684,13 @@ private String getProxyHostProperty() {
* variable HTTP_PROXY/http_proxy.
*/
private String getProxyHostEnvironment() {
if (getProtocol() == Protocol.HTTPS) {
if (getHttpsProxyUrlFromEnvironment() != null) {
return getHttpsProxyUrlFromEnvironment().getHost();
}
} else {
if (getHttpProxyUrlFromEnvironment() != null) {
return getHttpProxyUrlFromEnvironment().getHost();
}
try {
return getProtocol() == Protocol.HTTPS
? new URL(getEnvironmentVariableCaseInsensitive("HTTPS_PROXY")).getHost()
: new URL(getEnvironmentVariableCaseInsensitive("HTTP_PROXY")).getHost();
} catch (MalformedURLException e) {
return null;
}
return null;
}

/**
Expand Down Expand Up @@ -793,16 +767,13 @@ private int getProxyPortProperty() {
* variable HTTP_PROXY/http_proxy.
*/
private int getProxyPortEnvironment() {
if (getProtocol() == Protocol.HTTPS) {
if (getHttpsProxyUrlFromEnvironment() != null) {
return getHttpsProxyUrlFromEnvironment().getPort();
}
} else {
if (getHttpProxyUrlFromEnvironment() != null) {
return getHttpProxyUrlFromEnvironment().getPort();
}
try {
return getProtocol() == Protocol.HTTPS
? new URL(getEnvironmentVariableCaseInsensitive("HTTPS_PROXY")).getPort()
: new URL(getEnvironmentVariableCaseInsensitive("HTTP_PROXY")).getPort();
} catch (MalformedURLException e) {
return proxyPort;
}
return proxyPort;
}

/**
Expand Down Expand Up @@ -901,16 +872,13 @@ private String getProxyUsernameProperty() {
* the value of the environment variable HTTP_PROXY/http_proxy.
*/
private String getProxyUsernameEnvironment() {
if (getProtocol() == Protocol.HTTPS) {
if (getHttpsProxyUrlFromEnvironment() != null) {
return getHttpsProxyUrlFromEnvironment().getUserInfo().split(":", 2)[0];
}
} else {
if (getHttpProxyUrlFromEnvironment() != null) {
return getHttpProxyUrlFromEnvironment().getUserInfo().split(":", 2)[0];
}
try {
return getProtocol() == Protocol.HTTPS
? new URL(getEnvironmentVariableCaseInsensitive("HTTPS_PROXY")).getUserInfo().split(":", 2)[0]
: new URL(getEnvironmentVariableCaseInsensitive("HTTP_PROXY")).getUserInfo().split(":", 2)[0];
} catch (MalformedURLException e) {
return null;
}
return null;
}

/**
Expand Down Expand Up @@ -982,16 +950,13 @@ private String getProxyPasswordProperty() {
* variable HTTP_PROXY/http_proxy.
*/
private String getProxyPasswordEnvironment() {
if (getProtocol() == Protocol.HTTPS) {
if (getHttpsProxyUrlFromEnvironment() != null) {
return getHttpsProxyUrlFromEnvironment().getUserInfo().split(":", 2)[1];
}
} else {
if (getHttpProxyUrlFromEnvironment() != null) {
return getHttpProxyUrlFromEnvironment().getUserInfo().split(":", 2)[1];
}
try {
return getProtocol() == Protocol.HTTPS
? new URL(getEnvironmentVariableCaseInsensitive("HTTPS_PROXY")).getUserInfo().split(":", 2)[1]
: new URL(getEnvironmentVariableCaseInsensitive("HTTP_PROXY")).getUserInfo().split(":", 2)[1];
} catch (MalformedURLException e) {
return null;
}
return null;
}

/**
Expand Down Expand Up @@ -1125,11 +1090,7 @@ private String getNonProxyHostsProperty() {
* Returns the value of the environment variable NO_PROXY/no_proxy.
*/
private String getNonProxyHostsEnvironment() {
if (getEnvironmentVariable("NO_PROXY") != null) {
return getEnvironmentVariable("NO_PROXY");
} else {
return getEnvironmentVariable("no_proxy");
}
return getEnvironmentVariableCaseInsensitive("NO_PROXY");
}

/**
Expand Down

0 comments on commit 5a45ba0

Please sign in to comment.