software.amazon.awssdk.services.sns.model.SubscribeResponse Java Examples

The following examples show how to use software.amazon.awssdk.services.sns.model.SubscribeResponse. 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: SubscribeLambda.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String subLambda(SnsClient snsClient, String topicArn, String lambdaArn) {

        try {

            SubscribeRequest request = SubscribeRequest.builder()
                .protocol("lambda")
                .endpoint(lambdaArn)
                .returnSubscriptionArn(true)
                .topicArn(topicArn)
                .build();

            SubscribeResponse result = snsClient.subscribe(request);

            return result.subscriptionArn();


         } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
        }
        return "";
     //snippet-end:[sns.java2.SubscribeLambda.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 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 #3
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 #4
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 #5
Source File: QuarksShieldAsyncResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/subscribe")
public Uni<Response> subscribe() {
    return Uni.createFrom()
        .completionStage(sns.subscribe(s -> s.topicArn(topicArn).protocol("http").endpoint(notificationEndpoint())))
        .onItem().apply(SubscribeResponse::subscriptionArn)
        .onItem().invoke(this::setSubscriptionArn)
        .onItem().invoke(arn -> LOGGER.infov("Subscribed Quarks shield with id = {0} ", arn))
        .onItem().apply(arn -> Response.ok().entity(arn).build());
}
 
Example #6
Source File: QuarksShieldSyncResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/subscribe")
public Response subscribe() {
    String notificationEndpoint = notificationEndpoint();
    SubscribeResponse response = sns.subscribe(s -> s.topicArn(topicArn).protocol("http").endpoint(notificationEndpoint));
    subscriptionArn = response.subscriptionArn();
    LOGGER.infov("Subscribed Quarks shield <{0}> : {1} ", notificationEndpoint, response.subscriptionArn());
    return Response.ok().entity(response.subscriptionArn()).build();
}
 
Example #7
Source File: Topics.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Subscribes an existing Amazon SQS queue to an existing Amazon SNS topic.
 * <p>
 * The specified Amazon SNS client will be used to send the subscription
 * request, and the Amazon SQS client will be used to modify the policy on
 * the queue to allow it to receive messages from the SNS topic.
 * <p>
 * The policy applied to the SQS queue is similar to this:
 * <pre>
 * {
 *    "Version" : "2008-10-17",
 *    "Statement" : [{
 *       "Sid" : "topic-subscription-arn:aws:sns:us-west-2:599109622955:myTopic",
 *       "Effect" : "Allow",
 *       "Principal" : {
 *          "AWS":["*"]
 *       },
 *       "Action" : ["sqs:SendMessage"],
 *       "Resource":["arn:aws:sqs:us-west-2:599109622955:myQueue"],
 *       "Condition" : {
 *          "ArnLike":{
 *             "aws:SourceArn":["arn:aws:sns:us-west-2:599109622955:myTopic"]
 *          }
 *       }
 *    }]
 * }
 * </pre>
 * <p>
 * <b>IMPORTANT</b>: There might be a small time period immediately after
 * subscribing the SQS queue to the SNS topic and updating the SQS queue's
 * policy, where messages are not able to be delivered to the queue. After a
 * moment, the new queue policy will propagate and the queue will be able to
 * receive messages. This delay only occurs immediately after initially
 * subscribing the queue.
 * <p>
 * <b>IMPORTANT</b>: The specified queue and topic (as well as the SNS and
 * SQS client) should both be located in the same AWS region.
 *
 * @param sns
 *            The Amazon SNS client to use when subscribing the queue to the
 *            topic.
 * @param sqs
 *            The Amazon SQS client to use when applying the policy to allow
 *            subscribing to the topic.
 * @param snsTopicArn
 *            The Amazon Resource Name (ARN) uniquely identifying the Amazon
 *            SNS topic. This value is returned form Amazon SNS when
 *            creating the topic.
 * @param sqsQueueUrl
 *            The URL uniquely identifying the Amazon SQS queue. This value
 *            is returned from Amazon SQS when creating the queue.
 * @param extendPolicy
 *            Decides behavior to overwrite the existing policy or extend it.
 *
 * @return The subscription ARN as returned by Amazon SNS when the queue is
 *         successfully subscribed to the topic.
 *
 * @throws SdkClientException
 *             If any internal errors are encountered inside the client
 *             while attempting to make the request or handle the response.
 *             For example if a network connection is not available.
 * @throws SdkServiceException
 *             If an error response is returned by SnsClient indicating
 *             either a problem with the data in the request, or a server
 *             side issue.
 */
public static String subscribeQueue(SnsClient sns, SqsClient sqs, String snsTopicArn, String sqsQueueUrl,
                                    boolean extendPolicy)
        throws SdkClientException, SdkServiceException {
    List<String> sqsAttrNames = Arrays.asList(QueueAttributeName.QUEUE_ARN.toString(),
                                              QueueAttributeName.POLICY.toString());
    Map<String, String> sqsAttrs =
            sqs.getQueueAttributes(GetQueueAttributesRequest.builder()
                    .queueUrl(sqsQueueUrl)
                    .attributeNamesWithStrings(sqsAttrNames)
                    .build())
                    .attributesAsStrings();
    String sqsQueueArn = sqsAttrs.get(QueueAttributeName.QUEUE_ARN.toString());

    String policyJson = sqsAttrs.get(QueueAttributeName.POLICY.toString());
    Policy policy = extendPolicy && policyJson != null && policyJson.length() > 0
                    ? Policy.fromJson(policyJson) : new Policy();
    policy.getStatements().add(new Statement(Effect.Allow)
                                       .withId("topic-subscription-" + snsTopicArn)
                                       .withPrincipals(Principal.ALL_USERS)
                                       .withActions(new Action("sqs:SendMessage"))
                                       .withResources(new Resource(sqsQueueArn))
                                       .withConditions(ConditionFactory.newSourceArnCondition(snsTopicArn)));

    Map<String, String> newAttrs = new HashMap<String, String>();
    newAttrs.put(QueueAttributeName.POLICY.toString(), policy.toJson());
    sqs.setQueueAttributes(SetQueueAttributesRequest.builder().queueUrl(sqsQueueUrl).attributesWithStrings(newAttrs).build());

    SubscribeResponse subscribeResult = sns.subscribe(SubscribeRequest.builder()
            .topicArn(snsTopicArn).protocol("sqs")
            .endpoint(sqsQueueArn)
            .build());
    return subscribeResult.subscriptionArn();
}