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;
}
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;
}
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);
}
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();
}
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());
}
Aggregations