software.amazon.awssdk.services.sqs.model.SendMessageResponse Java Examples

The following examples show how to use software.amazon.awssdk.services.sqs.model.SendMessageResponse. 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: MessageMD5ChecksumInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void afterExecution(Context.AfterExecution context, ExecutionAttributes executionAttributes) {
    SdkResponse response = context.response();
    SdkRequest originalRequest = context.request();
    if (response != null) {
        if (originalRequest instanceof SendMessageRequest) {
            SendMessageRequest sendMessageRequest = (SendMessageRequest) originalRequest;
            SendMessageResponse sendMessageResult = (SendMessageResponse) response;
            sendMessageOperationMd5Check(sendMessageRequest, sendMessageResult);

        } else if (originalRequest instanceof ReceiveMessageRequest) {
            ReceiveMessageResponse receiveMessageResult = (ReceiveMessageResponse) response;
            receiveMessageResultMd5Check(receiveMessageResult);

        } else if (originalRequest instanceof SendMessageBatchRequest) {
            SendMessageBatchRequest sendMessageBatchRequest = (SendMessageBatchRequest) originalRequest;
            SendMessageBatchResponse sendMessageBatchResult = (SendMessageBatchResponse) response;
            sendMessageBatchOperationMd5Check(sendMessageBatchRequest, sendMessageBatchResult);
        }
    }
}
 
Example #2
Source File: MessageMD5ChecksumInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Throw an exception if the MD5 checksums returned in the SendMessageResponse do not match the
 * client-side calculation based on the original message in the SendMessageRequest.
 */
private static void sendMessageOperationMd5Check(SendMessageRequest sendMessageRequest,
                                                 SendMessageResponse sendMessageResult) {
    String messageBodySent = sendMessageRequest.messageBody();
    String bodyMd5Returned = sendMessageResult.md5OfMessageBody();
    String clientSideBodyMd5 = calculateMessageBodyMd5(messageBodySent);
    if (!clientSideBodyMd5.equals(bodyMd5Returned)) {
        throw SdkClientException.builder()
                                .message(String.format(MD5_MISMATCH_ERROR_MESSAGE, MESSAGE_BODY, clientSideBodyMd5,
                                                      bodyMd5Returned))
                                .build();
    }

    Map<String, MessageAttributeValue> messageAttrSent = sendMessageRequest.messageAttributes();
    if (messageAttrSent != null && !messageAttrSent.isEmpty()) {
        String clientSideAttrMd5 = calculateMessageAttributesMd5(messageAttrSent);
        String attrMd5Returned = sendMessageResult.md5OfMessageAttributes();
        if (!clientSideAttrMd5.equals(attrMd5Returned)) {
            throw SdkClientException.builder()
                                    .message(String.format(MD5_MISMATCH_ERROR_MESSAGE, MESSAGE_ATTRIBUTES,
                                                          clientSideAttrMd5, attrMd5Returned))
                                    .build();
        }
    }
}
 
Example #3
Source File: QuarksCannonSyncResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/shoot")
@Consumes(MediaType.APPLICATION_JSON)
public Response sendMessage(Quark quark) throws Exception {
    String message = QUARK_WRITER.writeValueAsString(quark);
    SendMessageResponse response = sqs.sendMessage(m -> m.queueUrl(queueUrl).messageBody(message));
    LOGGER.infov("Fired Quark[{0}, {1}}]", quark.getFlavor(), quark.getSpin());
    return Response.ok().entity(response.messageId()).build();
}
 
Example #4
Source File: QuarksCannonAsyncResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/shoot")
@Consumes(MediaType.APPLICATION_JSON)
public Uni<Response> sendMessage(Quark quark) throws Exception {
    String message = QUARK_WRITER.writeValueAsString(quark);
    return Uni.createFrom()
        .completionStage(sqs.sendMessage(m -> m.queueUrl(queueUrl).messageBody(message)))
        .onItem().invoke(item -> LOGGER.infov("Fired Quark[{0}, {1}}]", quark.getFlavor(), quark.getSpin()))
        .onItem().apply(SendMessageResponse::messageId)
        .onItem().apply(id -> Response.ok().entity(id).build());
}
 
Example #5
Source File: MessageAttributesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
    if (context.response() instanceof SendMessageResponse) {
        return ((SendMessageResponse) context.response()).toBuilder()
                                                         .md5OfMessageBody("invalid-md5")
                                                         .build();
    }
    return context.response();
}
 
Example #6
Source File: MessageMD5ChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void sendMessagePassesValidChecksums() {
    SendMessageRequest request = SendMessageRequest.builder()
                                                   .messageBody(messageBody())
                                                   .messageAttributes(messageAttributes())
                                                   .build();

    SendMessageResponse response = SendMessageResponse.builder()
                                                      .md5OfMessageBody(messageBodyChecksum())
                                                      .md5OfMessageAttributes(messageAttributesChecksum())
                                                      .build();

    assertSuccess(request, response);
}
 
Example #7
Source File: MessageMD5ChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void sendMessageFailsInvalidBodyChecksum() {
    SendMessageRequest request = SendMessageRequest.builder()
                                                   .messageBody(messageBody())
                                                   .messageAttributes(messageAttributes())
                                                   .build();

    SendMessageResponse response = SendMessageResponse.builder()
                                                      .md5OfMessageBody("bad")
                                                      .md5OfMessageAttributes(messageAttributesChecksum())
                                                      .build();

    assertFailure(request, response);
}
 
Example #8
Source File: MessageMD5ChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void sendMessageFailsInvalidAttributeChecksum() {
    SendMessageRequest request = SendMessageRequest.builder()
                                                   .messageBody(messageBody())
                                                   .messageAttributes(messageAttributes())
                                                   .build();

    SendMessageResponse response = SendMessageResponse.builder()
                                                      .md5OfMessageBody(messageBodyChecksum())
                                                      .md5OfMessageAttributes("bad")
                                                      .build();

    assertFailure(request, response);
}
 
Example #9
Source File: MessageAttributesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void sendMessage_WithMessageAttributes_ResultHasMd5OfMessageAttributes() {
    SendMessageResponse sendMessageResult = sendTestMessage();
    assertNotEmpty(sendMessageResult.md5OfMessageBody());
    assertNotEmpty(sendMessageResult.md5OfMessageAttributes());
}
 
Example #10
Source File: MessageAttributesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private SendMessageResponse sendTestMessage() {
    SendMessageResponse sendMessageResult = sqsAsync.sendMessage(SendMessageRequest.builder().queueUrl(queueUrl).messageBody(MESSAGE_BODY)
            .messageAttributes(createRandomAttributeValues(10)).build()).join();
    return sendMessageResult;
}
 
Example #11
Source File: SQSAsyncSender.java    From zipkin-aws with Apache License 2.0 4 votes vote down vote up
@Override protected void doCancel() {
  CompletableFuture<SendMessageResponse> maybeFuture = future;
  if (maybeFuture != null) maybeFuture.cancel(true);
}
 
Example #12
Source File: SQSAsyncSender.java    From zipkin-aws with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean doIsCanceled() {
  CompletableFuture<SendMessageResponse> maybeFuture = future;
  return maybeFuture != null && maybeFuture.isCancelled();
}