Search in sources :

Example 1 with MessageAttributeValue

use of com.amazonaws.services.sns.model.MessageAttributeValue in project adeptj-modules by AdeptJ.

the class AwsSnsService method start.

@Activate
protected void start(SmsConfig smsConfig) {
    this.smsAttributes = new HashMap<>();
    this.smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue().withStringValue(smsConfig.senderId()).withDataType("String"));
    this.smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue(smsConfig.smsType()).withDataType("String"));
    try {
        this.asyncSNS = AmazonSNSAsyncClient.asyncBuilder().withEndpointConfiguration(AwsUtil.getEndpointConfig(smsConfig.serviceEndpoint(), smsConfig.signingRegion())).withCredentials(AwsUtil.getCredentialsProvider(smsConfig.accessKey(), smsConfig.secretKey())).build();
    } catch (Exception ex) {
        LOGGER.error("Exception while starting SmsService!!", ex);
        throw new AwsException(ex.getMessage(), ex);
    }
}
Also used : AwsException(com.adeptj.modules.aws.core.AwsException) AwsException(com.adeptj.modules.aws.core.AwsException) MessageAttributeValue(com.amazonaws.services.sns.model.MessageAttributeValue) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with MessageAttributeValue

use of com.amazonaws.services.sns.model.MessageAttributeValue 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 3 with MessageAttributeValue

use of com.amazonaws.services.sns.model.MessageAttributeValue in project camel by apache.

the class SnsProducer method translateAttributes.

private Map<String, MessageAttributeValue> translateAttributes(Map<String, Object> headers, Exchange exchange) {
    Map<String, MessageAttributeValue> result = new HashMap<String, MessageAttributeValue>();
    HeaderFilterStrategy headerFilterStrategy = getEndpoint().getHeaderFilterStrategy();
    for (Entry<String, Object> entry : headers.entrySet()) {
        // only put the message header which is not filtered into the message attribute
        if (!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), entry.getValue(), exchange)) {
            Object value = entry.getValue();
            if (value instanceof String) {
                MessageAttributeValue mav = new MessageAttributeValue();
                mav.setDataType("String");
                mav.withStringValue((String) value);
                result.put(entry.getKey(), mav);
            } else if (value instanceof ByteBuffer) {
                MessageAttributeValue mav = new MessageAttributeValue();
                mav.setDataType("Binary");
                mav.withBinaryValue((ByteBuffer) value);
                result.put(entry.getKey(), mav);
            } else {
                // cannot translate the message header to message attribute value
                LOG.warn("Cannot put the message header key={}, value={} into Sns MessageAttribute", entry.getKey(), entry.getValue());
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) ByteBuffer(java.nio.ByteBuffer) MessageAttributeValue(com.amazonaws.services.sns.model.MessageAttributeValue)

Example 4 with MessageAttributeValue

use of com.amazonaws.services.sns.model.MessageAttributeValue in project nifi by apache.

the class PutSNS method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    if (flowFile.getSize() > MAX_SIZE) {
        getLogger().error("Cannot publish {} to SNS because its size exceeds Amazon SNS's limit of 256KB; routing to failure", new Object[] { flowFile });
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    final Charset charset = Charset.forName(context.getProperty(CHARACTER_ENCODING).evaluateAttributeExpressions(flowFile).getValue());
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    session.exportTo(flowFile, baos);
    final String message = new String(baos.toByteArray(), charset);
    final AmazonSNSClient client = getClient();
    final PublishRequest request = new PublishRequest();
    request.setMessage(message);
    if (context.getProperty(USE_JSON_STRUCTURE).asBoolean()) {
        request.setMessageStructure("json");
    }
    final String arn = context.getProperty(ARN).evaluateAttributeExpressions(flowFile).getValue();
    final String arnType = context.getProperty(ARN_TYPE).getValue();
    if (arnType.equalsIgnoreCase(ARN_TYPE_TOPIC.getValue())) {
        request.setTopicArn(arn);
    } else {
        request.setTargetArn(arn);
    }
    final String subject = context.getProperty(SUBJECT).evaluateAttributeExpressions(flowFile).getValue();
    if (subject != null) {
        request.setSubject(subject);
    }
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        if (entry.getKey().isDynamic() && !StringUtils.isEmpty(entry.getValue())) {
            final MessageAttributeValue value = new MessageAttributeValue();
            value.setStringValue(context.getProperty(entry.getKey()).evaluateAttributeExpressions(flowFile).getValue());
            value.setDataType("String");
            request.addMessageAttributesEntry(entry.getKey().getName(), value);
        }
    }
    try {
        client.publish(request);
        session.transfer(flowFile, REL_SUCCESS);
        session.getProvenanceReporter().send(flowFile, arn);
        getLogger().info("Successfully published notification for {}", new Object[] { flowFile });
    } catch (final Exception e) {
        getLogger().error("Failed to publish Amazon SNS message for {} due to {}", new Object[] { flowFile, e });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AmazonSNSClient(com.amazonaws.services.sns.AmazonSNSClient) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) Map(java.util.Map) MessageAttributeValue(com.amazonaws.services.sns.model.MessageAttributeValue)

Example 5 with MessageAttributeValue

use of com.amazonaws.services.sns.model.MessageAttributeValue in project traccar by tananaev.

the class SnsSmsClient method sendMessageSync.

@Override
public void sendMessageSync(String destAddress, String message, boolean command) {
    Map<String, MessageAttributeValue> smsAttributes = new HashMap<>();
    smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue().withStringValue("SNS").withDataType("String"));
    smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String"));
    PublishRequest publishRequest = new PublishRequest().withMessage(message).withPhoneNumber(destAddress).withMessageAttributes(smsAttributes);
    snsClient.publishAsync(publishRequest, new AsyncHandler<PublishRequest, PublishResult>() {

        @Override
        public void onError(Exception exception) {
            LOGGER.error("SMS send failed", exception);
        }

        @Override
        public void onSuccess(PublishRequest request, PublishResult result) {
        }
    });
}
Also used : PublishResult(com.amazonaws.services.sns.model.PublishResult) HashMap(java.util.HashMap) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) MessageAttributeValue(com.amazonaws.services.sns.model.MessageAttributeValue)

Aggregations

MessageAttributeValue (com.amazonaws.services.sns.model.MessageAttributeValue)5 PublishRequest (com.amazonaws.services.sns.model.PublishRequest)3 HashMap (java.util.HashMap)2 AwsException (com.adeptj.modules.aws.core.AwsException)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AmazonSNSClient (com.amazonaws.services.sns.AmazonSNSClient)1 PublishResult (com.amazonaws.services.sns.model.PublishResult)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Charset (java.nio.charset.Charset)1 Map (java.util.Map)1 HeaderFilterStrategy (org.apache.camel.spi.HeaderFilterStrategy)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 Test (org.junit.Test)1 Activate (org.osgi.service.component.annotations.Activate)1 SnsBodyBuilder (org.springframework.integration.aws.support.SnsBodyBuilder)1