use of java.net.ProxySelector in project archi-modelrepository-plugin by archi-contribs.
the class ProxyAuthenticater method update.
/**
* Update the Proxy Authenticater
* Get settings from user prefs
* @throws IOException
*/
public static void update(String repositoryURL) throws IOException {
boolean useProxy = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getBoolean(IPreferenceConstants.PREFS_PROXY_USE);
if (!useProxy) {
Authenticator.setDefault(null);
ProxySelector.setDefault(DEFAULT_PROXY_SELECTOR);
// Test the connection - this is better to do it now
testConnection(repositoryURL, null);
return;
}
boolean useAuthentication = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getBoolean(IPreferenceConstants.PREFS_PROXY_REQUIRES_AUTHENTICATION);
if (useAuthentication) {
// $NON-NLS-1$ //$NON-NLS-2$
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
final SimpleCredentialsStorage sc = new SimpleCredentialsStorage(new File(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), IGraficoConstants.PROXY_CREDENTIALS_FILE));
final String userName = sc.getUsername();
final String password = sc.getPassword();
if (!StringUtils.isSet(userName) || !StringUtils.isSet(password)) {
return;
}
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password.toCharArray());
}
});
} else {
Authenticator.setDefault(null);
}
final String hostName = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_PROXY_HOST);
final int port = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getInt(IPreferenceConstants.PREFS_PROXY_PORT);
if (!StringUtils.isSet(hostName)) {
return;
}
InetAddress addr = InetAddress.getByName(hostName);
if (!addr.isReachable(2000)) {
// $NON-NLS-1$
throw new IOException(Messages.ProxyAuthenticater_0 + " " + hostName);
}
final InetSocketAddress socketAddress = new InetSocketAddress(addr, port);
final Proxy proxy = new Proxy(Type.HTTP, socketAddress);
ProxySelector.setDefault(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
return Arrays.asList(proxy);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
});
// Test the connection with the repository URL
testConnection(repositoryURL, proxy);
}
use of java.net.ProxySelector in project keystore-explorer by kaikramer.
the class ApplicationSettings method getCurrentProxySettings.
private void getCurrentProxySettings(Preferences preferences) {
// Get current proxy settings
ProxySelector proxySelector = ProxySelector.getDefault();
if (proxySelector instanceof NoProxySelector) {
preferences.put(KSE3_PROXY, ProxyConfigurationType.NONE.name());
} else if (proxySelector instanceof SystemProxySelector) {
preferences.put(KSE3_PROXY, ProxyConfigurationType.SYSTEM.name());
} else if (proxySelector instanceof PacProxySelector) {
PacProxySelector pacProxySelector = (PacProxySelector) proxySelector;
preferences.put(KSE3_PACURL, pacProxySelector.getPacUrl());
preferences.put(KSE3_PROXY, ProxyConfigurationType.PAC.name());
} else if (proxySelector instanceof ManualProxySelector) {
ManualProxySelector manualProxySelector = (ManualProxySelector) proxySelector;
ProxyAddress httpProxyAddress = manualProxySelector.getHttpProxyAddress();
if (httpProxyAddress != null) {
preferences.put(KSE3_HTTPHOST, httpProxyAddress.getHost());
preferences.putInt(KSE3_HTTPPORT, httpProxyAddress.getPort());
}
ProxyAddress httpsProxyAddress = manualProxySelector.getHttpsProxyAddress();
if (httpsProxyAddress != null) {
preferences.put(KSE3_HTTPSHOST, httpsProxyAddress.getHost());
preferences.putInt(KSE3_HTTPSPORT, httpsProxyAddress.getPort());
}
ProxyAddress socksProxyAddress = manualProxySelector.getSocksProxyAddress();
if (socksProxyAddress != null) {
preferences.put(KSE3_SOCKSHOST, socksProxyAddress.getHost());
preferences.putInt(KSE3_SOCKSPORT, socksProxyAddress.getPort());
}
preferences.put(KSE3_PROXY, ProxyConfigurationType.MANUAL.name());
}
}
use of java.net.ProxySelector in project Bytecoder by mirkosertic.
the class EmptyInputStream method plainConnect0.
protected void plainConnect0() throws IOException {
// try to see if request can be served from local cache
if (cacheHandler != null && getUseCaches()) {
try {
URI uri = ParseUtil.toURI(url);
if (uri != null) {
cachedResponse = cacheHandler.get(uri, getRequestMethod(), getUserSetHeaders().getHeaders());
if ("https".equalsIgnoreCase(uri.getScheme()) && !(cachedResponse instanceof SecureCacheResponse)) {
cachedResponse = null;
}
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Cache Request for " + uri + " / " + getRequestMethod());
logger.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null"));
}
if (cachedResponse != null) {
cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders());
cachedInputStream = cachedResponse.getBody();
}
}
} catch (IOException ioex) {
// ignore and commence normal connection
}
if (cachedHeaders != null && cachedInputStream != null) {
connected = true;
return;
} else {
cachedResponse = null;
}
}
try {
if (instProxy == null) {
// no instance Proxy is set
/**
* Do we have to use a proxy?
*/
ProxySelector sel = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<>() {
public ProxySelector run() {
return ProxySelector.getDefault();
}
});
if (sel != null) {
URI uri = sun.net.www.ParseUtil.toURI(url);
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("ProxySelector Request for " + uri);
}
Iterator<Proxy> it = sel.select(uri).iterator();
Proxy p;
while (it.hasNext()) {
p = it.next();
try {
if (!failedOnce) {
http = getNewHttpClient(url, p, connectTimeout);
http.setReadTimeout(readTimeout);
} else {
// make sure to construct new connection if first
// attempt failed
http = getNewHttpClient(url, p, connectTimeout, false);
http.setReadTimeout(readTimeout);
}
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
if (p != null) {
logger.finest("Proxy used: " + p.toString());
}
}
break;
} catch (IOException ioex) {
if (p != Proxy.NO_PROXY) {
sel.connectFailed(uri, p.address(), ioex);
if (!it.hasNext()) {
throw ioex;
}
} else {
throw ioex;
}
continue;
}
}
} else {
// No proxy selector, create http client with no proxy
if (!failedOnce) {
http = getNewHttpClient(url, null, connectTimeout);
http.setReadTimeout(readTimeout);
} else {
// make sure to construct new connection if first
// attempt failed
http = getNewHttpClient(url, null, connectTimeout, false);
http.setReadTimeout(readTimeout);
}
}
} else {
if (!failedOnce) {
http = getNewHttpClient(url, instProxy, connectTimeout);
http.setReadTimeout(readTimeout);
} else {
// make sure to construct new connection if first
// attempt failed
http = getNewHttpClient(url, instProxy, connectTimeout, false);
http.setReadTimeout(readTimeout);
}
}
ps = (PrintStream) http.getOutputStream();
} catch (IOException e) {
throw e;
}
// constructor to HTTP client calls openserver
connected = true;
}
use of java.net.ProxySelector in project Bytecoder by mirkosertic.
the class FtpURLConnection method connect.
/**
* Connects to the FTP server and logs in.
*
* @throws FtpLoginException if the login is unsuccessful
* @throws FtpProtocolException if an error occurs
* @throws UnknownHostException if trying to connect to an unknown host
*/
public synchronized void connect() throws IOException {
if (connected) {
return;
}
Proxy p = null;
if (instProxy == null) {
// no per connection proxy specified
/**
* Do we have to use a proxy?
*/
ProxySelector sel = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<ProxySelector>() {
public ProxySelector run() {
return ProxySelector.getDefault();
}
});
if (sel != null) {
URI uri = sun.net.www.ParseUtil.toURI(url);
Iterator<Proxy> it = sel.select(uri).iterator();
while (it.hasNext()) {
p = it.next();
if (p == null || p == Proxy.NO_PROXY || p.type() == Proxy.Type.SOCKS) {
break;
}
if (p.type() != Proxy.Type.HTTP || !(p.address() instanceof InetSocketAddress)) {
sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type"));
continue;
}
// OK, we have an http proxy
InetSocketAddress paddr = (InetSocketAddress) p.address();
try {
http = new HttpURLConnection(url, p);
http.setDoInput(getDoInput());
http.setDoOutput(getDoOutput());
if (connectTimeout >= 0) {
http.setConnectTimeout(connectTimeout);
}
if (readTimeout >= 0) {
http.setReadTimeout(readTimeout);
}
http.connect();
connected = true;
return;
} catch (IOException ioe) {
sel.connectFailed(uri, paddr, ioe);
http = null;
}
}
}
} else {
// per connection proxy specified
p = instProxy;
if (p.type() == Proxy.Type.HTTP) {
http = new HttpURLConnection(url, instProxy);
http.setDoInput(getDoInput());
http.setDoOutput(getDoOutput());
if (connectTimeout >= 0) {
http.setConnectTimeout(connectTimeout);
}
if (readTimeout >= 0) {
http.setReadTimeout(readTimeout);
}
http.connect();
connected = true;
return;
}
}
if (user == null) {
user = "anonymous";
Properties props = GetPropertyAction.privilegedGetProperties();
String vers = props.getProperty("java.version");
password = props.getProperty("ftp.protocol.user", "Java" + vers + "@");
}
try {
ftp = FtpClient.create();
if (p != null) {
ftp.setProxy(p);
}
setTimeouts();
if (port != -1) {
ftp.connect(new InetSocketAddress(host, port));
} else {
ftp.connect(new InetSocketAddress(host, FtpClient.defaultPort()));
}
} catch (UnknownHostException e) {
// Just keep throwing for now.
throw e;
} catch (FtpProtocolException fe) {
if (ftp != null) {
try {
ftp.close();
} catch (IOException ioe) {
fe.addSuppressed(ioe);
}
}
throw new IOException(fe);
}
try {
ftp.login(user, password == null ? null : password.toCharArray());
} catch (sun.net.ftp.FtpProtocolException e) {
ftp.close();
// Backward compatibility
throw new sun.net.ftp.FtpLoginException("Invalid username/password");
}
connected = true;
}
use of java.net.ProxySelector in project keystore-explorer by kaikramer.
the class DPreferences method initInternetProxyTab.
private void initInternetProxyTab() {
jrbNoProxy = new JRadioButton(res.getString("DPreferences.jrbNoProxy.text"));
jrbNoProxy.setToolTipText(res.getString("DPreferences.jrbNoProxy.tooltip"));
PlatformUtil.setMnemonic(jrbNoProxy, res.getString("DPreferences.jrbNoProxy.mnemonic").charAt(0));
jrbSystemProxySettings = new JRadioButton(res.getString("DPreferences.jrbSystemProxySettings.text"), true);
jrbSystemProxySettings.setToolTipText(res.getString("DPreferences.jrbSystemProxySettings.tooltip"));
PlatformUtil.setMnemonic(jrbSystemProxySettings, res.getString("DPreferences.jrbSystemProxySettings.mnemonic").charAt(0));
jrbManualProxyConfig = new JRadioButton(res.getString("DPreferences.jrbManualProxyConfig.text"));
jrbManualProxyConfig.setToolTipText(res.getString("DPreferences.jrbManualProxyConfig.tooltip"));
PlatformUtil.setMnemonic(jrbManualProxyConfig, res.getString("DPreferences.jrbManualProxyConfig.mnemonic").charAt(0));
jlHttpHost = new JLabel(res.getString("DPreferences.jlHttpHost.text"));
jtfHttpHost = new JTextField(20);
jtfHttpHost.setToolTipText(res.getString("DPreferences.jtfHttpHost.tooltip"));
jtfHttpHost.setEnabled(false);
jlHttpPort = new JLabel(res.getString("DPreferences.jlHttpPort.text"));
jtfHttpPort = new JTextField(5);
jtfHttpPort.setToolTipText(res.getString("DPreferences.jtfHttpPort.tooltip"));
jtfHttpPort.setEnabled(false);
jlHttpsHost = new JLabel(res.getString("DPreferences.jlHttpsHost.text"));
jtfHttpsHost = new JTextField(20);
jtfHttpsHost.setToolTipText(res.getString("DPreferences.jtfHttpsHost.tooltip"));
jtfHttpsHost.setEnabled(false);
jlHttpsPort = new JLabel(res.getString("DPreferences.jlHttpsPort.text"));
jtfHttpsPort = new JTextField(5);
jtfHttpsPort.setToolTipText(res.getString("DPreferences.jtfHttpsPort.tooltip"));
jtfHttpsPort.setEnabled(false);
jlSocksHost = new JLabel(res.getString("DPreferences.jlSocksHost.text"));
jtfSocksHost = new JTextField(20);
jtfSocksHost.setToolTipText(res.getString("DPreferences.jtfSocksHost.tooltip"));
jtfSocksHost.setEnabled(false);
jlSocksPort = new JLabel(res.getString("DPreferences.jlSocksPort.text"));
jtfSocksPort = new JTextField(5);
jtfSocksPort.setToolTipText(res.getString("DPreferences.jtfSocksPort.tooltip"));
jtfSocksPort.setEnabled(false);
jrbAutomaticProxyConfig = new JRadioButton(res.getString("DPreferences.jrbAutomaticProxyConfig.text"));
jrbAutomaticProxyConfig.setToolTipText(res.getString("DPreferences.jrbAutomaticProxyConfig.tooltip"));
PlatformUtil.setMnemonic(jrbAutomaticProxyConfig, res.getString("DPreferences.jrbAutomaticProxyConfig.mnemonic").charAt(0));
jlPacUrl = new JLabel(res.getString("DPreferences.jlPacUrl.text"));
jtfPacUrl = new JTextField(30);
jtfPacUrl.setToolTipText(res.getString("DPreferences.jtfPacUrl.tooltip"));
jtfPacUrl.setEnabled(false);
ButtonGroup bgProxies = new ButtonGroup();
bgProxies.add(jrbNoProxy);
bgProxies.add(jrbSystemProxySettings);
bgProxies.add(jrbManualProxyConfig);
bgProxies.add(jrbAutomaticProxyConfig);
// layout
jpInternetProxy = new JPanel();
jpInternetProxy.setLayout(new MigLayout("insets dialog", "[20][]", ""));
jpInternetProxy.add(jrbNoProxy, "left, span, wrap");
jpInternetProxy.add(jrbSystemProxySettings, "left, span, wrap");
jpInternetProxy.add(jrbManualProxyConfig, "left, span, wrap");
jpInternetProxy.add(jlHttpHost, "skip, right");
jpInternetProxy.add(jtfHttpHost, "");
jpInternetProxy.add(jlHttpPort, "gap unrel, right");
jpInternetProxy.add(jtfHttpPort, "wrap");
jpInternetProxy.add(jlHttpsHost, "skip, right");
jpInternetProxy.add(jtfHttpsHost, "");
jpInternetProxy.add(jlHttpsPort, "gap unrel, right");
jpInternetProxy.add(jtfHttpsPort, "wrap");
jpInternetProxy.add(jlSocksHost, "skip, right");
jpInternetProxy.add(jtfSocksHost, "");
jpInternetProxy.add(jlSocksPort, "gap unrel, right");
jpInternetProxy.add(jtfSocksPort, "wrap");
jpInternetProxy.add(jrbAutomaticProxyConfig, "left, span, wrap");
jpInternetProxy.add(jlPacUrl, "skip, right");
jpInternetProxy.add(jtfPacUrl, "span, wrap push");
jrbAutomaticProxyConfig.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
updateProxyControls();
}
});
jrbManualProxyConfig.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
updateProxyControls();
}
});
ProxySelector proxySelector = ProxySelector.getDefault();
if (proxySelector instanceof SystemProxySelector) {
jrbSystemProxySettings.setSelected(true);
} else if (proxySelector instanceof PacProxySelector) {
jrbAutomaticProxyConfig.setSelected(true);
PacProxySelector pacProxySelector = (PacProxySelector) proxySelector;
jtfPacUrl.setText(pacProxySelector.getPacUrl());
} else if (proxySelector instanceof ManualProxySelector) {
jrbManualProxyConfig.setSelected(true);
ManualProxySelector manualProxySelector = (ManualProxySelector) proxySelector;
ProxyAddress httpProxy = manualProxySelector.getHttpProxyAddress();
ProxyAddress httpsProxy = manualProxySelector.getHttpsProxyAddress();
ProxyAddress socksProxy = manualProxySelector.getSocksProxyAddress();
if (httpProxy != null) {
jtfHttpHost.setText(httpProxy.getHost());
jtfHttpHost.setCaretPosition(0);
jtfHttpPort.setText("" + httpProxy.getPort());
jtfHttpPort.setCaretPosition(0);
}
if (httpsProxy != null) {
jtfHttpsHost.setText(httpsProxy.getHost());
jtfHttpsHost.setCaretPosition(0);
jtfHttpsPort.setText("" + httpsProxy.getPort());
jtfHttpsPort.setCaretPosition(0);
}
if (socksProxy != null) {
jtfSocksHost.setText(socksProxy.getHost());
jtfSocksHost.setCaretPosition(0);
jtfSocksPort.setText("" + socksProxy.getPort());
jtfSocksPort.setCaretPosition(0);
}
} else {
jrbNoProxy.setSelected(true);
}
}
Aggregations