public static HttpClient createConnection()

in commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpClientFactory.java [58:133]


    public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme,
            final String hostname, final int port, final String username, final String password,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
        final HttpClient client;
        try {
            final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
            final HttpConnectionManagerParams connectionMgrParams = mgr.getParams();

            client = new HttpClient(mgr);

            final HostConfiguration config = new HostConfiguration();
            config.setHost(hostname, port, scheme);

            if (fileSystemOptions != null) {
                final String proxyHost = builder.getProxyHost(fileSystemOptions);
                final int proxyPort = builder.getProxyPort(fileSystemOptions);

                if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) {
                    config.setProxy(proxyHost, proxyPort);
                }

                final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
                if (proxyAuth != null) {
                    final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth,
                        new UserAuthenticationData.Type[] {UserAuthenticationData.USERNAME,
                            UserAuthenticationData.PASSWORD});

                    if (authData != null) {
                        final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(
                            UserAuthenticatorUtils.toString(
                                UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)),
                            UserAuthenticatorUtils.toString(
                                UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));

                        final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
                        client.getState().setProxyCredentials(scope, proxyCreds);
                    }

                    if (builder.isPreemptiveAuth(fileSystemOptions)) {
                        final HttpClientParams httpClientParams = new HttpClientParams();
                        httpClientParams.setAuthenticationPreemptive(true);
                        client.setParams(httpClientParams);
                    }
                }

                final Cookie[] cookies = builder.getCookies(fileSystemOptions);
                if (cookies != null) {
                    client.getState().addCookies(cookies);
                }
            }
            /*
             * ConnectionManager set methods must be called after the host & port and proxy host & port are set in the
             * HostConfiguration. They are all used as part of the key when HttpConnectionManagerParams tries to locate
             * the host configuration.
             */
            connectionMgrParams.setMaxConnectionsPerHost(config, builder.getMaxConnectionsPerHost(fileSystemOptions));
            connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));

            connectionMgrParams.setConnectionTimeout(
                DurationUtils.toMillisInt(builder.getConnectionTimeoutDuration(fileSystemOptions)));
            connectionMgrParams
                .setSoTimeout(DurationUtils.toMillisInt(builder.getSoTimeoutDuration(fileSystemOptions)));

            client.setHostConfiguration(config);

            if (username != null) {
                final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
                final AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
                client.getState().setCredentials(scope, creds);
            }
        } catch (final Exception exc) {
            throw new FileSystemException("vfs.provider.http/connect.error", exc, hostname);
        }

        return client;
    }