private AmazonHttpClient(ClientConfiguration clientConfig, RetryPolicy retryPolicy, RequestMetricCollector requestMetricCollector, HttpClientSettings httpClientSettings) { this.config = clientConfig; this.retryPolicy = retryPolicy == null ? new RetryPolicyAdapter(clientConfig.getRetryPolicy(), clientConfig) : retryPolicy; this.httpClientSettings = httpClientSettings; this.requestMetricCollector = requestMetricCollector; this.responseMetadataCache = clientConfig.getCacheResponseMetadata() ? new ResponseMetadataCache(clientConfig.getResponseMetadataCacheSize()) : new NullResponseMetadataCache(); this.httpRequestTimer = new HttpRequestTimer(); this.clientExecutionTimer = new ClientExecutionTimer(); // When enabled, total retry capacity is computed based on retry cost // and desired number of retries. int throttledRetryMaxCapacity = clientConfig.useThrottledRetries() ? THROTTLED_RETRY_COST * config.getMaxConsecutiveRetriesBeforeThrottling() : -1; this.retryCapacity = new CapacityManager(throttledRetryMaxCapacity); }
private static void verifyBaseAfterCopy(final ProgressListener listener, final AWSCredentials credentials, final RequestMetricCollector collector, final AmazonWebServiceRequest from, final AmazonWebServiceRequest to) { RequestClientOptions toOptions; Map<String, String> headers = to.getCustomRequestHeaders(); assertTrue(2 == headers.size()); assertEquals("v1", headers.get("k1")); assertEquals("v2", headers.get("k2")); Map<String, List<String>> parmas = to.getCustomQueryParameters(); assertTrue(2 == parmas.size()); assertEquals(Arrays.asList("v1"), parmas.get("k1")); assertEquals(Arrays.asList("v2a", "v2b"), parmas.get("k2")); assertSame(listener, to.getGeneralProgressListener()); assertSame(credentials, to.getRequestCredentials()); assertSame(collector, to.getRequestMetricCollector()); assertTrue(1234 == to.getReadLimit()); toOptions = to.getRequestClientOptions(); assertEquals( from.getRequestClientOptions().getClientMarker( Marker.USER_AGENT), toOptions.getClientMarker(Marker.USER_AGENT)); assertTrue(1234 == toOptions.getReadLimit()); }
/** * The sync client is tested less thoroughly then the async client primarily because the async * client exercises most of the same code paths so a bug introduced in the sync client builder * should be exposed via tests written against the async builder. This test is mainly for * additional coverage of the sync builder in case there is a regression specific to sync * builders. */ @Test public void syncClientBuilder() { final List<RequestHandler2> requestHandlers = createRequestHandlerList( new ConcreteRequestHandler(), new ConcreteRequestHandler()); final AWSCredentialsProvider credentials = mock(AWSCredentialsProvider.class); final RequestMetricCollector metrics = mock(RequestMetricCollector.class); //@formatter:off AmazonConcreteClient client = new ConcreteSyncBuilder() .withRegion(Regions.EU_CENTRAL_1) .withClientConfiguration(new ClientConfiguration().withSocketTimeout(1234)) .withCredentials(credentials) .withMetricsCollector(metrics) .withRequestHandlers(requestHandlers.toArray(new RequestHandler2[requestHandlers.size()])) .build(); //@formatter:on assertEquals(URI.create("https://mockprefix.eu-central-1.amazonaws.com"), client.getEndpoint()); assertEquals(1234, client.getSyncParams().getClientConfiguration().getSocketTimeout()); assertEquals(requestHandlers, client.getSyncParams().getRequestHandlers()); assertEquals(credentials, client.getSyncParams().getCredentialsProvider()); assertEquals(metrics, client.getSyncParams().getRequestMetricCollector()); }
/** * @deprecated use {@link AmazonS3EncryptionClientBuilder#withEncryptionMaterials(EncryptionMaterialsProvider)} and * {@link AmazonS3EncryptionClientBuilder#withCredentials(AWSCredentialsProvider)} and * {@link AmazonS3EncryptionClientBuilder#withCryptoConfiguration(CryptoConfiguration)} and * {@link AmazonS3EncryptionClientBuilder#withClientConfiguration(ClientConfiguration)} and * {@link AmazonS3EncryptionClientBuilder#withMetricsCollector(RequestMetricCollector)} and * {@link AmazonS3EncryptionClientBuilder#withKmsClient(AWSKMS)} */ @Deprecated public AmazonS3EncryptionClient(AWSKMSClient kms, AWSCredentialsProvider credentialsProvider, EncryptionMaterialsProvider kekMaterialsProvider, ClientConfiguration clientConfig, CryptoConfiguration cryptoConfig, RequestMetricCollector requestMetricCollector) { super(credentialsProvider, clientConfig, requestMetricCollector); assertParameterNotNull(kekMaterialsProvider, "EncryptionMaterialsProvider parameter must not be null."); assertParameterNotNull(cryptoConfig, "CryptoConfiguration parameter must not be null."); this.isKMSClientInternal = kms == null; this.kms = isKMSClientInternal ? newAWSKMSClient(credentialsProvider, clientConfig, cryptoConfig, requestMetricCollector) : kms; this.crypto = new CryptoModuleDispatcher(this.kms, new S3DirectImpl(), credentialsProvider, kekMaterialsProvider, cryptoConfig); }
/** * Specifies a request level metric collector which takes precedence over the ones at the http * client level and AWS SDK level. */ public <T extends AmazonWebServiceRequest> T withRequestMetricCollector(RequestMetricCollector metricCollector) { setRequestMetricCollector(metricCollector); @SuppressWarnings("unchecked") T t = (T) this; return t; }
@SdkProtectedApi protected AmazonWebServiceClient(ClientConfiguration clientConfiguration, RequestMetricCollector requestMetricCollector, boolean disableStrictHostNameVerification) { this.clientConfiguration = clientConfiguration; requestHandler2s = new CopyOnWriteArrayList<RequestHandler2>(); client = new AmazonHttpClient(clientConfiguration, requestMetricCollector, disableStrictHostNameVerification, calculateCRC32FromCompressedData()); }
/** * Returns true if request metric collection is applicable to the given * request; false otherwise. */ protected final boolean isRequestMetricsEnabled(AmazonWebServiceRequest req) { RequestMetricCollector c = req.getRequestMetricCollector(); // request level collector if (c != null && c.isEnabled()) { return true; } return isRMCEnabledAtClientOrSdkLevel(); }
/** * Returns the most specific request metric collector, starting from the request level, then * client level, then finally the AWS SDK level. */ private final RequestMetricCollector findRequestMetricCollector( RequestMetricCollector reqLevelMetricsCollector) { if (reqLevelMetricsCollector != null) { return reqLevelMetricsCollector; } else if (getRequestMetricsCollector() != null) { return getRequestMetricsCollector(); } else { return AwsSdkMetrics.getRequestMetricCollector(); } }
/** * Common routine to end a client AWS request/response execution and collect * the request metrics. Caller of this routine is responsible for starting * the event for {@link Field#ClientExecuteTime} and call this method * in a try-finally block. * * @param loggingAwsRequestMetrics deprecated and ignored */ protected final void endClientExecution( AWSRequestMetrics awsRequestMetrics, Request<?> request, Response<?> response, @Deprecated boolean loggingAwsRequestMetrics) { if (request != null) { awsRequestMetrics.endEvent(Field.ClientExecuteTime); awsRequestMetrics.getTimingInfo().endTiming(); RequestMetricCollector c = findRequestMetricCollector( request.getOriginalRequest().getRequestMetricCollector()); c.collectMetrics(request, response); awsRequestMetrics.log(); } }
private AmazonHttpClient(ClientConfiguration config, RetryPolicy retryPolicy, RequestMetricCollector requestMetricCollector, boolean useBrowserCompatibleHostNameVerifier, boolean calculateCRC32FromCompressedData) { this(config, retryPolicy, requestMetricCollector, HttpClientSettings.adapt(config, useBrowserCompatibleHostNameVerifier, calculateCRC32FromCompressedData)); this.httpClient = httpClientFactory.create(this.httpClientSettings); }
/** * Package-protected constructor for unit test purposes. */ @SdkTestInternalApi public AmazonHttpClient(ClientConfiguration clientConfig, ConnectionManagerAwareHttpClient httpClient, RequestMetricCollector requestMetricCollector) { this(clientConfig, null, requestMetricCollector, HttpClientSettings.adapt(clientConfig, false)); this.httpClient = httpClient; }
/** * Convenient method to end the client execution without logging the awsRequestMetrics. */ private void endClientExecution(AWSRequestMetrics awsRequestMetrics, RequestConfig requestConfig, Request<?> request, Response<?> response) { if (request != null) { awsRequestMetrics.endEvent(AWSRequestMetrics.Field.ClientExecuteTime); awsRequestMetrics.getTimingInfo().endTiming(); RequestMetricCollector metricCollector = findRequestMetricCollector(requestConfig); metricCollector.collectMetrics(request, response); awsRequestMetrics.log(); } }
/** * Returns the most specific request metric collector, starting from the request level, then * client level, then finally the AWS SDK level. */ private RequestMetricCollector findRequestMetricCollector(RequestConfig requestConfig) { RequestMetricCollector reqLevelMetricsCollector = requestConfig .getRequestMetricsCollector(); if (reqLevelMetricsCollector != null) { return reqLevelMetricsCollector; } else if (clientLevelMetricCollector != null) { return clientLevelMetricCollector; } else { return AwsSdkMetrics.getRequestMetricCollector(); } }
@Test public void copyBaseTo() { final ProgressListener listener = new SyncProgressListener() { @Override public void progressChanged(ProgressEvent progressEvent) { } }; final AWSCredentials credentials = new BasicAWSCredentials("accesskey", "accessid"); final RequestMetricCollector collector = new RequestMetricCollector() { @Override public void collectMetrics(Request<?> request, Response<?> response) { } }; final AmazonWebServiceRequest from = new AmazonWebServiceRequest() { }; from.setGeneralProgressListener(listener); from.setRequestCredentials(credentials); from.setRequestMetricCollector(collector); from.putCustomRequestHeader("k1", "v1"); from.putCustomRequestHeader("k2", "v2"); from.putCustomQueryParameter("k1", "v1"); from.putCustomQueryParameter("k2", "v2a"); from.putCustomQueryParameter("k2", "v2b"); from.getRequestClientOptions().setReadLimit(1234); final AmazonWebServiceRequest to = new AmazonWebServiceRequest() { }; // Before copy RequestClientOptions toOptions; verifyBaseBeforeCopy(to); // After copy from.copyBaseTo(to); verifyBaseAfterCopy(listener, credentials, collector, from, to); }
@Test public void metricsCollectorExplicitlySet_UsesExplicitMetricsCollector() throws Exception { RequestMetricCollector metricCollector = RequestMetricCollector.NONE; AwsAsyncClientParams params = builderWithRegion().withMetricsCollector(metricCollector) .build().getAsyncParams(); assertEquals(metricCollector, params.getRequestMetricCollector()); }
@Test public void customMetricsCollectorSetInBaseRequest_IsSetOnAdapter() { EmptyAmazonWebServiceRequest request = new EmptyAmazonWebServiceRequest(); RequestMetricCollector metricCollector = mock(RequestMetricCollector.class); request.setRequestMetricCollector(metricCollector); AmazonWebServiceRequestAdapter adapter = new AmazonWebServiceRequestAdapter(request); assertEquals(metricCollector, adapter.getRequestMetricsCollector()); }
/** * Same as {@link #setObjectAcl(String, String, String, AccessControlList)} * but allows specifying a request metric collector. */ public void setObjectAcl(String bucketName, String key, String versionId, AccessControlList acl, RequestMetricCollector requestMetricCollector) throws SdkClientException, AmazonServiceException { setObjectAcl(new SetObjectAclRequest(bucketName, key, versionId, acl) .<SetObjectAclRequest> withRequestMetricCollector(requestMetricCollector)); }
/** * Same as {@link #setObjectAcl(String, String, String, CannedAccessControlList)} * but allows specifying a request metric collector. */ public void setObjectAcl(String bucketName, String key, String versionId, CannedAccessControlList acl, RequestMetricCollector requestMetricCollector) { setObjectAcl(new SetObjectAclRequest(bucketName, key, versionId, acl) .<SetObjectAclRequest> withRequestMetricCollector(requestMetricCollector)); }
/** * Same as {@link #setBucketAcl(String, AccessControlList)} * but allows specifying a request metric collector. */ public void setBucketAcl(String bucketName, AccessControlList acl, RequestMetricCollector requestMetricCollector) { SetBucketAclRequest request = new SetBucketAclRequest(bucketName, acl) .withRequestMetricCollector(requestMetricCollector); setBucketAcl(request); }
/** * Same as {@link #setBucketAcl(String, CannedAccessControlList)} * but allows specifying a request metric collector. */ public void setBucketAcl(String bucketName, CannedAccessControlList cannedAcl, RequestMetricCollector requestMetricCollector) throws SdkClientException, AmazonServiceException { SetBucketAclRequest request = new SetBucketAclRequest(bucketName, cannedAcl) .withRequestMetricCollector(requestMetricCollector); setBucketAcl(request); }
/** * @deprecated use {@link AmazonS3EncryptionClientBuilder#withEncryptionMaterials(EncryptionMaterialsProvider)} and * {@link AmazonS3EncryptionClientBuilder#withCredentials(AWSCredentialsProvider)} and * {@link AmazonS3EncryptionClientBuilder#withCryptoConfiguration(CryptoConfiguration)} and * {@link AmazonS3EncryptionClientBuilder#withClientConfiguration(ClientConfiguration)} and * {@link AmazonS3EncryptionClientBuilder#withMetricsCollector(RequestMetricCollector)} */ @Deprecated public AmazonS3EncryptionClient( AWSCredentialsProvider credentialsProvider, EncryptionMaterialsProvider kekMaterialsProvider, ClientConfiguration clientConfig, CryptoConfiguration cryptoConfig, RequestMetricCollector requestMetricCollector) { this(null, // KMS client credentialsProvider, kekMaterialsProvider, clientConfig, cryptoConfig, requestMetricCollector); }
/** * Creates and returns a new instance of AWS KMS client in the case when * an explicit AWS KMS client is not specified. */ private AWSKMSClient newAWSKMSClient( AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfig, CryptoConfiguration cryptoConfig, RequestMetricCollector requestMetricCollector ) { final AWSKMSClient kmsClient = new AWSKMSClient( credentialsProvider, clientConfig, requestMetricCollector); final Region kmsRegion = cryptoConfig.getAwsKmsRegion(); if (kmsRegion != null) kmsClient.setRegion(kmsRegion); return kmsClient; }
/** * Returns a request level metric collector; or null if not specified. */ public RequestMetricCollector getRequestMetricCollector() { return requestMetricCollector; }
/** * Returns true if request metric collection is enabled at the service * client or AWS SDK level request; false otherwise. */ private boolean isRMCEnabledAtClientOrSdkLevel() { RequestMetricCollector c = requestMetricCollector(); return c != null && c.isEnabled(); }
/** * Returns the client specific request metric collector if there is one; or * the one at the AWS SDK level otherwise. */ protected RequestMetricCollector requestMetricCollector() { RequestMetricCollector mc = client.getRequestMetricCollector(); return mc == null ? AwsSdkMetrics.getRequestMetricCollector() : mc; }
/** * Returns true if request metric collection is enabled at the service client or AWS SDK level * request; false otherwise. */ private boolean isRMCEnabledAtClientOrSdkLevel() { RequestMetricCollector collector = requestMetricCollector(); return collector != null && collector.isEnabled(); }
/** * Returns the client specific request metric collector if there is one; or the one at the AWS * SDK level otherwise. */ private RequestMetricCollector requestMetricCollector() { return clientLevelMetricCollector != null ? clientLevelMetricCollector : AwsSdkMetrics.getRequestMetricCollector(); }
/** * Gets the {@link RequestMetricCollector} in use by the builder. */ public final RequestMetricCollector getMetricsCollector() { return this.metricsCollector; }
@Override public RequestMetricCollector getRequestMetricCollector() { return this._metricsCollector; }
@Override public RequestMetricCollector getRequestMetricsCollector() { return request.getRequestMetricCollector(); }
@Override public RequestMetricCollector getRequestMetricCollector() { return requestMetricCollector; }
/** * Constructs a new AWS client using the specified client configuration options (ex: max retry * attempts, proxy httpClientSettings, etc), and request metric collector. * * @param config Configuration options specifying how this client will * communicate with AWS (ex: proxy httpClientSettings, * retry count, etc.). * @param requestMetricCollector client specific request metric collector, which takes * precedence over the one at the AWS SDK level; or null * if there is none. * @param calculateCRC32FromCompressedData The flag indicating whether the CRC32 checksum is * calculated from compressed data or not. It is only * applicable when the header "x-amz-crc32" is set in * the response. */ public AmazonHttpClient(ClientConfiguration config, RequestMetricCollector requestMetricCollector, boolean useBrowserCompatibleHostNameVerifier, boolean calculateCRC32FromCompressedData) { this(config, null, requestMetricCollector, useBrowserCompatibleHostNameVerifier, calculateCRC32FromCompressedData); }
/** * Constructs a new Amazon S3 client using the specified AWS credentials, * client configuration and request metric collector to access Amazon S3. * * @param credentialsProvider * The AWS credentials provider which will provide credentials * to authenticate requests with AWS services. * @param clientConfiguration * The client configuration options controlling how this client * connects to Amazon S3 (e.g. proxy settings, retry counts, etc). * @param requestMetricCollector request metric collector * @deprecated use {@link AmazonS3ClientBuilder#withCredentials(AWSCredentialsProvider)} and * {@link AmazonS3ClientBuilder#withClientConfiguration(ClientConfiguration)} and * {@link AmazonS3ClientBuilder#withMetricsCollector(RequestMetricCollector)} */ @Deprecated public AmazonS3Client(AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfiguration, RequestMetricCollector requestMetricCollector) { this(credentialsProvider, clientConfiguration, requestMetricCollector, SkipMd5CheckStrategy.INSTANCE); }
/** * Constructs a new Amazon S3 client using the specified AWS credentials, * client configuration and request metric collector to access Amazon S3. * * @param credentialsProvider * The AWS credentials provider which will provide credentials * to authenticate requests with AWS services. * @param clientConfiguration * The client configuration options controlling how this client * connects to Amazon S3 (e.g. proxy settings, retry counts, etc). * @param requestMetricCollector request metric collector */ @SdkTestInternalApi AmazonS3Client(AWSCredentialsProvider credentialsProvider, ClientConfiguration clientConfiguration, RequestMetricCollector requestMetricCollector, SkipMd5CheckStrategy skipMd5CheckStrategy) { super(clientConfiguration, requestMetricCollector, true); this.awsCredentialsProvider = credentialsProvider; this.skipMd5CheckStrategy = skipMd5CheckStrategy; init(); }
/** * Sets a request level request metric collector which takes precedence over the ones at the * http client level and AWS SDK level. */ public void setRequestMetricCollector(RequestMetricCollector requestMetricCollector) { this.requestMetricCollector = requestMetricCollector; }
/** * Constructs a new AmazonWebServiceClient object using the specified * configuration and request metric collector. * * @param clientConfiguration * The client configuration for this client. * @param requestMetricCollector * optional request metric collector to be used at the http * client level; can be null. */ public AmazonWebServiceClient(ClientConfiguration clientConfiguration, RequestMetricCollector requestMetricCollector) { this(clientConfiguration, requestMetricCollector, false); }
/** * Returns the client specific {@link RequestMetricCollector}; or null if * there is none. */ public RequestMetricCollector getRequestMetricsCollector() { return client.getRequestMetricCollector(); }
/** * Constructs a new AWS client using the specified client configuration options (ex: max retry * attempts, proxy httpClientSettings, etc), and request metric collector. * * @param config Configuration options specifying how this client will * communicate with AWS (ex: proxy httpClientSettings, retry * count, etc.). * @param requestMetricCollector client specific request metric collector, which takes * precedence over the one at the AWS SDK level; or null if there * is none. */ public AmazonHttpClient(ClientConfiguration config, RequestMetricCollector requestMetricCollector) { this(config, requestMetricCollector, false); }
/** * Constructs a new AWS client using the specified client configuration options (ex: max retry * attempts, proxy httpClientSettings, etc), and request metric collector. * * @param config Configuration options specifying how this client will * communicate with AWS (ex: proxy httpClientSettings, retry * count, etc.). * @param requestMetricCollector client specific request metric collector, which takes * precedence over the one at the AWS SDK level; or null if there * is none. */ public AmazonHttpClient(ClientConfiguration config, RequestMetricCollector requestMetricCollector, boolean useBrowserCompatibleHostNameVerifier) { this(config, requestMetricCollector, useBrowserCompatibleHostNameVerifier, false); }
/** * Returns the httpClientSettings client specific request metric collector; or null if there is * none. */ public RequestMetricCollector getRequestMetricCollector() { return requestMetricCollector; }