Search in sources :

Example 96 with Proxy

use of java.net.Proxy in project robovm by robovm.

the class OldProxyTest method test_hashCode.

public void test_hashCode() {
    SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
    Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1);
    Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address1);
    assertTrue(proxy1.hashCode() == proxy2.hashCode());
    new Proxy(Proxy.Type.SOCKS, address1);
    Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address1);
    assertTrue(proxy1.hashCode() == proxy2.hashCode());
    assertTrue(proxy1.hashCode() != proxy4.hashCode());
    SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1235);
    Proxy proxy5 = new Proxy(Proxy.Type.SOCKS, address1);
    Proxy proxy6 = new Proxy(Proxy.Type.SOCKS, address2);
    assertTrue(proxy5.hashCode() != proxy6.hashCode());
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 97 with Proxy

use of java.net.Proxy in project robovm by robovm.

the class HttpURLConnectionTest method testUsingProxy.

/**
     * Test checks if the proxy specified in openConnection
     * method will be used for connection to the server
     */
public void testUsingProxy() throws Exception {
    // Regression for HARMONY-570
    MockServer server = new MockServer("server");
    MockServer proxy = new MockServer("proxy");
    URL url = new URL("http://localhost:" + server.port());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxy.port())));
    connection.setConnectTimeout(2000);
    connection.setReadTimeout(2000);
    server.start();
    synchronized (bound) {
        if (!server.started)
            bound.wait(5000);
    }
    proxy.start();
    synchronized (bound) {
        if (!proxy.started)
            bound.wait(5000);
    }
    connection.connect();
    // wait while server and proxy run
    server.join();
    proxy.join();
    assertTrue("Connection does not use proxy", connection.usingProxy());
    assertTrue("Proxy server was not used", proxy.accepted);
    HttpURLConnection huc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
    assertFalse(huc.usingProxy());
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL)

Example 98 with Proxy

use of java.net.Proxy in project robovm by robovm.

the class HttpsURLConnectionTest method testProxyAuthConnectionFailed.

/**
     * Tests HTTPS connection process made through the proxy server.
     * Proxy server needs authentication but client fails to authenticate
     * (Authenticator was not set up in the system).
     */
public void testProxyAuthConnectionFailed() 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);
    // 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
    try {
        doInteraction(connection, ss, AUTHENTICATION_REQUIRED_CODE, true);
    } catch (IOException e) {
        // SSL Tunnelling failed
        if (DO_LOG) {
            System.out.println("Got expected IOException: " + e.getMessage());
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 99 with Proxy

use of java.net.Proxy in project robovm by robovm.

the class HttpsURLConnectionTest method testProxyConnection_Not_Found_Response.

/**
     * Tests the behaviour of HTTPS connection in case of unavailability
     * of requested resource.
     */
public void testProxyConnection_Not_Found_Response() 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);
    // create HttpsURLConnection to be tested
    URL url = new URL("https://localhost:" + ss.getLocalPort());
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    try {
        // NOT FOUND
        doInteraction(connection, ss, NOT_FOUND_CODE);
        fail("Expected exception was not thrown.");
    } catch (FileNotFoundException e) {
        if (DO_LOG) {
            System.out.println("Expected exception was thrown: " + e.getMessage());
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) FileNotFoundException(java.io.FileNotFoundException) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 100 with Proxy

use of java.net.Proxy in project okhttp by square.

the class JavaNetAuthenticator method authenticate.

@Override
public Request authenticate(Route route, Response response) throws IOException {
    List<Challenge> challenges = response.challenges();
    Request request = response.request();
    HttpUrl url = request.url();
    boolean proxyAuthorization = response.code() == 407;
    Proxy proxy = route.proxy();
    for (int i = 0, size = challenges.size(); i < size; i++) {
        Challenge challenge = challenges.get(i);
        if (!"Basic".equalsIgnoreCase(challenge.scheme()))
            continue;
        PasswordAuthentication auth;
        if (proxyAuthorization) {
            InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
            auth = java.net.Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.PROXY);
        } else {
            auth = java.net.Authenticator.requestPasswordAuthentication(url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.SERVER);
        }
        if (auth != null) {
            String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword()));
            return request.newBuilder().header(proxyAuthorization ? "Proxy-Authorization" : "Authorization", credential).build();
        }
    }
    // No challenges were satisfied!
    return null;
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Proxy (java.net.Proxy)132 InetSocketAddress (java.net.InetSocketAddress)71 URL (java.net.URL)44 IOException (java.io.IOException)35 Test (org.junit.Test)22 URI (java.net.URI)21 ProxySelector (java.net.ProxySelector)20 HttpURLConnection (java.net.HttpURLConnection)19 SocketAddress (java.net.SocketAddress)19 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)13 URISyntaxException (java.net.URISyntaxException)11 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 InterruptedIOException (java.io.InterruptedIOException)6 URLConnection (java.net.URLConnection)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 List (java.util.List)5 SSLSocket (javax.net.ssl.SSLSocket)5 OkHttpClient (okhttp3.OkHttpClient)5