Example usage for org.apache.commons.net.nntp Article getReferences

List of usage examples for org.apache.commons.net.nntp Article getReferences

Introduction

In this page you can find the example usage for org.apache.commons.net.nntp Article getReferences.

Prototype

public String[] getReferences() 

Source Link

Document

Returns the MessageId references as an array of Strings

Usage

From source file:com.almarsoft.GroundhogReader.lib.HeaderItemClass.java

public HeaderItemClass(Article article, int level) {

    mArticle = article;/*from ww  w . ja  v  a 2 s  .c o m*/
    mLevel = level;

    if (!article.isDummy()) {

        String from = article.getFrom();
        int idx = from.indexOf('<');

        if (idx != -1) {
            mFromNoEmail = from.substring(0, from.indexOf('<'));

        } else
            mFromNoEmail = from;

        mFromNoEmail = mFromNoEmail.replaceAll("\"", "").trim();

        // Fix for some articles that have level 0 even being inside a thread
        // (probably bug in the Threader)
        if (article.getReferences().length > 0 && mLevel == 0)
            mLevel = 1;
    }
}

From source file:com.almarsoft.GroundhogReader.lib.ServerManager.java

private long insertArticleInDB(Article articleInfo, long articleNumber, String decodedfrom, String charset,
        SQLiteDatabase catchedDB) throws UsenetReaderException {

    // Get the reference list as a string instead of as an array of strings
    // for insertion into the DB
    String[] references = articleInfo.getReferences();
    StringBuilder references_buff = new StringBuilder();
    references_buff.append("");
    int referencesLen = references.length;

    for (int i = 0; i < referencesLen; i++) {
        if (i == (referencesLen - 1)) {
            references_buff.append(references[i]);
        } else {//from  w w  w .  j  a  v  a  2s.c o m
            references_buff.append(references[i]);
            references_buff.append(" ");
        }
    }

    String finalRefs = references_buff.toString();
    //String finalSubject = MessageTextProcessor.decodeHeaderInArticleInfo(articleInfo.getSubject(), charset);
    String subject = articleInfo.getSubject();
    subject = subject.replaceAll(" +", " ");

    // Now insert the Article into the DB
    return DBUtils.insertArticleToGroupID(mGroupID, articleInfo, finalRefs, decodedfrom, subject, mContext,
            catchedDB);
}

From source file:com.almarsoft.GroundhogReader.MessageListActivity.java

private void fillListNonRecursive(Article root, int depth, String replyto) {

    Stack<MiniHeader> stack = new Stack<MiniHeader>();

    boolean markReplies = mPrefs.getBoolean("markReplies", true);
    boolean finished = false;

    String clean_subject;/*from  w  w w .  j  a va  2 s.c o m*/
    MiniHeader tmpMiniItem;
    HeaderItemClass ih = null;
    String[] refsArray;
    String msgId;

    ArrayList<HeaderItemClass> nonStarredItems = new ArrayList<HeaderItemClass>();
    HashSet<String> bannedTrollsSet = DBUtils.getBannedTrolls(getApplicationContext());
    HashSet<String> starredSet = DBUtils.getStarredSubjectsSet(getApplicationContext());

    // Proxy for speed
    HashSet<String> myPostsSetProxy = mMyPostsSet;
    ArrayList<HeaderItemClass> headerItemsListProxy = new ArrayList<HeaderItemClass>();
    int refsArrayLen;

    while (!finished) {

        if (root == null)
            finished = true;

        root.setReplyTo(replyto);

        if (!root.isDummy()) {
            ih = new HeaderItemClass(root, depth);

            // Don't feed the troll
            if (!bannedTrollsSet.contains(root.getFrom())) {

                // Put the replies in red (if configured)
                if (markReplies) {
                    refsArray = root.getReferences();
                    refsArrayLen = refsArray.length;
                    msgId = null;

                    if (refsArray != null && refsArrayLen > 0) {
                        msgId = refsArray[refsArrayLen - 1];
                    }

                    if (msgId != null && myPostsSetProxy != null && myPostsSetProxy.contains(msgId))
                        ih.myreply = true;
                    else
                        ih.myreply = false;
                }

                clean_subject = root.simplifiedSubject();
                if (starredSet.contains(clean_subject)) {
                    ih.starred = true;
                    headerItemsListProxy.add(ih); // Starred items first
                } else {
                    // Nonstarred items will be added to mHeaderItemsList at the end
                    nonStarredItems.add(ih);
                }
            }
        }

        if (root.next != null) {
            tmpMiniItem = new MiniHeader(root.next, depth, replyto);
            stack.push(tmpMiniItem);
        }

        if (root.kid != null) {

            replyto = root.getFrom();
            if (!root.isDummy())
                ++depth;
            root = root.kid;

        } else if (!stack.empty()) {

            tmpMiniItem = stack.pop();
            root = tmpMiniItem.article;
            depth = tmpMiniItem.depth;
            replyto = tmpMiniItem.replyto;

        } else
            finished = true;

    }

    // Now add the non starred items after the starred ones
    int nonStarredItemsLen = nonStarredItems.size();
    for (int i = 0; i < nonStarredItemsLen; i++) {
        headerItemsListProxy.add(nonStarredItems.get(i));
    }

    mHeaderItemsList = headerItemsListProxy;
    nonStarredItems = null;
}

From source file:org.ossmeter.platform.communicationchannel.nntp.local.NNTPDownloader.java

public void downloadMessages(NntpNewsGroup newsgroup) throws Exception {
    final long startTime = System.currentTimeMillis();
    long previousTime = startTime;
    previousTime = printTimeMessage(startTime, previousTime, "Download started");

    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);

    NewsgroupInfo newsgroupInfo = NntpUtil.selectNewsgroup(nntpClient, newsgroup);

    int lastArticleChecked = newsgroupInfo.getFirstArticle();
    previousTime = printTimeMessage(startTime, previousTime,
            "First message in newsgroup:\t" + lastArticleChecked);

    int lastArticle = newsgroupInfo.getLastArticle();
    previousTime = printTimeMessage(startTime, previousTime, "Last message in newsgroup:\t" + lastArticle);

    previousTime = printTimeMessage(startTime, previousTime,
            "Articles in newsgroup:\t" + newsgroupInfo.getArticleCount());
    System.err.println();//from   ww w  .  j a  v  a  2 s. co m

    Mongo mongo = new Mongo();
    DB db = mongo.getDB(newsgroup.getName() + "LocalStorage");
    Messages dbMessages = new Messages(db);

    NewsgroupData newsgroupData = dbMessages.getNewsgroup().findOneByName(newsgroup.getName());
    if (newsgroupData != null) {
        int newsgroupLastArticleChecked = Integer.parseInt(newsgroupData.getLastArticleChecked());
        if (newsgroupLastArticleChecked > lastArticleChecked) {
            lastArticleChecked = newsgroupLastArticleChecked;
        }
        previousTime = printTimeMessage(startTime, previousTime,
                "Last article checked set to:\t" + lastArticleChecked);
    } else {
        newsgroupData = new NewsgroupData();
        newsgroupData.setName(newsgroup.getName());
        newsgroupData.setUrl(newsgroup.getUrl());
        newsgroupData.setAuthenticationRequired(newsgroup.getAuthenticationRequired());
        newsgroupData.setUsername(newsgroup.getUsername());
        newsgroupData.setPassword(newsgroup.getPassword());
        newsgroupData.setPort(newsgroup.getPort());
        newsgroupData.setInterval(newsgroup.getInterval());
        newsgroupData.setFirstArticle(lastArticleChecked + "");
        dbMessages.getNewsgroup().add(newsgroupData);
    }

    int retrievalStep = RETRIEVAL_STEP;
    while (lastArticleChecked < lastArticle) {
        if (lastArticleChecked + retrievalStep > lastArticle) {
            retrievalStep = lastArticle - lastArticleChecked;
        }
        Article[] articles = NntpUtil.getArticleInfo(nntpClient, lastArticleChecked + 1,
                lastArticleChecked + retrievalStep);
        if (articles.length > 0) {
            Article lastArticleRetrieved = articles[articles.length - 1];
            lastArticleChecked = lastArticleRetrieved.getArticleNumber();
            newsgroupData.setLastArticleChecked(lastArticleChecked + "");
        }
        previousTime = printTimeMessage(startTime, previousTime,
                "downloaded:\t" + articles.length + " nntp articles");
        previousTime = printTimeMessage(startTime, previousTime, "downloading contents\t");

        for (Article article : articles) {
            ArticleData articleData = new ArticleData();
            articleData.setUrl(newsgroup.getUrl());
            articleData.setArticleNumber(article.getArticleNumber());
            articleData.setArticleId(article.getArticleId());
            articleData.setDate(article.getDate());
            articleData.setFrom(article.getFrom());
            articleData.setSubject(article.getSubject());
            for (String referenceId : article.getReferences())
                articleData.getReferences().add(referenceId);
            articleData.setBody(NntpUtil.getArticleBody(nntpClient, article.getArticleNumber()));
            dbMessages.getArticles().add(articleData);
        }
        dbMessages.sync();
        previousTime = printTimeMessage(startTime, previousTime, "stored:\t" + dbMessages.getArticles().size()
                + " / " + newsgroupInfo.getArticleCount() + " nntp articles sofar");
        System.err.println();
    }
    nntpClient.disconnect();
    dbMessages.sync();
    previousTime = printTimeMessage(startTime, previousTime, "stored:\t" + dbMessages.getArticles().size()
            + " / " + newsgroupInfo.getArticleCount() + " nntp articles");
}

From source file:org.ossmeter.platform.communicationchannel.nntp.NntpManager.java

@Override
public CommunicationChannelDelta getDelta(DB db, NntpNewsGroup newsgroup, Date date) throws Exception {
    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);

    NewsgroupInfo newsgroupInfo = NntpUtil.selectNewsgroup(nntpClient, newsgroup);
    int lastArticle = newsgroupInfo.getLastArticle();

    //       The following statement is not really needed, but I added it to speed up running,
    //       in the date is far latter than the first day of the newsgroup.
    //      if (Integer.parseInt(newsgroup.getLastArticleChecked())<134500)
    //         newsgroup.setLastArticleChecked("134500"); //137500");

    String lac = newsgroup.getLastArticleChecked();
    if (lac == null || lac.equals("") || lac.equals("null"))
        lac = "-1";
    int lastArticleChecked = Integer.parseInt(lac);
    if (lastArticleChecked < 0)
        lastArticleChecked = newsgroupInfo.getFirstArticle();

    // FIXME: certain eclipse newsgroups return 0 for both FirstArticle and LastArticle which causes exceptions
    if (lastArticleChecked == 0)
        return null;

    CommunicationChannelDelta delta = new CommunicationChannelDelta();
    delta.setNewsgroup(newsgroup);/*w ww  .ja v a2 s. c  o  m*/

    int retrievalStep = RETRIEVAL_STEP;
    Boolean dayCompleted = false;
    while (!dayCompleted) {
        if (lastArticleChecked + retrievalStep > lastArticle) {
            retrievalStep = lastArticle - lastArticleChecked;
            dayCompleted = true;
        }
        Article[] articles;
        Date articleDate = date;
        // The following loop discards messages for days earlier than the required one.
        do {
            articles = NntpUtil.getArticleInfo(nntpClient, lastArticleChecked + 1,
                    lastArticleChecked + retrievalStep);
            if (articles.length > 0) {
                Article lastArticleRetrieved = articles[articles.length - 1];
                java.util.Date javaArticleDate = NntpUtil.parseDate(lastArticleRetrieved.getDate());
                articleDate = new Date(javaArticleDate);
                if (date.compareTo(articleDate) > 0)
                    lastArticleChecked = lastArticleRetrieved.getArticleNumber();
            }
        } while (date.compareTo(articleDate) > 0);

        for (Article article : articles) {
            java.util.Date javaArticleDate = NntpUtil.parseDate(article.getDate());
            if (javaArticleDate != null) {
                articleDate = new Date(javaArticleDate);
                if (date.compareTo(articleDate) < 0) {
                    dayCompleted = true;
                    //                  System.out.println("dayCompleted");
                } else if (date.compareTo(articleDate) == 0) {
                    CommunicationChannelArticle communicationChannelArticle = new CommunicationChannelArticle();
                    communicationChannelArticle.setArticleId(article.getArticleId());
                    communicationChannelArticle.setArticleNumber(article.getArticleNumber());
                    communicationChannelArticle.setDate(javaArticleDate);
                    //                  I haven't seen any messageThreadIds on NNTP servers, yet.
                    //                  communicationChannelArticle.setMessageThreadId(article.messageThreadId());
                    NntpNewsGroup newNewsgroup = new NntpNewsGroup();
                    newNewsgroup.setUrl(newsgroup.getUrl());
                    newNewsgroup.setAuthenticationRequired(newsgroup.getAuthenticationRequired());
                    newNewsgroup.setUsername(newsgroup.getUsername());
                    newNewsgroup.setPassword(newsgroup.getPassword());
                    newNewsgroup.setNewsGroupName(newsgroup.getNewsGroupName());
                    newNewsgroup.setPort(newsgroup.getPort());
                    newNewsgroup.setInterval(newsgroup.getInterval());
                    communicationChannelArticle.setNewsgroup(newNewsgroup);
                    communicationChannelArticle.setReferences(article.getReferences());
                    communicationChannelArticle.setSubject(article.getSubject());
                    communicationChannelArticle.setUser(article.getFrom());
                    communicationChannelArticle
                            .setText(getContents(db, newNewsgroup, communicationChannelArticle));
                    delta.getArticles().add(communicationChannelArticle);
                    lastArticleChecked = article.getArticleNumber();
                    //                  System.out.println("dayNOTCompleted");
                } else {

                    //TODO: In this case, there are unprocessed articles whose date is earlier than the date requested.
                    //      This means that the deltas of those article dates are incomplete, 
                    //      i.e. the deltas did not contain all articles of those dates.
                }
            } else {
                // If an article has no correct date, then ignore it
                System.err.println("\t\tUnparsable article date: " + article.getDate());
            }
        }
    }
    nntpClient.disconnect();
    newsgroup.setLastArticleChecked(lastArticleChecked + "");
    System.out.println(
            "delta (" + date.toString() + ") contains:\t" + delta.getArticles().size() + " nntp articles");

    return delta;
}