Search in sources :

Example 1 with ExecutionContext

use of software.amazon.awssdk.core.http.ExecutionContext in project aws-sdk-java-v2 by aws.

the class AsyncHttpClientApiCallTimeoutTests method errorResponse_SlowErrorResponseHandler_ThrowsApiCallTimeoutException.

@Test
public void errorResponse_SlowErrorResponseHandler_ThrowsApiCallTimeoutException() {
    stubFor(get(anyUrl()).willReturn(aResponse().withStatus(500).withBody("{}")));
    ExecutionContext executionContext = ClientExecutionAndRequestTimerTestUtils.executionContext(null);
    CompletableFuture future = httpClient.requestExecutionBuilder().originalRequest(NoopTestRequest.builder().build()).executionContext(executionContext).request(generateRequest()).execute(combinedAsyncResponseHandler(noOpResponseHandler(), superSlowResponseHandler(API_CALL_TIMEOUT.toMillis())));
    assertThatThrownBy(future::join).hasCauseInstanceOf(ApiCallTimeoutException.class);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) Test(org.junit.Test)

Example 2 with ExecutionContext

use of software.amazon.awssdk.core.http.ExecutionContext in project aws-sdk-java-v2 by aws.

the class HttpClientApiCallAttemptTimeoutTest method errorResponse_SlowErrorResponseHandler_ThrowsApiCallTimeoutException.

@Test
public void errorResponse_SlowErrorResponseHandler_ThrowsApiCallTimeoutException() {
    stubFor(get(anyUrl()).willReturn(aResponse().withStatus(500).withBody("{}")));
    ExecutionContext executionContext = ClientExecutionAndRequestTimerTestUtils.executionContext(null);
    assertThatThrownBy(() -> httpClient.requestExecutionBuilder().originalRequest(NoopTestRequest.builder().build()).executionContext(executionContext).request(generateRequest()).execute(combinedSyncResponseHandler(null, superSlowResponseHandler(API_CALL_TIMEOUT.toMillis())))).isInstanceOf(ApiCallAttemptTimeoutException.class);
}
Also used : ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) Test(org.junit.Test)

Example 3 with ExecutionContext

use of software.amazon.awssdk.core.http.ExecutionContext in project aws-sdk-java-v2 by aws.

the class DefaultS3Presigner method initializePresignedRequest.

/**
 * Initialize the provided presigned request.
 */
private void initializePresignedRequest(PresignedRequest.Builder presignedRequest, ExecutionContext execCtx, SdkHttpFullRequest signedHttpRequest) {
    SdkBytes signedPayload = signedHttpRequest.contentStreamProvider().map(p -> SdkBytes.fromInputStream(p.newStream())).orElse(null);
    List<String> signedHeadersQueryParam = signedHttpRequest.rawQueryParameters().get("X-Amz-SignedHeaders");
    Validate.validState(signedHeadersQueryParam != null, "Only SigV4 presigners are supported at this time, but the configured " + "presigner (%s) did not seem to generate a SigV4 signature.", execCtx.signer());
    Map<String, List<String>> signedHeaders = signedHeadersQueryParam.stream().flatMap(h -> Stream.of(h.split(";"))).collect(toMap(h -> h, h -> signedHttpRequest.firstMatchingHeader(h).map(Collections::singletonList).orElseGet(ArrayList::new)));
    boolean isBrowserExecutable = signedHttpRequest.method() == SdkHttpMethod.GET && signedPayload == null && (signedHeaders.isEmpty() || (signedHeaders.size() == 1 && signedHeaders.containsKey("host")));
    presignedRequest.expiration(execCtx.executionAttributes().getAttribute(PRESIGNER_EXPIRATION)).isBrowserExecutable(isBrowserExecutable).httpRequest(signedHttpRequest).signedHeaders(signedHeaders).signedPayload(signedPayload);
}
Also used : InterceptorContext(software.amazon.awssdk.core.interceptor.InterceptorContext) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) SdkBytes(software.amazon.awssdk.core.SdkBytes) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) SdkClientOption(software.amazon.awssdk.core.client.config.SdkClientOption) URI(java.net.URI) Validate(software.amazon.awssdk.utils.Validate) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) PresignRequest(software.amazon.awssdk.awscore.presigner.PresignRequest) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) PresignedCompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCompleteMultipartUploadRequest) Instant(java.time.Instant) PutObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.PutObjectRequestMarshaller) UploadPartPresignRequest(software.amazon.awssdk.services.s3.presigner.model.UploadPartPresignRequest) List(java.util.List) Stream(java.util.stream.Stream) PresignedCreateMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCreateMultipartUploadRequest) Optional(java.util.Optional) S3Configuration(software.amazon.awssdk.services.s3.S3Configuration) SdkRequest(software.amazon.awssdk.core.SdkRequest) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) PRESIGNER_EXPIRATION(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute.PRESIGNER_EXPIRATION) CollectionUtils.mergeLists(software.amazon.awssdk.utils.CollectionUtils.mergeLists) AbortMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.AbortMultipartUploadRequestMarshaller) ExecutionInterceptorChain(software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain) ClientType(software.amazon.awssdk.core.ClientType) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) Function(java.util.function.Function) PresignedAbortMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedAbortMultipartUploadRequest) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) ArrayList(java.util.ArrayList) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Presigner(software.amazon.awssdk.core.signer.Presigner) ClasspathInterceptorChainFactory(software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory) GetObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.GetObjectRequestMarshaller) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkDefaultClientBuilder(software.amazon.awssdk.core.client.builder.SdkDefaultClientBuilder) UploadPartRequestMarshaller(software.amazon.awssdk.services.s3.transform.UploadPartRequestMarshaller) CompleteMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CompleteMultipartUploadRequestMarshaller) AwsSignerExecutionAttribute(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) SdkExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkExecutionAttribute) CreateMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CreateMultipartUploadPresignRequest) AwsExecutionContextBuilder(software.amazon.awssdk.awscore.internal.AwsExecutionContextBuilder) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Logger(software.amazon.awssdk.utils.Logger) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) CompleteMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CompleteMultipartUploadPresignRequest) GetObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest) AwsExecutionAttribute(software.amazon.awssdk.awscore.AwsExecutionAttribute) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) AwsS3ProtocolFactory(software.amazon.awssdk.protocols.xml.AwsS3ProtocolFactory) AwsS3V4Signer(software.amazon.awssdk.auth.signer.AwsS3V4Signer) AbortMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.AbortMultipartUploadPresignRequest) CreateMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CreateMultipartUploadRequestMarshaller) AwsDefaultClientBuilder(software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder) DefaultServiceEndpointBuilder(software.amazon.awssdk.awscore.endpoint.DefaultServiceEndpointBuilder) RequestBody(software.amazon.awssdk.core.sync.RequestBody) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) Collections(java.util.Collections) IoUtils(software.amazon.awssdk.utils.IoUtils) SdkBytes(software.amazon.awssdk.core.SdkBytes) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with ExecutionContext

use of software.amazon.awssdk.core.http.ExecutionContext in project aws-sdk-java-v2 by aws.

the class DefaultS3Presigner method addRequestLevelHeadersAndQueryParameters.

/**
 * Update the provided HTTP request by adding any HTTP headers or query parameters specified as part of the
 * {@link SdkRequest}.
 */
private void addRequestLevelHeadersAndQueryParameters(ExecutionContext execCtx) {
    SdkHttpRequest httpRequest = execCtx.interceptorContext().httpRequest();
    SdkRequest sdkRequest = execCtx.interceptorContext().request();
    SdkHttpRequest updatedHttpRequest = httpRequest.toBuilder().applyMutation(b -> addRequestLevelHeaders(b, sdkRequest)).applyMutation(b -> addRequestLeveQueryParameters(b, sdkRequest)).build();
    execCtx.interceptorContext(execCtx.interceptorContext().copy(c -> c.httpRequest(updatedHttpRequest)));
}
Also used : InterceptorContext(software.amazon.awssdk.core.interceptor.InterceptorContext) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) SdkBytes(software.amazon.awssdk.core.SdkBytes) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) SdkClientOption(software.amazon.awssdk.core.client.config.SdkClientOption) URI(java.net.URI) Validate(software.amazon.awssdk.utils.Validate) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) PresignRequest(software.amazon.awssdk.awscore.presigner.PresignRequest) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) PresignedCompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCompleteMultipartUploadRequest) Instant(java.time.Instant) PutObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.PutObjectRequestMarshaller) UploadPartPresignRequest(software.amazon.awssdk.services.s3.presigner.model.UploadPartPresignRequest) List(java.util.List) Stream(java.util.stream.Stream) PresignedCreateMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCreateMultipartUploadRequest) Optional(java.util.Optional) S3Configuration(software.amazon.awssdk.services.s3.S3Configuration) SdkRequest(software.amazon.awssdk.core.SdkRequest) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) PRESIGNER_EXPIRATION(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute.PRESIGNER_EXPIRATION) CollectionUtils.mergeLists(software.amazon.awssdk.utils.CollectionUtils.mergeLists) AbortMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.AbortMultipartUploadRequestMarshaller) ExecutionInterceptorChain(software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain) ClientType(software.amazon.awssdk.core.ClientType) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) Function(java.util.function.Function) PresignedAbortMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedAbortMultipartUploadRequest) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) ArrayList(java.util.ArrayList) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Presigner(software.amazon.awssdk.core.signer.Presigner) ClasspathInterceptorChainFactory(software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory) GetObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.GetObjectRequestMarshaller) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkDefaultClientBuilder(software.amazon.awssdk.core.client.builder.SdkDefaultClientBuilder) UploadPartRequestMarshaller(software.amazon.awssdk.services.s3.transform.UploadPartRequestMarshaller) CompleteMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CompleteMultipartUploadRequestMarshaller) AwsSignerExecutionAttribute(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) SdkExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkExecutionAttribute) CreateMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CreateMultipartUploadPresignRequest) AwsExecutionContextBuilder(software.amazon.awssdk.awscore.internal.AwsExecutionContextBuilder) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Logger(software.amazon.awssdk.utils.Logger) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) CompleteMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CompleteMultipartUploadPresignRequest) GetObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest) AwsExecutionAttribute(software.amazon.awssdk.awscore.AwsExecutionAttribute) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) AwsS3ProtocolFactory(software.amazon.awssdk.protocols.xml.AwsS3ProtocolFactory) AwsS3V4Signer(software.amazon.awssdk.auth.signer.AwsS3V4Signer) AbortMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.AbortMultipartUploadPresignRequest) CreateMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CreateMultipartUploadRequestMarshaller) AwsDefaultClientBuilder(software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder) DefaultServiceEndpointBuilder(software.amazon.awssdk.awscore.endpoint.DefaultServiceEndpointBuilder) RequestBody(software.amazon.awssdk.core.sync.RequestBody) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) Collections(java.util.Collections) IoUtils(software.amazon.awssdk.utils.IoUtils) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) SdkRequest(software.amazon.awssdk.core.SdkRequest)

Example 5 with ExecutionContext

use of software.amazon.awssdk.core.http.ExecutionContext in project aws-sdk-java-v2 by aws.

the class AwsExecutionContextBuilderTest method verifyCoreExecutionAttributesTakePrecedence.

@Test
public void verifyCoreExecutionAttributesTakePrecedence() {
    ExecutionAttributes requestOverrides = ExecutionAttributes.builder().put(SdkExecutionAttribute.SERVICE_NAME, "RequestOverrideServiceName").build();
    Optional requestOverrideConfiguration = Optional.of(AwsRequestOverrideConfiguration.builder().executionAttributes(requestOverrides).build());
    when(sdkRequest.overrideConfiguration()).thenReturn(requestOverrideConfiguration);
    ExecutionAttributes clientConfigOverrides = ExecutionAttributes.builder().put(SdkExecutionAttribute.SERVICE_NAME, "ClientConfigServiceName").build();
    SdkClientConfiguration testClientConfiguration = testClientConfiguration().option(SdkClientOption.SERVICE_NAME, "DoNotOverrideService").option(SdkClientOption.EXECUTION_ATTRIBUTES, clientConfigOverrides).build();
    ExecutionContext executionContext = AwsExecutionContextBuilder.invokeInterceptorsAndCreateExecutionContext(clientExecutionParams(), testClientConfiguration);
    assertThat(executionContext.executionAttributes().getAttribute(SdkExecutionAttribute.SERVICE_NAME)).isEqualTo("DoNotOverrideService");
}
Also used : ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) Optional(java.util.Optional) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) Test(org.junit.Test)

Aggregations

ExecutionContext (software.amazon.awssdk.core.http.ExecutionContext)18 Test (org.junit.Test)9 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)9 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)8 Optional (java.util.Optional)6 SdkClientConfiguration (software.amazon.awssdk.core.client.config.SdkClientConfiguration)5 ExecutionInterceptorChain (software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain)5 InterceptorContext (software.amazon.awssdk.core.interceptor.InterceptorContext)5 URI (java.net.URI)4 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)4 SdkRequest (software.amazon.awssdk.core.SdkRequest)4 SdkClientOption (software.amazon.awssdk.core.client.config.SdkClientOption)4 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 Function (java.util.function.Function)3 Collectors.toMap (java.util.stream.Collectors.toMap)3 Stream (java.util.stream.Stream)3