Example usage for com.amazonaws.services.sns.model SetEndpointAttributesRequest SetEndpointAttributesRequest

List of usage examples for com.amazonaws.services.sns.model SetEndpointAttributesRequest SetEndpointAttributesRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.sns.model SetEndpointAttributesRequest SetEndpointAttributesRequest.

Prototype

SetEndpointAttributesRequest

Source Link

Usage

From source file:com.screensaver.util.AmazonSNSClientWrapper.java

License:Open Source License

public void demoNotification(Platform platform, String principal, String credential, String platformToken,
        String applicationName, Map<Platform, Map<String, MessageAttributeValue>> attrsMap) {

    String prefEndpointArn = PrefUtils.getString(context, Constants.PREF_END_POINT_ARN, "");
    boolean updateNeeded = false;
    boolean createNeeded = false;
    if (prefEndpointArn != null && !prefEndpointArn.equalsIgnoreCase("")) {
        createNeeded = false;// w  ww  . j  av  a2 s  .c o  m
    } else {
        createNeeded = true;
    }
    if (createNeeded) {
        // Create Platform Application. This corresponds to an app on a
        // platform.
        try {

            CreatePlatformApplicationResult platformApplicationResult = createPlatformApplication(
                    applicationName, platform, principal, credential);

            // The Platform Application Arn can be used to uniquely identify the
            // Platform Application.
            String platformApplicationArn = platformApplicationResult.getPlatformApplicationArn();

            // Create an Endpoint. This corresponds to an app on a device.

            CreatePlatformEndpointResult platformEndpointResult = createPlatformEndpoint(platform,
                    "CustomData - Useful to store endpoint specific data", platformToken,
                    platformApplicationArn);
            System.out.println(platformEndpointResult);
            prefEndpointArn = platformEndpointResult.getEndpointArn();
            PrefUtils.saveString(context, Constants.PREF_END_POINT_ARN, prefEndpointArn);

            // Publish a push notification to an Endpoint.
            CreateTopicResult createTopicResult = createTopic(platform,
                    "CustomData - Useful to store endpoint specific data", platformToken,
                    platformApplicationArn);
            SubscribeRequest subscribeRequest = new SubscribeRequest(createTopicResult.getTopicArn(),
                    "application", platformEndpointResult.getEndpointArn());
            snsClient.subscribe(subscribeRequest);
            createNeeded = false;
        } catch (Exception e) {
            String message = e.getMessage();
            System.out.println("Exception message: " + message);
            Pattern p = Pattern
                    .compile(".*Endpoint (arn:aws:sns[^ ]+) already exists " + "with the same token.*");
            Matcher m = p.matcher(message);
            if (m.matches()) {
                // The platform endpoint already exists for this token, but with
                // additional custom data that
                // createEndpoint doesn't want to overwrite. Just use the
                // existing platform endpoint.
                prefEndpointArn = m.group(1);
                PrefUtils.saveString(context, Constants.PREF_END_POINT_ARN, prefEndpointArn);
            } else {
                // Rethrow the exception, the input is actually bad.
                throw e;
            }

        }
    }

    try {
        GetEndpointAttributesRequest geaReq = new GetEndpointAttributesRequest()
                .withEndpointArn(prefEndpointArn);
        GetEndpointAttributesResult geaRes = snsClient.getEndpointAttributes(geaReq);

        updateNeeded = !geaRes.getAttributes().get("Token").equals(platformToken)
                || !geaRes.getAttributes().get("Enabled").equalsIgnoreCase("true");

    } catch (Exception nfe) {
        // We had a stored ARN, but the platform endpoint associated with it
        // disappeared. Recreate it.
        createNeeded = true;
    }

    if (createNeeded) {
        // Create Platform Application. This corresponds to an app on a
        // platform.
        try {

            CreatePlatformApplicationResult platformApplicationResult = createPlatformApplication(
                    applicationName, platform, principal, credential);

            // The Platform Application Arn can be used to uniquely identify the
            // Platform Application.
            String platformApplicationArn = platformApplicationResult.getPlatformApplicationArn();

            // Create an Endpoint. This corresponds to an app on a device.

            CreatePlatformEndpointResult platformEndpointResult = createPlatformEndpoint(platform, "",
                    platformToken, platformApplicationArn);
            System.out.println(platformEndpointResult);
            prefEndpointArn = platformEndpointResult.getEndpointArn();
            PrefUtils.saveString(context, Constants.PREF_END_POINT_ARN, prefEndpointArn);

            // Publish a push notification to an Endpoint.
            CreateTopicResult createTopicResult = createTopic(platform, "", platformToken,
                    platformApplicationArn);
            SubscribeRequest subscribeRequest = new SubscribeRequest(createTopicResult.getTopicArn(),
                    "application", platformEndpointResult.getEndpointArn());
            snsClient.subscribe(subscribeRequest);
            createNeeded = false;
        } catch (Exception e) {
            String message = e.getMessage();
            System.out.println("Exception message: " + message);
            Pattern p = Pattern
                    .compile(".*Endpoint (arn:aws:sns[^ ]+) already exists " + "with the same token.*");
            Matcher m = p.matcher(message);
            if (m.matches()) {
                // The platform endpoint already exists for this token, but with
                // additional custom data that
                // createEndpoint doesn't want to overwrite. Just use the
                // existing platform endpoint.
                prefEndpointArn = m.group(1);
                PrefUtils.saveString(context, Constants.PREF_END_POINT_ARN, prefEndpointArn);
            } else {
                // Rethrow the exception, the input is actually bad.
                throw e;
            }

        }
    }
    if (updateNeeded) {
        Map attribs = new HashMap();
        attribs.put("Token", platformToken);
        attribs.put("Enabled", "true");
        SetEndpointAttributesRequest saeReq = new SetEndpointAttributesRequest()
                .withEndpointArn(prefEndpointArn).withAttributes(attribs);
        snsClient.setEndpointAttributes(saeReq);
    }

    //        PublishResult publishResult = publish(subscribeRequest.getEndpoint(), platform, attrsMap);

    // Delete the Platform Application since we will no longer be using it.
    //        deletePlatformApplication(platformApplicationArn);
}