Search in sources :

Example 1 with SdkResponse

use of software.amazon.awssdk.core.SdkResponse in project aws-xray-sdk-java by aws.

the class TracingInterceptor method extractResponseParameters.

private HashMap<String, Object> extractResponseParameters(Context.AfterExecution context, ExecutionAttributes executionAttributes) {
    HashMap<String, Object> parameters = new HashMap<>();
    AWSOperationHandler operationHandler = getOperationHandler(executionAttributes);
    if (operationHandler == null) {
        return parameters;
    }
    if (operationHandler.getResponseParameters() != null) {
        operationHandler.getResponseParameters().forEach((parameterName) -> {
            SdkResponse response = context.response();
            Optional<Object> parameterValue = response.getValueForField(parameterName, Object.class);
            if (parameterValue.isPresent()) {
                parameters.put(StringTransform.toSnakeCase(parameterName), parameterValue.get());
            }
        });
    }
    if (operationHandler.getResponseDescriptors() != null) {
        operationHandler.getResponseDescriptors().forEach((key, descriptor) -> {
            if (descriptor.isMap() && descriptor.shouldGetKeys()) {
                SdkResponse response = context.response();
                Optional<Map> parameterValue = response.getValueForField(key, Map.class);
                if (parameterValue.isPresent()) {
                    String renameTo = descriptor.getRenameTo() != null ? descriptor.getRenameTo() : key;
                    parameters.put(StringTransform.toSnakeCase(renameTo), parameterValue.get().keySet());
                }
            } else if (descriptor.isList() && descriptor.shouldGetCount()) {
                SdkResponse response = context.response();
                Optional<List> parameterValue = response.getValueForField(key, List.class);
                if (parameterValue.isPresent()) {
                    String renameTo = descriptor.getRenameTo() != null ? descriptor.getRenameTo() : key;
                    parameters.put(StringTransform.toSnakeCase(renameTo), parameterValue.get().size());
                }
            }
        });
    }
    return parameters;
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) AWSOperationHandler(com.amazonaws.xray.handlers.config.AWSOperationHandler) SdkResponse(software.amazon.awssdk.core.SdkResponse) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SdkResponse

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

the class EnableTrailingChecksumInterceptor method modifyResponse.

/**
 * Subtract the contentLength of {@link GetObjectResponse} if trailing checksums is enabled.
 */
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
    SdkResponse response = context.response();
    SdkHttpResponse httpResponse = context.httpResponse();
    if (getObjectChecksumEnabledPerResponse(context.request(), httpResponse)) {
        GetObjectResponse getResponse = (GetObjectResponse) response;
        Long contentLength = getResponse.contentLength();
        Validate.notNull(contentLength, "Service returned null 'Content-Length'.");
        return getResponse.toBuilder().contentLength(contentLength - S3_MD5_CHECKSUM_LENGTH).build();
    }
    return response;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse)

Example 3 with SdkResponse

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

the class EventStreamAsyncResponseTransformerTest method multipleEventsInChunk_OnlyDeliversOneEvent.

@Test
public void multipleEventsInChunk_OnlyDeliversOneEvent() throws InterruptedException {
    Message eventMessage = new Message(ImmutableMap.of(":message-type", HeaderValue.fromString("event"), ":event-type", HeaderValue.fromString("foo")), new byte[0]);
    CountDownLatch latch = new CountDownLatch(1);
    Flowable<ByteBuffer> bytePublisher = Flowable.just(eventMessage.toByteBuffer(), eventMessage.toByteBuffer()).doOnCancel(latch::countDown);
    AtomicInteger numEvents = new AtomicInteger(0);
    // Request one event then cancel
    Subscriber<Object> requestOneSubscriber = new Subscriber<Object>() {

        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription subscription) {
            this.subscription = subscription;
            subscription.request(1);
        }

        @Override
        public void onNext(Object o) {
            numEvents.incrementAndGet();
            subscription.cancel();
        }

        @Override
        public void onError(Throwable throwable) {
        }

        @Override
        public void onComplete() {
        }
    };
    AsyncResponseTransformer<SdkResponse, Void> transformer = EventStreamAsyncResponseTransformer.builder().eventStreamResponseHandler(onEventStream(p -> p.subscribe(requestOneSubscriber))).eventResponseHandler((r, e) -> new Object()).executor(Executors.newSingleThreadExecutor()).future(new CompletableFuture<>()).build();
    transformer.prepare();
    transformer.onStream(SdkPublisher.adapt(bytePublisher));
    latch.await();
    assertThat(numEvents).as("Expected only one event to be delivered").hasValue(1);
}
Also used : ImmutableMap(software.amazon.awssdk.utils.ImmutableMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) Message(software.amazon.eventstream.Message) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Subscription(org.reactivestreams.Subscription) SdkResponse(software.amazon.awssdk.core.SdkResponse) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) Subscriber(org.reactivestreams.Subscriber) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscriber(org.reactivestreams.Subscriber) SdkResponse(software.amazon.awssdk.core.SdkResponse) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 4 with SdkResponse

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

the class EventStreamAsyncResponseTransformerTest method verifyExceptionThrown.

private void verifyExceptionThrown(Map<String, HeaderValue> headers) {
    SdkServiceException exception = SdkServiceException.builder().build();
    Message exceptionMessage = new Message(headers, new byte[0]);
    Flowable<ByteBuffer> bytePublisher = Flowable.just(exceptionMessage.toByteBuffer());
    SubscribingResponseHandler handler = new SubscribingResponseHandler();
    AsyncResponseTransformer<SdkResponse, Void> transformer = EventStreamAsyncResponseTransformer.builder().eventStreamResponseHandler(handler).exceptionResponseHandler((response, executionAttributes) -> exception).executor(Executors.newSingleThreadExecutor()).future(new CompletableFuture<>()).build();
    CompletableFuture<Void> cf = transformer.prepare();
    transformer.onResponse(null);
    transformer.onStream(SdkPublisher.adapt(bytePublisher));
    assertThatThrownBy(() -> {
        try {
            cf.join();
        } catch (CompletionException e) {
            if (e.getCause() instanceof SdkServiceException) {
                throw e.getCause();
            }
        }
    }).isSameAs(exception);
    assertThat(handler.exceptionOccurredCalled).isTrue();
}
Also used : ImmutableMap(software.amazon.awssdk.utils.ImmutableMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) Message(software.amazon.eventstream.Message) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Subscription(org.reactivestreams.Subscription) SdkResponse(software.amazon.awssdk.core.SdkResponse) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) Subscriber(org.reactivestreams.Subscriber) HeaderValue(software.amazon.eventstream.HeaderValue) CompletableFuture(java.util.concurrent.CompletableFuture) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) Message(software.amazon.eventstream.Message) CompletionException(java.util.concurrent.CompletionException) SdkResponse(software.amazon.awssdk.core.SdkResponse) ByteBuffer(java.nio.ByteBuffer)

Example 5 with SdkResponse

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

the class AsyncClientHandlerTransformerVerificationTest method testSetup.

@Before
public void testSetup() throws Exception {
    mockClient = mock(SdkAsyncHttpClient.class);
    executionParams = new ClientExecutionParams<SdkRequest, SdkResponse>().withInput(request).withMarshaller(marshaller).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler);
    SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder().option(SdkClientOption.ASYNC_HTTP_CLIENT, mockClient).option(SdkClientOption.RETRY_POLICY, RETRY_POLICY).option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR, Runnable::run).build();
    clientHandler = new SdkAsyncClientHandler(config);
    when(request.overrideConfiguration()).thenReturn(Optional.empty());
    when(marshaller.marshall(eq(request))).thenReturn(ValidSdkObjects.sdkHttpFullRequest().build());
    when(responseHandler.handle(any(), any())).thenReturn(VoidSdkResponse.builder().build());
}
Also used : SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) SdkRequest(software.amazon.awssdk.core.SdkRequest) VoidSdkResponse(software.amazon.awssdk.core.protocol.VoidSdkResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) SdkAsyncHttpClient(software.amazon.awssdk.http.async.SdkAsyncHttpClient) Before(org.junit.Before)

Aggregations

SdkResponse (software.amazon.awssdk.core.SdkResponse)25 CompletableFuture (java.util.concurrent.CompletableFuture)12 Test (org.junit.Test)12 ByteBuffer (java.nio.ByteBuffer)11 VoidSdkResponse (software.amazon.awssdk.core.protocol.VoidSdkResponse)11 HashMap (java.util.HashMap)9 Consumer (java.util.function.Consumer)9 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)9 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 SdkPublisher (software.amazon.awssdk.core.async.SdkPublisher)8 SdkServiceException (software.amazon.awssdk.core.exception.SdkServiceException)8 Map (java.util.Map)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Flowable (io.reactivex.Flowable)6 List (java.util.List)6 CompletionException (java.util.concurrent.CompletionException)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Executors (java.util.concurrent.Executors)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6