'Intercept and modify SQS SendMessageRequest

I'm using Java AWS SDK v1 to send messages to a SQS Queue, and I want to intercept this event to add some MessageAttributes in the message.

I created a new RequestHandler class that extend the RequestHandler2 from the SDK to modify it before the request:

class CustomMessagesAttributesRequestHandler : RequestHandler2() {

    override fun beforeRequest(request: Request<*>) {
        val originalRequest = request.originalRequestObject

        if (originalRequest is SendMessageRequest) {
            originalRequest.messageAttributes["my-custom-attribute"] =
                MessageAttributeValue().withStringValue("123456789")
        }
    }
}

The solution above kinda works, it's called before the request, the Message Attribute is added in the originalRequestObject, but when the SQS client send the request it throws an exception:

Unable to calculate the MD5 hash of the message attributes

Digging into AWS SDK code, I see there is a default handler to check the message request with the result to compare both body and attributes MD5, and of course, since I modified the original object they does not match.

Is there a better way to achieve that besides the custom RequestHandler? Or there is a way to recalculate the originalRequestObject MD5?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source