use of java.net.Proxy in project robovm by robovm.
the class HttpsURLConnectionTest method testProxyAuthConnection.
/**
* Tests HTTPS connection process made through the proxy server.
* Proxy server needs authentication.
*/
public void testProxyAuthConnection() throws Throwable {
// setting up the properties pointing to the key/trust stores
setUpStoreProperties();
// create the SSLServerSocket which will be used by server side
ServerSocket ss = new ServerSocket(0);
// create the HostnameVerifier to check that Hostname verification
// is done
TestHostnameVerifier hnv = new TestHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
// create HttpsURLConnection to be tested
URL url = new URL("https://requested.host:55555/requested.data");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
connection.setSSLSocketFactory(getContext().getSocketFactory());
// perform the interaction between the peers and check the results
SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
checkConnectionStateParameters(connection, peerSocket);
// should silently exit
connection.connect();
}
use of java.net.Proxy in project robovm by robovm.
the class HttpURLConnectionTest method testProxyAuthorization.
@SideEffect("Suffers from side effect of other, currently unknown test")
public void testProxyAuthorization() throws Exception {
// Set up test Authenticator
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
try {
MockProxyServer proxy = new MockProxyServer("ProxyServer");
URL url = new URL("http://remotehost:55555/requested.data");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxy.port())));
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
proxy.start();
connection.connect();
assertEquals("unexpected response code", 200, connection.getResponseCode());
proxy.join();
assertTrue("Connection did not send proxy authorization request", proxy.acceptedAuthorizedRequest);
} finally {
// remove previously set authenticator
Authenticator.setDefault(null);
}
}
use of java.net.Proxy in project openstack4j by ContainX.
the class ClientFactory method addProxy.
private static void addProxy(ClientConfig cc, Config config) {
if (config.getProxy() != null) {
HttpUrlConnectorProvider cp = new HttpUrlConnectorProvider();
cc.connectorProvider(cp);
final Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(config.getProxy().getRawHost(), config.getProxy().getPort()));
cp.connectionFactory(new ConnectionFactory() {
@Override
public HttpURLConnection getConnection(URL url) throws IOException {
return (HttpURLConnection) url.openConnection(proxy);
}
});
}
}
use of java.net.Proxy in project openstack4j by ContainX.
the class HttpCommand method initialize.
private void initialize() {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
Config config = request.getConfig();
if (config.getProxy() != null) {
okHttpClientBuilder.proxy(new Proxy(Type.HTTP, new InetSocketAddress(config.getProxy().getRawHost(), config.getProxy().getPort())));
}
if (config.getConnectTimeout() > 0)
okHttpClientBuilder.connectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS);
if (config.getReadTimeout() > 0)
okHttpClientBuilder.readTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS);
if (config.isIgnoreSSLVerification()) {
okHttpClientBuilder.hostnameVerifier(UntrustedSSL.getHostnameVerifier());
okHttpClientBuilder.sslSocketFactory(UntrustedSSL.getSSLContext().getSocketFactory());
}
if (config.getSslContext() != null)
okHttpClientBuilder.sslSocketFactory(config.getSslContext().getSocketFactory());
if (config.getHostNameVerifier() != null)
okHttpClientBuilder.hostnameVerifier(config.getHostNameVerifier());
if (HttpLoggingFilter.isLoggingEnabled()) {
okHttpClientBuilder.addInterceptor(new LoggingInterceptor());
}
client = okHttpClientBuilder.build();
clientReq = new Request.Builder();
populateHeaders(request);
populateQueryParams(request);
}
use of java.net.Proxy in project jdk8u_jdk by JetBrains.
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<ProxySelector>() {
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()) {
// fallback to direct connection
http = getNewHttpClient(url, null, connectTimeout, false);
http.setReadTimeout(readTimeout);
break;
}
} 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;
}
Aggregations