Search in sources :

Example 1 with HttpURLConnection

use of sun.net.www.protocol.http.HttpURLConnection in project jdk8u_jdk by JetBrains.

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";
        String vers = java.security.AccessController.doPrivileged(new GetPropertyAction("java.version"));
        password = java.security.AccessController.doPrivileged(new GetPropertyAction("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) {
        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;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) FtpProtocolException(sun.net.ftp.FtpProtocolException) URI(java.net.URI) ProxySelector(java.net.ProxySelector) FtpProtocolException(sun.net.ftp.FtpProtocolException) Proxy(java.net.Proxy) GetPropertyAction(sun.security.action.GetPropertyAction) HttpURLConnection(sun.net.www.protocol.http.HttpURLConnection)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 ProxySelector (java.net.ProxySelector)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1 FtpProtocolException (sun.net.ftp.FtpProtocolException)1 HttpURLConnection (sun.net.www.protocol.http.HttpURLConnection)1 GetPropertyAction (sun.security.action.GetPropertyAction)1