List of usage examples for com.amazonaws ClientConfiguration withProxyHost
public ClientConfiguration withProxyHost(String proxyHost)
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 w ww . j a v a 2 s . co 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 . j av a 2s . co m 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 .ja v a 2s.c om*/ } // 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.finra.herd.dao.helper.AwsHelper.java
License:Apache License
/** * Creates a client configuration object that contains client configuration options such as proxy settings and max retry attempts. * * @param awsParamsDto the AWS related parameters that contain optional proxy information * * @return the client configuration object *//*from www.ja v a 2 s. co m*/ public ClientConfiguration getClientConfiguration(AwsParamsDto awsParamsDto) { ClientConfiguration clientConfiguration = new ClientConfiguration(); // Set a retry policy. clientConfiguration.withRetryPolicy(retryPolicyFactory.getRetryPolicy()); // If the proxy hostname and port both are configured, set the HTTP proxy information. if (StringUtils.isNotBlank(awsParamsDto.getHttpProxyHost()) && awsParamsDto.getHttpProxyPort() != null) { clientConfiguration.withProxyHost(awsParamsDto.getHttpProxyHost()) .withProxyPort(awsParamsDto.getHttpProxyPort()); } return clientConfiguration; }