Search in sources :

Example 11 with Proxy

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

the class FtpURLConnection method connect.

/**
     * Establishes the connection to the resource specified by this
     * <code>URL</code>
     *
     * @see #connected
     * @see java.io.IOException
     * @see URLStreamHandler
     */
@Override
public void connect() throws IOException {
    // Use system-wide ProxySelect to select proxy list,
    // then try to connect via elements in the proxy list.
    List<Proxy> proxyList = null;
    if (proxy != null) {
        proxyList = new ArrayList<Proxy>(1);
        proxyList.add(proxy);
    } else {
        ProxySelector selector = ProxySelector.getDefault();
        if (selector != null) {
            proxyList = selector.select(uri);
        }
    }
    if (proxyList == null) {
        currentProxy = null;
        connectInternal();
    } else {
        ProxySelector selector = ProxySelector.getDefault();
        Iterator<Proxy> iter = proxyList.iterator();
        boolean connectOK = false;
        String failureReason = "";
        while (iter.hasNext() && !connectOK) {
            currentProxy = iter.next();
            try {
                connectInternal();
                connectOK = true;
            } catch (IOException ioe) {
                failureReason = ioe.getLocalizedMessage();
                // should be invoked.
                if (selector != null && Proxy.NO_PROXY != currentProxy) {
                    selector.connectFailed(uri, currentProxy.address(), ioe);
                }
            }
        }
        if (!connectOK) {
            throw new IOException("Unable to connect to server: " + failureReason);
        }
    }
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 12 with Proxy

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

the class OldSocketTest method test_ConstructorLjava_net_Proxy_Exception.

public void test_ConstructorLjava_net_Proxy_Exception() {
    SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1", 80);
    SocketAddress addr2 = new InetSocketAddress("localhost", 80);
    Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
    // IllegalArgumentException test
    try {
        new Socket(proxy1);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1);
    // should not throw any exception
    new Socket(proxy2);
    new Socket(Proxy.NO_PROXY);
    try {
        new Socket((Proxy) null);
        fail("IllegalArgumentException was not thrown.");
    } catch (IllegalArgumentException iae) {
    //expected
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 13 with Proxy

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

the class OldProxyTest method test_address.

public void test_address() {
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, address);
    assertEquals(address, proxy.address());
    try {
        new Proxy(Proxy.Type.SOCKS, null);
        fail("IllegalArgumentException was thrown.");
    } catch (IllegalArgumentException iae) {
    //expected
    }
}
Also used : Proxy(java.net.Proxy)

Example 14 with Proxy

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

the class OldProxyTest method test_type.

public void test_type() {
    Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
    assertEquals(Proxy.Type.HTTP, proxy.type());
    proxy = new Proxy(Proxy.Type.SOCKS, address);
    assertEquals(Proxy.Type.SOCKS, proxy.type());
    proxy = Proxy.NO_PROXY;
    assertEquals(Proxy.Type.DIRECT, proxy.type());
}
Also used : Proxy(java.net.Proxy)

Example 15 with Proxy

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

the class HttpsURLConnectionTest method testProxyConnection.

/**
     * Tests HTTPS connection process made through the proxy server.
     */
public void testProxyConnection() 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:55556/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) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

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