Search in sources :

Example 6 with HeaderValue

use of software.amazon.eventstream.HeaderValue in project aws-sdk-java-v2 by aws.

the class SelectObjectContentTest method encodedEvents.

private byte[] encodedEvents(List<SelectObjectContentEventStream> events) {
    ByteArrayOutputStream eventStreamBytes = new ByteArrayOutputStream();
    MarshallingVisitor marshaller = new MarshallingVisitor();
    events.stream().map(e -> {
        marshaller.reset();
        e.accept(marshaller);
        Map<String, HeaderValue> headers = new HashMap<>();
        headers.put(":message-type", HeaderValue.fromString("event"));
        headers.put(":event-type", HeaderValue.fromString(e.sdkEventType().toString()));
        return new Message(headers, marshaller.marshalledBytes());
    }).forEach(m -> m.encode(eventStreamBytes));
    return eventStreamBytes.toByteArray();
}
Also used : Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) Stats(software.amazon.awssdk.services.s3.model.Stats) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EndEvent(software.amazon.awssdk.services.s3.model.EndEvent) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) SelectObjectContentResponseHandler(software.amazon.awssdk.services.s3.model.SelectObjectContentResponseHandler) SdkBytes(software.amazon.awssdk.core.SdkBytes) Message(software.amazon.eventstream.Message) ArrayList(java.util.ArrayList) ExpressionType(software.amazon.awssdk.services.s3.model.ExpressionType) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) SelectObjectContentEventStream(software.amazon.awssdk.services.s3.model.SelectObjectContentEventStream) SelectObjectContentResponse(software.amazon.awssdk.services.s3.model.SelectObjectContentResponse) StatsEvent(software.amazon.awssdk.services.s3.model.StatsEvent) Map(java.util.Map) After(org.junit.After) ProgressEvent(software.amazon.awssdk.services.s3.model.ProgressEvent) CompressionType(software.amazon.awssdk.services.s3.model.CompressionType) URI(java.net.URI) RecordsEvent(software.amazon.awssdk.services.s3.model.RecordsEvent) Region(software.amazon.awssdk.regions.Region) Before(org.junit.Before) OutputSerialization(software.amazon.awssdk.services.s3.model.OutputSerialization) SelectObjectContentRequest(software.amazon.awssdk.services.s3.model.SelectObjectContentRequest) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) CSVOutput(software.amazon.awssdk.services.s3.model.CSVOutput) IOException(java.io.IOException) Test(org.junit.Test) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) CSVInput(software.amazon.awssdk.services.s3.model.CSVInput) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) Rule(org.junit.Rule) ContinuationEvent(software.amazon.awssdk.services.s3.model.ContinuationEvent) InputSerialization(software.amazon.awssdk.services.s3.model.InputSerialization) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.anyUrl(com.github.tomakehurst.wiremock.client.WireMock.anyUrl) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Progress(software.amazon.awssdk.services.s3.model.Progress) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with HeaderValue

use of software.amazon.eventstream.HeaderValue in project aws-sdk-java-v2 by aws.

the class BaseEventStreamAsyncAws4Signer method getDataFrameSigner.

private Function<ByteBuffer, ByteBuffer> getDataFrameSigner(String headerSignature, AwsCredentials credentials, Aws4SignerRequestParams signerRequestParams) {
    return new Function<ByteBuffer, ByteBuffer>() {

        final Aws4SignerRequestParams requestParams = signerRequestParams;

        /**
         * Initiate rolling signature with header signature
         */
        String priorSignature = headerSignature;

        @Override
        public ByteBuffer apply(ByteBuffer byteBuffer) {
            /**
             * Signing Date
             */
            Map<String, HeaderValue> nonSignatureHeaders = new HashMap<>();
            Instant signingInstant = requestParams.getSigningClock().instant();
            nonSignatureHeaders.put(EVENT_STREAM_DATE, HeaderValue.fromTimestamp(signingInstant));
            /**
             * Derive Signing Key
             */
            AwsCredentials sanitizedCredentials = sanitizeCredentials(credentials);
            byte[] signingKey = deriveSigningKey(sanitizedCredentials, signingInstant, requestParams.getRegionName(), requestParams.getServiceSigningName());
            /**
             * Calculate rolling signature
             */
            byte[] payload = byteBuffer.array();
            byte[] signatureBytes = signEventStream(priorSignature, signingKey, signingInstant, requestParams, nonSignatureHeaders, payload);
            priorSignature = BinaryUtils.toHex(signatureBytes);
            /**
             * Add signing layer event-stream headers
             */
            Map<String, HeaderValue> headers = new HashMap<>(nonSignatureHeaders);
            // Signature headers
            headers.put(EVENT_STREAM_SIGNATURE, HeaderValue.fromByteArray(signatureBytes));
            /**
             * Encode signed event to byte
             */
            Message signedMessage = new Message(sortHeaders(headers), payload);
            if (LOG.isLoggingLevelEnabled("trace")) {
                LOG.trace(() -> "Signed message: " + toDebugString(signedMessage, false));
            } else {
                LOG.debug(() -> "Signed message: " + toDebugString(signedMessage, true));
            }
            return signedMessage.toByteBuffer();
        }
    };
}
Also used : Function(java.util.function.Function) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) HashMap(java.util.HashMap) Instant(java.time.Instant) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) ByteBuffer(java.nio.ByteBuffer)

Aggregations

HeaderValue (software.amazon.eventstream.HeaderValue)7 Message (software.amazon.eventstream.Message)7 ByteBuffer (java.nio.ByteBuffer)6 HashMap (java.util.HashMap)5 Map (java.util.Map)4 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)3 Flowable (io.reactivex.Flowable)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 Subscriber (org.reactivestreams.Subscriber)2