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

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

Introduction

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

Prototype

public SetTopicAttributesRequest(String topicArn, String attributeName, String attributeValue) 

Source Link

Document

Constructs a new SetTopicAttributesRequest object.

Usage

From source file:com.clicktravel.infrastructure.messaging.aws.sns.SnsTopicResource.java

License:Apache License

/**
 * Sets the {@link Policy} of the AWS SNS topic
 * @param policy {@link Policy} to set//  ww  w . j  a  v  a 2 s .  c o  m
 * @throws AmazonClientException
 */
public void setPolicy(final Policy policy) throws AmazonClientException {
    amazonSnsClient.setTopicAttributes(
            new SetTopicAttributesRequest(topicArn, TOPIC_POLICY_ATTRIBUTE, policy.toJson()));
}

From source file:com.comcast.cns.controller.CNSEditTopicDeliveryPolicyPage.java

License:Apache License

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (redirectUnauthenticatedUser(request, response)) {
        return;//from w  ww  .ja v a  2  s.  c o m
    }

    CMBControllerServlet.valueAccumulator.initializeAllCounters();
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String topicArn = request.getParameter("topicArn");
    String userId = request.getParameter("userId");
    Map<?, ?> params = request.getParameterMap();

    connect(request);

    out.println("<html>");

    simpleHeader(request, out, "View/Edit Topic Delivery Policy");

    if (params.containsKey("Update")) {

        String numRetries = request.getParameter("numRetries");
        String retriesNoDelay = request.getParameter("retriesNoDelay");
        String minDelay = request.getParameter("minDelay");
        String minDelayRetries = request.getParameter("minDelayRetries");
        String maxDelay = request.getParameter("maxDelay");
        String maxDelayRetries = request.getParameter("maxDelayRetries");
        String maxReceiveRate = request.getParameter("maxReceiveRate");
        String backoffFunc = request.getParameter("backoffFunc");
        String ignoreOverride = request.getParameter("ignoreOverride");
        CNSTopicDeliveryPolicy deliveryPolicy = new CNSTopicDeliveryPolicy();
        CNSRetryPolicy defaultHealthyRetryPolicy = new CNSRetryPolicy();

        if (maxDelay.trim().length() > 0) {
            defaultHealthyRetryPolicy.setMaxDelayTarget(Integer.parseInt(maxDelay));
        }

        if (minDelay.trim().length() > 0) {
            defaultHealthyRetryPolicy.setMinDelayTarget(Integer.parseInt(minDelay));
        }

        if (maxDelayRetries.trim().length() > 0) {
            defaultHealthyRetryPolicy.setNumMaxDelayRetries(Integer.parseInt(maxDelayRetries));
        }

        if (minDelayRetries.trim().length() > 0) {
            defaultHealthyRetryPolicy.setNumMinDelayRetries(Integer.parseInt(minDelayRetries));
        }

        if (retriesNoDelay.trim().length() > 0) {
            defaultHealthyRetryPolicy.setNumNoDelayRetries(Integer.parseInt(retriesNoDelay));
        }

        if (numRetries.trim().length() > 0) {
            defaultHealthyRetryPolicy.setNumRetries(Integer.parseInt(numRetries));
        }

        defaultHealthyRetryPolicy.setBackOffFunction(CnsBackoffFunction.valueOf(backoffFunc));
        deliveryPolicy.setDefaultHealthyRetryPolicy(defaultHealthyRetryPolicy);
        deliveryPolicy.setDisableSubscriptionOverrides(ignoreOverride != null ? true : false);
        CNSThrottlePolicy defaultThrottle = new CNSThrottlePolicy();

        if (maxReceiveRate.trim().length() > 0) {
            defaultThrottle.setMaxReceivesPerSecond(Integer.parseInt(maxReceiveRate));
        }

        deliveryPolicy.setDefaultThrottlePolicy(defaultThrottle);

        try {

            SetTopicAttributesRequest setTopicAttributesRequest = new SetTopicAttributesRequest(topicArn,
                    "DeliveryPolicy", deliveryPolicy.toString());
            sns.setTopicAttributes(setTopicAttributesRequest);

            logger.debug("event=set_delivery_policy topic_arn=" + topicArn + " userId= " + userId);

        } catch (Exception ex) {
            logger.error("event=set_delivery_policy user_id= " + userId, ex);
            throw new ServletException(ex);
        }

        out.println("<body onload='javascript:window.opener.location.reload();window.close();'>");

    } else {

        int numRetries = 0, retriesNoDelay = 0, minDelay = 0, minDelayRetries = 0, maxDelay = 0,
                maxDelayRetries = 0, maxReceiveRate = 0;
        String retryBackoff = "linear";
        boolean ignoreOverride = false;

        if (topicArn != null) {

            Map<String, String> attributes = null;
            CNSTopicDeliveryPolicy deliveryPolicy = null;

            try {
                GetTopicAttributesRequest getTopicAttributesRequest = new GetTopicAttributesRequest(topicArn);
                GetTopicAttributesResult getTopicAttributesResult = sns
                        .getTopicAttributes(getTopicAttributesRequest);
                attributes = getTopicAttributesResult.getAttributes();
                deliveryPolicy = new CNSTopicDeliveryPolicy(new JSONObject(attributes.get("DeliveryPolicy")));
            } catch (Exception ex) {
                logger.error("event=failed_to_get_attributes arn=" + topicArn, ex);
                throw new ServletException(ex);
            }

            if (deliveryPolicy != null) {

                CNSRetryPolicy healPol = deliveryPolicy.getDefaultHealthyRetryPolicy();

                if (healPol != null) {
                    numRetries = healPol.getNumRetries();
                    retriesNoDelay = healPol.getNumNoDelayRetries();
                    minDelay = healPol.getMinDelayTarget();
                    minDelayRetries = healPol.getNumMinDelayRetries();
                    maxDelay = healPol.getMaxDelayTarget();
                    maxDelayRetries = healPol.getNumMaxDelayRetries();
                    retryBackoff = healPol.getBackOffFunction().toString();
                }

                CNSThrottlePolicy throttlePol = deliveryPolicy.getDefaultThrottlePolicy();

                if (throttlePol != null) {

                    if (throttlePol.getMaxReceivesPerSecond() != null) {
                        maxReceiveRate = throttlePol.getMaxReceivesPerSecond().intValue();
                    }
                }

                ignoreOverride = deliveryPolicy.isDisableSubscriptionOverrides();
            }
        }

        out.println("<body>");
        out.println("<h1>View/Edit Topic Delivery Policy</h1>");
        out.println(
                "<form action=\"/webui/cnsuser/editdeliverypolicy?topicArn=" + topicArn + "\" method=POST>");
        out.println("<input type='hidden' name='userId' value='" + userId + "'>");
        out.println("<table>");
        out.println("<tr><td colspan=2><b><font color='orange'>Delivery Policy</font></b></td></tr>");
        out.println("<tr><td colspan=2><b>Apply these delivery policies for the topic:</b></td></tr>");
        out.println("<tr><td>Number of retries:</td><td><input type='text' name='numRetries' size='50' value='"
                + numRetries + "'></td></tr>");
        out.println("<tr><td>&nbsp;</td><td><I><font color='grey'>Between 0 - 100</font></I></td></tr>");
        out.println(
                "<tr><td>Retries with no delay:</td><td><input type='text' name='retriesNoDelay' size='50' value='"
                        + retriesNoDelay + "'></td></tr>");
        out.println(
                "<tr><td>&nbsp;</td><td><I><font color='grey'>Between (0 - number of retries)</font></I></td></tr>");
        out.println("<tr><td>Minimum delay:</td><td><input type='text' name='minDelay' size='50' value='"
                + minDelay + "'></td></tr>");
        out.println(
                "<tr><td>&nbsp;</td><td><I><font color='grey'>In seconds.Between 0 - maximum delay</font></I></td></tr>");
        out.println(
                "<tr><td>Minimum delay retries:</td><td><input type='text' name='minDelayRetries' size='50' value='"
                        + minDelayRetries + "'></td></tr>");
        out.println(
                "<tr><td>&nbsp;</td><td><I><font color='grey'>Between (0 - number of retries)</font></I></td></tr>");
        out.println("<tr><td>Maximum delay:</td><td><input type='text' name='maxDelay' size='50' value='"
                + maxDelay + "'></td></tr>");
        out.println(
                "<tr><td>&nbsp;</td><td><I><font color='grey'>In seconds. Between minimum delay - 3600</font></I></td></tr>");
        out.println(
                "<tr><td>Maximum delay retries:</td><td><input type='text' name='maxDelayRetries' size='50' value='"
                        + maxDelayRetries + "'></td></tr>");
        out.println(
                "<tr><td>&nbsp;</td><td><I><font color='grey'>Between (0 - number of retries)</font></I></td></tr>");
        out.println(
                "<tr><td>Maximum receive rate:</td><td><input type='text' name='maxReceiveRate' size='50' value='"
                        + maxReceiveRate + "'></td></tr>");
        out.println(
                "<tr><td>&nbsp;</td><td><I><font color='grey'>Receives per second. >= 1</font></I></td></tr>");
        out.println("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");

        if (retryBackoff.equals("linear")) {
            out.println(
                    "<tr><td>Retry backoff function:</td><td><select name='backoffFunc'><option value='linear' selected>Linear</option><option value='arithmetic'>Arithmetic</option><option value='geometric'>Geometric</option><option value='exponential'>Exponential</option></select></td></tr>");
        } else if (retryBackoff.equals("arithmetic")) {
            out.println(
                    "<tr><td>Retry backoff function:</td><td><select name='backoffFunc'><option value='linear'>Linear</option><option value='arithmetic' selected>Arithmetic</option><option value='geometric'>Geometric</option><option value='exponential'>Exponential</option></select></td></tr>");
        } else if (retryBackoff.equals("geometric")) {
            out.println(
                    "<tr><td>Retry backoff function:</td><td><select name='backoffFunc'><option value='linear'>Linear</option><option value='arithmetic'>Arithmetic</option><option value='geometric' selected>Geometric</option><option value='exponential'>Exponential</option></select></td></tr>");
        } else if (retryBackoff.equals("exponential")) {
            out.println(
                    "<tr><td>Retry backoff function:</td><td><select name='backoffFunc'><option value='linear'>Linear</option><option value='arithmetic'>Arithmetic</option><option value='geometric'>Geometric</option><option value='exponential' selected>Exponential</option></select></td></tr>");
        }

        if (ignoreOverride) {
            out.println(
                    "<tr><td>Ignore subscription override:</td><td><input type='checkbox' name='ignoreOverride' checked></td></tr>");
        } else {
            out.println(
                    "<tr><td>Ignore subscription override:</td><td><input type='checkbox' name='ignoreOverride'></td></tr>");
        }

        out.println("<tr><td colspan=2><hr/></td></tr>");
        out.println(
                "<tr><td colspan=2 align=right><input type='button' onclick='window.close()' value='Cancel'><input type='submit' name='Update' value='Update'></td></tr></table></form>");
    }

    out.println("</body></html>");

    CMBControllerServlet.valueAccumulator.deleteAllCounters();
}

From source file:com.comcast.cns.controller.CNSEditTopicDisplayNamePage.java

License:Apache License

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (redirectUnauthenticatedUser(request, response)) {
        return;//from w ww  .ja v a  2s .  c om
    }

    CMBControllerServlet.valueAccumulator.initializeAllCounters();
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String topicArn = request.getParameter("topicArn");
    String displayName = request.getParameter("displayName");
    String userId = request.getParameter("userId");
    Map<?, ?> parameters = request.getParameterMap();

    connect(request);

    out.println("<html>");

    simpleHeader(request, out, "Edit Topic Display Name");

    if (parameters.containsKey("Edit")) {

        try {
            SetTopicAttributesRequest setTopicAttributesRequest = new SetTopicAttributesRequest(topicArn,
                    "DisplayName", displayName);
            sns.setTopicAttributes(setTopicAttributesRequest);
            logger.debug("event=update_display_name topic_arn=" + topicArn + " user_id= " + userId);
        } catch (Exception ex) {
            logger.error("event=update_display_name topic_arn= " + topicArn + " user_id= " + userId, ex);
            throw new ServletException(ex);
        }

        out.println("<body onload='javascript:window.opener.location.reload();window.close();'>");

    } else {

        Map<String, String> attributes = null;

        try {
            GetTopicAttributesRequest getTopicAttributesRequest = new GetTopicAttributesRequest(topicArn);
            GetTopicAttributesResult getTopicAttributesResult = sns
                    .getTopicAttributes(getTopicAttributesRequest);
            attributes = getTopicAttributesResult.getAttributes();
        } catch (Exception ex) {
            logger.error("event=get_topic_attributes topic_arn= " + topicArn + " user_id= " + userId, ex);
            throw new ServletException(ex);
        }

        out.println("<body>");
        out.println("<h1>Edit Topic Display Name</h1>");
        out.println("<form action=\"/webui/cnsuser/editdisplayname?topicArn=" + topicArn + "\" method=POST>");
        out.println("<input type='hidden' name='userId' value='" + userId + "'>");
        out.println(
                "<p>The Display Name of a topic will be used, if present, in the \"From:\" field of any email notifications from the topic. It is also required and included in every SMS notification sent out.</p>");
        out.println("<p><b>Display Name:</b> <input type='text' name='displayName' size='100' value= '"
                + (attributes.get("DisplayName") == null ? "" : attributes.get("DisplayName")) + "'></p>");
        out.println("<br/><I><font color='grey'>Up to 100 printable ASCII characters</font></I>");
        out.println("<hr/>");
        out.println(
                "<input type='button' value='Cancel' onclick='window.close();' style='float:right;'><input type='submit' value='Edit' name='Edit' style='float:right;'></form>");
    }

    out.println("</body></html>");

    CMBControllerServlet.valueAccumulator.deleteAllCounters();
}

From source file:com.comcast.cns.controller.CNSUserPageServlet.java

License:Apache License

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (redirectUnauthenticatedUser(request, response)) {
        return;//from  www  .  j a  va 2 s  .com
    }

    CMBControllerServlet.valueAccumulator.initializeAllCounters();
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    Map<?, ?> parameters = request.getParameterMap();

    String userId = request.getParameter("userId");
    String topicName = request.getParameter("topic");
    String arn = request.getParameter("arn");
    String displayName = request.getParameter("display");
    String nextToken = request.getParameter("nextToken");

    List<Topic> topics = new ArrayList<Topic>();

    connect(request);

    if (parameters.containsKey("Create")) {

        try {

            CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName);
            CreateTopicResult createTopicResult = sns.createTopic(createTopicRequest);

            arn = createTopicResult.getTopicArn();

            SetTopicAttributesRequest setTopicAttributesRequest = new SetTopicAttributesRequest(arn,
                    "DisplayName", displayName);
            sns.setTopicAttributes(setTopicAttributesRequest);

            logger.debug("event=create topic_name=" + topicName + " topic_arn=" + arn + " user_id=" + userId);

        } catch (Exception ex) {
            logger.error("event=create topic_name=" + topicName + " user_id=" + userId);
            throw new ServletException(ex);
        }

    } else if (parameters.containsKey("Delete")) {

        try {
            DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(arn);
            sns.deleteTopic(deleteTopicRequest);
            logger.debug("event=delete_topic topic_arn=" + arn + " user_id= " + userId);
        } catch (Exception ex) {
            logger.error("event=delete_topic topic_arn=" + arn + " user_id= " + userId, ex);
            throw new ServletException(ex);
        }

    } else if (parameters.containsKey("DeleteAll")) {

        try {

            ListTopicsRequest listTopicRequest = new ListTopicsRequest();

            if (nextToken != null) {
                listTopicRequest.setNextToken(nextToken);
            }

            ListTopicsResult listTopicResult = sns.listTopics(listTopicRequest);
            topics = listTopicResult.getTopics();

        } catch (Exception ex) {
            logger.error("event=list_topics user_id= " + userId, ex);
            throw new ServletException(ex);
        }

        for (int i = 0; topics != null && i < topics.size(); i++) {

            Topic t = topics.get(i);

            try {
                DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(t.getTopicArn());
                sns.deleteTopic(deleteTopicRequest);
                logger.debug("event=delete_topic topic_arn=" + (t != null ? t.getTopicArn() : "null")
                        + " user_id= " + userId);
            } catch (Exception ex) {
                logger.error("event=delete_topic topic_arn=" + (t != null ? t.getTopicArn() : "null")
                        + " user_id= " + userId, ex);
            }
        }
    }

    out.println("<html>");

    header(request, out, "Topics");

    out.println("<body>");

    out.println("<h2>Topics</h2>");

    long numTopics = 0;

    try {
        numTopics = PersistenceFactory.getUserPersistence().getNumUserTopics(userId);
    } catch (PersistenceException ex) {
        logger.warn("event=queue_count_failure", ex);
    }

    if (user != null) {
        out.println("<table><tr><td><b>User Name:</b></td><td>" + user.getUserName() + "</td></tr>");
        out.println("<tr><td><b>User ID:</b></td><td>" + user.getUserId() + "</td></tr>");
        out.println("<tr><td><b>Access Key:</b></td><td>" + user.getAccessKey() + "</td></tr>");
        out.println("<tr><td><b>Access Secret:</b></td><td>" + user.getAccessSecret() + "</td>");
        out.println("<tr><td><b>Topic Count</b></td><td>" + numTopics + "</td></tr></table>");
    }

    out.println("<p><table>");
    out.println("<tr><td><b>Topic Name</b></td><td><b>Topic Display Name</b></td><td></td></tr>");
    out.println("<form action=\"/webui/cnsuser?userId=" + userId + "\" " + "method=POST>");
    out.println(
            "<tr><td><input type='text' name='topic' /></td><td><input type='text' name='display'><input type='hidden' name='userId' value='"
                    + userId
                    + "'></td><td><input type='submit' value='Create' name='Create' /></td></tr></form></table></p>");

    out.println("<p><table>");
    out.println("<form action=\"/webui/cnsuser?userId=" + userId + "\" " + "method=POST>");
    out.println("<tr><td><input type='hidden' name='userId' value='" + userId
            + "'></td><td><input type='submit' value='Delete All' name='DeleteAll' onclick=\"return confirm('Are you sure you want to delete all topics?')\" /></td></tr></form></table></p>");

    ListTopicsResult listTopicResult = null;

    try {

        ListTopicsRequest listTopicRequest = new ListTopicsRequest();

        if (nextToken != null) {
            listTopicRequest.setNextToken(nextToken);
        }

        listTopicResult = sns.listTopics(listTopicRequest);
        topics = listTopicResult.getTopics();

    } catch (Exception ex) {
        logger.error("event=list_topics user_id= " + userId, ex);
        throw new ServletException(ex);
    }

    out.println("<p><hr width='100%' align='left' /></p>");
    out.println("<p><table class = 'alternatecolortable' border='1'>");
    out.println("<tr><th>&nbsp;</th>");
    out.println("<th>Topic Arn</th>");
    out.println("<th>Topic Name</th>");
    out.println("<th>Topic Display Name</th>");
    out.println("<th>User ID</th>");
    out.println("<th>Subscriptions Pending</th>");
    out.println("<th>Subscriptions Confirmed</th>");
    out.println("<th>&nbsp;</th>");
    out.println("<th>&nbsp;</th>");
    out.println("<th>&nbsp;</th>");
    out.println("<th>&nbsp;</th>");
    out.println("<th>&nbsp;</th></tr>");

    for (int i = 0; topics != null && i < topics.size(); i++) {

        Topic t = topics.get(i);

        GetTopicAttributesRequest getTopicAttributesRequest = new GetTopicAttributesRequest(t.getTopicArn());
        GetTopicAttributesResult getTopicAttributesResult = sns.getTopicAttributes(getTopicAttributesRequest);
        Map<String, String> attributes = getTopicAttributesResult.getAttributes();

        out.println("<tr>");
        out.println("<form action=\"/webui/cnsuser?userId=" + userId + "\" method=POST>");
        out.println("<td>" + i + "</td>");
        out.println("<td>" + t.getTopicArn() + "<input type='hidden' name='arn' value=" + t.getTopicArn()
                + "></td>");
        out.println("<td>" + Util.getNameFromTopicArn(t.getTopicArn()) + "</td>");
        out.println("<td><a href='' onclick=\"window.open('/webui/cnsuser/editdisplayname?topicArn="
                + t.getTopicArn() + "&userId=" + userId
                + "', 'EditDisplayName', 'height=300,width=700,toolbar=no')\">"
                + (attributes.get("DisplayName") == null ? "{unset}" : attributes.get("DisplayName"))
                + "</a></td>");
        out.println("<td>" + user.getUserId() + "<input type='hidden' name='userId' value=" + user.getUserId()
                + "></td>");
        out.println("<td>" + attributes.get("SubscriptionsPending") + "</td>");
        out.println("<td>" + attributes.get("SubscriptionsConfirmed") + "</td>");
        out.println("<td><a href='/webui/cnsuser/subscription?userId=" + userId + "&topicArn=" + t.getTopicArn()
                + "'>Subscriptions</a></td>");
        out.println("<td><a href='/webui/cnsuser/publish?userId=" + userId + "&topicArn=" + t.getTopicArn()
                + "' target='_blank'>Publish</a></td>");
        out.println("<td><a href='' onclick=\"window.open('/webui/cnsuser/editdeliverypolicy?topicArn="
                + t.getTopicArn() + "&userId=" + userId
                + "', 'EditDeliveryPolicy', 'height=630,width=580,toolbar=no')\">View/Edit Topic Delivery Policy</a></td>");
        out.println("<td><a href='/webui/cnsuser/permission?topicArn=" + t.getTopicArn() + "&userId=" + userId
                + "'>Permission</a></td>");
        out.println(
                "<td><input type='submit' value='Delete' name='Delete' onclick=\"return confirm('Are you sure you want to delete topic "
                        + Util.getNameFromTopicArn(t.getTopicArn()) + "?')\" /></td></form></tr>");
    }

    out.println("</table></p>");

    if (listTopicResult != null && listTopicResult.getNextToken() != null) {
        out.println("<p><a href='/webui/cnsuser?userId=" + userId + "&nextToken="
                + response.encodeURL(listTopicResult.getNextToken()) + "'>next&nbsp;&gt;</a></p>");
    }

    out.println("<h5 style='text-align:center;'><a href='/webui'>ADMIN HOME</a></h5>");
    out.println("</body></html>");

    CMBControllerServlet.valueAccumulator.deleteAllCounters();
}

From source file:com.pocketdealhunter.HotDealsMessagesUtil.java

License:Open Source License

protected String createTopic() {
    try {//from   w  w w . ja va  2s . c o m
        CreateTopicRequest ctr = new CreateTopicRequest(Constants.TOPIC_NAME);
        CreateTopicResult result = snsClient.createTopic(ctr);

        // Adding the DisplayName attribute to the Topic allows for SMS
        // notifications.
        SetTopicAttributesRequest tar = new SetTopicAttributesRequest(result.getTopicArn(), "DisplayName",
                "MessageBoard");
        this.snsClient.setTopicAttributes(tar);

        return result.getTopicArn();
    } catch (Exception exception) {
        System.out.println("Exception  = " + exception);
        return null;
    }
}

From source file:io.konig.maven.CreateAwsSnsTopicAction.java

License:Apache License

public AwsDeployment from(String path) throws Exception {
    String cfTemplatePresent = System.getProperty("cfTemplatePresent");
    if (cfTemplatePresent == null || cfTemplatePresent.equals("N")) {
        try {/*from w  w  w  .  j ava 2s.  c  om*/
            File file = deployment.file(path);
            ObjectMapper mapper = new ObjectMapper();
            S3Bucket bucket = mapper.readValue(file, S3Bucket.class);
            deployment.verifyAWSCredentials();
            String envtName = "";
            if (System.getProperty("environmentName") != null) {
                envtName = System.getProperty("environmentName");
            }
            String bucketName = StringUtils.replaceOnce(bucket.getBucketName(), "${environmentName}", envtName);
            TopicConfiguration notificationConfig = bucket.getNotificationConfiguration()
                    .getTopicConfiguration();
            if (notificationConfig != null && notificationConfig.getTopic() != null) {
                Topic topic = notificationConfig.getTopic();
                Regions regions = Regions.fromName(topic.getRegion());
                AmazonSNS sns = AmazonSNSClientBuilder.standard().withCredentials(deployment.getCredential())
                        .withRegion(regions).build();
                CreateTopicResult result = sns.createTopic(topic.getResourceName());
                deployment.setResponse("Topic with ARN : " + result.getTopicArn() + " is created");

                Policy policy = new Policy().withStatements(new Statement(Effect.Allow)
                        .withPrincipals(Principal.AllUsers).withActions(SNSActions.Publish)
                        .withResources(new Resource(result.getTopicArn()))
                        .withConditions(new ArnCondition(ArnComparisonType.ArnEquals,
                                ConditionFactory.SOURCE_ARN_CONDITION_KEY, "arn:aws:s3:*:*:" + bucketName)));

                sns.setTopicAttributes(
                        new SetTopicAttributesRequest(result.getTopicArn(), "Policy", policy.toJson()));
            } else {
                deployment.setResponse("No topic is configured to the S3 Bucket");
            }

        } catch (Exception e) {
            throw e;
        }
    } else {
        deployment.setResponse("Topic will be created through cloud formation template");
    }
    return deployment;
}

From source file:org.apache.camel.component.aws.sns.SnsEndpoint.java

License:Apache License

@Override
public void doStart() throws Exception {
    super.doStart();

    // creates a new topic, or returns the URL of an existing one
    CreateTopicRequest request = new CreateTopicRequest(configuration.getTopicName());

    LOG.trace("Creating topic [{}] with request [{}]...", configuration.getTopicName(), request);

    CreateTopicResult result = getSNSClient().createTopic(request);
    configuration.setTopicArn(result.getTopicArn());

    LOG.trace("Topic created with Amazon resource name: {}", configuration.getTopicArn());

    if (configuration.getPolicy() != null) {
        LOG.trace("Updating topic [{}] with policy [{}]", configuration.getTopicArn(),
                configuration.getPolicy());

        getSNSClient().setTopicAttributes(new SetTopicAttributesRequest(configuration.getTopicArn(), "Policy",
                configuration.getPolicy()));

        LOG.trace("Topic policy updated");
    }//from   w  w w  .j  a v a  2 s .  c  om
}