//snippet-sourcedescription:[SubscribeHTTPS.java demonstrates how to demonstrates how to subscribe to an Amazon Simple Notification Service (Amazon SNS) HTTPs endpoint.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-keyword:[Amazon Simple Notification Service]

/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   SPDX-License-Identifier: Apache-2.0
*/

package com.example.sns;

//snippet-start:[sns.java2.SubscribeHTTPS.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.SnsException;
import software.amazon.awssdk.services.sns.model.SubscribeRequest;
import software.amazon.awssdk.services.sns.model.SubscribeResponse;
//snippet-end:[sns.java2.SubscribeHTTPS.import]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class SubscribeHTTPS {

    public static void main(String[] args) {

        final String usage = "\n" +
            "Usage: " +
            "   <topicArn> <url>\n\n" +
            "Where:\n" +
            "   topicArn - The ARN of the topic to subscribe.\n\n" +
            "   url - The HTTPS endpoint that you want to receive notifications.\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_EAST_1)
            .credentialsProvider(ProfileCredentialsProvider.create())
            .build();

        subHTTPS(snsClient, topicArn, url) ;
        snsClient.close();
    }

    //snippet-start:[sns.java2.SubscribeHTTPS.main]
    public static void subHTTPS(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 is " + result.subscriptionArn() + "\n\n Status is " + result.sdkHttpResponse().statusCode());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
         }
    }
    //snippet-end:[sns.java2.SubscribeHTTPS.main]
}

@@ -2246,16 +2246,17 @@
 ol(%22http
+s
 %22)%0D%0A    

Use the "https" protocol instead of "http".