Search in sources :

Example 1 with HttpFileSystemConfigBuilder

use of org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder in project artisynth_core by artisynth.

the class FileCacher method setDefaultFsOptions.

public static void setDefaultFsOptions(FileSystemOptions opts) throws FileSystemException {
    // SSH Defaults
    // Don't check host key
    // Use paths relative to root (as opposed to the user's home dir)
    // 10 second timeout
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
    /**
     * Allow connection to silly UBC servers who don't update their credentials
     */
    TrustStrategy[] ts = { new UnsafeTrustStrategy() };
    HttpFileSystemConfigBuilder httpBuilder = HttpFileSystemConfigBuilder.getInstance();
    WebdavFileSystemConfigBuilder webdavBuilder = WebdavFileSystemConfigBuilder.getInstance();
    // allow all SSL connections
    httpBuilder.setTrustStrategies(opts, ts);
    webdavBuilder.setTrustStrategies(opts, ts);
    // silly deprecated UBC cipher suite
    String[] ciphers = httpBuilder.getDefaultSSLCipherSuites();
    ciphers = Arrays.copyOf(ciphers, ciphers.length + 1);
    ciphers[ciphers.length - 1] = "SSL_RSA_WITH_RC4_128_SHA";
    httpBuilder.setEnabledSSLCipherSuites(opts, ciphers);
    webdavBuilder.setEnabledSSLCipherSuites(opts, ciphers);
}
Also used : TrustStrategy(org.apache.http.ssl.TrustStrategy) HttpFileSystemConfigBuilder(org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder) WebdavFileSystemConfigBuilder(org.apache.commons.vfs2.provider.webdav.WebdavFileSystemConfigBuilder)

Example 2 with HttpFileSystemConfigBuilder

use of org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder in project commons-vfs by apache.

the class HttpClientFactory method createConnection.

/**
 * Creates a new connection to the server.
 *
 * @param builder The HttpFileSystemConfigBuilder.
 * @param scheme The protocol.
 * @param hostname The hostname.
 * @param port The port number.
 * @param username The username.
 * @param password The password
 * @param fileSystemOptions The file system options.
 * @return a new HttpClient connection.
 * @throws FileSystemException if an error occurs.
 * @since 2.0
 */
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;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) UserAuthenticator(org.apache.commons.vfs2.UserAuthenticator) FileSystemException(org.apache.commons.vfs2.FileSystemException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) FileSystemException(org.apache.commons.vfs2.FileSystemException) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) AuthScope(org.apache.commons.httpclient.auth.AuthScope) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Example 3 with HttpFileSystemConfigBuilder

use of org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder in project commons-vfs by apache.

the class HttpsGetContentInfoFunctionalTest method getOptionsWithProxy.

FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
    // get proxy host and port from env var "https_proxy"
    String proxyHost = null;
    int proxyPort = -1;
    final String proxyUrl = System.getenv("https_proxy");
    if (proxyUrl != null) {
        final URL url = new URL(proxyUrl);
        proxyHost = url.getHost();
        proxyPort = url.getPort();
    }
    // return null if proxy host or port invalid
    if (proxyHost == null || proxyPort == -1) {
        return null;
    }
    // return options with proxy
    final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
    final FileSystemOptions opts = new FileSystemOptions();
    builder.setProxyHost(opts, proxyHost);
    builder.setProxyPort(opts, proxyPort);
    return opts;
}
Also used : HttpFileSystemConfigBuilder(org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder) URL(java.net.URL) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 4 with HttpFileSystemConfigBuilder

use of org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder in project commons-vfs by apache.

the class HttpGetContentInfoFunctionalTest method getOptionsWithProxy.

FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
    // get proxy host and port from env var "https_proxy"
    String proxyHost = null;
    int proxyPort = -1;
    final String proxyUrl = System.getenv("https_proxy");
    if (proxyUrl != null) {
        final URL url = new URL(proxyUrl);
        proxyHost = url.getHost();
        proxyPort = url.getPort();
    }
    // return null if proxy host or port invalid
    if (proxyHost == null || proxyPort == -1) {
        return null;
    }
    // return options with proxy
    final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
    final FileSystemOptions opts = new FileSystemOptions();
    builder.setProxyHost(opts, proxyHost);
    builder.setProxyPort(opts, proxyPort);
    return opts;
}
Also used : URL(java.net.URL) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 5 with HttpFileSystemConfigBuilder

use of org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder in project commons-vfs by apache.

the class HttpProviderTestCase method testHttpTimeoutConfig.

/**
 * Ensure VFS-453 options are present.
 */
@Test
public void testHttpTimeoutConfig() {
    final FileSystemOptions options = new FileSystemOptions();
    final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
    // ensure defaults are 0
    assertEquals(0, builder.getConnectionTimeout(options));
    assertEquals(0, builder.getConnectionTimeoutDuration(options).toMillis());
    assertEquals(0, builder.getSoTimeout(options));
    assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(options));
    // Set with deprecated milliseconds APIs.
    builder.setConnectionTimeout(options, 60000);
    builder.setSoTimeout(options, 60000);
    builder.setUserAgent(options, "foo/bar");
    // ensure changes are visible
    assertEquals(60000, builder.getConnectionTimeout(options));
    assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
    assertEquals(60000, builder.getSoTimeout(options));
    assertEquals("foo/bar", builder.getUserAgent(options));
    // Set with Duration APIs.
    builder.setConnectionTimeout(options, ONE_MINUTE);
    builder.setSoTimeout(options, ONE_MINUTE);
    // ensure changes are visible
    assertEquals(60000, builder.getConnectionTimeout(options));
    assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
    assertEquals(60000, builder.getSoTimeout(options));
    assertEquals(ONE_MINUTE, builder.getSoTimeoutDuration(options));
    assertEquals("foo/bar", builder.getUserAgent(options));
// TODO: should also check the created HTTPClient
}
Also used : FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Aggregations

FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)3 URL (java.net.URL)2 HttpFileSystemConfigBuilder (org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder)2 Cookie (org.apache.commons.httpclient.Cookie)1 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpConnectionManager (org.apache.commons.httpclient.HttpConnectionManager)1 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 AuthScope (org.apache.commons.httpclient.auth.AuthScope)1 HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)1 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 UserAuthenticationData (org.apache.commons.vfs2.UserAuthenticationData)1 UserAuthenticator (org.apache.commons.vfs2.UserAuthenticator)1 WebdavFileSystemConfigBuilder (org.apache.commons.vfs2.provider.webdav.WebdavFileSystemConfigBuilder)1 TrustStrategy (org.apache.http.ssl.TrustStrategy)1 Test (org.junit.Test)1