@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); } }
@Override public PublishResult publish(AwsParamsDto awsParamsDto, String topicArn, String messageText, List<MessageHeader> messageHeaders) { Map<String, MessageAttributeValue> messageAttributes = null; if (CollectionUtils.isNotEmpty(messageHeaders)) { messageAttributes = new HashMap<>(); for (MessageHeader messageHeader : messageHeaders) { messageAttributes.put(messageHeader.getKey(), new MessageAttributeValue().withDataType("String").withStringValue(messageHeader.getValue())); } } return snsOperations.publish(topicArn, messageText, messageAttributes, awsClientFactory.getAmazonSNSClient(awsParamsDto)); }
public PublishResult sendPushMessage(AmazonSNS snsClient, PushPlatform platform, String targetArn, Map<String, Object> customAppMessageMap, Map<String, MessageAttributeValue> messageAttributes) throws IOException { Map<String, Object> appMessageMap = new HashMap<String, Object>(); if (platform == PushPlatform.GCM) { appMessageMap.put("collapse_key", "single"); appMessageMap.put("delay_while_idle", true); appMessageMap.put("time_to_live", 30); appMessageMap.put("dry_run", false); } if (customAppMessageMap != null) { appMessageMap.putAll(customAppMessageMap); } String message = ServerUtil.asJson(appMessageMap); return sendPushMessage(snsClient, platform, targetArn, message, messageAttributes); }
public PublishResult sendPushMessage(AmazonSNS snsClient, PushPlatform platform, String targetArn, String message, Map<String, MessageAttributeValue> messageAttributes) throws IOException { Map<String, String> messageMap = new HashMap<String, String>(); messageMap.put(platform.name(), message); message = ServerUtil.asJson(messageMap); PublishRequest publishRequest = new PublishRequest(); publishRequest.setMessageStructure("json"); if (messageAttributes != null) { publishRequest.setMessageAttributes(messageAttributes); } publishRequest.setTargetArn(targetArn); publishRequest.setMessage(message); PublishResult publishResult = snsClient.publish(publishRequest); return publishResult; }
private Map<String, MessageAttributeValue> getMessageAttributes(Message<?> message) { HashMap<String, MessageAttributeValue> messageAttributes = new HashMap<>(); for (Map.Entry<String, Object> messageHeader : message.getHeaders().entrySet()) { String messageHeaderName = messageHeader.getKey(); Object messageHeaderValue = messageHeader.getValue(); if (MessageHeaders.CONTENT_TYPE.equals(messageHeaderName) && messageHeaderValue != null) { messageAttributes.put(messageHeaderName, getContentTypeMessageAttribute(messageHeaderValue)); } else if (MessageHeaders.ID.equals(messageHeaderName) && messageHeaderValue != null) { messageAttributes.put(messageHeaderName, getStringMessageAttribute(messageHeaderValue.toString())); } else if (messageHeaderValue instanceof String) { messageAttributes.put(messageHeaderName, getStringMessageAttribute((String) messageHeaderValue)); } else if (messageHeaderValue instanceof Number) { messageAttributes.put(messageHeaderName, getNumberMessageAttribute(messageHeaderValue)); } else if (messageHeaderValue instanceof ByteBuffer) { messageAttributes.put(messageHeaderName, getBinaryMessageAttribute((ByteBuffer) messageHeaderValue)); } else { this.logger.warn(String.format("Message header with name '%s' and type '%s' cannot be sent as" + " message attribute because it is not supported by SNS.", messageHeaderName, messageHeaderValue != null ? messageHeaderValue.getClass().getName() : "")); } } return messageAttributes; }
private PublishResult publish(String endpointArn, Platform platform, Map<Platform, Map<String, MessageAttributeValue>> attributesMap) { PublishRequest publishRequest = new PublishRequest(); Map<String, MessageAttributeValue> notificationAttributes = getValidNotificationAttributes(attributesMap .get(platform)); if (notificationAttributes != null && !notificationAttributes.isEmpty()) { publishRequest.setMessageAttributes(notificationAttributes); } publishRequest.setMessageStructure("json"); // If the message attributes are not set in the requisite method, // notification is sent with default attributes String message = getPlatformSampleMessage(platform); Map<String, String> messageMap = new HashMap<String, String>(); messageMap.put(platform.name(), message); message = SampleMessageGenerator.jsonify(messageMap); // For direct publish to mobile end points, topicArn is not relevant. publishRequest.setTargetArn(endpointArn); // Display the message that will be sent to the endpoint/ System.out.println("{Message Body: " + message + "}"); StringBuilder builder = new StringBuilder(); builder.append("{Message Attributes: "); for (Map.Entry<String, MessageAttributeValue> entry : notificationAttributes .entrySet()) { builder.append("(\"" + entry.getKey() + "\": \"" + entry.getValue().getStringValue() + "\"),"); } builder.deleteCharAt(builder.length() - 1); builder.append("}"); System.out.println(builder.toString()); publishRequest.setMessage(message); return snsClient.publish(publishRequest); }
public void demoNotification(Platform platform, String principal, String credential, String platformToken, String applicationName, Map<Platform, Map<String, MessageAttributeValue>> attrsMap) { // Create Platform Application. This corresponds to an app on a // platform. CreatePlatformApplicationResult platformApplicationResult = createPlatformApplication( applicationName, platform, principal, credential); System.out.println(platformApplicationResult); // The Platform Application Arn can be used to uniquely identify the // Platform Application. String platformApplicationArn = platformApplicationResult .getPlatformApplicationArn(); // Create an Endpoint. This corresponds to an app on a device. CreatePlatformEndpointResult platformEndpointResult = createPlatformEndpoint( platform, "CustomData - Useful to store endpoint specific data", platformToken, platformApplicationArn); System.out.println(platformEndpointResult); // Publish a push notification to an Endpoint. PublishResult publishResult = publish( platformEndpointResult.getEndpointArn(), platform, attrsMap); System.out.println("Published! \n{MessageId=" + publishResult.getMessageId() + "}"); // Delete the Platform Application since we will no longer be using it. deletePlatformApplication(platformApplicationArn); }
public static Map<String, MessageAttributeValue> getValidNotificationAttributes( Map<String, MessageAttributeValue> notificationAttributes) { Map<String, MessageAttributeValue> validAttributes = new HashMap<String, MessageAttributeValue>(); if (notificationAttributes == null) return validAttributes; for (Map.Entry<String, MessageAttributeValue> entry : notificationAttributes .entrySet()) { if (!StringUtils.isBlank(entry.getValue().getStringValue())) { validAttributes.put(entry.getKey(), entry.getValue()); } } return validAttributes; }
private static Map<String, MessageAttributeValue> addBaiduNotificationAttributes() { Map<String, MessageAttributeValue> notificationAttributes = new HashMap<String, MessageAttributeValue>(); notificationAttributes.put("AWS.SNS.MOBILE.BAIDU.DeployStatus", new MessageAttributeValue().withDataType("String") .withStringValue("1")); notificationAttributes.put("AWS.SNS.MOBILE.BAIDU.MessageKey", new MessageAttributeValue().withDataType("String") .withStringValue("default-channel-msg-key")); notificationAttributes.put("AWS.SNS.MOBILE.BAIDU.MessageType", new MessageAttributeValue().withDataType("String") .withStringValue("0")); return notificationAttributes; }
private static Map<String, MessageAttributeValue> addWNSNotificationAttributes() { Map<String, MessageAttributeValue> notificationAttributes = new HashMap<String, MessageAttributeValue>(); notificationAttributes.put("AWS.SNS.MOBILE.WNS.CachePolicy", new MessageAttributeValue().withDataType("String") .withStringValue("cache")); notificationAttributes.put("AWS.SNS.MOBILE.WNS.Type", new MessageAttributeValue().withDataType("String") .withStringValue("wns/badge")); return notificationAttributes; }
private static Map<String, MessageAttributeValue> addMPNSNotificationAttributes() { Map<String, MessageAttributeValue> notificationAttributes = new HashMap<String, MessageAttributeValue>(); notificationAttributes.put("AWS.SNS.MOBILE.MPNS.Type", new MessageAttributeValue().withDataType("String") .withStringValue("token")); // This attribute is required. notificationAttributes.put("AWS.SNS.MOBILE.MPNS.NotificationClass", new MessageAttributeValue().withDataType("String") .withStringValue("realtime")); // This attribute is required. return notificationAttributes; }
/** * Publish a message with subject to the AWS SNS topic. * @param subject "Subject" line of message to publish * @param message Content of message to publish * @throws AmazonClientException */ public void publish(final String subject, final String message) throws AmazonClientException { // Auto setting the subject as a message attribute to support sns message filtering by subject final Map<String, MessageAttributeValue> attributes = new HashMap<String, MessageAttributeValue>(); attributes.put("subject", new MessageAttributeValue().withDataType("String").withStringValue(subject)); final PublishRequest request = new PublishRequest().withTopicArn(topicArn).withSubject(subject) .withMessage(message).withMessageAttributes(attributes); amazonSnsClient.publish(request); }
@Override protected boolean sendInternal(Message<?> message, long timeout) { PublishRequest publishRequest = new PublishRequest(this.topicArn, message.getPayload().toString(), findNotificationSubject(message)); Map<String, MessageAttributeValue> messageAttributes = getMessageAttributes(message); if (!messageAttributes.isEmpty()) { publishRequest.withMessageAttributes(messageAttributes); } this.amazonSns.publish(publishRequest); return true; }
private MessageAttributeValue getContentTypeMessageAttribute(Object messageHeaderValue) { if (messageHeaderValue instanceof MimeType) { return new MessageAttributeValue().withDataType(MessageAttributeDataTypes.STRING).withStringValue(messageHeaderValue.toString()); } else if (messageHeaderValue instanceof String) { return new MessageAttributeValue().withDataType(MessageAttributeDataTypes.STRING).withStringValue((String) messageHeaderValue); } return null; }
private static Map<String, MessageAttributeValue> addMPNSNotificationAttributes() { Map<String, MessageAttributeValue> notificationAttributes = new HashMap<String, MessageAttributeValue>(); notificationAttributes.put("AWS.SNS.MOBILE.MPNS.Type", new MessageAttributeValue().withDataType("String") .withStringValue("token")); // This attribute is // required. notificationAttributes.put("AWS.SNS.MOBILE.MPNS.NotificationClass", new MessageAttributeValue().withDataType("String") .withStringValue("realtime")); // This attribute is // required. return notificationAttributes; }
@Override public PublishResult publish(String topicArn, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSNS amazonSNS) { return amazonSNS.publish(new PublishRequest().withTopicArn(topicArn).withMessage(messageText).withMessageAttributes(messageAttributes)); }
@Override public PublishResult publish(String topicArn, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSNS amazonSNS) { // Nothing to do in the normal case since our unit tests aren't reading messages once they have been published. return new PublishResult().withMessageId(AbstractDaoTest.MESSAGE_ID); }
private MessageAttributeValue getBinaryMessageAttribute(ByteBuffer messageHeaderValue) { return new MessageAttributeValue().withDataType(MessageAttributeDataTypes.BINARY).withBinaryValue(messageHeaderValue); }
private MessageAttributeValue getStringMessageAttribute(String messageHeaderValue) { return new MessageAttributeValue().withDataType(MessageAttributeDataTypes.STRING).withStringValue(messageHeaderValue); }
private MessageAttributeValue getNumberMessageAttribute(Object messageHeaderValue) { Assert.isTrue(NumberUtils.STANDARD_NUMBER_TYPES.contains(messageHeaderValue.getClass()), "Only standard number types are accepted as message header."); return new MessageAttributeValue().withDataType(MessageAttributeDataTypes.NUMBER + "." + messageHeaderValue.getClass().getName()).withStringValue(messageHeaderValue.toString()); }
@Test public void sendMessage_withNumericMessageHeaders_shouldBeSentAsTopicMessageAttributes() throws Exception { // Arrange AmazonSNS amazonSns = mock(AmazonSNS.class); ArgumentCaptor<PublishRequest> publishRequestArgumentCaptor = ArgumentCaptor.forClass(PublishRequest.class); when(amazonSns.publish(publishRequestArgumentCaptor.capture())).thenReturn(new PublishResult()); double doubleValue = 1234.56; long longValue = 1234L; int integerValue = 1234; byte byteValue = 2; short shortValue = 12; float floatValue = 1234.56f; BigInteger bigIntegerValue = new BigInteger("616416546156"); BigDecimal bigDecimalValue = new BigDecimal("7834938"); Message<String> message = MessageBuilder.withPayload("Hello") .setHeader("double", doubleValue) .setHeader("long", longValue) .setHeader("integer", integerValue) .setHeader("byte", byteValue) .setHeader("short", shortValue) .setHeader("float", floatValue) .setHeader("bigInteger", bigIntegerValue) .setHeader("bigDecimal", bigDecimalValue) .build(); MessageChannel messageChannel = new TopicMessageChannel(amazonSns, "topicArn"); // Act boolean sent = messageChannel.send(message); // Assert assertTrue(sent); Map<String, MessageAttributeValue> messageAttributes = publishRequestArgumentCaptor.getValue().getMessageAttributes(); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.lang.Double", messageAttributes.get("double").getDataType()); assertEquals(String.valueOf(doubleValue), messageAttributes.get("double").getStringValue()); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.lang.Long", messageAttributes.get("long").getDataType()); assertEquals(String.valueOf(longValue), messageAttributes.get("long").getStringValue()); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.lang.Integer", messageAttributes.get("integer").getDataType()); assertEquals(String.valueOf(integerValue), messageAttributes.get("integer").getStringValue()); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.lang.Byte", messageAttributes.get("byte").getDataType()); assertEquals(String.valueOf(byteValue), messageAttributes.get("byte").getStringValue()); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.lang.Short", messageAttributes.get("short").getDataType()); assertEquals(String.valueOf(shortValue), messageAttributes.get("short").getStringValue()); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.lang.Float", messageAttributes.get("float").getDataType()); assertEquals(String.valueOf(floatValue), messageAttributes.get("float").getStringValue()); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.math.BigInteger", messageAttributes.get("bigInteger").getDataType()); assertEquals(String.valueOf(bigIntegerValue), messageAttributes.get("bigInteger").getStringValue()); assertEquals(MessageAttributeDataTypes.NUMBER + ".java.math.BigDecimal", messageAttributes.get("bigDecimal").getDataType()); assertEquals(String.valueOf(bigDecimalValue), messageAttributes.get("bigDecimal").getStringValue()); }
/** * Sends a message to all of a topic's subscribed endpoints. * * @param topicArn the topic to publish the message to * @param messageText the text of the message * @param messageAttributes the optional SNS message attributes * @param amazonSNS the client for accessing AWS SNS * * @return the result of the publish operation returned by the service */ public PublishResult publish(String topicArn, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSNS amazonSNS);