Example usage for com.amazonaws.services.sns.model ListTopicsResult getTopics

List of usage examples for com.amazonaws.services.sns.model ListTopicsResult getTopics

Introduction

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

Prototype


public java.util.List<Topic> getTopics() 

Source Link

Document

A list of topic ARNs.

Usage

From source file:com.carrotgarden.log4j.aws.sns.Appender.java

License:BSD License

/** resolve topic ARN from topic name */
protected boolean ensureTopicARN() {

    try {//from  ww w .j a v a  2  s.co m

        final ListTopicsResult result = amazonClient.listTopics();

        final List<Topic> topicList = result.getTopics();

        for (final Topic entry : topicList) {

            final String arn = entry.getTopicArn();
            final String name = Util.topicNameFromARN(arn);

            if (getTopicName().equals(name)) {
                topicARN = arn;
                return true;
            }

        }

        LogLog.error("sns: unknown topic name", new IllegalArgumentException(getTopicName()));

        return false;

    } catch (final Exception e) {

        LogLog.error("sns: amazon topic lookup failure", e);

        return false;

    }

}

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

License:Apache License

private String pollForTopicArnForName(final String name) {
    String nextToken = null;/*w  w w. j  av  a 2  s  . c o  m*/
    String topicArn = null;
    while (topicArn == null) {
        final ListTopicsResult listTopicsResult = amazonSnsClient.listTopics(nextToken);
        topicArn = topicArnForNameInTopics(name, listTopicsResult.getTopics());
        nextToken = listTopicsResult.getNextToken();
        if (nextToken == null) {
            break;
        }
    }
    return topicArn;
}

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 w ww .j a v  a  2s . c  o m
    }

    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.intuit.tank.vmManager.environment.amazon.CloudwatchInstance.java

License:Open Source License

/**
 * /*  w  w  w.j a va  2s. c  o  m*/
 * @param email
 * @return
 */
public String getOrCreateNotification(String email) {
    String ret = null;
    String topicName = getTopicName(email);
    String nextToken = null;
    do {
        ListTopicsResult listTopics = asyncSnsClient.listTopics(nextToken);
        List<Topic> topics = listTopics.getTopics();
        for (Topic s : topics) {
            if (s.getTopicArn().endsWith(topicName)) {
                ret = s.getTopicArn();
                break;
            }
        }
        nextToken = listTopics.getNextToken();
    } while (ret == null && nextToken != null);
    if (ret == null) {
        // create the topic and the subscription
        CreateTopicResult topic = asyncSnsClient.createTopic(topicName);
        SubscribeRequest req = new SubscribeRequest(topic.getTopicArn(), "email", email);
        asyncSnsClient.subscribeAsync(req);
        ret = topic.getTopicArn();
    }

    return ret;
}

From source file:com.netflix.spinnaker.front50.model.TemporarySQSQueue.java

License:Apache License

private String getSnsTopicArn(AmazonSNS amazonSNS, String topicName) {
    ListTopicsResult listTopicsResult = amazonSNS.listTopics();
    String nextToken = listTopicsResult.getNextToken();
    List<Topic> topics = listTopicsResult.getTopics();

    while (nextToken != null) {
        listTopicsResult = amazonSNS.listTopics(nextToken);
        nextToken = listTopicsResult.getNextToken();
        topics.addAll(listTopicsResult.getTopics());
    }// w w w  .  ja  v  a  2s .  c o m

    return topics.stream().filter(t -> t.getTopicArn().toLowerCase().endsWith(":" + topicName.toLowerCase()))
            .map(Topic::getTopicArn).findFirst().orElseThrow(
                    () -> new IllegalArgumentException("No SNS topic found (topicName: " + topicName + ")"));
}

From source file:com.pocketdealhunter.HotDealsMessagesUtil.java

License:Open Source License

protected String findTopicArn() {
    try {/*ww w  . j  a va  2 s . c  o m*/
        String topicNameToFind = ":" + Constants.TOPIC_NAME;
        String nextToken = null;
        do {
            ListTopicsRequest listTopicsRequest = new ListTopicsRequest(nextToken);
            ListTopicsResult result = this.snsClient.listTopics(listTopicsRequest);

            for (Topic topic : (List<Topic>) result.getTopics()) {
                if (topic.getTopicArn().endsWith(topicNameToFind)) {
                    return topic.getTopicArn();
                }
            }

            nextToken = result.getNextToken();
        } while (nextToken != null);

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

From source file:com.zotoh.cloudapi.aws.SNS.java

License:Open Source License

@Override
public Collection<Topic> listTopics() throws CloudException, InternalException {
    List<Topic> rc = LT();//  ww w. jav  a2 s  . co m
    ListTopicsRequest req;
    ListTopicsResult res;
    String token = null;
    List<com.amazonaws.services.sns.model.Topic> lst;
    do {
        req = new ListTopicsRequest();
        if (!isEmpty(token)) {
            req.setNextToken(token);
        }
        res = _svc.getCloud().getSNS().listTopics(req);
        if (res != null) {
            token = res.getNextToken();
            lst = res.getTopics();
        } else {
            token = null;
            lst = null;
        }
        if (lst != null)
            for (int i = 0; i < lst.size(); ++i) {
                rc.add(toTopic(lst.get(i)));
            }
    } while (!isEmpty(token));
    return rc;
}

From source file:io.ignitr.dispatchr.manager.service.TopicService.java

License:Apache License

/**
 * Retrieves all of the topics in the AWS account.
 *
 * @param offset/*  w  ww.  j  a va2s  .  c  o m*/
 * @param limit
 * @param sortDir
 * @return
 */
public Observable<Topic> findAll(long offset, long limit, String sortDir) {
    List<Topic> topicMetadatas = new ArrayList<>();

    return repo.findAll().collect(() -> topicMetadatas, List::add).map(metadatas -> {
        List<String> topicNames = new ArrayList<>(metadatas.size());
        metadatas.forEach(metadata -> topicNames.add(metadata.getName()));
        return topicNames;
    }).flatMap(topicNames -> Observable.create(subscriber -> {
        ListTopicsResult listTopicsResult = null;
        do {
            if (listTopicsResult == null) {
                listTopicsResult = snsClient.listTopics();
            } else {
                listTopicsResult = snsClient.listTopics(listTopicsResult.getNextToken());
            }

            listTopicsResult.getTopics().forEach(topic -> {
                Topic topicMetadata = new Topic();
                topicMetadata.setName(topic.getTopicArn().substring(topic.getTopicArn().lastIndexOf(":") + 1));
                topicMetadata.setArn(topic.getTopicArn());
                topicMetadata.setRegistered(topicNames.contains(topicMetadata.getName()));

                subscriber.onNext(topicMetadata);
            });

        } while (listTopicsResult.getNextToken() != null);

        subscriber.onCompleted();
    }));
}

From source file:org.lendingclub.mercator.aws.SNSScanner.java

License:Apache License

@Override
protected void doScan() {

    ListTopicsResult result = getClient().listTopics();
    String token = null;/*from  w  w w  .j  a v  a  2 s.  c o  m*/
    do {
        token = result.getNextToken();
        for (Topic topic : result.getTopics()) {
            try {
                projectTopic(topic);
                scanSubscriptions(topic);
            } catch (RuntimeException e) {
                maybeThrow(e);
            }
        }
        result = getClient().listTopics(token);
    } while (tokenHasNext(token));

}

From source file:org.springframework.cloud.aws.messaging.support.destination.DynamicTopicDestinationResolver.java

License:Apache License

private String getTopicResourceName(String marker, String topicName) {
    ListTopicsResult listTopicsResult = this.amazonSns.listTopics(new ListTopicsRequest(marker));
    for (Topic topic : listTopicsResult.getTopics()) {
        if (AmazonResourceName.isValidAmazonResourceName(topicName)) {
            if (topic.getTopicArn().equals(topicName)) {
                return topic.getTopicArn();
            }//from   www . ja v a  2 s . c o m
        } else {
            AmazonResourceName resourceName = AmazonResourceName.fromString(topic.getTopicArn());
            if (resourceName.getResourceType().equals(topicName)) {
                return topic.getTopicArn();
            }
        }
    }

    if (StringUtils.hasText(listTopicsResult.getNextToken())) {
        return getTopicResourceName(listTopicsResult.getNextToken(), topicName);
    } else {
        throw new IllegalArgumentException("No topic found for name :'" + topicName + "'");
    }
}