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

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

Introduction

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

Prototype

public PublishRequest() 

Source Link

Document

Default constructor for PublishRequest object.

Usage

From source file:tools.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  a2  s  .  co  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);

    PublishResult result = null;
    try {
        result = snsClient.publish(publishRequest);
    } catch (EndpointDisabledException e) {
        System.out.println("Endpoint disabled:  TODO remove from dynamo DB");
    } catch (Exception e) {
        log.log(Level.SEVERE, e.getMessage(), e);
    }

    return result;
}