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: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);
    }/*from   w  ww.  j  a v a  2s .  c om*/
    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:awslabs.lab31.SolutionCode.java

License:Open Source License

@Override
public void publishTopicMessage(AmazonSNSClient snsClient, String topicArn, String subject, String message) {
    // TODO: Construct a PublishRequest object using the provided subject, message, and topic ARN.
    PublishRequest publishRequest = new PublishRequest().withMessage(message).withSubject(subject)
            .withTopicArn(topicArn);/*ww  w  .  ja v a  2  s.  c  o m*/

    // TODO: Submit the request using the publish method of the snsClient object.
    snsClient.publish(publishRequest);
}

From source file:awslabs.lab31.StudentCode.java

License:Open Source License

/**
 * Publish a message to the specified SNS topic. Hint: Use the publish() method of the client object.
 * //w w w. j a v a2 s .com
 * @param snsClient The SNS client object.
 * @param topicArn The ARN for the topic to post the message to.
 * @param subject The subject of the message to publish.
 * @param message The body of the message to publish.
 */
@Override
public void publishTopicMessage(AmazonSNSClient snsClient, String topicArn, String subject, String message) {
    PublishRequest request = new PublishRequest().withSubject(subject).withMessage(message)
            .withTopicArn(topicArn);

    snsClient.publish(request);
}

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  ww  w.  j a  v a  2 s.co  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;/*from  w w 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.adeptj.modules.aws.sns.internal.AwsSnsService.java

License:Apache License

@Override
public SmsResponse sendSms(SmsRequest smsRequest) {
    try {//from  ww w  .j a  v a 2 s . c om
        PublishResult result = this.asyncSNS.publish(new PublishRequest().withMessage(smsRequest.getMessage())
                .withPhoneNumber(smsRequest.getCountryCode() + smsRequest.getPhoneNumber())
                .withMessageAttributes(this.smsAttributes));
        return new SmsResponse(result.getMessageId(), result.getSdkHttpMetadata().getHttpStatusCode(),
                result.getSdkHttpMetadata().getHttpHeaders());
    } catch (Exception ex) {
        LOGGER.error("Exception while sending SMS!!", ex);
        throw new AwsException(ex.getMessage(), ex);
    }
}

From source file:com.adeptj.modules.aws.sns.internal.AwsSnsService.java

License:Apache License

@Override
public void sendSmsAsync(SmsRequest smsRequest) {
    try {//from  w  w w .  jav a2  s . c  o m
        this.asyncSNS.publishAsync(new PublishRequest().withMessage(smsRequest.getMessage())
                .withPhoneNumber(smsRequest.getCountryCode() + smsRequest.getPhoneNumber())
                .withMessageAttributes(this.smsAttributes), this.asyncHandler);
    } catch (Exception ex) {
        LOGGER.error("Exception while sending SMS asynchronously!!", ex);
        throw new AwsException(ex.getMessage(), ex);
    }
}

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 .  ja  v  a2  s  .  com
 * 
 * @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 w  w w  . j  av  a2 s .c om
    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.clicktravel.infrastructure.messaging.aws.sns.SnsTopicResource.java

License:Apache License

/**
 * 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/*from   w  w  w. j  av a  2  s .  c o m*/
 */
public void publish(final String subject, final String message) throws AmazonClientException {
    amazonSnsClient
            .publish(new PublishRequest().withTopicArn(topicArn).withSubject(subject).withMessage(message));
    logger.debug("Successfully published message; subject:[" + subject + "] message:[" + message + "] snsName:["
            + topicName + "]");
}