146 lines
4.3 KiB
Java
146 lines
4.3 KiB
Java
package org.gcube.dataanalysys.dataminerpoolmanager.util;
|
|
|
|
***REMOVED***
|
|
import java.net.Authenticator;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.PasswordAuthentication;
|
|
import java.net.Proxy;
|
|
import java.net.ProxySelector;
|
|
import java.net.SocketAddress;
|
|
import java.net.URI;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
***REMOVED***
|
|
|
|
import org.apache.commons.configuration.ConfigurationException;
|
|
import org.apache.commons.configuration.PropertiesConfiguration;
|
|
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
|
|
|
|
interface NetworkConfiguration ***REMOVED***
|
|
|
|
public String getProxyHost();
|
|
|
|
public String getProxyPort();
|
|
|
|
public String getProxyUser();
|
|
|
|
public String getProxyPassword();
|
|
|
|
public String getNonProxyHosts();
|
|
|
|
***REMOVED***
|
|
|
|
class FileBasedProxyConfiguration implements NetworkConfiguration ***REMOVED***
|
|
|
|
private static PropertiesConfiguration configuration;
|
|
|
|
public FileBasedProxyConfiguration(String path) ***REMOVED***
|
|
try ***REMOVED***
|
|
***REMOVED*** load the configuration
|
|
configuration = new PropertiesConfiguration(path);
|
|
***REMOVED*** set the reloading strategy to enable hot-configuration
|
|
FileChangedReloadingStrategy fcrs = new FileChangedReloadingStrategy();
|
|
configuration.setReloadingStrategy(fcrs);
|
|
***REMOVED*** catch (ConfigurationException e) ***REMOVED***
|
|
e.printStackTrace();
|
|
***REMOVED***
|
|
***REMOVED***
|
|
|
|
@Override
|
|
public String getProxyHost() ***REMOVED***
|
|
return configuration.getString("proxyHost");
|
|
***REMOVED***
|
|
|
|
@Override
|
|
public String getProxyPort() ***REMOVED***
|
|
return configuration.getString("proxyPort");
|
|
***REMOVED***
|
|
|
|
@Override
|
|
public String getProxyUser() ***REMOVED***
|
|
return configuration.getString("proxyUser");
|
|
***REMOVED***
|
|
|
|
@Override
|
|
public String getProxyPassword() ***REMOVED***
|
|
return configuration.getString("proxyPassword");
|
|
***REMOVED***
|
|
|
|
@Override
|
|
public String getNonProxyHosts() ***REMOVED***
|
|
return configuration.getString("nonProxyHosts");
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
public class PropertiesBasedProxySelector extends ProxySelector ***REMOVED***
|
|
|
|
List<Proxy> proxies = null;
|
|
|
|
List<String> nonProxyHosts = null;
|
|
|
|
public PropertiesBasedProxySelector(String proxySettingsPath) ***REMOVED***
|
|
this(new FileBasedProxyConfiguration(proxySettingsPath));
|
|
***REMOVED***
|
|
|
|
public PropertiesBasedProxySelector(NetworkConfiguration config) ***REMOVED***
|
|
if (config == null || config.getProxyHost() == null) ***REMOVED***
|
|
this.proxies = null;
|
|
return;
|
|
***REMOVED***
|
|
|
|
String host = config.getProxyHost();
|
|
|
|
int port = 80;
|
|
|
|
if (config.getProxyPort() != null) ***REMOVED***
|
|
port = Integer.valueOf(config.getProxyPort());
|
|
***REMOVED***
|
|
|
|
if (config.getNonProxyHosts() != null) ***REMOVED***
|
|
this.nonProxyHosts = Arrays
|
|
.asList(config.getNonProxyHosts().split("\\|"));
|
|
***REMOVED***
|
|
|
|
this.proxies = new ArrayList<Proxy>();
|
|
this.proxies.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host,
|
|
port)));
|
|
|
|
if (config.getProxyUser() != null) ***REMOVED***
|
|
final String username = config.getProxyUser();
|
|
final String password = config.getProxyPassword();
|
|
|
|
Authenticator.setDefault(new Authenticator() ***REMOVED***
|
|
@Override
|
|
protected PasswordAuthentication getPasswordAuthentication() ***REMOVED***
|
|
return new PasswordAuthentication(username, password.toCharArray());
|
|
***REMOVED***
|
|
***REMOVED***);
|
|
|
|
***REMOVED***
|
|
***REMOVED***
|
|
|
|
@Override
|
|
public List<Proxy> select(URI uri) ***REMOVED***
|
|
if (this.nonProxyHosts == null) ***REMOVED***
|
|
return Arrays.asList(Proxy.NO_PROXY);
|
|
***REMOVED*** else ***REMOVED***
|
|
for (String entry : this.nonProxyHosts) ***REMOVED***
|
|
entry = entry.trim();
|
|
if (entry.startsWith("*") && uri.getHost().endsWith(entry.substring(1))) ***REMOVED***
|
|
return Arrays.asList(Proxy.NO_PROXY);
|
|
***REMOVED***
|
|
if (uri.getHost().equals(entry)) ***REMOVED***
|
|
return Arrays.asList(Proxy.NO_PROXY);
|
|
***REMOVED***
|
|
***REMOVED***
|
|
return this.proxies;
|
|
***REMOVED***
|
|
***REMOVED***
|
|
|
|
@Override
|
|
public void connectFailed(URI uri, SocketAddress socketAddress, IOException e) ***REMOVED***
|
|
|
|
***REMOVED***
|
|
***REMOVED***
|