Search in sources :

Example 1 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class HttpClientApiCallAttemptTimeoutTest method successfulResponse_SlowAfterResponseExecutionInterceptor_ThrowsApiCallTimeoutException.

@Test
public void successfulResponse_SlowAfterResponseExecutionInterceptor_ThrowsApiCallTimeoutException() {
    stubFor(get(anyUrl()).willReturn(aResponse().withStatus(200).withBody("{}")));
    ExecutionInterceptor interceptor = new SlowExecutionInterceptor().afterTransmissionWaitInSeconds(SLOW_REQUEST_HANDLER_TIMEOUT);
    assertThatThrownBy(() -> requestBuilder().executionContext(withInterceptors(interceptor)).execute(noOpSyncResponseHandler())).isInstanceOf(ApiCallAttemptTimeoutException.class);
}
Also used : SlowExecutionInterceptor(software.amazon.awssdk.core.internal.http.request.SlowExecutionInterceptor) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) SlowExecutionInterceptor(software.amazon.awssdk.core.internal.http.request.SlowExecutionInterceptor) Test(org.junit.Test)

Example 2 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class DefaultS3Presigner method initializeInterceptors.

/**
 * Copied from {@code DefaultS3BaseClientBuilder} and {@link SdkDefaultClientBuilder}.
 */
private List<ExecutionInterceptor> initializeInterceptors() {
    ClasspathInterceptorChainFactory interceptorFactory = new ClasspathInterceptorChainFactory();
    List<ExecutionInterceptor> s3Interceptors = interceptorFactory.getInterceptors("software/amazon/awssdk/services/s3/execution.interceptors");
    return mergeLists(interceptorFactory.getGlobalInterceptors(), s3Interceptors);
}
Also used : ClasspathInterceptorChainFactory(software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor)

Example 3 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class EndpointDiscoveryTest method canBeEnabledViaProfileOnOverrideConfiguration.

@Test(timeout = 10_000)
public void canBeEnabledViaProfileOnOverrideConfiguration() throws InterruptedException {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);
    String profileFileContent = "[default]\n" + "aws_endpoint_discovery_enabled = true";
    ProfileFile profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION).content(new StringInputStream(profileFileContent)).build();
    DynamoDbClient dynamoDb = DynamoDbClient.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create()).overrideConfiguration(c -> c.defaultProfileFile(profileFile).defaultProfileName("default").addExecutionInterceptor(interceptor).retryPolicy(r -> r.numRetries(0))).build();
    assertThatThrownBy(dynamoDb::listTables).isInstanceOf(SdkException.class);
    ArgumentCaptor<Context.BeforeTransmission> context;
    do {
        Thread.sleep(1);
        context = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
        Mockito.verify(interceptor, atLeastOnce()).beforeTransmission(context.capture(), any());
    } while (context.getAllValues().size() < 2);
    assertThat(context.getAllValues().stream().anyMatch(v -> v.httpRequest().firstMatchingHeader("X-Amz-Target").map(h -> h.equals("DynamoDB_20120810.DescribeEndpoints")).orElse(false))).isTrue();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) SdkException(software.amazon.awssdk.core.exception.SdkException) Test(org.junit.Test) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) Context(software.amazon.awssdk.core.interceptor.Context) Mockito(org.mockito.Mockito) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AnonymousCredentialsProvider(software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider) Region(software.amazon.awssdk.regions.Region) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test)

Example 4 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class ExecutionAttributesTest method syncClient_disableHostPrefixInjection_isPresent.

@Test
public void syncClient_disableHostPrefixInjection_isPresent() {
    ExecutionInterceptor interceptor = mock(ExecutionInterceptor.class);
    ArgumentCaptor<ExecutionAttributes> attributesCaptor = ArgumentCaptor.forClass(ExecutionAttributes.class);
    doThrow(new RuntimeException("BOOM")).when(interceptor).beforeExecution(any(Context.BeforeExecution.class), attributesCaptor.capture());
    ProtocolRestJsonClient sync = ProtocolRestJsonClient.builder().credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"))).region(Region.US_WEST_2).overrideConfiguration(c -> c.addExecutionInterceptor(interceptor).putAdvancedOption(SdkAdvancedClientOption.DISABLE_HOST_PREFIX_INJECTION, true)).build();
    thrown.expect(RuntimeException.class);
    try {
        sync.allTypes();
    } finally {
        ExecutionAttributes attributes = attributesCaptor.getValue();
        assertThat(attributes.getAttribute(SdkInternalExecutionAttribute.DISABLE_HOST_PREFIX_INJECTION)).isTrue();
        sync.close();
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) SdkAdvancedClientOption(software.amazon.awssdk.core.client.config.SdkAdvancedClientOption) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) ConcurrentMap(java.util.concurrent.ConcurrentMap) Context(software.amazon.awssdk.core.interceptor.Context) Mockito.doThrow(org.mockito.Mockito.doThrow) Rule(org.junit.Rule) ArgumentCaptor(org.mockito.ArgumentCaptor) ExecutionAttribute(software.amazon.awssdk.core.interceptor.ExecutionAttribute) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Region(software.amazon.awssdk.regions.Region) ExpectedException(org.junit.rules.ExpectedException) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) Mockito.mock(org.mockito.Mockito.mock) AllTypesRequest(software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) Test(org.junit.Test)

Example 5 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class ExecutionAttributesTest method asyncClient_disableHostPrefixInjection_isPresent.

@Test
public void asyncClient_disableHostPrefixInjection_isPresent() {
    ExecutionInterceptor interceptor = mock(ExecutionInterceptor.class);
    ArgumentCaptor<ExecutionAttributes> attributesCaptor = ArgumentCaptor.forClass(ExecutionAttributes.class);
    doThrow(new RuntimeException("BOOM")).when(interceptor).beforeExecution(any(Context.BeforeExecution.class), attributesCaptor.capture());
    ProtocolRestJsonAsyncClient async = ProtocolRestJsonAsyncClient.builder().credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"))).region(Region.US_WEST_2).overrideConfiguration(c -> c.addExecutionInterceptor(interceptor).putAdvancedOption(SdkAdvancedClientOption.DISABLE_HOST_PREFIX_INJECTION, true)).build();
    thrown.expect(CompletionException.class);
    try {
        async.allTypes().join();
    } finally {
        ExecutionAttributes attributes = attributesCaptor.getValue();
        assertThat(attributes.getAttribute(SdkInternalExecutionAttribute.DISABLE_HOST_PREFIX_INJECTION)).isTrue();
        async.close();
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) SdkAdvancedClientOption(software.amazon.awssdk.core.client.config.SdkAdvancedClientOption) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) ConcurrentMap(java.util.concurrent.ConcurrentMap) Context(software.amazon.awssdk.core.interceptor.Context) Mockito.doThrow(org.mockito.Mockito.doThrow) Rule(org.junit.Rule) ArgumentCaptor(org.mockito.ArgumentCaptor) ExecutionAttribute(software.amazon.awssdk.core.interceptor.ExecutionAttribute) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Region(software.amazon.awssdk.regions.Region) ExpectedException(org.junit.rules.ExpectedException) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) Mockito.mock(org.mockito.Mockito.mock) AllTypesRequest(software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) Test(org.junit.Test)

Aggregations

ExecutionInterceptor (software.amazon.awssdk.core.interceptor.ExecutionInterceptor)33 Test (org.junit.Test)25 Context (software.amazon.awssdk.core.interceptor.Context)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)12 ProtocolRestJsonAsyncClient (software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient)12 ProtocolRestJsonClient (software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient)12 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)11 ArgumentCaptor (org.mockito.ArgumentCaptor)10 Region (software.amazon.awssdk.regions.Region)10 Mockito.mock (org.mockito.Mockito.mock)9 SdkAdvancedClientOption (software.amazon.awssdk.core.client.config.SdkAdvancedClientOption)9 ExecutionAttribute (software.amazon.awssdk.core.interceptor.ExecutionAttribute)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CompletionException (java.util.concurrent.CompletionException)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)7 Rule (org.junit.Rule)7 ExpectedException (org.junit.rules.ExpectedException)7 Mockito.doThrow (org.mockito.Mockito.doThrow)7 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)7