Search in sources :

Example 21 with SdkHttpFullResponse

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

the class EventStreamAsyncResponseTransformer method transformEventMessage.

private Iterable<EventT> transformEventMessage(Message message) throws Exception {
    SdkHttpFullResponse response = adaptMessageToResponse(message, false);
    if (message.getHeaders().get(":event-type").getString().equals("initial-response")) {
        ResponseT initialResponse = initialResponseHandler.handle(response, attributesFactory.get());
        eventStreamResponseHandler.responseReceived(initialResponse);
        log.debug(() -> getLogPrefix() + "Decoded initial response: " + initialResponse);
        return emptyList();
    }
    EventT event = eventResponseHandler.handle(response, attributesFactory.get());
    log.debug(() -> getLogPrefix() + "Decoded event: " + event);
    return singleton(event);
}
Also used : SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse)

Example 22 with SdkHttpFullResponse

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

the class CoreMetricsTest method testApiCall_serviceReturnsError_errorInfoIncludedInMetrics.

@Test
public void testApiCall_serviceReturnsError_errorInfoIncludedInMetrics() throws IOException {
    AbortableInputStream content = contentStream("{}");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder().statusCode(500).putHeader("x-amz-request-id", REQUEST_ID).putHeader("x-amz-id-2", EXTENDED_REQUEST_ID).putHeader("X-Amzn-Errortype", "EmptyModeledException").content(content).build();
    HttpExecuteResponse response = mockExecuteResponse(httpResponse);
    ExecutableHttpRequest mockExecuteRequest = mock(ExecutableHttpRequest.class);
    when(mockExecuteRequest.call()).thenReturn(response);
    when(mockHttpClient.prepareRequest(any(HttpExecuteRequest.class))).thenReturn(mockExecuteRequest);
    thrown.expect(EmptyModeledException.class);
    try {
        client.allTypes();
    } finally {
        ArgumentCaptor<MetricCollection> collectionCaptor = ArgumentCaptor.forClass(MetricCollection.class);
        verify(mockPublisher).publish(collectionCaptor.capture());
        MetricCollection capturedCollection = collectionCaptor.getValue();
        assertThat(capturedCollection.children()).hasSize(MAX_RETRIES + 1);
        assertThat(capturedCollection.metricValues(CoreMetric.RETRY_COUNT)).containsExactly(MAX_RETRIES);
        assertThat(capturedCollection.metricValues(CoreMetric.API_CALL_SUCCESSFUL)).containsExactly(false);
        for (MetricCollection requestMetrics : capturedCollection.children()) {
            // A service exception is still a successful HTTP execution so
            // we should still have HTTP metrics as well.
            assertThat(requestMetrics.metricValues(HttpMetric.HTTP_STATUS_CODE)).containsExactly(500);
            assertThat(requestMetrics.metricValues(CoreMetric.AWS_REQUEST_ID)).containsExactly(REQUEST_ID);
            assertThat(requestMetrics.metricValues(CoreMetric.AWS_EXTENDED_REQUEST_ID)).containsExactly(EXTENDED_REQUEST_ID);
            assertThat(requestMetrics.metricValues(CoreMetric.SERVICE_CALL_DURATION)).hasOnlyOneElementSatisfying(d -> {
                assertThat(d).isGreaterThanOrEqualTo(Duration.ZERO);
            });
            assertThat(requestMetrics.metricValues(CoreMetric.UNMARSHALLING_DURATION)).hasOnlyOneElementSatisfying(d -> {
                assertThat(d).isGreaterThanOrEqualTo(Duration.ZERO);
            });
        }
    }
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) MetricCollection(software.amazon.awssdk.metrics.MetricCollection) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest) Test(org.junit.Test)

Example 23 with SdkHttpFullResponse

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

the class CoreMetricsTest method setup.

@Before
public void setup() throws IOException {
    client = ProtocolRestJsonClient.builder().httpClient(mockHttpClient).region(Region.US_WEST_2).credentialsProvider(mockCredentialsProvider).overrideConfiguration(c -> c.addMetricPublisher(mockPublisher).retryPolicy(b -> b.numRetries(MAX_RETRIES))).build();
    AbortableInputStream content = contentStream("{}");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder().statusCode(200).putHeader("x-amz-request-id", REQUEST_ID).putHeader("x-amz-id-2", EXTENDED_REQUEST_ID).content(content).build();
    HttpExecuteResponse mockResponse = mockExecuteResponse(httpResponse);
    ExecutableHttpRequest mockExecuteRequest = mock(ExecutableHttpRequest.class);
    when(mockExecuteRequest.call()).thenAnswer(invocation -> {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        return mockResponse;
    });
    when(mockHttpClient.prepareRequest(any(HttpExecuteRequest.class))).thenReturn(mockExecuteRequest);
    when(mockCredentialsProvider.resolveCredentials()).thenAnswer(invocation -> {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        return AwsBasicCredentials.create("foo", "bar");
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) Mock(org.mockito.Mock) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) CoreMetric(software.amazon.awssdk.core.metrics.CoreMetric) MetricPublisher(software.amazon.awssdk.metrics.MetricPublisher) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteArrayInputStream(java.io.ByteArrayInputStream) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmptyModeledException(software.amazon.awssdk.services.protocolrestjson.model.EmptyModeledException) Duration(java.time.Duration) After(org.junit.After) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) Region(software.amazon.awssdk.regions.Region) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) HttpMetric(software.amazon.awssdk.http.HttpMetric) MetricCollection(software.amazon.awssdk.metrics.MetricCollection) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) PaginatedOperationWithResultKeyIterable(software.amazon.awssdk.services.protocolrestjson.paginators.PaginatedOperationWithResultKeyIterable) Collectors(java.util.stream.Collectors) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest) Mockito.verify(org.mockito.Mockito.verify) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) List(java.util.List) Rule(org.junit.Rule) SimpleStruct(software.amazon.awssdk.services.protocolrestjson.model.SimpleStruct) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest) Before(org.junit.Before)

Example 24 with SdkHttpFullResponse

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

the class SyncClientMetricPublisherResolutionTest method clientWithPublishers.

private ProtocolRestJsonClient clientWithPublishers(MetricPublisher... metricPublishers) throws IOException {
    ProtocolRestJsonClientBuilder builder = ProtocolRestJsonClient.builder().httpClient(mockHttpClient).region(Region.US_WEST_2).credentialsProvider(mockCredentialsProvider);
    AbortableInputStream content = AbortableInputStream.create(new ByteArrayInputStream("{}".getBytes()));
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder().statusCode(200).content(content).build();
    HttpExecuteResponse mockResponse = mockExecuteResponse(httpResponse);
    ExecutableHttpRequest mockExecuteRequest = mock(ExecutableHttpRequest.class);
    when(mockExecuteRequest.call()).thenAnswer(invocation -> {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        return mockResponse;
    });
    when(mockHttpClient.prepareRequest(any(HttpExecuteRequest.class))).thenReturn(mockExecuteRequest);
    when(mockCredentialsProvider.resolveCredentials()).thenAnswer(invocation -> {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        return AwsBasicCredentials.create("foo", "bar");
    });
    if (metricPublishers != null) {
        builder.overrideConfiguration(o -> o.metricPublishers(Arrays.asList(metricPublishers)));
    }
    return builder.build();
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProtocolRestJsonClientBuilder(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClientBuilder) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest)

Example 25 with SdkHttpFullResponse

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

the class CombinedResponseAsyncHttpResponseHandler method prepare.

@Override
public CompletableFuture<Response<OutputT>> prepare() {
    headersFuture = new CompletableFuture<>();
    CompletableFuture<OutputT> preparedTransformFuture = successResponseHandler.prepare();
    CompletableFuture<? extends SdkException> preparedErrorTransformFuture = errorResponseHandler == null ? null : errorResponseHandler.prepare();
    return headersFuture.thenCompose(headers -> {
        SdkHttpFullResponse sdkHttpFullResponse = toFullResponse(headers);
        if (headers.isSuccessful()) {
            return preparedTransformFuture.thenApply(r -> Response.<OutputT>builder().response(r).httpResponse(sdkHttpFullResponse).isSuccess(true).build());
        }
        if (preparedErrorTransformFuture != null) {
            return preparedErrorTransformFuture.thenApply(e -> Response.<OutputT>builder().exception(e).httpResponse(sdkHttpFullResponse).isSuccess(false).build());
        }
        return CompletableFuture.completedFuture(Response.<OutputT>builder().httpResponse(sdkHttpFullResponse).isSuccess(false).build());
    });
}
Also used : SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse)

Aggregations

SdkHttpFullResponse (software.amazon.awssdk.http.SdkHttpFullResponse)36 Test (org.junit.jupiter.api.Test)12 Test (org.junit.Test)11 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)10 InputStream (java.io.InputStream)6 List (java.util.List)6 ExecutableHttpRequest (software.amazon.awssdk.http.ExecutableHttpRequest)6 StringInputStream (software.amazon.awssdk.utils.StringInputStream)6 XmlElement (software.amazon.awssdk.protocols.query.unmarshall.XmlElement)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Duration (java.time.Duration)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 Before (org.junit.Before)4 SdkBytes (software.amazon.awssdk.core.SdkBytes)4 SdkPojo (software.amazon.awssdk.core.SdkPojo)4 Crc32ChecksumValidatingInputStream (software.amazon.awssdk.core.internal.util.Crc32ChecksumValidatingInputStream)4 HttpExecuteResponse (software.amazon.awssdk.http.HttpExecuteResponse)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3