Example usage for com.amazonaws.services.sns.model PublishRequest setMessage

List of usage examples for com.amazonaws.services.sns.model PublishRequest setMessage

Introduction

In this page you can find the example usage for com.amazonaws.services.sns.model PublishRequest setMessage.

Prototype

public void setMessage(String message) 

Source Link

Document

The message you want to send.

Usage

From source file:AmazonSNSClientWrapper.java

License:Open Source License

private PublishResult publish(String endpointArn, SampleMessageGenerator.Platform platform,
        Map<SampleMessageGenerator.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);
    }/*ww w  . ja  va 2  s  .  c o m*/
    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);
}

From source file:ch.admin.isb.hermes5.tools.filebackup.FileBackup.java

License:Apache License

private void sendMessageThroughSNS(String topicArn, AmazonSNS sns, String message, String subject) {
    if (message.isEmpty()) {
        return;//from   w  w w.j a va  2 s. c  o  m
    }
    PublishRequest publishRequest = new PublishRequest();
    publishRequest.setTopicArn(topicArn);
    publishRequest.setSubject(subject);
    publishRequest.setMessage(truncateUTF8(message, 8192));
    sns.publish(publishRequest);
}

From source file:ch.admin.isb.hermes5.tools.lognotification.LogNotification.java

License:Apache License

private void sendMessageThroughSNS(String topicArn, AmazonSNS sns, String message, String subject) {
    if (message.isEmpty()) {
        return;/*ww w. j a v  a2 s.  co m*/
    }
    PublishRequest publishRequest = new PublishRequest();
    publishRequest.setTopicArn(topicArn);
    publishRequest.setSubject(subject);
    publishRequest.setMessage(truncateUTF8Tail(message, 8192));
    sns.publish(publishRequest);
}

From source file:com.amazon.aws.samplecode.travellog.aws.TravelLogSNSManager.java

License:Open Source License

/**
 * Publishes a comment to the specified entry. The method takes the comment and
 * builds an SNS PublishRequest object.  Then the comment is published to the topic associated
 * with the incoming entry./*from   w ww.  j  a va 2s  . co m*/
 * 
 * @param entry the entry to publish to
 * @param comment the comment to publish
 * @return the result returned from AWS
 */
public PublishResult publish(Entry entry, Comment comment) {
    PublishRequest request = new PublishRequest();
    request.setTopicArn(entry.getSnsArn());

    StringBuilder subject = new StringBuilder("Comment Posted to Entry '");
    subject.append(entry.getTitle()).append("'");
    request.setSubject(subject.toString());

    StringBuilder body = new StringBuilder();
    body.append("The following comment was posted to the post '").append(entry.getTitle()).append("'\n");
    body.append("Posted by: ").append(comment.getCommenter().getName()).append("\n\n");
    body.append(comment.getBody());

    request.setMessage(body.toString());

    return snsClient.publish(request);
}

From source file:com.aws.sns.service.notifications.sns.AmazonSNSClientWrapper.java

License:Open Source License

private PublishResult publish(String endpointArn, Platform platform,
        Map<Platform, Map<String, MessageAttributeValue>> attributesMap, String messageText,
        String payloadAction, String collapseKey) {
    PublishRequest publishRequest = new PublishRequest();
    Map<String, MessageAttributeValue> notificationAttributes = getValidNotificationAttributes(
            attributesMap.get(platform));
    if (notificationAttributes != null && !notificationAttributes.isEmpty()) {
        publishRequest.setMessageAttributes(notificationAttributes);
    }/*from  ww  w  .j a  v a 2  s .c  o  m*/
    publishRequest.setMessageStructure("json");
    // If the message attributes are not set in the requisite method,
    // notification is sent with default attributes
    String message = getPlatformSampleMessage(platform, messageText, payloadAction, collapseKey);
    Map<String, String> messageMap = new HashMap<String, String>();
    messageMap.put(platform.name(), message);
    message = SNSPlatformHelper.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);
}

From source file:com.cloudbees.gasp.services.SNSMobile.java

License:Open Source License

private PublishResult apnPublish(String endpointArn, Platform platform, String theMessage) {
    PublishRequest publishRequest = new PublishRequest();
    Map<String, String> messageMap = new HashMap<String, String>();
    String message;/*from  w  w  w. ja  v a2  s  . c  o  m*/
    messageMap.put("default", defaultMessage);
    messageMap.put(platform.name(), theMessage);
    // For direct publish to mobile end points, topicArn is not relevant.
    publishRequest.setTargetArn(endpointArn);
    publishRequest.setMessageStructure("json");
    message = jsonify(messageMap);

    // Display the message that will be sent to the endpoint/
    LOGGER.debug(message);

    publishRequest.setMessage(message);
    return snsClient.publish(publishRequest);
}

From source file:com.comcast.cmb.test.tools.CMBTutorial.java

License:Apache License

public static void main(String[] args) {

    try {/*w  ww. ja  v a2s  .c o  m*/

        Util.initLog4jTest();

        //TODO: set user id and credentials for two distinct users

        // user "cqs_test_1"

        //BasicAWSCredentials user1Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>");

        BasicAWSCredentials user1Credentials = new BasicAWSCredentials("Z2DVBFRNZ2C2SSXDWS5F",
                "bH2UQiJkpctBaE3eaDob19fj5J9Q1FVafrZantBp");

        // user "cqs_test_2"

        //String user2Id = "<user_id>";
        String user2Id = "389653920093";

        //BasicAWSCredentials user2Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>");

        BasicAWSCredentials user2Credentials = new BasicAWSCredentials("QL8Q1VOBCSJC5FZ2DMIU",
                "n6a82gyJZ04Z+Xqp7OgfqPtbbKqVc3UbuOTNrF+7");

        // service urls

        //TODO: add service URLs

        //String cqsServerUrl = "http://<host>:<port>";
        //String cnsServerUrl = "http://<host>:<port>";

        String cqsServerUrl = "http://localhost:6059";
        String cnsServerUrl = "http://localhost:6061";

        // initialize service

        AmazonSQSClient sqs = new AmazonSQSClient(user1Credentials);
        sqs.setEndpoint(cqsServerUrl);

        AmazonSNSClient sns = new AmazonSNSClient(user2Credentials);
        sns.setEndpoint(cnsServerUrl);

        // create queue

        Random randomGenerator = new Random();

        String queueName = QUEUE_PREFIX + randomGenerator.nextLong();

        HashMap<String, String> attributeParams = new HashMap<String, String>();
        CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
        createQueueRequest.setAttributes(attributeParams);
        String queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

        AddPermissionRequest addPermissionRequest = new AddPermissionRequest();
        addPermissionRequest.setQueueUrl(queueUrl);
        addPermissionRequest.setActions(Arrays.asList("SendMessage"));
        addPermissionRequest.setLabel(UUID.randomUUID().toString());
        addPermissionRequest.setAWSAccountIds(Arrays.asList(user2Id));
        sqs.addPermission(addPermissionRequest);

        // create topic

        String topicName = "TSTT" + randomGenerator.nextLong();

        CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName);
        CreateTopicResult createTopicResult = sns.createTopic(createTopicRequest);
        String topicArn = createTopicResult.getTopicArn();

        // subscribe and confirm cqs endpoint

        SubscribeRequest subscribeRequest = new SubscribeRequest();
        String queueArn = getArnForQueueUrl(queueUrl);
        subscribeRequest.setEndpoint(queueArn);
        subscribeRequest.setProtocol("cqs");
        subscribeRequest.setTopicArn(topicArn);
        SubscribeResult subscribeResult = sns.subscribe(subscribeRequest);
        String subscriptionArn = subscribeResult.getSubscriptionArn();

        if (subscriptionArn.equals("pending confirmation")) {

            Thread.sleep(500);

            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
            receiveMessageRequest.setQueueUrl(queueUrl);
            receiveMessageRequest.setMaxNumberOfMessages(1);
            ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);

            List<Message> messages = receiveMessageResult.getMessages();

            if (messages != null && messages.size() == 1) {

                JSONObject o = new JSONObject(messages.get(0).getBody());

                if (!o.has("SubscribeURL")) {
                    throw new Exception("message is not a confirmation messsage");
                }

                String subscriptionUrl = o.getString("SubscribeURL");
                httpGet(subscriptionUrl);

                DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest();
                deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle());
                deleteMessageRequest.setQueueUrl(queueUrl);
                sqs.deleteMessage(deleteMessageRequest);

            } else {
                throw new Exception("no confirmation message found");
            }
        }

        // publish and receive message

        PublishRequest publishRequest = new PublishRequest();
        String messageText = "quamvis sint sub aqua, sub aqua maledicere temptant";
        publishRequest.setMessage(messageText);
        publishRequest.setSubject("unit test message");
        publishRequest.setTopicArn(topicArn);
        sns.publish(publishRequest);

        Thread.sleep(500);

        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
        receiveMessageRequest.setQueueUrl(queueUrl);
        receiveMessageRequest.setMaxNumberOfMessages(1);
        ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);

        List<Message> messages = receiveMessageResult.getMessages();

        if (messages != null && messages.size() == 1) {

            String messageBody = messages.get(0).getBody();

            if (!messageBody.contains(messageText)) {
                throw new Exception("message text not found");
            }

            DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest();
            deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle());
            deleteMessageRequest.setQueueUrl(queueUrl);
            sqs.deleteMessage(deleteMessageRequest);

        } else {
            throw new Exception("no messages found");
        }

        // subscribe and confirm http endpoint

        String id = randomGenerator.nextLong() + "";
        String endPointUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "recv/" + id;
        String lastMessageUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "info/" + id + "?showLast=true";

        subscribeRequest = new SubscribeRequest();
        subscribeRequest.setEndpoint(endPointUrl);
        subscribeRequest.setProtocol("http");
        subscribeRequest.setTopicArn(topicArn);
        subscribeResult = sns.subscribe(subscribeRequest);
        subscriptionArn = subscribeResult.getSubscriptionArn();

        if (subscriptionArn.equals("pending confirmation")) {

            Thread.sleep(500);

            String response = httpGet(lastMessageUrl);

            JSONObject o = new JSONObject(response);

            if (!o.has("SubscribeURL")) {
                throw new Exception("message is not a confirmation messsage");
            }

            String subscriptionUrl = o.getString("SubscribeURL");

            response = httpGet(subscriptionUrl);
        }

        // publish and receive message

        publishRequest = new PublishRequest();
        publishRequest.setMessage(messageText);
        publishRequest.setSubject("unit test message");
        publishRequest.setTopicArn(topicArn);
        sns.publish(publishRequest);

        Thread.sleep(500);

        String response = httpGet(lastMessageUrl);

        if (response != null && response.length() > 0) {

            if (!response.contains(messageText)) {
                throw new Exception("message text not found");
            }

        } else {
            throw new Exception("no messages found");
        }

        // delete queue and topic

        DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn);
        sns.deleteTopic(deleteTopicRequest);

        sqs.deleteQueue(new DeleteQueueRequest(queueUrl));

        System.out.println("OK");

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.dev.appx.sns.tools.AmazonSNSClientWrapper.java

License:Open Source License

public 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);
    }/*ww w. j  ava2  s . com*/
    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);
}

From source file:com.gafful.AmazonSNSClientWrapper.java

License:Open Source License

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);
    }/*from  w w w . j  a  v a  2 s . c o  m*/
    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);
}

From source file:com.mistminds.service.AmazonSNSClientWrapper.java

License:Open Source License

private PublishResult publish(String endpointArn, Platform platform,
        Map<Platform, Map<String, MessageAttributeValue>> attributesMap, Map<String, String> pushData) {
    PublishRequest publishRequest = new PublishRequest();
    Map<String, MessageAttributeValue> notificationAttributes = getValidNotificationAttributes(
            attributesMap.get(platform));
    if (notificationAttributes != null && !notificationAttributes.isEmpty()) {
        publishRequest.setMessageAttributes(notificationAttributes);
    }//from  www  .  ja  v a 2  s  .c  o  m
    publishRequest.setMessageStructure("json");
    // If the message attributes are not set in the requisite method,
    // notification is sent with default attributes
    String message = SampleMessageGenerator.getSampleAndroidMessage(pushData);
    Map<String, String> messageMap = new HashMap<String, String>();
    messageMap.put("default", 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);
}