Search in sources :

Example 6 with AwsRequestOverrideConfiguration

use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.

the class DefaultEndpointDiscoveryTestAsyncClient method testDiscoveryIdentifiersRequired.

/**
 * Invokes the TestDiscoveryIdentifiersRequired operation asynchronously.
 *
 * @param testDiscoveryIdentifiersRequiredRequest
 * @return A Java Future containing the result of the TestDiscoveryIdentifiersRequired 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.TestDiscoveryIdentifiersRequired
 */
@Override
public CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> testDiscoveryIdentifiersRequired(TestDiscoveryIdentifiersRequiredRequest testDiscoveryIdentifiersRequiredRequest) {
    List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null));
    MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
    try {
        apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest");
        apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryIdentifiersRequired");
        JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false).isPayloadJson(true).build();
        HttpResponseHandler<TestDiscoveryIdentifiersRequiredResponse> responseHandler = protocolFactory.createResponseHandler(operationMetadata, TestDiscoveryIdentifiersRequiredResponse::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 = testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsProvider).orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_PROVIDER)).resolveCredentials().accessKeyId();
            EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true).defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT)).overrideConfiguration(testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)).build();
            cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest);
        }
        CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<TestDiscoveryIdentifiersRequiredRequest, TestDiscoveryIdentifiersRequiredResponse>().withOperationName("TestDiscoveryIdentifiersRequired").withMarshaller(new TestDiscoveryIdentifiersRequiredRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryIdentifiersRequiredRequest));
        CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> 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);
    }
}
Also used : AwsJsonProtocol(software.amazon.awssdk.protocols.json.AwsJsonProtocol) AwsAsyncClientHandler(software.amazon.awssdk.awscore.client.handler.AwsAsyncClientHandler) LoggerFactory(org.slf4j.LoggerFactory) CompletableFutureUtils(software.amazon.awssdk.utils.CompletableFutureUtils) CompletableFuture(java.util.concurrent.CompletableFuture) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) DescribeEndpointsResponse(software.amazon.awssdk.services.endpointdiscoverytest.model.DescribeEndpointsResponse) TestDiscoveryRequiredRequestMarshaller(software.amazon.awssdk.services.endpointdiscoverytest.transform.TestDiscoveryRequiredRequestMarshaller) CoreMetric(software.amazon.awssdk.core.metrics.CoreMetric) TestDiscoveryIdentifiersRequiredRequestMarshaller(software.amazon.awssdk.services.endpointdiscoverytest.transform.TestDiscoveryIdentifiersRequiredRequestMarshaller) TestDiscoveryOptionalRequestMarshaller(software.amazon.awssdk.services.endpointdiscoverytest.transform.TestDiscoveryOptionalRequestMarshaller) MetricPublisher(software.amazon.awssdk.metrics.MetricPublisher) JsonOperationMetadata(software.amazon.awssdk.protocols.json.JsonOperationMetadata) BaseAwsJsonProtocolFactory(software.amazon.awssdk.protocols.json.BaseAwsJsonProtocolFactory) AwsClientOption(software.amazon.awssdk.awscore.client.config.AwsClientOption) RequestOverrideConfiguration(software.amazon.awssdk.core.RequestOverrideConfiguration) SdkClientOption(software.amazon.awssdk.core.client.config.SdkClientOption) URI(java.net.URI) NoOpMetricCollector(software.amazon.awssdk.metrics.NoOpMetricCollector) AsyncClientHandler(software.amazon.awssdk.core.client.handler.AsyncClientHandler) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) Logger(org.slf4j.Logger) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) EndpointDiscoveryRefreshCache(software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRefreshCache) DescribeEndpointsRequestMarshaller(software.amazon.awssdk.services.endpointdiscoverytest.transform.DescribeEndpointsRequestMarshaller) EndpointDiscoveryTestException(software.amazon.awssdk.services.endpointdiscoverytest.model.EndpointDiscoveryTestException) TestDiscoveryOptionalResponse(software.amazon.awssdk.services.endpointdiscoverytest.model.TestDiscoveryOptionalResponse) List(java.util.List) TestDiscoveryRequiredResponse(software.amazon.awssdk.services.endpointdiscoverytest.model.TestDiscoveryRequiredResponse) ClientExecutionParams(software.amazon.awssdk.core.client.handler.ClientExecutionParams) EndpointDiscoveryRequest(software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRequest) TestDiscoveryRequiredRequest(software.amazon.awssdk.services.endpointdiscoverytest.model.TestDiscoveryRequiredRequest) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) HttpResponseHandler(software.amazon.awssdk.core.http.HttpResponseHandler) TestDiscoveryIdentifiersRequiredRequest(software.amazon.awssdk.services.endpointdiscoverytest.model.TestDiscoveryIdentifiersRequiredRequest) AwsJsonProtocolFactory(software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory) FunctionalUtils.runAndLogError(software.amazon.awssdk.utils.FunctionalUtils.runAndLogError) Collections(java.util.Collections) TestDiscoveryIdentifiersRequiredResponse(software.amazon.awssdk.services.endpointdiscoverytest.model.TestDiscoveryIdentifiersRequiredResponse) Generated(software.amazon.awssdk.annotations.Generated) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration) DescribeEndpointsRequest(software.amazon.awssdk.services.endpointdiscoverytest.model.DescribeEndpointsRequest) TestDiscoveryOptionalRequest(software.amazon.awssdk.services.endpointdiscoverytest.model.TestDiscoveryOptionalRequest) ClientExecutionParams(software.amazon.awssdk.core.client.handler.ClientExecutionParams) TestDiscoveryIdentifiersRequiredRequestMarshaller(software.amazon.awssdk.services.endpointdiscoverytest.transform.TestDiscoveryIdentifiersRequiredRequestMarshaller) MetricPublisher(software.amazon.awssdk.metrics.MetricPublisher) TestDiscoveryIdentifiersRequiredResponse(software.amazon.awssdk.services.endpointdiscoverytest.model.TestDiscoveryIdentifiersRequiredResponse) EndpointDiscoveryRequest(software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRequest) JsonOperationMetadata(software.amazon.awssdk.protocols.json.JsonOperationMetadata) URI(java.net.URI) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) NoOpMetricCollector(software.amazon.awssdk.metrics.NoOpMetricCollector) MetricCollector(software.amazon.awssdk.metrics.MetricCollector)

Example 7 with AwsRequestOverrideConfiguration

use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.

the class S3PresignerTest method getObject_CredentialsCanBeOverriddenAtTheRequestLevel.

@Test
public void getObject_CredentialsCanBeOverriddenAtTheRequestLevel() {
    AwsCredentials clientCredentials = AwsBasicCredentials.create("a", "a");
    AwsCredentials requestCredentials = AwsBasicCredentials.create("b", "b");
    S3Presigner presigner = presignerBuilder().credentialsProvider(() -> clientCredentials).build();
    AwsRequestOverrideConfiguration overrideConfiguration = AwsRequestOverrideConfiguration.builder().credentialsProvider(() -> requestCredentials).build();
    PresignedGetObjectRequest presignedWithClientCredentials = presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5)).getObjectRequest(go -> go.bucket("foo34343434").key("bar")));
    PresignedGetObjectRequest presignedWithRequestCredentials = presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5)).getObjectRequest(go -> go.bucket("foo34343434").key("bar").overrideConfiguration(overrideConfiguration)));
    assertThat(presignedWithClientCredentials.httpRequest().rawQueryParameters().get("X-Amz-Credential").get(0)).startsWith("a");
    assertThat(presignedWithRequestCredentials.httpRequest().rawQueryParameters().get("X-Amz-Credential").get(0)).startsWith("b");
}
Also used : Offset(org.assertj.core.data.Offset) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) AbstractAwsS3V4Signer(software.amazon.awssdk.auth.signer.internal.AbstractAwsS3V4Signer) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) AwsS3V4SignerParams(software.amazon.awssdk.auth.signer.params.AwsS3V4SignerParams) ChecksumConstant(software.amazon.awssdk.services.s3.checksums.ChecksumConstant) NoOpSigner(software.amazon.awssdk.core.signer.NoOpSigner) RequestPayer(software.amazon.awssdk.services.s3.model.RequestPayer) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SignerConstant(software.amazon.awssdk.auth.signer.internal.SignerConstant) Duration(java.time.Duration) After(org.junit.After) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) Aws4SignerRequestParams(software.amazon.awssdk.auth.signer.internal.Aws4SignerRequestParams) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) URI(java.net.URI) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Region(software.amazon.awssdk.regions.Region) Aws4PresignerParams(software.amazon.awssdk.auth.signer.params.Aws4PresignerParams) Before(org.junit.Before) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Test(org.junit.Test) AwsS3V4Signer(software.amazon.awssdk.auth.signer.AwsS3V4Signer) Instant(java.time.Instant) ZoneId(java.time.ZoneId) AbstractAws4Signer(software.amazon.awssdk.auth.signer.internal.AbstractAws4Signer) Mockito(org.mockito.Mockito) LocalDate(java.time.LocalDate) Clock(java.time.Clock) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) DateUtils(software.amazon.awssdk.utils.DateUtils) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) Test(org.junit.Test)

Example 8 with AwsRequestOverrideConfiguration

use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.

the class EndpointDiscoveryTestAsyncEndpointDiscoveryCacheLoader method discoverEndpoint.

@Override
public CompletableFuture<EndpointDiscoveryEndpoint> discoverEndpoint(EndpointDiscoveryRequest endpointDiscoveryRequest) {
    AwsRequestOverrideConfiguration requestConfig = AwsRequestOverrideConfiguration.from(endpointDiscoveryRequest.overrideConfiguration().orElse(null));
    return client.describeEndpoints(software.amazon.awssdk.services.endpointdiscoverytest.model.DescribeEndpointsRequest.builder().overrideConfiguration(requestConfig).build()).thenApply(r -> {
        List<Endpoint> endpoints = r.endpoints();
        Validate.notEmpty(endpoints, "Endpoints returned by service for endpoint discovery must not be empty.");
        Endpoint endpoint = endpoints.get(0);
        return EndpointDiscoveryEndpoint.builder().endpoint(toUri(endpoint.address(), endpointDiscoveryRequest.defaultEndpoint())).expirationTime(Instant.now().plus(endpoint.cachePeriodInMinutes(), ChronoUnit.MINUTES)).build();
    });
}
Also used : Endpoint(software.amazon.awssdk.services.endpointdiscoverytest.model.Endpoint) EndpointDiscoveryEndpoint(software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryEndpoint) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration)

Example 9 with AwsRequestOverrideConfiguration

use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.

the class EndpointDiscoveryTestEndpointDiscoveryCacheLoader method discoverEndpoint.

@Override
public CompletableFuture<EndpointDiscoveryEndpoint> discoverEndpoint(EndpointDiscoveryRequest endpointDiscoveryRequest) {
    return CompletableFuture.supplyAsync(() -> {
        AwsRequestOverrideConfiguration requestConfig = AwsRequestOverrideConfiguration.from(endpointDiscoveryRequest.overrideConfiguration().orElse(null));
        DescribeEndpointsResponse response = client.describeEndpoints(software.amazon.awssdk.services.endpointdiscoverytest.model.DescribeEndpointsRequest.builder().overrideConfiguration(requestConfig).build());
        List<Endpoint> endpoints = response.endpoints();
        Validate.notEmpty(endpoints, "Endpoints returned by service for endpoint discovery must not be empty.");
        Endpoint endpoint = endpoints.get(0);
        return EndpointDiscoveryEndpoint.builder().endpoint(toUri(endpoint.address(), endpointDiscoveryRequest.defaultEndpoint())).expirationTime(Instant.now().plus(endpoint.cachePeriodInMinutes(), ChronoUnit.MINUTES)).build();
    });
}
Also used : Endpoint(software.amazon.awssdk.services.endpointdiscoverytest.model.Endpoint) EndpointDiscoveryEndpoint(software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryEndpoint) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration) DescribeEndpointsResponse(software.amazon.awssdk.services.endpointdiscoverytest.model.DescribeEndpointsResponse)

Example 10 with AwsRequestOverrideConfiguration

use of software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration in project aws-sdk-java-v2 by aws.

the class DefaultQueryAsyncWaiter method applyWaitersUserAgent.

private <T extends QueryRequest> T applyWaitersUserAgent(T request) {
    Consumer<AwsRequestOverrideConfiguration.Builder> userAgentApplier = b -> b.addApiName(ApiName.builder().version("waiter").name("hll").build());
    AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration().map(c -> c.toBuilder().applyMutation(userAgentApplier).build()).orElse((AwsRequestOverrideConfiguration.builder().applyMutation(userAgentApplier).build()));
    return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build();
}
Also used : SdkAutoCloseable(software.amazon.awssdk.utils.SdkAutoCloseable) CompletableFuture(java.util.concurrent.CompletableFuture) ThreadSafe(software.amazon.awssdk.annotations.ThreadSafe) AttributeMap(software.amazon.awssdk.utils.AttributeMap) AsyncWaiter(software.amazon.awssdk.core.waiters.AsyncWaiter) ArrayList(java.util.ArrayList) ApiName(software.amazon.awssdk.core.ApiName) BackoffStrategy(software.amazon.awssdk.core.retry.backoff.BackoffStrategy) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) APostOperationResponse(software.amazon.awssdk.services.query.model.APostOperationResponse) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) WaiterOverrideConfiguration(software.amazon.awssdk.core.waiters.WaiterOverrideConfiguration) WaiterAcceptor(software.amazon.awssdk.core.waiters.WaiterAcceptor) WaiterAttribute(software.amazon.awssdk.core.internal.waiters.WaiterAttribute) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) ThreadFactoryBuilder(software.amazon.awssdk.utils.ThreadFactoryBuilder) WaitersRuntime(software.amazon.awssdk.services.query.waiters.internal.WaitersRuntime) Executors(java.util.concurrent.Executors) FixedDelayBackoffStrategy(software.amazon.awssdk.core.retry.backoff.FixedDelayBackoffStrategy) QueryRequest(software.amazon.awssdk.services.query.model.QueryRequest) Objects(java.util.Objects) Consumer(java.util.function.Consumer) QueryAsyncClient(software.amazon.awssdk.services.query.QueryAsyncClient) List(java.util.List) WaiterState(software.amazon.awssdk.core.waiters.WaiterState) APostOperationRequest(software.amazon.awssdk.services.query.model.APostOperationRequest) Optional(java.util.Optional) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) Generated(software.amazon.awssdk.annotations.Generated) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration) ThreadFactoryBuilder(software.amazon.awssdk.utils.ThreadFactoryBuilder)

Aggregations

AwsRequestOverrideConfiguration (software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration)23 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)13 URI (java.net.URI)11 List (java.util.List)11 Generated (software.amazon.awssdk.annotations.Generated)11 AwsServiceException (software.amazon.awssdk.awscore.exception.AwsServiceException)11 Duration (java.time.Duration)10 Consumer (java.util.function.Consumer)10 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)10 Collections (java.util.Collections)9 RequestOverrideConfiguration (software.amazon.awssdk.core.RequestOverrideConfiguration)9 SdkClientConfiguration (software.amazon.awssdk.core.client.config.SdkClientConfiguration)9 SdkClientOption (software.amazon.awssdk.core.client.config.SdkClientOption)9 ClientExecutionParams (software.amazon.awssdk.core.client.handler.ClientExecutionParams)9 HttpResponseHandler (software.amazon.awssdk.core.http.HttpResponseHandler)9 Clock (java.time.Clock)8 Instant (java.time.Instant)8 LocalDate (java.time.LocalDate)8 LocalDateTime (java.time.LocalDateTime)8 ZoneId (java.time.ZoneId)8