Java tutorial
/* * Author Stephen * * Copyright (c) 2015 Stephen Booysen, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of * Stephen Booysen. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Stephen Booysen * * Stephen Booysen MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ /** * */ package shnakkydoodle.notifying.provider.aws; import shnakkydoodle.notifying.common.INotificationProvider; import shnakkydoodle.notifying.common.entity.Notification; import shnakkydoodle.notifying.common.entity.NotificationHistory; import shnakkydoodle.notifying.common.entity.NotificationSubscription; import shnakkydoodle.notifying.common.entity.NotificationTopic; import java.util.ArrayList; import java.util.Date; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.model.Region; import com.amazonaws.services.sns.AmazonSNSClient; import com.amazonaws.services.sns.model.CreateTopicRequest; import com.amazonaws.services.sns.model.CreateTopicResult; import com.amazonaws.services.sns.model.GetTopicAttributesResult; import com.amazonaws.services.sns.model.PublishRequest; import com.amazonaws.services.sns.model.PublishResult; import com.amazonaws.services.sns.model.SubscribeRequest; import com.amazonaws.services.sns.model.Subscription; import com.amazonaws.services.sns.model.Topic; /** * @author Stephen * */ public class AwsProvider implements INotificationProvider { // region Private Members // Thre credentials AWSCredentials credentials = null; /** * Create a aws sanitised queuename * * @param queuename * @return */ private String awsName(String name) { return name.replace(" ", "-").replaceAll("\\.", "-"); } /** * Get the SNS Client already initialised * * @return */ private AmazonSNSClient getSNSClient() { AmazonSNSClient snsClient = new AmazonSNSClient(this.credentials); snsClient.setRegion(Region.EU_Ireland.toAWSRegion()); return snsClient; } /** * Retrieve a specific subsctipion id * * @param topicName * @param protocol * @param endpoint * @return */ private String retrieveSubscriptionId(String topicName, String protocol, String endpoint) { // find the topic NotificationTopic notificationTopic = this.retrieveNotificationTopic(topicName); if (notificationTopic == null) { return null; } AmazonSNSClient snsClient = getSNSClient(); for (Subscription subscription : snsClient.listSubscriptions().getSubscriptions()) { if (subscription.getTopicArn().equals(notificationTopic.getTopicId())) { if (subscription.getProtocol().equals(protocol) && subscription.getEndpoint().equals(endpoint)) { return subscription.getSubscriptionArn(); } } } return null; } // endregion // region Public Members /** * Constructor * * @param awsaccesskeyid * @param awssecretaccesskey */ public AwsProvider(String awsaccesskeyid, String awssecretaccesskey) { credentials = new BasicAWSCredentials(awsaccesskeyid, awssecretaccesskey); } /** * Create a notification topic * * @param name * @paramd escription */ @Override public void insertNotificationTopic(String name, String description) { AmazonSNSClient snsClient = getSNSClient(); CreateTopicRequest createTopicRequest = new CreateTopicRequest(awsName(name)); CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest); snsClient.setTopicAttributes(createTopicResult.getTopicArn(), "DisplayName", name); } /** * Update a notification topic * * @param name * @paramd escription */ @Override public void updateNotificationTopic(String topicId, String name, String description) { AmazonSNSClient snsClient = getSNSClient(); snsClient.setTopicAttributes(topicId, "DisplayName", name); } /** * Delete a notification topic * * @param name * @paramd escription */ @Override public void deleteNotificationTopic(String topicName) { AmazonSNSClient snsClient = getSNSClient(); NotificationTopic notificationTopic = this.retrieveNotificationTopic(topicName); if (notificationTopic != null) { snsClient.deleteTopic(notificationTopic.getTopicId()); } } /** * Retrieve a notification topic * * @param name * @paramd Notifiation Topic */ @Override public NotificationTopic retrieveNotificationTopic(String name) { AmazonSNSClient snsClient = getSNSClient(); for (Topic topic : snsClient.listTopics().getTopics()) { GetTopicAttributesResult result = snsClient.getTopicAttributes(topic.getTopicArn()); if (result.getAttributes().get("DisplayName").equals(name)) { NotificationTopic instance = new NotificationTopic(); instance.setTopicId(topic.getTopicArn()); instance.setName(result.getAttributes().get("DisplayName")); instance.setDescription(""); return instance; } } return null; } /** * Retrieve a specific notification topic for its id * * @param topicId * @paramd Notifiation Topic */ @Override public NotificationTopic retrieveNotificationTopicForId(String topicId) { AmazonSNSClient snsClient = getSNSClient(); for (Topic topic : snsClient.listTopics().getTopics()) { GetTopicAttributesResult result = snsClient.getTopicAttributes(topic.getTopicArn()); if (topic.getTopicArn().equals(topicId)) { NotificationTopic instance = new NotificationTopic(); instance.setTopicId(topic.getTopicArn()); instance.setName(result.getAttributes().get("DisplayName")); instance.setDescription(""); return instance; } } return null; } /** * Retrieve all notification topics * * @return NotificationTopics */ @Override public ArrayList<NotificationTopic> retrieveNotificationTopics() { ArrayList<NotificationTopic> data = new ArrayList<NotificationTopic>(); AmazonSNSClient snsClient = getSNSClient(); for (Topic topic : snsClient.listTopics().getTopics()) { GetTopicAttributesResult result = snsClient.getTopicAttributes(topic.getTopicArn()); NotificationTopic instance = new NotificationTopic(); instance.setTopicId(topic.getTopicArn()); instance.setName(result.getAttributes().get("DisplayName")); instance.setDescription(result.getAttributes().get("Description")); data.add(instance); } return data; } /** * Insert a notificaition subscription * * @param topicName * @param protocol * @param endpoint * */ @Override public void insertNotificationSubscription(String topicName, String protocol, String endpoint) { AmazonSNSClient snsClient = getSNSClient(); NotificationTopic notificationTopic = this.retrieveNotificationTopic(topicName); if (notificationTopic == null) { this.insertNotificationTopic(topicName, ""); } notificationTopic = this.retrieveNotificationTopic(topicName); SubscribeRequest subRequest = new SubscribeRequest(notificationTopic.getTopicId(), protocol, endpoint); snsClient.subscribe(subRequest); } /** * Delete and endpoint * * @param topicName * @param protocol * @param endpoint */ @Override public void deleteNotificationSubscription(String topicName, String protocol, String endpoint) { AmazonSNSClient snsClient = getSNSClient(); String subscriptionId = retrieveSubscriptionId(topicName, protocol, endpoint); if (subscriptionId != null && !subscriptionId.equals("")) { snsClient.unsubscribe(subscriptionId); } } /** * Return a notification subscription */ @Override public NotificationSubscription retrieveNotificationSubscription(String topicName, String protocol, String endpoint) { ArrayList<NotificationSubscription> notificationSubscriptions = this .retrieveNotificationSubscriptions(topicName); for (NotificationSubscription notificationSubscription : notificationSubscriptions) { if (notificationSubscription.getProtocol().equals(protocol) && notificationSubscription.getEndPoint().equals(endpoint)) { return notificationSubscription; } } return null; } /** * Retrieve the notifcation subscriptions for a topic * * @param topicName */ @Override public ArrayList<NotificationSubscription> retrieveNotificationSubscriptions(String topicName) { ArrayList<NotificationSubscription> data = new ArrayList<NotificationSubscription>(); // find the topic NotificationTopic notificationTopic = this.retrieveNotificationTopic(topicName); if (notificationTopic == null) { return data; } AmazonSNSClient snsClient = getSNSClient(); for (Subscription subscription : snsClient.listSubscriptions().getSubscriptions()) { if (subscription.getTopicArn().equals(notificationTopic.getTopicId())) { NotificationSubscription instance = new NotificationSubscription(); instance.setNotificationTopic(notificationTopic); instance.setProtocol(subscription.getProtocol()); instance.setEndPoint(subscription.getEndpoint()); data.add(instance); } } return data; } /** * Retrieve the notifcation subscriptions for a topic * * @param topicName */ @Override public Integer retrieveTotalNotificationSubscriptions(String topicName) { return this.retrieveNotificationSubscriptions(topicName).size(); } /** * Publish a notification */ @Override public String insertNotification(String topicName, String subject, String message) { AmazonSNSClient snsClient = getSNSClient(); // find the topic NotificationTopic notificationTopic = this.retrieveNotificationTopic(topicName); if (notificationTopic == null) { this.insertNotificationTopic(topicName, ""); } // publish to an SNS topic PublishRequest publishRequest = new PublishRequest(this.retrieveNotificationTopic(topicName).getTopicId(), subject); PublishResult publishResult = snsClient.publish(publishRequest); return publishResult.getMessageId(); } /** * Notifications cannot be deleted */ @Override public void deleteNotification(String notificationid) { AmazonSNSClient snsClient = getSNSClient(); } /** * Delete all notifications for a topic by deleting the topic */ @Override public void deleteNotificationForTopic(String topicName) { ArrayList<NotificationSubscription> subscriptions = this.retrieveNotificationSubscriptions(topicName); this.deleteNotificationTopic(topicName); this.insertNotificationTopic(topicName, ""); for (NotificationSubscription subscription : subscriptions) { this.insertNotificationSubscription(topicName, subscription.getProtocol(), subscription.getEndPoint()); } } @Override public Notification retrieveNotification(String notificationId) { // Not implemented return null; } @Override public ArrayList<Notification> retrieveNotifications(String topicName) { // Not implemented return null; } @Override public ArrayList<Notification> retrieveNotifications(String topicName, Date startDate, Date endDate) { // Not implemented return null; } @Override public Integer retrieveTotalNotificationData(String topicName) { // Not implemented return null; } @Override public void insertNotificationHistory(String notificationId, String protocol, String endpoint) { // Not implemented } @Override public void deleteNotificationHistory(String notificationId, String protocol, String endpoint) { // Not implemented } @Override public void deleteNotificationHistoryForNotification(String notificationId) { // Not implemented } @Override public void deleteNotificationHistoryForTopic(String topicName) { // Not implemented } @Override public NotificationHistory retrieveNotificationHistory(String notificationId, String protocol, String endpoint) { // Not implemented return null; } @Override public ArrayList<NotificationHistory> retrieveNotificationHistoryForNotification(String notificationId) { // Not implemented return null; } /** * This provider does its onw notifications */ public Boolean doNotifications() { return false; } // endregion }