Search in sources :

Example 56 with Proxy

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();
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Authenticator(java.net.Authenticator) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 57 with Proxy

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);
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InetSocketAddress(java.net.InetSocketAddress) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) SideEffect(dalvik.annotation.SideEffect)

Example 58 with Proxy

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);
            }
        });
    }
}
Also used : Proxy(java.net.Proxy) ConnectionFactory(org.glassfish.jersey.client.HttpUrlConnectorProvider.ConnectionFactory) HttpURLConnection(java.net.HttpURLConnection) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) URL(java.net.URL)

Example 59 with 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);
}
Also used : Proxy(java.net.Proxy) OkHttpClient(okhttp3.OkHttpClient) Config(org.openstack4j.core.transport.Config) InetSocketAddress(java.net.InetSocketAddress) HttpRequest(org.openstack4j.core.transport.HttpRequest) Request(okhttp3.Request)

Example 60 with Proxy

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;
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) SecureCacheResponse(java.net.SecureCacheResponse) URI(java.net.URI)

Aggregations

Proxy (java.net.Proxy)146 InetSocketAddress (java.net.InetSocketAddress)78 URL (java.net.URL)51 IOException (java.io.IOException)38 URI (java.net.URI)23 Test (org.junit.Test)22 HttpURLConnection (java.net.HttpURLConnection)21 ProxySelector (java.net.ProxySelector)21 SocketAddress (java.net.SocketAddress)20 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)14 URISyntaxException (java.net.URISyntaxException)12 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 URLConnection (java.net.URLConnection)7 HttpHost (org.apache.http.HttpHost)7 InterruptedIOException (java.io.InterruptedIOException)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 InputStream (java.io.InputStream)5 List (java.util.List)5