use of java.net.ProxySelector in project keystore-explorer by kaikramer.
the class DPreferences method storeProxyPreferences.
private boolean storeProxyPreferences() {
// Store current proxy selector - compare with new one to see if default needs updated
ProxySelector defaultProxySelector = ProxySelector.getDefault();
if (jrbNoProxy.isSelected()) {
NoProxySelector noProxySelector = new NoProxySelector();
if (!noProxySelector.equals(defaultProxySelector)) {
ProxySelector.setDefault(noProxySelector);
}
} else if (jrbSystemProxySettings.isSelected()) {
SystemProxySelector systemProxySelector = new SystemProxySelector();
if (!systemProxySelector.equals(defaultProxySelector)) {
ProxySelector.setDefault(systemProxySelector);
}
} else if (jrbManualProxyConfig.isSelected()) {
String httpHost = jtfHttpHost.getText().trim();
String httpPortStr = jtfHttpPort.getText().trim();
String httpsHost = jtfHttpsHost.getText().trim();
String httpsPortStr = jtfHttpsPort.getText().trim();
String socksHost = jtfSocksHost.getText().trim();
String socksPortStr = jtfSocksPort.getText().trim();
ProxyAddress httpProxyAddress = null;
ProxyAddress httpsProxyAddress = null;
ProxyAddress socksProxyAddress = null;
// Require at least one of the HTTP host or HTTPS host or SOCKS host manual settings
if ((httpHost.length() == 0) && (httpsHost.length() == 0) && (socksHost.length() == 0)) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.ManualConfigReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
} else {
if (httpHost.length() > 0) {
if (httpPortStr.length() == 0) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqHttp.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
} else {
try {
int httpPort = Integer.parseInt(httpPortStr);
if (httpPort < 1) {
throw new NumberFormatException();
}
httpProxyAddress = new ProxyAddress(httpHost, httpPort);
} catch (NumberFormatException ex) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqHttp.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
}
}
}
if (httpsHost.length() > 0) {
if (httpsPortStr.length() == 0) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqHttps.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
} else {
try {
int httpsPort = Integer.parseInt(httpsPortStr);
if (httpsPort < 1) {
throw new NumberFormatException();
}
httpsProxyAddress = new ProxyAddress(httpsHost, httpsPort);
} catch (NumberFormatException ex) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqHttps.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
}
}
}
if (socksHost.length() > 0) {
if (socksPortStr.length() == 0) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqSocks.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
} else {
try {
int socksPort = Integer.parseInt(socksPortStr);
if (socksPort < 1) {
throw new NumberFormatException();
}
socksProxyAddress = new ProxyAddress(socksHost, socksPort);
} catch (NumberFormatException ex) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqSocks.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
}
}
}
ManualProxySelector manualProxySelector = new ManualProxySelector(httpProxyAddress, httpsProxyAddress, null, socksProxyAddress);
if (!manualProxySelector.equals(defaultProxySelector)) {
ProxySelector.setDefault(manualProxySelector);
}
}
} else if (jrbAutomaticProxyConfig.isSelected()) {
String pacUrl = jtfPacUrl.getText().trim();
if (pacUrl.length() == 0) {
jtpPreferences.setSelectedIndex(3);
JOptionPane.showMessageDialog(this, res.getString("DPreferences.PacUrlReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
return false;
}
PacProxySelector pacProxySelector = new PacProxySelector(pacUrl);
if (!pacProxySelector.equals(defaultProxySelector)) {
ProxySelector.setDefault(pacProxySelector);
}
}
return true;
}
use of java.net.ProxySelector in project ant by apache.
the class ProxyDiagnostics method toString.
/**
* Get the diagnostics for proxy information.
* @return the information.
*/
@Override
public String toString() {
ProxySelector selector = ProxySelector.getDefault();
StringBuilder result = new StringBuilder();
for (Proxy proxy : selector.select(destURI)) {
SocketAddress address = proxy.address();
if (address == null) {
result.append("Direct connection\n");
continue;
}
result.append(proxy);
if (address instanceof InetSocketAddress) {
InetSocketAddress ina = (InetSocketAddress) address;
result.append(' ');
result.append(ina.getHostName());
result.append(':');
result.append(ina.getPort());
if (ina.isUnresolved()) {
result.append(" [unresolved]");
} else {
InetAddress addr = ina.getAddress();
result.append(" [");
result.append(addr.getHostAddress());
result.append(']');
}
}
result.append('\n');
}
return result.toString();
}
use of java.net.ProxySelector in project triplea by triplea-game.
the class HttpProxy method getSystemProxy.
private static Optional<InetSocketAddress> getSystemProxy() {
// this property is temporarily needed to turn on proxying
SystemProperties.setJavaNetUseSystemProxies("true");
try {
final ProxySelector def = ProxySelector.getDefault();
if (def != null) {
// TODO: if we switch to HTTPS, we will potentially need an https URL, proxies can very by protocol.
final String anyUrlThatShouldAvailable = "http://sourceforge.net/";
final List<Proxy> proxyList = def.select(new URI(anyUrlThatShouldAvailable));
ProxySelector.setDefault(null);
if (proxyList != null && !proxyList.isEmpty()) {
final Proxy proxy = proxyList.get(0);
final InetSocketAddress address = (InetSocketAddress) proxy.address();
return Optional.ofNullable(address);
}
}
} catch (final Exception e) {
ClientLogger.logQuietly("Failed to get system HTTP proxy", e);
} finally {
SystemProperties.setJavaNetUseSystemProxies("false");
}
return Optional.empty();
}
use of java.net.ProxySelector in project dropwizard by dropwizard.
the class JerseyClientBuilderTest method usesACustomHttpRoutePlanner.
@Test
void usesACustomHttpRoutePlanner() {
final HttpRoutePlanner customHttpRoutePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.53.12", 8080)));
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
});
builder.using(customHttpRoutePlanner);
verify(apacheHttpClientBuilder).using(customHttpRoutePlanner);
}
use of java.net.ProxySelector in project grpc-java by grpc.
the class ProxyDetectorImpl method detectProxy.
private ProxiedSocketAddress detectProxy(InetSocketAddress targetAddr) throws IOException {
URI uri;
String host;
try {
host = GrpcUtil.getHost(targetAddr);
} catch (Throwable t) {
// Workaround for Android API levels < 19 if getHostName causes a NetworkOnMainThreadException
log.log(Level.WARNING, "Failed to get host for proxy lookup, proceeding without proxy", t);
return null;
}
try {
uri = new URI(PROXY_SCHEME, null, /* userInfo */
host, targetAddr.getPort(), null, /* path */
null, /* query */
null);
} catch (final URISyntaxException e) {
log.log(Level.WARNING, "Failed to construct URI for proxy lookup, proceeding without proxy", e);
return null;
}
ProxySelector proxySelector = this.proxySelector.get();
if (proxySelector == null) {
log.log(Level.FINE, "proxy selector is null, so continuing without proxy lookup");
return null;
}
List<Proxy> proxies = proxySelector.select(uri);
if (proxies.size() > 1) {
log.warning("More than 1 proxy detected, gRPC will select the first one");
}
Proxy proxy = proxies.get(0);
if (proxy.type() == Proxy.Type.DIRECT) {
return null;
}
InetSocketAddress proxyAddr = (InetSocketAddress) proxy.address();
// The prompt string should be the realm as returned by the server.
// We don't have it because we are avoiding the full handshake.
String promptString = "";
PasswordAuthentication auth = authenticationProvider.requestPasswordAuthentication(GrpcUtil.getHost(proxyAddr), proxyAddr.getAddress(), proxyAddr.getPort(), PROXY_SCHEME, promptString, null);
final InetSocketAddress resolvedProxyAddr;
if (proxyAddr.isUnresolved()) {
InetAddress resolvedAddress = InetAddress.getByName(proxyAddr.getHostName());
resolvedProxyAddr = new InetSocketAddress(resolvedAddress, proxyAddr.getPort());
} else {
resolvedProxyAddr = proxyAddr;
}
HttpConnectProxiedSocketAddress.Builder builder = HttpConnectProxiedSocketAddress.newBuilder().setTargetAddress(targetAddr).setProxyAddress(resolvedProxyAddr);
if (auth == null) {
return builder.build();
}
return builder.setUsername(auth.getUserName()).setPassword(auth.getPassword() == null ? null : new String(auth.getPassword())).build();
}
Aggregations