Search in sources :

Example 1 with SendingRequestEvent

use of com.microsoft.azure.storage.SendingRequestEvent in project cyberduck by iterate-ch.

the class AzureSession method connect.

@Override
protected CloudBlobClient connect(final Proxy proxy, final HostKeyCallback callback, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
    try {
        final StorageCredentials credentials;
        if (host.getCredentials().isTokenAuthentication()) {
            credentials = new StorageCredentialsSharedAccessSignature(host.getCredentials().getToken());
        } else {
            credentials = new StorageCredentialsAccountAndKey(host.getCredentials().getUsername(), "null");
        }
        // Client configured with no credentials
        final URI uri = new URI(String.format("%s://%s", Scheme.https, host.getHostname()));
        final CloudBlobClient client = new CloudBlobClient(uri, credentials);
        client.setDirectoryDelimiter(String.valueOf(Path.DELIMITER));
        context.setLoggingEnabled(true);
        context.setLogger(LoggerFactory.getLogger(log.getName()));
        context.setUserHeaders(new HashMap<>(Collections.singletonMap(HttpHeaders.USER_AGENT, new PreferencesUseragentProvider().get())));
        context.getSendingRequestEventHandler().addListener(listener = new StorageEvent<SendingRequestEvent>() {

            @Override
            public void eventOccurred(final SendingRequestEvent event) {
                if (event.getConnectionObject() instanceof HttpsURLConnection) {
                    final HttpsURLConnection connection = (HttpsURLConnection) event.getConnectionObject();
                    connection.setSSLSocketFactory(new CustomTrustSSLProtocolSocketFactory(trust, key));
                    connection.setHostnameVerifier(new DisabledX509HostnameVerifier());
                }
            }
        });
        switch(proxy.getType()) {
            case SOCKS:
                {
                    if (log.isInfoEnabled()) {
                        log.info(String.format("Configured to use SOCKS proxy %s", proxy));
                    }
                    final java.net.Proxy socksProxy = new java.net.Proxy(java.net.Proxy.Type.SOCKS, new InetSocketAddress(proxy.getHostname(), proxy.getPort()));
                    context.setProxy(socksProxy);
                    break;
                }
            case HTTP:
            case HTTPS:
                {
                    if (log.isInfoEnabled()) {
                        log.info(String.format("Configured to use HTTP proxy %s", proxy));
                    }
                    final java.net.Proxy httpProxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostname(), proxy.getPort()));
                    context.setProxy(httpProxy);
                    break;
                }
        }
        return client;
    } catch (URISyntaxException e) {
        throw new LoginFailureException(e.getMessage(), e);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) StorageEvent(com.microsoft.azure.storage.StorageEvent) InetSocketAddress(java.net.InetSocketAddress) PreferencesUseragentProvider(ch.cyberduck.core.PreferencesUseragentProvider) URISyntaxException(java.net.URISyntaxException) StorageCredentialsAccountAndKey(com.microsoft.azure.storage.StorageCredentialsAccountAndKey) URI(java.net.URI) StorageCredentialsSharedAccessSignature(com.microsoft.azure.storage.StorageCredentialsSharedAccessSignature) StorageCredentials(com.microsoft.azure.storage.StorageCredentials) Proxy(ch.cyberduck.core.proxy.Proxy) LoginFailureException(ch.cyberduck.core.exception.LoginFailureException) SendingRequestEvent(com.microsoft.azure.storage.SendingRequestEvent) CustomTrustSSLProtocolSocketFactory(ch.cyberduck.core.ssl.CustomTrustSSLProtocolSocketFactory) DisabledX509HostnameVerifier(ch.cyberduck.core.http.DisabledX509HostnameVerifier) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

PreferencesUseragentProvider (ch.cyberduck.core.PreferencesUseragentProvider)1 LoginFailureException (ch.cyberduck.core.exception.LoginFailureException)1 DisabledX509HostnameVerifier (ch.cyberduck.core.http.DisabledX509HostnameVerifier)1 Proxy (ch.cyberduck.core.proxy.Proxy)1 CustomTrustSSLProtocolSocketFactory (ch.cyberduck.core.ssl.CustomTrustSSLProtocolSocketFactory)1 SendingRequestEvent (com.microsoft.azure.storage.SendingRequestEvent)1 StorageCredentials (com.microsoft.azure.storage.StorageCredentials)1 StorageCredentialsAccountAndKey (com.microsoft.azure.storage.StorageCredentialsAccountAndKey)1 StorageCredentialsSharedAccessSignature (com.microsoft.azure.storage.StorageCredentialsSharedAccessSignature)1 StorageEvent (com.microsoft.azure.storage.StorageEvent)1 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1