Example usage for com.amazonaws ClientConfiguration ClientConfiguration

List of usage examples for com.amazonaws ClientConfiguration ClientConfiguration

Introduction

In this page you can find the example usage for com.amazonaws ClientConfiguration ClientConfiguration.

Prototype

public ClientConfiguration() 

Source Link

Usage

From source file:org.elasticsearch.cloud.aws.AwsEc2Service.java

License:Apache License

public synchronized AmazonEC2 client() {
    if (client != null) {
        return client;
    }//from ww  w  .j  a v a  2s  . c o m

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    String protocol = componentSettings.get("protocol", "http").toLowerCase();
    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new ElasticsearchIllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }
    String account = componentSettings.get("access_key", settings.get("cloud.account"));
    String key = componentSettings.get("secret_key", settings.get("cloud.key"));

    String proxyHost = componentSettings.get("proxy_host");
    if (proxyHost != null) {
        String portString = componentSettings.get("proxy_port", "80");
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new ElasticsearchIllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }

    this.client = new AmazonEC2Client(credentials, clientConfiguration);

    if (componentSettings.get("ec2.endpoint") != null) {
        String endpoint = componentSettings.get("ec2.endpoint");
        logger.debug("using explicit ec2 endpoint [{}]", endpoint);
        client.setEndpoint(endpoint);
    } else if (componentSettings.get("region") != null) {
        String region = componentSettings.get("region").toLowerCase();
        String endpoint;
        if (region.equals("us-east-1") || region.equals("us-east")) {
            endpoint = "ec2.us-east-1.amazonaws.com";
        } else if (region.equals("us-west") || region.equals("us-west-1")) {
            endpoint = "ec2.us-west-1.amazonaws.com";
        } else if (region.equals("us-west-2")) {
            endpoint = "ec2.us-west-2.amazonaws.com";
        } else if (region.equals("ap-southeast") || region.equals("ap-southeast-1")) {
            endpoint = "ec2.ap-southeast-1.amazonaws.com";
        } else if (region.equals("ap-southeast-2")) {
            endpoint = "ec2.ap-southeast-2.amazonaws.com";
        } else if (region.equals("ap-northeast") || region.equals("ap-northeast-1")) {
            endpoint = "ec2.ap-northeast-1.amazonaws.com";
        } else if (region.equals("eu-west") || region.equals("eu-west-1")) {
            endpoint = "ec2.eu-west-1.amazonaws.com";
        } else if (region.equals("sa-east") || region.equals("sa-east-1")) {
            endpoint = "ec2.sa-east-1.amazonaws.com";
        } else {
            throw new ElasticsearchIllegalArgumentException(
                    "No automatic endpoint could be derived from region [" + region + "]");
        }
        if (endpoint != null) {
            logger.debug("using ec2 region [{}], with endpoint [{}]", region, endpoint);
            client.setEndpoint(endpoint);
        }
    }

    return this.client;

}

From source file:org.elasticsearch.cloud.aws.AwsEc2ServiceImpl.java

License:Apache License

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(CLOUD_EC2.PROTOCOL_SETTING.get(settings));

    if (PROXY_HOST_SETTING.exists(settings) || CLOUD_EC2.PROXY_HOST_SETTING.exists(settings)) {
        String proxyHost = CLOUD_EC2.PROXY_HOST_SETTING.get(settings);
        Integer proxyPort = CLOUD_EC2.PROXY_PORT_SETTING.get(settings);
        String proxyUsername = CLOUD_EC2.PROXY_USERNAME_SETTING.get(settings);
        String proxyPassword = CLOUD_EC2.PROXY_PASSWORD_SETTING.get(settings);

        clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort).withProxyUsername(proxyUsername)
                .withProxyPassword(proxyPassword);
    }//  w w w.  j a  va  2s  . c o  m

    // #155: we might have 3rd party users using older EC2 API version
    String awsSigner = CLOUD_EC2.SIGNER_SETTING.get(settings);
    if (Strings.hasText(awsSigner)) {
        logger.debug("using AWS API signer [{}]", awsSigner);
        AwsSigner.configureSigner(awsSigner, clientConfiguration);
    }

    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    RetryPolicy retryPolicy = new RetryPolicy(RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
            new RetryPolicy.BackoffStrategy() {
                @Override
                public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest,
                        AmazonClientException exception, int retriesAttempted) {
                    // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
                    logger.warn("EC2 API request failed, retry again. Reason was:", exception);
                    return 1000L
                            * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
                }
            }, 10, false);
    clientConfiguration.setRetryPolicy(retryPolicy);

    return clientConfiguration;
}

From source file:org.elasticsearch.cloud.aws.AwsS3Service.java

License:Apache License

private synchronized AmazonS3 getClient(String endpoint, String account, String key) {
    Tuple<String, String> clientDescriptor = new Tuple<String, String>(endpoint, account);
    AmazonS3Client client = clients.get(clientDescriptor);
    if (client != null) {
        return client;
    }/*from  ww  w  .  j a  va2s .c o m*/

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    String protocol = componentSettings.get("protocol", "http").toLowerCase();
    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new ElasticsearchIllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }

    String proxyHost = componentSettings.get("proxy_host");
    if (proxyHost != null) {
        String portString = componentSettings.get("proxy_port", "80");
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new ElasticsearchIllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }
    client = new AmazonS3Client(credentials, clientConfiguration);

    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }
    clients.put(clientDescriptor, client);
    return client;
}

From source file:org.elasticsearch.cloud.aws.InternalAwsS3Service.java

License:Apache License

private synchronized AmazonS3 getClient(String endpoint, String protocol, String account, String key,
        Integer maxRetries) {/* w w w . ja  v  a2 s.c om*/
    Tuple<String, String> clientDescriptor = new Tuple<String, String>(endpoint, account);
    AmazonS3Client client = clients.get(clientDescriptor);
    if (client != null) {
        return client;
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    if (protocol == null) {
        protocol = settings.get("cloud.aws.protocol", "https").toLowerCase();
        protocol = settings.get("cloud.aws.s3.protocol", protocol).toLowerCase();
    }

    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new IllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }

    String proxyHost = settings.get("cloud.aws.proxy_host");
    proxyHost = settings.get("cloud.aws.s3.proxy_host", proxyHost);
    if (proxyHost != null) {
        String portString = settings.get("cloud.aws.proxy_port", "80");
        portString = settings.get("cloud.aws.s3.proxy_port", portString);
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    if (maxRetries != null) {
        // If not explicitly set, default to 3 with exponential backoff policy
        clientConfiguration.setMaxErrorRetry(maxRetries);
    }

    // #155: we might have 3rd party users using older S3 API version
    String awsSigner = settings.get("cloud.aws.s3.signer", settings.get("cloud.aws.signer"));
    if (awsSigner != null) {
        logger.debug("using AWS API signer [{}]", awsSigner);
        try {
            AwsSigner.configureSigner(awsSigner, clientConfiguration);
        } catch (IllegalArgumentException e) {
            logger.warn("wrong signer set for [cloud.aws.s3.signer] or [cloud.aws.signer]: [{}]", awsSigner);
        }
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }
    client = new AmazonS3Client(credentials, clientConfiguration);

    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }
    clients.put(clientDescriptor, client);
    return client;
}

From source file:org.elasticsearch.discovery.ec2.AwsEc2ServiceImpl.java

License:Apache License

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(PROTOCOL_SETTING.get(settings));

    if (PROXY_HOST_SETTING.exists(settings)) {
        String proxyHost = PROXY_HOST_SETTING.get(settings);
        Integer proxyPort = PROXY_PORT_SETTING.get(settings);
        try (SecureString proxyUsername = PROXY_USERNAME_SETTING.get(settings);
                SecureString proxyPassword = PROXY_PASSWORD_SETTING.get(settings)) {

            clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort)
                    .withProxyUsername(proxyUsername.toString()).withProxyPassword(proxyPassword.toString());
        }// w w w . j a v  a  2  s. com
    }

    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    RetryPolicy retryPolicy = new RetryPolicy(RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
            new RetryPolicy.BackoffStrategy() {
                @Override
                public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest,
                        AmazonClientException exception, int retriesAttempted) {
                    // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
                    logger.warn("EC2 API request failed, retry again. Reason was:", exception);
                    return 1000L
                            * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
                }
            }, 10, false);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout((int) READ_TIMEOUT_SETTING.get(settings).millis());

    return clientConfiguration;
}

From source file:org.elasticsearch.repositories.s3.InternalAwsS3Service.java

License:Apache License

static ClientConfiguration buildConfiguration(S3ClientSettings clientSettings, Settings repositorySettings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(clientSettings.protocol);

    if (Strings.hasText(clientSettings.proxyHost)) {
        // TODO: remove this leniency, these settings should exist together and be validated
        clientConfiguration.setProxyHost(clientSettings.proxyHost);
        clientConfiguration.setProxyPort(clientSettings.proxyPort);
        clientConfiguration.setProxyUsername(clientSettings.proxyUsername);
        clientConfiguration.setProxyPassword(clientSettings.proxyPassword);
    }/*from   ww w .  ja  v  a2s . c o  m*/

    clientConfiguration.setMaxErrorRetry(clientSettings.maxRetries);
    clientConfiguration.setUseThrottleRetries(clientSettings.throttleRetries);
    clientConfiguration.setSocketTimeout(clientSettings.readTimeoutMillis);

    return clientConfiguration;
}

From source file:org.elasticsearch.repositories.s3.S3Service.java

License:Apache License

static ClientConfiguration buildConfiguration(S3ClientSettings clientSettings) {
    final ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(clientSettings.protocol);

    if (Strings.hasText(clientSettings.proxyHost)) {
        // TODO: remove this leniency, these settings should exist together and be validated
        clientConfiguration.setProxyHost(clientSettings.proxyHost);
        clientConfiguration.setProxyPort(clientSettings.proxyPort);
        clientConfiguration.setProxyUsername(clientSettings.proxyUsername);
        clientConfiguration.setProxyPassword(clientSettings.proxyPassword);
    }//  ww  w  .ja  v  a 2s  .  co m

    clientConfiguration.setMaxErrorRetry(clientSettings.maxRetries);
    clientConfiguration.setUseThrottleRetries(clientSettings.throttleRetries);
    clientConfiguration.setSocketTimeout(clientSettings.readTimeoutMillis);

    return clientConfiguration;
}

From source file:org.eluder.logback.ext.aws.core.AwsSupport.java

License:Open Source License

public ClientConfiguration getClientConfiguration() {
    return new ClientConfiguration();
}

From source file:org.exem.flamingo.web.filesystem.s3.AwsS3Factory.java

License:Apache License

/**
 * Properties? Input Stream? ? .//from w  w  w . j a v a  2s  .  c  om
 * Properties? <tt>accessKey</tt> <tt>secretKey</tt>? ?? .
 *
 * @return {@link AmazonS3}
 * @throws IOException Properties ?? Input Stream?    
 */
private static AmazonS3 createS3Client(InputStream is) throws IOException {
    AWSCredentials credentials = new PropertiesCredentials(is);
    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setProtocol(Protocol.HTTP);
    return new AmazonS3Client(credentials, clientConfig);
}

From source file:org.finra.dm.dao.helper.AwsHelper.java

License:Apache License

/**
 * Returns client configuration options that might contain proxy settings set per specified AWS parameters DTO.
 *
 * @param awsParamsDto the AWS params DTO object
 *
 * @return the client configuration options
 *///from  w  ww  . j  a  v  a 2s.  c o  m
public ClientConfiguration getClientConfiguration(AwsParamsDto awsParamsDto) {
    ClientConfiguration clientConfiguration;

    // Only set the proxy hostname and port if they're both configured.
    if (StringUtils.isNotBlank(awsParamsDto.getHttpProxyHost()) && awsParamsDto.getHttpProxyPort() != null) {
        clientConfiguration = new ClientConfiguration().withProxyHost(awsParamsDto.getHttpProxyHost())
                .withProxyPort(awsParamsDto.getHttpProxyPort());
    } else {
        clientConfiguration = new ClientConfiguration();
    }

    return clientConfiguration;
}