Ejemplo n.º 1
0
  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);
  }
  /**
   * 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.
   *
   * @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);
  }