Search in sources :

Example 1 with SnsBodyBuilder

use of org.springframework.integration.aws.support.SnsBodyBuilder in project spring-integration-aws by spring-projects.

the class SnsMessageHandlerTests method testSnsMessageHandler.

@Test
@SuppressWarnings("unchecked")
public void testSnsMessageHandler() {
    SnsBodyBuilder payload = SnsBodyBuilder.withDefault("foo").forProtocols("{\"foo\" : \"bar\"}", "sms");
    Message<?> message = MessageBuilder.withPayload(payload).setHeader("topic", "topic").setHeader("subject", "subject").setHeader("foo", "bar").build();
    this.sendToSnsChannel.send(message);
    Message<?> reply = this.resultChannel.receive(10000);
    assertThat(reply).isNotNull();
    ArgumentCaptor<PublishRequest> captor = ArgumentCaptor.forClass(PublishRequest.class);
    verify(this.amazonSNS).publishAsync(captor.capture(), any(AsyncHandler.class));
    PublishRequest publishRequest = captor.getValue();
    assertThat(publishRequest.getMessageStructure()).isEqualTo("json");
    assertThat(publishRequest.getTopicArn()).isEqualTo("topic");
    assertThat(publishRequest.getSubject()).isEqualTo("subject");
    assertThat(publishRequest.getMessage()).isEqualTo("{\"default\":\"foo\",\"sms\":\"{\\\"foo\\\" : \\\"bar\\\"}\"}");
    Map<String, MessageAttributeValue> messageAttributes = publishRequest.getMessageAttributes();
    assertThat(messageAttributes).doesNotContainKey(MessageHeaders.ID);
    assertThat(messageAttributes).doesNotContainKey(MessageHeaders.TIMESTAMP);
    assertThat(messageAttributes).containsKey("foo");
    assertThat(messageAttributes.get("foo").getStringValue()).isEqualTo("bar");
    assertThat(reply.getHeaders().get(AwsHeaders.MESSAGE_ID)).isEqualTo("111");
    assertThat(reply.getHeaders().get(AwsHeaders.TOPIC)).isEqualTo("topic");
    assertThat(reply.getPayload()).isSameAs(payload);
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) SnsBodyBuilder(org.springframework.integration.aws.support.SnsBodyBuilder) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) MessageAttributeValue(com.amazonaws.services.sns.model.MessageAttributeValue) Test(org.junit.Test)

Example 2 with SnsBodyBuilder

use of org.springframework.integration.aws.support.SnsBodyBuilder in project spring-integration-aws by spring-projects.

the class SnsMessageHandler method handleMessageToAws.

@Override
protected Future<?> handleMessageToAws(Message<?> message) {
    Object payload = message.getPayload();
    PublishRequest publishRequest = null;
    if (payload instanceof PublishRequest) {
        publishRequest = (PublishRequest) payload;
    } else {
        Assert.state(this.topicArnExpression != null, "'topicArn' or 'topicArnExpression' must be specified.");
        publishRequest = new PublishRequest();
        String topicArn = this.topicArnExpression.getValue(getEvaluationContext(), message, String.class);
        if (this.resourceIdResolver != null) {
            topicArn = this.resourceIdResolver.resolveToPhysicalResourceId(topicArn);
        }
        publishRequest.setTopicArn(topicArn);
        if (this.subjectExpression != null) {
            String subject = this.subjectExpression.getValue(getEvaluationContext(), message, String.class);
            publishRequest.setSubject(subject);
        }
        Object snsMessage = message.getPayload();
        if (this.bodyExpression != null) {
            snsMessage = this.bodyExpression.getValue(getEvaluationContext(), message);
        }
        if (snsMessage instanceof SnsBodyBuilder) {
            publishRequest.withMessageStructure("json").setMessage(((SnsBodyBuilder) snsMessage).build());
        } else {
            publishRequest.setMessage(getConversionService().convert(snsMessage, String.class));
        }
        HeaderMapper<Map<String, MessageAttributeValue>> headerMapper = getHeaderMapper();
        if (headerMapper != null) {
            mapHeaders(message, publishRequest, headerMapper);
        }
    }
    AsyncHandler<PublishRequest, PublishResult> asyncHandler = obtainAsyncHandler(message, publishRequest);
    return this.amazonSns.publishAsync(publishRequest, asyncHandler);
}
Also used : PublishResult(com.amazonaws.services.sns.model.PublishResult) SnsBodyBuilder(org.springframework.integration.aws.support.SnsBodyBuilder) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PublishRequest (com.amazonaws.services.sns.model.PublishRequest)2 SnsBodyBuilder (org.springframework.integration.aws.support.SnsBodyBuilder)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 MessageAttributeValue (com.amazonaws.services.sns.model.MessageAttributeValue)1 PublishResult (com.amazonaws.services.sns.model.PublishResult)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1