Java Code Examples for software.amazon.awssdk.core.SdkBytes#fromByteArray()

The following examples show how to use software.amazon.awssdk.core.SdkBytes#fromByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubscribeToShardIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Puts a random record to the stream.
 *
 * @param len The number of bytes to generate for the record.
 * @return Record data that was put.
 */
private Optional<SdkBytes> putRecord(int len) {
    try {
        SdkBytes data = SdkBytes.fromByteArray(RandomUtils.nextBytes(len));
        asyncClient.putRecord(PutRecordRequest.builder()
                                              .streamName(streamName)
                                              .data(data)
                                              .partitionKey(UUID.randomUUID().toString())
                                              .build())
                   .join();
        return Optional.of(data);
    } catch (Exception e) {
        e.printStackTrace();
        return Optional.empty();
    }
}
 
Example 2
Source File: KinesisStabilityTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private void putRecords() {
    log.info(() -> "Starting to test putRecord");
    producedData = new ArrayList<>();
    SdkBytes data = SdkBytes.fromByteArray(RandomUtils.nextBytes(20));
    IntFunction<CompletableFuture<?>> futureFunction =
        i -> asyncClient.putRecord(PutRecordRequest.builder()
                                                   .streamName(streamName)
                                                   .data(data)
                                                   .partitionKey(UUID.randomUUID().toString())
                                                   .build())
                        .thenApply(b -> producedData.add(data));

    StabilityTestRunner.newRunner()
                       .requestCountPerRun(CONCURRENCY)
                       .testName("KinesisStabilityTest.putRecords")
                       .futureFactory(futureFunction)
                       .run();
}
 
Example 3
Source File: QuarkusKmsSyncResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/decrypt")
public String decrypt(String data) {
    SdkBytes encryptedData = SdkBytes.fromByteArray(Base64.decodeBase64(data.getBytes()));
    DecryptResponse decrypted = kms.decrypt(req -> req.keyId(keyArn).ciphertextBlob(encryptedData));

    return decrypted.plaintext().asUtf8String();
}
 
Example 4
Source File: KinesisVerticle.java    From reactive-refarch-cloudformation with Apache License 2.0 5 votes vote down vote up
private void sendMessageToKinesis(byte [] byteMessage, String partitionKey) throws KinesisException {
    if (null == kinesisAsyncClient) {
        throw new KinesisException("AmazonKinesisAsync is not initialized");
    }

    SdkBytes payload = SdkBytes.fromByteArray(byteMessage);
    PutRecordRequest putRecordRequest = PutRecordRequest.builder()
            .partitionKey(partitionKey)
            .streamName(eventStream)
            .data(payload)
            .build();

    LOGGER.info("Writing to streamName " + eventStream + " using partitionkey " + partitionKey);

    try {
        CompletableFuture<PutRecordResponse> future = kinesisAsyncClient.putRecord(putRecordRequest);

        future.whenComplete((result, e) -> vertx.runOnContext(none -> {
            if (e != null) {
                LOGGER.error("Something happened ... 1");
                LOGGER.error(e);
                e.printStackTrace();
            } else {
                String sequenceNumber = result.sequenceNumber();
                LOGGER.debug("Message sequence number: " + sequenceNumber);
            }
        }));
    }
    catch (Exception exc) {
        LOGGER.error("Something happened ... 2");
        exc.printStackTrace();
        LOGGER.error(exc);
    }
}
 
Example 5
Source File: RedisUpdate.java    From reactive-refarch-cloudformation with Apache License 2.0 5 votes vote down vote up
public static void main (String ... args) {

        KinesisAsyncClient kinesisAsync = createClient();

        byte[] msg = prepareData();

        SdkBytes payload = SdkBytes.fromByteArray(msg);
        PutRecordRequest putRecordRequest = PutRecordRequest.builder()
                .partitionKey("test")
                .streamName(kinesisStream)
                .data(payload)
                .build();

        CompletableFuture<PutRecordResponse> future = kinesisAsync.putRecord(putRecordRequest);

        future.whenComplete((result, e) -> {
            if (e != null) {
                System.out.println(e);
            } else {
                String sequenceNumber = result.sequenceNumber();
                System.out.println("Message sequence number: " + sequenceNumber);
            }
        });
    }
 
Example 6
Source File: JsonProtocolUnmarshaller.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static SdkBytes unmarshallSdkBytes(JsonUnmarshallerContext context,
                                           SdkJsonNode jsonContent,
                                           SdkField<SdkBytes> field) {
    if (jsonContent == null || jsonContent.isNull()) {
        return null;
    }
    // Binary protocols like CBOR may already have the raw bytes extracted.
    if (jsonContent.embeddedObject() != null) {
        return SdkBytes.fromByteArray((byte[]) jsonContent.embeddedObject());
    } else {
        // Otherwise decode the JSON string as Base64
        return TO_SDK_BYTES.convert(jsonContent.asText(), field);
    }
}
 
Example 7
Source File: KinesisEncoder.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Override
public PutRecordsRequestEntry apply(final Message<String> message) {
    final String encodedMessage = textEncoder.apply(message);
    final SdkBytes sdkBytes = encodedMessage != null
            ? SdkBytes.fromString(encodedMessage, UTF_8)
            : SdkBytes.fromByteArray(new byte[]{});

    return PutRecordsRequestEntry.builder()
            .partitionKey(message.getKey().partitionKey())
            .data(sdkBytes)
            .build();
}
 
Example 8
Source File: KinesisAppender.java    From kinesis-logback-appender with Apache License 2.0 5 votes vote down vote up
@Override
protected void putMessage(String message) throws Exception {
  SdkBytes data = SdkBytes.fromByteArray(message.getBytes(getEncoding()));
  getClient().putRecord(builder -> 
    builder
      .partitionKey(UUID.randomUUID().toString())
      .streamName(getStreamName())
      .data(data)
      .build()).whenCompleteAsync(asyncCallHandler);
}
 
Example 9
Source File: FirehoseAppender.java    From kinesis-logback-appender with Apache License 2.0 5 votes vote down vote up
@Override
protected void putMessage(String message) throws Exception {
  SdkBytes data = SdkBytes.fromByteArray(message.getBytes(getEncoding()));
  getClient().putRecord(builder -> 
    builder
      .deliveryStreamName(getStreamName())
      .record(b -> b.data(data).build())
      .build()).whenCompleteAsync(asyncCallHandler);
}
 
Example 10
Source File: PrefetchRecordsPublisherIntegrationTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
@Ignore
    @Test
    public void testDifferentShardCaches() {
        final ExecutorService executorService2 = spy(Executors.newFixedThreadPool(1));
        final KinesisDataFetcher kinesisDataFetcher = spy(new KinesisDataFetcher(kinesisClient, streamName, shardId, MAX_RECORDS_PER_CALL, NULL_METRICS_FACTORY));
        final GetRecordsRetrievalStrategy getRecordsRetrievalStrategy2 =
                spy(new AsynchronousGetRecordsRetrievalStrategy(kinesisDataFetcher, 5 , 5, shardId));
        final PrefetchRecordsPublisher recordsPublisher2 = new PrefetchRecordsPublisher(
                MAX_SIZE,
                MAX_BYTE_SIZE,
                MAX_RECORDS_COUNT,
                MAX_RECORDS_PER_CALL,
                getRecordsRetrievalStrategy2,
                executorService2,
                IDLE_MILLIS_BETWEEN_CALLS,
                new NullMetricsFactory(),
                operation,
                "test-shard-2");

        getRecordsCache.start(extendedSequenceNumber, initialPosition);
        sleep(IDLE_MILLIS_BETWEEN_CALLS);

        final Record record = mock(Record.class);
        final SdkBytes byteBuffer = SdkBytes.fromByteArray(new byte[512 * 1024]);
        when(record.data()).thenReturn(byteBuffer);

        records.add(record);
        records.add(record);
        records.add(record);
        records.add(record);
        recordsPublisher2.start(extendedSequenceNumber, initialPosition);

        sleep(IDLE_MILLIS_BETWEEN_CALLS);

        ProcessRecordsInput p1 = evictPublishedEvent(getRecordsCache, shardId).processRecordsInput();

        ProcessRecordsInput p2 = evictPublishedEvent(recordsPublisher2, shardId).processRecordsInput();

        assertNotEquals(p1, p2);
        assertTrue(p1.records().isEmpty());
        assertFalse(p2.records().isEmpty());
        assertEquals(p2.records().size(), records.size());

        recordsPublisher2.shutdown();
        sleep(100L);
        verify(executorService2).shutdownNow();
//        verify(getRecordsRetrievalStrategy2).shutdown();
    }
 
Example 11
Source File: PrefetchRecordsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
private SdkBytes createByteBufferWithSize(int size) {
    return SdkBytes.fromByteArray(new byte[size]);
}
 
Example 12
Source File: FanOutRecordsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
private Record makeRecord(int sequenceNumber) {
    SdkBytes buffer = SdkBytes.fromByteArray(new byte[] { 1, 2, 3 });
    return Record.builder().data(buffer).approximateArrivalTimestamp(Instant.now())
            .sequenceNumber(Integer.toString(sequenceNumber)).partitionKey("A").build();
}
 
Example 13
Source File: SendMessage.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void send(SesClient client,
                        String sender,
                        String recipient,
                        String subject,
                        String bodyText,
                        String bodyHTML
) throws AddressException, MessagingException, IOException {

    Session session = Session.getDefaultInstance(new Properties());

    // Create a new MimeMessage object.
    MimeMessage message = new MimeMessage(session);

    // Add subject, from and to lines.
    message.setSubject(subject, "UTF-8");
    message.setFrom(new InternetAddress(sender));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));

    // Create a multipart/alternative child container.
    MimeMultipart msgBody = new MimeMultipart("alternative");

    // Create a wrapper for the HTML and text parts.
    MimeBodyPart wrap = new MimeBodyPart();

    // Define the text part.
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent(bodyText, "text/plain; charset=UTF-8");

    // Define the HTML part.
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");

    // Add the text and HTML parts to the child container.
    msgBody.addBodyPart(textPart);
    msgBody.addBodyPart(htmlPart);

    // Add the child container to the wrapper object.
    wrap.setContent(msgBody);

    // Create a multipart/mixed parent container.
    MimeMultipart msg = new MimeMultipart("mixed");

    // Add the parent container to the message.
    message.setContent(msg);

    // Add the multipart/alternative part to the message.
    msg.addBodyPart(wrap);

    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        message.writeTo(outputStream);

        ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());

        byte[] arr = new byte[buf.remaining()];
        buf.get(arr);

        SdkBytes data = SdkBytes.fromByteArray(arr);

        RawMessage rawMessage = RawMessage.builder()
                .data(data)
                .build();

        SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                .rawMessage(rawMessage)
                .build();

        client.sendRawEmail(rawEmailRequest);

    } catch (SesException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }


}
 
Example 14
Source File: SendMessage.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void send() throws AddressException, MessagingException, IOException {

        Session session = Session.getDefaultInstance(new Properties());

        // Create a new MimeMessage object
        MimeMessage message = new MimeMessage(session);

        // Add subject, from and to lines
        message.setSubject(SUBJECT, "UTF-8");
        message.setFrom(new InternetAddress(SENDER));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(RECIPIENT));

        // Create a multipart/alternative child container
        MimeMultipart msgBody = new MimeMultipart("alternative");

        // Create a wrapper for the HTML and text parts
        MimeBodyPart wrap = new MimeBodyPart();

        // Define the text part
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent(BODY_TEXT, "text/plain; charset=UTF-8");

        // Define the HTML part
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(BODY_HTML, "text/html; charset=UTF-8");

        // Add the text and HTML parts to the child container
        msgBody.addBodyPart(textPart);
        msgBody.addBodyPart(htmlPart);

        // Add the child container to the wrapper object
        wrap.setContent(msgBody);

        // Create a multipart/mixed parent container
        MimeMultipart msg = new MimeMultipart("mixed");

        // Add the parent container to the message
        message.setContent(msg);

        // Add the multipart/alternative part to the message
        msg.addBodyPart(wrap);


        try {
            System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");

            Region region = Region.US_WEST_2;

            SesClient client = SesClient.builder().region(region).build();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            message.writeTo(outputStream);

            ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());

            byte[] arr = new byte[buf.remaining()];
            buf.get(arr);

            SdkBytes data = SdkBytes.fromByteArray(arr);

            RawMessage rawMessage = RawMessage.builder()
                    .data(data)
                    .build();

            SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                    .rawMessage(rawMessage)
                    .build();

            client.sendRawEmail(rawEmailRequest);

        } catch (SdkException e) {
            e.getStackTrace();
        }
        // snippet-end:[ses.java2.sendmessage.main]
    }
 
Example 15
Source File: SendMessageAttachment.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void send(byte[] attachment) throws AddressException, MessagingException, IOException {

        Session session = Session.getDefaultInstance(new Properties());

        // Create a new MimeMessage object
        MimeMessage message = new MimeMessage(session);

        // Add subject, from and to lines
        message.setSubject(SUBJECT, "UTF-8");
        message.setFrom(new InternetAddress(SENDER));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(RECIPIENT));

        // Create a multipart/alternative child container
        MimeMultipart msgBody = new MimeMultipart("alternative");

        // Create a wrapper for the HTML and text parts
        MimeBodyPart wrap = new MimeBodyPart();

        // Define the text part
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent(BODY_TEXT, "text/plain; charset=UTF-8");

        // Define the HTML part
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(BODY_HTML, "text/html; charset=UTF-8");

        // Add the text and HTML parts to the child container
        msgBody.addBodyPart(textPart);
        msgBody.addBodyPart(htmlPart);

        // Add the child container to the wrapper object
        wrap.setContent(msgBody);

        // Create a multipart/mixed parent container
        MimeMultipart msg = new MimeMultipart("mixed");

        // Add the parent container to the message
        message.setContent(msg);

        // Add the multipart/alternative part to the message
        msg.addBodyPart(wrap);

        // Define the attachment
        MimeBodyPart att = new MimeBodyPart();
        DataSource fds = new ByteArrayDataSource(attachment, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        att.setDataHandler(new DataHandler(fds));

        // Set the attachment name
        String reportName = "WorkReport.xls";
        att.setFileName(reportName);

        // Add the attachment to the message
        msg.addBodyPart(att);

        try {
            System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");

            Region region = Region.US_WEST_2;

            SesClient client = SesClient.builder().region(region).build();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            message.writeTo(outputStream);

            ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());

            byte[] arr = new byte[buf.remaining()];
            buf.get(arr);

            SdkBytes data = SdkBytes.fromByteArray(arr);

            RawMessage rawMessage = RawMessage.builder()
                    .data(data)
                    .build();

            SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                    .rawMessage(rawMessage)
                    .build();

            client.sendRawEmail(rawEmailRequest);

        } catch (SdkException e) {
            e.getStackTrace();
        }
        // snippet-end:[ses.java2.sendmessageattachment.main]
    }
 
Example 16
Source File: StringToValueConverter.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private static SdkBytes toSdkBytes(String s) {
    return SdkBytes.fromByteArray(BinaryUtils.fromBase64(s));
}
 
Example 17
Source File: SdkBytesStringConverter.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public SdkBytes fromString(String string) {
    return SdkBytes.fromByteArray(BinaryUtils.fromBase64(string));
}
 
Example 18
Source File: SdkPojoDeserializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 4 votes vote down vote up
private Object readObject(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException {

        MarshallingType<?> type = field.marshallingType();
        switch (p.currentToken()) {
            case VALUE_FALSE:
            case VALUE_TRUE: {
                if (type.equals(MarshallingType.BOOLEAN)) {
                    return p.getBooleanValue();
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got boolean field value");
            }

            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT: {
                if (type.equals(MarshallingType.INTEGER)) {
                    return p.getIntValue();
                } else if (type.equals(MarshallingType.LONG)) {
                    return p.getLongValue();
                } else if (type.equals(MarshallingType.FLOAT)) {
                    return p.getFloatValue(); // coerce should work
                } else if (type.equals(MarshallingType.DOUBLE)) {
                    return p.getDoubleValue(); // coerce should work
                } else if (type.equals(MarshallingType.BIG_DECIMAL)) {
                    return p.getDecimalValue(); // coerce should work
                } else if (type.equals(MarshallingType.INSTANT)) { // we serialize as BigDecimals
                    JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(Instant.class));
                    return deser.deserialize(p, ctxt);
                }
                throw new JsonMappingException(p,
                                               "Type mismatch, expecting " + type + " got int/float/double/big_decimal/instant");
            }

            case VALUE_STRING: {
                if (type.equals(MarshallingType.STRING)) {
                    return p.getText();
                } else if (type.equals(MarshallingType.SDK_BYTES)) {
                    byte[] bytes = p.getBinaryValue();
                    return SdkBytes.fromByteArray(bytes);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got string/bytes");
            }

            case START_OBJECT: {
                if (type.equals(MarshallingType.MAP)) {
                    return readMap(field, p, ctxt);
                } else if (type.equals(MarshallingType.SDK_POJO)) {
                    return readPojo(field.constructor().get(), p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo");
            }

            case START_ARRAY: {
                if (type.equals(MarshallingType.LIST)) {
                    return readList(field, p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got List type");
            }

            case VALUE_NULL:
                return null;

            default:
                throw new JsonMappingException(p, "Can not map type " + type + " Token = " + p.currentToken());
        }
    }