aboutsummaryrefslogtreecommitdiff
path: root/id/server/moa-id-commons/src/main/java/org
diff options
context:
space:
mode:
authorThomas Lenz <thomas.lenz@egiz.gv.at>2020-09-21 10:10:16 +0200
committerThomas Lenz <thomas.lenz@egiz.gv.at>2020-09-21 10:10:16 +0200
commit775f70c77247ab91a58b48c3798fbf91d104e4d2 (patch)
treedc5aec4b9aeb012ae81c9801f4cf428848d628b7 /id/server/moa-id-commons/src/main/java/org
parent570ac427fc0841c1ac49054b0ee854b57a2029d0 (diff)
downloadmoa-id-spss-775f70c77247ab91a58b48c3798fbf91d104e4d2.tar.gz
moa-id-spss-775f70c77247ab91a58b48c3798fbf91d104e4d2.tar.bz2
moa-id-spss-775f70c77247ab91a58b48c3798fbf91d104e4d2.zip
add support for http.nonProxyHosts
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/org')
-rw-r--r--id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java104
1 files changed, 70 insertions, 34 deletions
diff --git a/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java b/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java
index 4d4c7fa88..04e9a8ab9 100644
--- a/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java
+++ b/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java
@@ -25,6 +25,7 @@ package org.apache.commons.httpclient;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.StringTokenizer;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.protocol.Protocol;
@@ -43,42 +44,9 @@ public class MOAHttpClient extends HttpClient {
public MOAHttpClient() {
super();
- injectProxyCredentials();
}
- public void injectProxyCredentials() {
- //set proxy functionality
- String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$
- String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$
- String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$
- String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$
-
- if (MiscUtil.isNotEmpty(host)) {
- int p = -1;
- if (MiscUtil.isNotEmpty(port)) {
- try {
- p = Integer.parseInt(port);
-
- } catch (Exception e) {
- Logger.error("'http.proxyPort' not valid!", e);
-
- }
- }
- Logger.info("Set HTTP-Proxy to Host: " + host + " and port: " + p);
- getHostConfiguration().setProxy(host, p);
- if (MiscUtil.isNotEmpty(user) && pass != null) {
- //set proxy credentials
- AuthScope authscope = new AuthScope(host, p);
- Credentials credentials = new UsernamePasswordCredentials(user, pass);
- getState().setProxyCredentials(authscope, credentials);
- Logger.info(" Use Proxy with Username: " + user + " and password: "
- + (Logger.isTraceEnabled() ? pass : "*******"));
-
- }
-
- }
- }
public void setCustomSSLTrustStore(String metadataURL, ProtocolSocketFactory protoSocketFactory)
throws MalformedURLException {
@@ -92,6 +60,7 @@ public class MOAHttpClient extends HttpClient {
Protocol authhttps = new Protocol("https", protoSocketFactory, 443);
getHostConfiguration().setHost(url.getHost(), port, authhttps);
+
}
@@ -122,12 +91,79 @@ public class MOAHttpClient extends HttpClient {
}
}
+ HttpState interalState = (state == null ? getState() : state);
+
+ if (requiresProxy(uri.getURI())) {
+ injectProxyCredentials(hostconfig, interalState);
+
+ }
+
HttpMethodDirector methodDirector = new HttpMethodDirector(
getHttpConnectionManager(),
hostconfig,
getParams(),
- (state == null ? getState() : state));
+ state);
methodDirector.executeMethod(method);
return method.getStatusCode();
}
+
+ public void injectProxyCredentials(HostConfiguration hostconfig, HttpState interalState) {
+ //set proxy functionality
+ String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$
+ String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$
+ String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$
+ String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$
+
+ if (MiscUtil.isNotEmpty(host)) {
+ int p = -1;
+ if (MiscUtil.isNotEmpty(port)) {
+ try {
+ p = Integer.parseInt(port);
+
+ } catch (Exception e) {
+ Logger.error("'http.proxyPort' not valid!", e);
+
+ }
+ }
+ Logger.debug("Set HTTP-Proxy to Host: " + host + " and port: " + p);
+ hostconfig.setProxy(host, p);
+ if (MiscUtil.isNotEmpty(user) && pass != null) {
+ //set proxy credentials
+ AuthScope authscope = new AuthScope(host, p);
+ Credentials credentials = new UsernamePasswordCredentials(user, pass);
+ interalState.setProxyCredentials(authscope, credentials);
+ Logger.debug(" Use Proxy with Username: " + user + " and password: "
+ + (Logger.isTraceEnabled() ? pass : "*******"));
+
+ }
+
+ }
+ }
+
+ /**
+ * Return true unless the given target host is specified in the <code>http.nonProxyHosts</code> system property (used for both protocols, http and https).
+ * See <a href=http://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html>Networking Properties</a>.
+ * @param targetHost Non-null host name to verify
+ * @return true if not specified in the list, false if it is specified and therefore should be excluded from proxy
+ */
+ private static boolean requiresProxy(final String targetHost)
+ {
+ boolean requiresProxy = true;
+ final String nonProxyHosts = System.getProperty("http.nonProxyHosts");
+ if (nonProxyHosts != null)
+ {
+ StringTokenizer tokenizer = new StringTokenizer(nonProxyHosts, "|");
+ while (tokenizer.hasMoreTokens())
+ {
+ String pattern = tokenizer.nextToken();
+ pattern = pattern.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*");
+ if (targetHost.matches(pattern))
+ {
+ requiresProxy = false;
+ break;
+ }
+ }
+ }
+ return requiresProxy;
+ }
}