Search in sources :

Example 1 with S3MetaRequestOptions

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;
    }
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) CrtS3RuntimeException(software.amazon.awssdk.crt.s3.CrtS3RuntimeException) String(java.lang.String) ByteBuffer(java.nio.ByteBuffer) CrtS3RuntimeException(software.amazon.awssdk.crt.s3.CrtS3RuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CrtS3RuntimeException(software.amazon.awssdk.crt.s3.CrtS3RuntimeException) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader)

Example 2 with S3MetaRequestOptions

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;
}
Also used : SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) CompletableFuture(java.util.concurrent.CompletableFuture) S3MetaRequestOptions(software.amazon.awssdk.crt.s3.S3MetaRequestOptions) S3MetaRequest(software.amazon.awssdk.crt.s3.S3MetaRequest)

Example 3 with S3MetaRequestOptions

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);
}
Also used : AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) S3MetaRequestOptions(software.amazon.awssdk.crt.s3.S3MetaRequestOptions) Test(org.junit.Test)

Example 4 with S3MetaRequestOptions

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);
}
Also used : AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) S3MetaRequestOptions(software.amazon.awssdk.crt.s3.S3MetaRequestOptions) Test(org.junit.Test)

Example 5 with S3MetaRequestOptions

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");
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) S3MetaRequestOptions(software.amazon.awssdk.crt.s3.S3MetaRequestOptions) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

S3MetaRequestOptions (software.amazon.awssdk.crt.s3.S3MetaRequestOptions)8 Test (org.junit.Test)7 HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)7 AsyncExecuteRequest (software.amazon.awssdk.http.async.AsyncExecuteRequest)6 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)4 ByteBuffer (java.nio.ByteBuffer)3 HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 String (java.lang.String)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)2 CrtS3RuntimeException (software.amazon.awssdk.crt.s3.CrtS3RuntimeException)2 S3MetaRequest (software.amazon.awssdk.crt.s3.S3MetaRequest)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 BufferOverflowException (java.nio.BufferOverflowException)1