use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.
the class DefaultQueryAsyncClient method applySignerOverride.
private <T extends QueryRequest> T applySignerOverride(T request, Signer signer) {
if (request.overrideConfiguration().flatMap(c -> c.signer()).isPresent()) {
return request;
}
Consumer<AwsRequestOverrideConfiguration.Builder> signerOverride = b -> b.signer(signer).build();
AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration().map(c -> c.toBuilder().applyMutation(signerOverride).build()).orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build()));
return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build();
}
use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.
the class DefaultEndpointDiscoveryTestAsyncClient method testDiscoveryRequired.
/**
* Invokes the TestDiscoveryRequired operation asynchronously.
*
* @param testDiscoveryRequiredRequest
* @return A Java Future containing the result of the TestDiscoveryRequired operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>EndpointDiscoveryTestException Base class for all service exceptions. Unknown exceptions will be
* thrown as an instance of this type.</li>
* </ul>
* @sample EndpointDiscoveryTestAsyncClient.TestDiscoveryRequired
*/
@Override
public CompletableFuture<TestDiscoveryRequiredResponse> testDiscoveryRequired(TestDiscoveryRequiredRequest testDiscoveryRequiredRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, testDiscoveryRequiredRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryRequired");
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false).isPayloadJson(true).build();
HttpResponseHandler<TestDiscoveryRequiredResponse> responseHandler = protocolFactory.createResponseHandler(operationMetadata, TestDiscoveryRequiredResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata);
boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED);
boolean endpointOverridden = clientConfiguration.option(SdkClientOption.ENDPOINT_OVERRIDDEN) == Boolean.TRUE;
if (endpointOverridden) {
throw new IllegalStateException("This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported.");
}
if (!endpointDiscoveryEnabled) {
throw new IllegalStateException("This operation requires endpoint discovery, but endpoint discovery was disabled on the client.");
}
URI cachedEndpoint = null;
if (endpointDiscoveryEnabled) {
String key = testDiscoveryRequiredRequest.overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsProvider).orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_PROVIDER)).resolveCredentials().accessKeyId();
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true).defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT)).overrideConfiguration(testDiscoveryRequiredRequest.overrideConfiguration().orElse(null)).build();
cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest);
}
CompletableFuture<TestDiscoveryRequiredResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<TestDiscoveryRequiredRequest, TestDiscoveryRequiredResponse>().withOperationName("TestDiscoveryRequired").withMarshaller(new TestDiscoveryRequiredRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryRequiredRequest));
CompletableFuture<TestDiscoveryRequiredResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return executeFuture;
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.
the class DefaultEndpointDiscoveryTestAsyncClient method testDiscoveryOptional.
/**
* Invokes the TestDiscoveryOptional operation asynchronously.
*
* @param testDiscoveryOptionalRequest
* @return A Java Future containing the result of the TestDiscoveryOptional operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>EndpointDiscoveryTestException Base class for all service exceptions. Unknown exceptions will be
* thrown as an instance of this type.</li>
* </ul>
* @sample EndpointDiscoveryTestAsyncClient.TestDiscoveryOptional
*/
@Override
public CompletableFuture<TestDiscoveryOptionalResponse> testDiscoveryOptional(TestDiscoveryOptionalRequest testDiscoveryOptionalRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, testDiscoveryOptionalRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryOptional");
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false).isPayloadJson(true).build();
HttpResponseHandler<TestDiscoveryOptionalResponse> responseHandler = protocolFactory.createResponseHandler(operationMetadata, TestDiscoveryOptionalResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata);
boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED);
boolean endpointOverridden = clientConfiguration.option(SdkClientOption.ENDPOINT_OVERRIDDEN) == Boolean.TRUE;
URI cachedEndpoint = null;
if (endpointDiscoveryEnabled) {
String key = testDiscoveryOptionalRequest.overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsProvider).orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_PROVIDER)).resolveCredentials().accessKeyId();
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(false).defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT)).overrideConfiguration(testDiscoveryOptionalRequest.overrideConfiguration().orElse(null)).build();
cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest);
}
CompletableFuture<TestDiscoveryOptionalResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<TestDiscoveryOptionalRequest, TestDiscoveryOptionalResponse>().withOperationName("TestDiscoveryOptional").withMarshaller(new TestDiscoveryOptionalRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryOptionalRequest));
CompletableFuture<TestDiscoveryOptionalResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return executeFuture;
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.
the class S3PresignerTest method getObject_NonSigV4SignersRaisesException.
@Test
public void getObject_NonSigV4SignersRaisesException() {
AwsRequestOverrideConfiguration override = AwsRequestOverrideConfiguration.builder().signer(new NoOpSigner()).build();
assertThatThrownBy(() -> presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5)).getObjectRequest(go -> go.bucket("foo34343434").key("bar").overrideConfiguration(override)))).isInstanceOf(IllegalStateException.class).hasMessageContaining("NoOpSigner");
}
use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.
the class S3PresignerTest method putObject_AdditionalHeadersAndQueryStringsCanBeAdded.
@Test
public void putObject_AdditionalHeadersAndQueryStringsCanBeAdded() {
AwsRequestOverrideConfiguration override = AwsRequestOverrideConfiguration.builder().putHeader("X-Amz-AdditionalHeader", "foo1").putRawQueryParameter("additionalQueryParam", "foo2").build();
PresignedPutObjectRequest presigned = presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5)).putObjectRequest(go -> go.bucket("foo34343434").key("bar").overrideConfiguration(override)));
assertThat(presigned.isBrowserExecutable()).isFalse();
assertThat(presigned.signedHeaders()).containsOnlyKeys("host", "x-amz-additionalheader");
assertThat(presigned.signedHeaders().get("x-amz-additionalheader")).containsExactly("foo1");
assertThat(presigned.httpRequest().headers()).containsKeys("x-amz-additionalheader");
assertThat(presigned.httpRequest().rawQueryParameters().get("additionalQueryParam").get(0)).isEqualTo("foo2");
}
Aggregations