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);
}
}
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);
}
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;
}
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);
}
}
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) {
}
});
}
Aggregations