Search in sources :

Example 11 with HttpExecuteRequest

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

the class MakeHttpRequestStageTest method testExecute_contextContainsMetricCollector_addsChildToExecuteRequest.

@Test
public void testExecute_contextContainsMetricCollector_addsChildToExecuteRequest() {
    SdkHttpFullRequest sdkRequest = SdkHttpFullRequest.builder().method(SdkHttpMethod.GET).host("mybucket.s3.us-west-2.amazonaws.com").protocol("https").build();
    MetricCollector mockCollector = mock(MetricCollector.class);
    MetricCollector childCollector = mock(MetricCollector.class);
    when(mockCollector.createChild(any(String.class))).thenReturn(childCollector);
    ExecutionContext executionContext = ExecutionContext.builder().executionAttributes(new ExecutionAttributes()).build();
    RequestExecutionContext context = RequestExecutionContext.builder().originalRequest(ValidSdkObjects.sdkRequest()).executionContext(executionContext).build();
    context.attemptMetricCollector(mockCollector);
    context.apiCallAttemptTimeoutTracker(mock(TimeoutTracker.class));
    context.apiCallTimeoutTracker(mock(TimeoutTracker.class));
    try {
        stage.execute(sdkRequest, context);
    } catch (Exception e) {
    // ignored, don't really care about successful execution of the stage in this case
    } finally {
        ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class);
        verify(mockCollector).createChild(eq("HttpClient"));
        verify(mockClient).prepareRequest(httpRequestCaptor.capture());
        assertThat(httpRequestCaptor.getValue().metricCollector()).contains(childCollector);
    }
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) RequestExecutionContext(software.amazon.awssdk.core.internal.http.RequestExecutionContext) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) TimeoutTracker(software.amazon.awssdk.core.internal.http.timers.TimeoutTracker) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) RequestExecutionContext(software.amazon.awssdk.core.internal.http.RequestExecutionContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with HttpExecuteRequest

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

the class AmazonHttpClientTest method testUserAgentContainsRetryModeInfo.

@Test
public void testUserAgentContainsRetryModeInfo() {
    HttpResponseHandler<?> handler = mock(HttpResponseHandler.class);
    SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder().option(SdkClientOption.SYNC_HTTP_CLIENT, sdkHttpClient).option(SdkClientOption.CLIENT_TYPE, ClientType.SYNC).option(SdkClientOption.ENDPOINT, URI.create("http://example.com")).option(SdkClientOption.RETRY_POLICY, RetryPolicy.forRetryMode(RetryMode.STANDARD)).build();
    AmazonSyncHttpClient client = new AmazonSyncHttpClient(config);
    client.requestExecutionBuilder().request(ValidSdkObjects.sdkHttpFullRequest().build()).originalRequest(NoopTestRequest.builder().build()).executionContext(ClientExecutionAndRequestTimerTestUtils.executionContext(null)).execute(combinedSyncResponseHandler(handler, null));
    ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class);
    verify(sdkHttpClient).prepareRequest(httpRequestCaptor.capture());
    final String userAgent = httpRequestCaptor.getValue().httpRequest().firstMatchingHeader("User-Agent").orElseThrow(() -> new AssertionError("User-Agent header was not found"));
    Assert.assertTrue(userAgent.contains("cfg/retry-mode/standard"));
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) AmazonSyncHttpClient(software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient) Test(org.junit.Test)

Example 13 with HttpExecuteRequest

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

the class AmazonHttpClientTest method testUserAgentPrefixAndSuffixAreAdded.

@Test
public void testUserAgentPrefixAndSuffixAreAdded() {
    String prefix = "somePrefix";
    String suffix = "someSuffix-blah-blah";
    HttpResponseHandler<?> handler = mock(HttpResponseHandler.class);
    SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder().option(SdkAdvancedClientOption.USER_AGENT_PREFIX, prefix).option(SdkAdvancedClientOption.USER_AGENT_SUFFIX, suffix).option(SdkClientOption.SYNC_HTTP_CLIENT, sdkHttpClient).option(SdkClientOption.ENDPOINT, URI.create("http://example.com")).build();
    AmazonSyncHttpClient client = new AmazonSyncHttpClient(config);
    client.requestExecutionBuilder().request(ValidSdkObjects.sdkHttpFullRequest().build()).originalRequest(NoopTestRequest.builder().build()).executionContext(ClientExecutionAndRequestTimerTestUtils.executionContext(null)).execute(combinedSyncResponseHandler(handler, null));
    ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class);
    verify(sdkHttpClient).prepareRequest(httpRequestCaptor.capture());
    final String userAgent = httpRequestCaptor.getValue().httpRequest().firstMatchingHeader("User-Agent").orElseThrow(() -> new AssertionError("User-Agent header was not found"));
    Assert.assertTrue(userAgent.startsWith(prefix));
    Assert.assertTrue(userAgent.endsWith(suffix));
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) AmazonSyncHttpClient(software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient) Test(org.junit.Test)

Example 14 with HttpExecuteRequest

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

the class AmazonHttpClientTest method testUserAgentContainsHttpClientInfo.

@Test
public void testUserAgentContainsHttpClientInfo() {
    HttpResponseHandler<?> handler = mock(HttpResponseHandler.class);
    SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder().option(SdkClientOption.SYNC_HTTP_CLIENT, sdkHttpClient).option(SdkClientOption.CLIENT_TYPE, ClientType.SYNC).option(SdkClientOption.ENDPOINT, URI.create("http://example.com")).build();
    AmazonSyncHttpClient client = new AmazonSyncHttpClient(config);
    client.requestExecutionBuilder().request(ValidSdkObjects.sdkHttpFullRequest().build()).originalRequest(NoopTestRequest.builder().build()).executionContext(ClientExecutionAndRequestTimerTestUtils.executionContext(null)).execute(combinedSyncResponseHandler(handler, null));
    ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class);
    verify(sdkHttpClient).prepareRequest(httpRequestCaptor.capture());
    final String userAgent = httpRequestCaptor.getValue().httpRequest().firstMatchingHeader("User-Agent").orElseThrow(() -> new AssertionError("User-Agent header was not found"));
    Assert.assertTrue(userAgent.contains("io/sync"));
    Assert.assertTrue(userAgent.contains("http/UNKNOWN"));
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) AmazonSyncHttpClient(software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient) Test(org.junit.Test)

Example 15 with HttpExecuteRequest

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

the class S3MrapIntegrationTest method applyPresignedUrl.

private String applyPresignedUrl(PresignedRequest presignedRequest, String content) {
    try {
        HttpExecuteRequest.Builder builder = HttpExecuteRequest.builder().request(presignedRequest.httpRequest());
        if (!isEmpty(content)) {
            builder.contentStreamProvider(() -> new StringInputStream(content));
        }
        HttpExecuteRequest request = builder.build();
        HttpExecuteResponse response = ApacheHttpClient.create().prepareRequest(request).call();
        return response.responseBody().map(stream -> invokeSafely(() -> IoUtils.toUtf8String(stream))).orElseThrow(() -> new IOException("No input stream"));
    } catch (IOException e) {
        log.error(() -> "Error occurred ", e);
    }
    return null;
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) ApacheHttpClient(software.amazon.awssdk.http.apache.ApacheHttpClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BucketAlreadyOwnedByYouException(software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException) Context(software.amazon.awssdk.core.interceptor.Context) ListMultiRegionAccessPointsResponse(software.amazon.awssdk.services.s3control.model.ListMultiRegionAccessPointsResponse) BucketAlreadyExistsException(software.amazon.awssdk.services.s3control.model.BucketAlreadyExistsException) SignerConstant(software.amazon.awssdk.auth.signer.internal.SignerConstant) Duration(java.time.Duration) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) StsClient(software.amazon.awssdk.services.sts.StsClient) CreateMultiRegionAccessPointInput(software.amazon.awssdk.services.s3control.model.CreateMultiRegionAccessPointInput) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) StringUtils.isEmpty(software.amazon.awssdk.utils.StringUtils.isEmpty) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) S3SignerExecutionAttribute(software.amazon.awssdk.auth.signer.S3SignerExecutionAttribute) MultiRegionAccessPointStatus(software.amazon.awssdk.services.s3control.model.MultiRegionAccessPointStatus) StandardCharsets(java.nio.charset.StandardCharsets) Waiter(software.amazon.awssdk.core.waiters.Waiter) List(java.util.List) StringInputStream(software.amazon.awssdk.utils.StringInputStream) Optional(java.util.Optional) S3Configuration(software.amazon.awssdk.services.s3.S3Configuration) SdkRequest(software.amazon.awssdk.core.SdkRequest) Bucket(software.amazon.awssdk.services.s3.model.Bucket) NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) BeforeClass(org.junit.BeforeClass) GetMultiRegionAccessPointResponse(software.amazon.awssdk.services.s3control.model.GetMultiRegionAccessPointResponse) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) ArrayList(java.util.ArrayList) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) Region(software.amazon.awssdk.regions.Region) WaiterAcceptor(software.amazon.awssdk.core.waiters.WaiterAcceptor) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) S3Client(software.amazon.awssdk.services.s3.S3Client) Logger(software.amazon.awssdk.utils.Logger) IOException(java.io.IOException) Test(org.junit.Test) FixedDelayBackoffStrategy(software.amazon.awssdk.core.retry.backoff.FixedDelayBackoffStrategy) RequestBody(software.amazon.awssdk.core.sync.RequestBody) CreateMultiRegionAccessPointResponse(software.amazon.awssdk.services.s3control.model.CreateMultiRegionAccessPointResponse) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ClientOverrideConfiguration(software.amazon.awssdk.core.client.config.ClientOverrideConfiguration) IoUtils(software.amazon.awssdk.utils.IoUtils) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) IOException(java.io.IOException)

Aggregations

HttpExecuteRequest (software.amazon.awssdk.http.HttpExecuteRequest)26 HttpExecuteResponse (software.amazon.awssdk.http.HttpExecuteResponse)11 SdkHttpClient (software.amazon.awssdk.http.SdkHttpClient)11 Test (org.junit.Test)10 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)7 StringInputStream (software.amazon.awssdk.utils.StringInputStream)7 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)5 Duration (java.time.Duration)4 Optional (java.util.Optional)4 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)4 IoUtils (software.amazon.awssdk.utils.IoUtils)4 HttpURLConnection (java.net.HttpURLConnection)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 SdkClientConfiguration (software.amazon.awssdk.core.client.config.SdkClientConfiguration)3 AmazonSyncHttpClient (software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient)3 ContentStreamProvider (software.amazon.awssdk.http.ContentStreamProvider)3 MetricCollector (software.amazon.awssdk.metrics.MetricCollector)3 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)2