/** * Subscribe a given commenter to future comments posted to the given entry. * * @param entry the entry to subscribe the commenter to * @param commenter the commenter to be subscribed * @return the result returned by AWS */ public SubscribeResult subscribe(Entry entry, Commenter commenter) { if (StringUtils.isEmpty(entry.getSnsArn())) { // If ARN isn't set then entry didn't have an SNS topic created with it so we ignore logger.log(Level.WARNING, "Entry did not have an SNS topic associated with it"); return null; } SubscribeRequest request = new SubscribeRequest(entry.getSnsArn(), EMAIL_PROTOCOL, commenter.getEmail()); SubscribeResult result = snsClient.subscribe(request); return result; }
/** * 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); }
/** * Deletes a previously created topic associated with the entry. * * @param entry the entry to be deleted */ public void deleteTopic(Entry entry) { DeleteTopicRequest request = new DeleteTopicRequest(entry.getSnsArn()); snsClient.deleteTopic(request); }
/** * Creates the SNS topic associated with an entry. When the topic is created, we will get an ARN * (Amazon Resource Name) which uniquely identifies the SNS topic. We write that ARN to the entry * entity so that we can refer to it later when subscribing commenters, etc. * * @param entry the new entry that's associated with the topic * @return the result returned from AWS */ public CreateTopicResult createTopic(Entry entry) { CreateTopicRequest request = new CreateTopicRequest(getTopicName(entry)); CreateTopicResult result = snsClient.createTopic(request); entry.setSnsArn(result.getTopicArn()); return result; }
/** * This method returns a unique topic name by using the entry id. * * @param entry the entry to get a topic name for * @return returns the topic name */ private String getTopicName(Entry entry) { return "entry" + StageUtils.getResourceSuffixForCurrentStage() + "-" + entry.getId(); }