software.amazon.awssdk.services.sns.SnsClient Java Examples

The following examples show how to use software.amazon.awssdk.services.sns.SnsClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Unsubscribe.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void unSub(SnsClient snsClient, String subscriptionToken) {

        try {
            UnsubscribeRequest request = UnsubscribeRequest.builder()
                .subscriptionArn(subscriptionToken)
                .build();

            UnsubscribeResponse result = snsClient.unsubscribe(request);

            System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode()
                + "\n\nSubscription was removed for " + request.subscriptionArn());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        //snippet-end:[sns.java2.Unsubscribe.main]
    }
 
Example #2
Source File: SubscribeTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "SubscribeTextSMS - send a confirmation message as a text message.\n" +
            "Usage: SubscribeTextSMS <topicArn> <phoneNumber>\n\n" +
            "Where:\n" +
            "  topicArn - the ARN of the topic to subscribe.\n\n" +
            "  phoneNumber - phone number to look up. Example: +1XXX5550100\n\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }


    String topicArn = args[0];
    String phoneNumber = args[1];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    subTextSNS(snsClient, topicArn, phoneNumber);
}
 
Example #3
Source File: SubscribeTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void subTextSNS( SnsClient snsClient, String topicArn,String phoneNumber) {

        try {

            SubscribeRequest request = SubscribeRequest.builder()
                .protocol("sms")
                .endpoint(phoneNumber)
                .returnSubscriptionArn(true)
                .topicArn(topicArn)
                .build();

            SubscribeResponse result = snsClient.subscribe(request);

            System.out.println("Subscription ARN: " + result.subscriptionArn() + "\n\n Status was " + result.sdkHttpResponse().statusCode());

    } catch (SnsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
        //snippet-end:[sns.java2.SubscribeTextSMS.main]
    }
 
Example #4
Source File: GetSMSAtrributes.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "GetSMSAtrributes - retrieve your default SMS type for Amazon SNS\n" +
            "Usage: GetSMSAtrributes <topicArn>\n\n" +
            "Where:\n" +
            "  topicArn - the ARN of the topic from which to retrieve attributes.\n\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    
    String topicArn = args[0];

    // Create an SnsClient object
    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    getSNSAttrutes(snsClient, topicArn);
}
 
Example #5
Source File: PublishTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "PublishTextSMS - send an SMS message\n" +
            "Usage: PublishTextSMS <message> <phoneNumber>\n\n" +
            "Where:\n" +
            "  message - message text to send.\n\n" +
            "  phoneNumber - phone number to look up. Example: +1XXX5550100\n\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String message = args[0];
    String phoneNumber = args[1];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    pubTextSMS(snsClient, message, phoneNumber);
}
 
Example #6
Source File: CheckOptOut.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
final String USAGE = "\n" +
        "CheckOptOut - look if phone number owner has opted out of receiving messages\n" +
        "Usage: CheckOptOut <phoneNumber>\n\n" +
        "Where:\n" +
        "  phoneNumber - phone number to look up. Example: +1XXX5550100\n\n";

if (args.length < 1) {
    System.out.println(USAGE);
    System.exit(1);
}

String phoneNumber = args[0];

SnsClient snsClient = SnsClient.builder()
            .region(Region.US_EAST_1)
            .build();

checkPhone(snsClient, phoneNumber);
}
 
Example #7
Source File: CheckOptOut.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void checkPhone(SnsClient snsClient, String phoneNumber) {

            try {
            CheckIfPhoneNumberIsOptedOutRequest request = CheckIfPhoneNumberIsOptedOutRequest.builder()
                .phoneNumber(phoneNumber)
                .build();

            CheckIfPhoneNumberIsOptedOutResponse result = snsClient.checkIfPhoneNumberIsOptedOut(request);

            System.out.println(result.isOptedOut() + "Phone Number " + phoneNumber + " has Opted Out of receiving sns messages." +
                "\n\nStatus was " + result.sdkHttpResponse().statusCode());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        //snippet-end:[sns.java2.CheckOptOut.main]
    }
 
Example #8
Source File: ConfirmSubscription.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "ConfirmSubscription - confirm a subscription to an Amazon SNS topic\n" +
            "Usage: ConfirmSubscription <subscriptionToken> <topicArn>\n\n" +
            "Where:\n" +
            "  subscriptionToken - endpoint token from Subscribe action.\n\n" +
            "  topicArn - the ARN of the topic to delete.\n\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String subscriptionToken = args[0];
    String topicArn = args[1];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    confirmSub(snsClient, subscriptionToken, topicArn ) ;
}
 
Example #9
Source File: ConfirmSubscription.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void confirmSub(SnsClient snsClient, String subscriptionToken, String topicArn ) {

        try {
             ConfirmSubscriptionRequest request = ConfirmSubscriptionRequest.builder()
                .token(subscriptionToken)
                .topicArn(topicArn)
                .build();

            ConfirmSubscriptionResponse result = snsClient.confirmSubscription(request);

            System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nSubscription Arn: \n\n" + result.subscriptionArn());
    } catch (SnsException e) {

        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
        //snippet-end:[sns.java2.ConfirmSubscription.main]
    }
 
Example #10
Source File: Unsubscribe.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "Unsubscribe - removes a subscription from a topic \n" +
            "Usage: Unsubscribe <subscriptionToken>\n\n" +
            "Where:\n" +
            "  subscriptionToken - endpoint token from Subscribe action.\n\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String subscriptionToken = args[0];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_EAST_1)
            .build();

    unSub(snsClient, subscriptionToken);
}
 
Example #11
Source File: PublishTopic.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void pubTopic(SnsClient snsClient, String message, String topicArn) {

        try {
            PublishRequest request = PublishRequest.builder()
                .message(message)
                .topicArn(topicArn)
                .build();

            PublishResponse result = snsClient.publish(request);
            System.out.println(result.messageId() + " Message sent. Status was " + result.sdkHttpResponse().statusCode());

         } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
              System.exit(1);
         }
        //snippet-end:[sns.java2.PublishTopic.main]
    }
 
Example #12
Source File: SubscribeHTTPS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "SubscribeHTTPS - send confirmation message to a URL endpoint.\n" +
            "Usage: SubscribeHTTPS <topicArn> <url>\n\n" +
            "Where:\n" +
            "  topicArn - the ARN of the topic to subscribe.\n\n" +
            "  url - HTTPS endpoint to subscribe.\n\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String topicArn = args[0];
    String url = args[1];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    subHTTS(snsClient, topicArn, url) ;
}
 
Example #13
Source File: SubscribeHTTPS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void subHTTS(SnsClient snsClient, String topicArn, String url ) {

        try {
            SubscribeRequest request = SubscribeRequest.builder()
                .protocol("http")
                .endpoint(url)
                .returnSubscriptionArn(true)
                .topicArn(topicArn)
                .build();

            SubscribeResponse result = snsClient.subscribe(request);
            System.out.println("Subscription ARN: " + result.subscriptionArn() + "\n\n Status was " + result.sdkHttpResponse().statusCode());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
         }

        //snippet-end:[sns.java2.SubscribeHTTPS.main]
    }
 
Example #14
Source File: SetSMSAttributes.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void setSNSAttributes( SnsClient snsClient, HashMap<String, String> attributes) {

        try {
            SetSmsAttributesRequest request = SetSmsAttributesRequest.builder()
                .attributes(attributes)
                .build();

            SetSmsAttributesResponse result = snsClient.setSMSAttributes(request);
            System.out.println("Set default Attributes to " + attributes + ". Status was " + result.sdkHttpResponse().statusCode());

    } catch (SnsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
        }
        //snippet-end:[sns.java2.SetSMSAttributes.main]
    }
 
Example #15
Source File: ListTopics.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void listSNSTopics(SnsClient snsClient) {

        try {
            ListTopicsRequest request = ListTopicsRequest.builder()
                    .build();

            ListTopicsResponse result = snsClient.listTopics(request);
            System.out.println("Status was " + result.sdkHttpResponse().statusCode() + "\n\nTopics\n\n" + result.topics());

    } catch (SnsException e) {

        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
        //snippet-end:[sns.java2.ListTopics.main]
    }
 
Example #16
Source File: SetSMSAttributes.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
final String USAGE = "\n" +
        "SetSMSAttributes - set your default SMS type for Amazon SNS.\n" +
        "Usage: SetSMSAttributes \n\n";


HashMap<String, String> attributes = new HashMap<>(1);
attributes.put("DefaultSMSType", "Transactional");
attributes.put("UsageReportS3Bucket", "janbucket77" );

SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

setSNSAttributes(snsClient, attributes);
}
 
Example #17
Source File: ListSubscriptions.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void listSNSSubscriptions( SnsClient snsClient) {

        try {
            ListSubscriptionsRequest request = ListSubscriptionsRequest.builder()
                    .build();

            ListSubscriptionsResponse result = snsClient.listSubscriptions(request);
            System.out.println(result.subscriptions());

        } catch (SnsException e) {

            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        //snippet-end:[sns.java2.ListSubscriptions.main]
    }
 
Example #18
Source File: CreateTopic.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "CreateTopic - create an Amazon SNS topic\n" +
            "Usage: CreateTopic <topicName>\n\n" +
            "Where:\n" +
            "  topicName - the name of the topic to create.\n\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String topicName = args[0];

    System.out.println("Creating a topic with name: " + topicName);

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    String arnVal = createSNSTopic(snsClient, topicName) ;
    System.out.println("The topic ARN is" +arnVal);
}
 
Example #19
Source File: CreateTopic.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String createSNSTopic(SnsClient snsClient, String topicName ) {

        CreateTopicResponse result = null;
        try {
            CreateTopicRequest request = CreateTopicRequest.builder()
                    .name(topicName)
                    .build();

            result = snsClient.createTopic(request);
            return result.topicArn();
        } catch (SnsException e) {

            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
        //snippet-end:[sns.java2.CreateTopic.main]
    }
 
Example #20
Source File: PublishTopic.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "PublishTopic - publish an Amazon SNS topic\n" +
            "Usage: PublishTopic <message> <topicArn>\n\n" +
            "Where:\n" +
            "  message - message text to send.\n\n" +
            "  topicArn - the ARN of the topic to look up.\n\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String message = args[0];
    String topicArn = args[1];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    pubTopic(snsClient, message, topicArn);
}
 
Example #21
Source File: PublishTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public void sendMessage(String id) {

        Region region = Region.US_EAST_1;
        SnsClient snsClient = SnsClient.builder()
                .region(region)
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .build();
        String message = "A new item with ID value "+ id +" was added to the DynamoDB table";
        String phoneNumber="ENTER MOBILE NUMBER"; //Replace with a mobile phone number

        try {
            PublishRequest request = PublishRequest.builder()
                    .message(message)
                    .phoneNumber(phoneNumber)
                    .build();

            PublishResponse result = snsClient.publish(request);

        } catch (SnsException e) {

            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example #22
Source File: SetTopicAttributes.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void setTopAttr(SnsClient snsClient, String attribute, String topicArn, String value) {

        try {

            SetTopicAttributesRequest request = SetTopicAttributesRequest.builder()
                .attributeName(attribute)
                .attributeValue(value)
                .topicArn(topicArn)
                .build();

            SetTopicAttributesResponse result = snsClient.setTopicAttributes(request);

            System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nTopic " + request.topicArn()
                + " updated " + request.attributeName() + " to " + request.attributeValue());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        //snippet-end:[sns.java2.SetTopicAttributes.main]
    }
 
Example #23
Source File: SetTopicAttributes.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "SetTopicAttributes - update defaults from a topic.\n" +
            "Usage: SetTopicAttributes <attribute> <topicArn> <value>\n\n" +
            "Where:\n" +
            "  attribute - Attribute action to use. Valid parameters: Policy | DisplayName | DeliveryPolicy .\n" +
            "  topicArn - The ARN of the topic to update. \n" +
            "  value - New value for the attribute.\n\n";

    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }


    String attribute = args[0];
    String topicArn = args[1];
    String value = args[2];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    setTopAttr(snsClient, attribute, topicArn, value);

}
 
Example #24
Source File: SubscribeEmail.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void subEmail(SnsClient snsClient, String topicArn, String email) {

        try {
            SubscribeRequest request = SubscribeRequest.builder()
                .protocol("email")
                .endpoint(email)
                .returnSubscriptionArn(true)
                .topicArn(topicArn)
                .build();

            SubscribeResponse result = snsClient.subscribe(request);
            System.out.println("Subscription ARN: " + result.subscriptionArn() + "\n\n Status was " + result.sdkHttpResponse().statusCode());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        //snippet-end:[sns.java2.SubscribeEmail.main]
    }
 
Example #25
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the AWS account info for the integration tests and creates an AutoScaling client for
 * tests to use.
 */
@BeforeClass
public static void setUp() throws IOException {
    setUpCredentials();
    autoscaling = AutoScalingClient.builder()
            .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
            .region(REGION)
            .build();
    autoscalingAsync = AutoScalingAsyncClient.builder()
            .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
            .region(REGION)
            .build();
    sns = SnsClient.builder()
            .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
            .region(REGION)
            .build();
}
 
Example #26
Source File: DeleteTopic.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "DeleteTopic - delete an Amazon SNS topic\n" +
            "Usage: DeleteTopic <topicArn>\n\n" +
            "Where:\n" +
            "  topicArn - the ARN of the topic to delete.\n\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String topicArn = args[0];
    System.out.println("Deleting a topic with name: " + topicArn);

    // Create an SnsClient object
    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    deleteSNSTopic(snsClient, topicArn);
}
 
Example #27
Source File: DeleteTopic.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteSNSTopic(SnsClient snsClient, String topicArn ) {

        try {
            DeleteTopicRequest request = DeleteTopicRequest.builder()
                .topicArn(topicArn)
                .build();

            DeleteTopicResponse result = snsClient.deleteTopic(request);
            System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        //snippet-end:[sns.java2.DeleteTopic.main]
    }
 
Example #28
Source File: SubscribeEmail.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE = "\n" +
                "SubscribeEmail - send a confirmation message to an email address.\n" +
                "Usage: SubscribeEmail  <topicArn> <email>\n\n" +
                "Where:\n" +
                "  topicArn - the ARN of the topic to subscribe.\n\n" +
                "  email - email address to subscribe.\n\n";

        if (args.length < 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String topicArn = args[0];
        String email = args[1];

        SnsClient snsClient = SnsClient.builder()
                .region(Region.US_WEST_2)
                .build();

        subEmail(snsClient, topicArn, email) ;
    }
 
Example #29
Source File: PublishTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void pubTextSMS(SnsClient snsClient, String message, String phoneNumber) {
    try {
        PublishRequest request = PublishRequest.builder()
            .message(message)
            .phoneNumber(phoneNumber)
            .build();

        PublishResponse result = snsClient.publish(request);

        System.out.println(result.messageId() + " Message sent. Status was " + result.sdkHttpResponse().statusCode());

    } catch (SnsException e) {

    System.err.println(e.awsErrorDetails().errorMessage());
    System.exit(1);
    }

    //snippet-end:[sns.java2.PublishTextSMS.main]
}
 
Example #30
Source File: SubscribeLambda.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "SubscribeLambda - subscribe an AWS Lambda function.\n" +
            "Usage: SubscribeLambda <topicArn> <lambdaArn>\n\n" +
            "Where:\n" +
            "  topicArn - the ARN of the topic to subscribe.\n\n" +
            "  lambdaArn - the ARN of an AWS Lambda function.\n\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String topicArn = args[0];
    String lambdaArn = args[1];

    SnsClient snsClient = SnsClient.builder()
            .region(Region.US_WEST_2)
            .build();

    String arnValue = subLambda(snsClient, topicArn, lambdaArn) ;
    System.out.println("Subscription ARN: " + arnValue);
}