use of software.amazon.awssdk.crt.s3.S3MetaRequestOptions in project aws-crt-java by awslabs.
the class S3NativeClient method getObject.
public CompletableFuture<GetObjectOutput> getObject(GetObjectRequest request, final ResponseDataConsumer<GetObjectOutput> dataHandler) {
final CompletableFuture<GetObjectOutput> resultFuture = new CompletableFuture<>();
final GetObjectOutput.Builder resultBuilder = GetObjectOutput.builder();
final S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {
private GetObjectOutput getObjectOutput;
@Override
public void onResponseHeaders(final int statusCode, final HttpHeader[] headers) {
for (int headerIndex = 0; headerIndex < headers.length; ++headerIndex) {
try {
populateGetObjectOutputHeader(resultBuilder, headers[headerIndex]);
} catch (Exception e) {
resultFuture.completeExceptionally(new RuntimeException(String.format("Could not process response header {%s}: " + headers[headerIndex].getName()), e));
}
}
populateGetObjectOutputUserMetadata(resultBuilder, headers);
dataHandler.onResponseHeaders(statusCode, headers);
getObjectOutput = resultBuilder.build();
dataHandler.onResponse(getObjectOutput);
}
@Override
public int onResponseBody(ByteBuffer bodyBytesIn, long objectRangeStart, long objectRangeEnd) {
dataHandler.onResponseData(bodyBytesIn);
return 0;
}
@Override
public void onFinished(int errorCode, int responseStatus, byte[] errorPayload) {
CrtS3RuntimeException ex = null;
try {
if (errorCode != CRT.AWS_CRT_SUCCESS) {
ex = new CrtS3RuntimeException(errorCode, responseStatus, errorPayload);
dataHandler.onException(ex);
} else {
dataHandler.onFinished();
}
} catch (Exception e) {
/* ignore user callback exception */
} finally {
if (ex != null) {
resultFuture.completeExceptionally(ex);
} else {
resultFuture.complete(getObjectOutput);
}
}
}
};
List<HttpHeader> headers = new LinkedList<>();
// TODO: additional logic needed for *special* partitions
headers.add(new HttpHeader("Host", request.bucket() + ".s3." + signingRegion + ".amazonaws.com"));
populateGetObjectRequestHeaders(header -> headers.add(header), request);
addCustomHeaders(headers, request.customHeaders());
String getObjectRequestQueryParameters = getGetObjectRequestQueryParameters(request);
String encodedPath = getEncodedPath(request.key(), getObjectRequestQueryParameters);
HttpRequest httpRequest = new HttpRequest("GET", encodedPath, headers.toArray(new HttpHeader[0]), null);
S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions().withMetaRequestType(S3MetaRequestOptions.MetaRequestType.GET_OBJECT).withHttpRequest(httpRequest).withResponseHandler(responseHandler);
try (final S3MetaRequest metaRequest = s3Client.makeMetaRequest(metaRequestOptions)) {
addCancelCheckToFuture(resultFuture, metaRequest);
return resultFuture;
}
}
use of software.amazon.awssdk.crt.s3.S3MetaRequestOptions in project aws-sdk-java-v2 by aws.
the class S3CrtAsyncHttpClient method execute.
@Override
public CompletableFuture<Void> execute(AsyncExecuteRequest request) {
CompletableFuture<Void> executeFuture = new CompletableFuture<>();
HttpRequest httpRequest = toCrtRequest(request);
S3CrtResponseHandlerAdapter responseHandler = new S3CrtResponseHandlerAdapter(executeFuture, request.responseHandler());
S3MetaRequestOptions.MetaRequestType requestType = requestType(request);
S3MetaRequestOptions requestOptions = new S3MetaRequestOptions().withHttpRequest(httpRequest).withMetaRequestType(requestType).withResponseHandler(responseHandler);
try (S3MetaRequest s3MetaRequest = crtS3Client.makeMetaRequest(requestOptions)) {
closeResourcesWhenComplete(executeFuture, s3MetaRequest);
}
return executeFuture;
}
use of software.amazon.awssdk.crt.s3.S3MetaRequestOptions in project aws-sdk-java-v2 by aws.
the class S3CrtAsyncHttpClientTest method putObject_shouldSetMetaRequestTypeCorrectly.
@Test
public void putObject_shouldSetMetaRequestTypeCorrectly() {
AsyncExecuteRequest asyncExecuteRequest = getExecuteRequestBuilder().putHttpExecutionAttribute(OPERATION_NAME, "PutObject").build();
ArgumentCaptor<S3MetaRequestOptions> s3MetaRequestOptionsArgumentCaptor = ArgumentCaptor.forClass(S3MetaRequestOptions.class);
asyncHttpClient.execute(asyncExecuteRequest);
verify(s3Client).makeMetaRequest(s3MetaRequestOptionsArgumentCaptor.capture());
S3MetaRequestOptions actual = s3MetaRequestOptionsArgumentCaptor.getValue();
assertThat(actual.getMetaRequestType()).isEqualTo(S3MetaRequestOptions.MetaRequestType.PUT_OBJECT);
}
use of software.amazon.awssdk.crt.s3.S3MetaRequestOptions in project aws-sdk-java-v2 by aws.
the class S3CrtAsyncHttpClientTest method putObject_shouldSetMetaRequestTypeCorrectly.
@Test
public void putObject_shouldSetMetaRequestTypeCorrectly() {
AsyncExecuteRequest asyncExecuteRequest = getExecuteRequestBuilder().putHttpExecutionAttribute(OPERATION_NAME, "PutObject").build();
ArgumentCaptor<S3MetaRequestOptions> s3MetaRequestOptionsArgumentCaptor = ArgumentCaptor.forClass(S3MetaRequestOptions.class);
asyncHttpClient.execute(asyncExecuteRequest);
verify(s3Client).makeMetaRequest(s3MetaRequestOptionsArgumentCaptor.capture());
S3MetaRequestOptions actual = s3MetaRequestOptionsArgumentCaptor.getValue();
assertThat(actual.getMetaRequestType()).isEqualTo(S3MetaRequestOptions.MetaRequestType.PUT_OBJECT);
}
use of software.amazon.awssdk.crt.s3.S3MetaRequestOptions in project aws-sdk-java-v2 by aws.
the class S3CrtAsyncHttpClientTest method defaultRequest_shouldSetMetaRequestOptionsCorrectly.
@Test
public void defaultRequest_shouldSetMetaRequestOptionsCorrectly() {
AsyncExecuteRequest asyncExecuteRequest = getExecuteRequestBuilder().build();
ArgumentCaptor<S3MetaRequestOptions> s3MetaRequestOptionsArgumentCaptor = ArgumentCaptor.forClass(S3MetaRequestOptions.class);
asyncHttpClient.execute(asyncExecuteRequest);
verify(s3Client).makeMetaRequest(s3MetaRequestOptionsArgumentCaptor.capture());
S3MetaRequestOptions actual = s3MetaRequestOptionsArgumentCaptor.getValue();
assertThat(actual.getMetaRequestType()).isEqualTo(S3MetaRequestOptions.MetaRequestType.DEFAULT);
assertThat(actual.getCredentialsProvider()).isNull();
assertThat(actual.getEndpoint().equals(DEFAULT_ENDPOINT));
HttpRequest httpRequest = actual.getHttpRequest();
assertThat(httpRequest.getEncodedPath()).isEqualTo("/key");
Map<String, String> headers = httpRequest.getHeaders().stream().collect(HashMap::new, (m, h) -> m.put(h.getName(), h.getValue()), Map::putAll);
assertThat(headers).hasSize(4).containsEntry("Host", DEFAULT_ENDPOINT.getHost()).containsEntry("custom-header", "foobar").containsEntry("amz-sdk-invocation-id", "1234").containsEntry("Content-Length", "100");
}
Aggregations