Example usage for org.apache.commons.net.nntp NewsgroupInfo NewsgroupInfo

List of usage examples for org.apache.commons.net.nntp NewsgroupInfo NewsgroupInfo

Introduction

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

Prototype

NewsgroupInfo

Source Link

Usage

From source file:examples.nntp.ArticleReader.java

public static void main(String[] args) throws SocketException, IOException {

    if (args.length != 2 && args.length != 3 && args.length != 5) {
        System.out.println(/*from  ww  w. ja  va2  s  .  c  o  m*/
                "Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
        return;
    }

    String hostname = args[0];
    String newsgroup = args[1];
    // Article specifier can be numeric or Id in form <m.n.o.x@host>
    String articleSpec = args.length >= 3 ? args[2] : null;

    NNTPClient client = new NNTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    client.connect(hostname);

    if (args.length == 5) { // Optional auth
        String user = args[3];
        String password = args[4];
        if (!client.authenticate(user, password)) {
            System.out.println("Authentication failed for user " + user + "!");
            System.exit(1);
        }
    }

    NewsgroupInfo group = new NewsgroupInfo();
    client.selectNewsgroup(newsgroup, group);

    BufferedReader br;
    String line;
    if (articleSpec != null) {
        br = (BufferedReader) client.retrieveArticleHeader(articleSpec);
    } else {
        long articleNum = group.getLastArticleLong();
        br = client.retrieveArticleHeader(articleNum);
    }
    if (br != null) {
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
    if (articleSpec != null) {
        br = (BufferedReader) client.retrieveArticleBody(articleSpec);
    } else {
        long articleNum = group.getLastArticleLong();
        br = client.retrieveArticleBody(articleNum);
    }
    if (br != null) {
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}

From source file:examples.nntp.MessageThreading.java

public static void main(String[] args) throws SocketException, IOException {

    if (args.length != 2 && args.length != 4) {
        System.out.println("Usage: MessageThreading <hostname> <groupname> [<user> <password>]");
        return;/*from www . j ava  2s. co m*/
    }

    String hostname = args[0];
    String newsgroup = args[1];

    NNTPClient client = new NNTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    client.connect(hostname);

    if (args.length == 4) { // Optional auth
        String user = args[2];
        String password = args[3];
        if (!client.authenticate(user, password)) {
            System.out.println("Authentication failed for user " + user + "!");
            System.exit(1);
        }
    }

    String fmt[] = client.listOverviewFmt();
    if (fmt != null) {
        System.out.println("LIST OVERVIEW.FMT:");
        for (String s : fmt) {
            System.out.println(s);
        }
    } else {
        System.out.println("Failed to get OVERVIEW.FMT");
    }
    NewsgroupInfo group = new NewsgroupInfo();
    client.selectNewsgroup(newsgroup, group);

    long lowArticleNumber = group.getFirstArticleLong();
    long highArticleNumber = lowArticleNumber + 5000;

    System.out
            .println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
    Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);

    System.out.println("Building message thread tree...");
    Threader threader = new Threader();
    Article root = (Article) threader.thread(articles);

    Article.printThread(root, 0);
}

From source file:examples.nntp.ExtendedNNTPOps.java

private void demo(String host, String user, String password) {
    try {/*  ww w. ja  v a 2  s.  c  o m*/
        client.connect(host);

        // AUTHINFO USER/AUTHINFO PASS
        if (user != null && password != null) {
            boolean success = client.authenticate(user, password);
            if (success) {
                System.out.println("Authentication succeeded");
            } else {
                System.out.println("Authentication failed, error =" + client.getReplyString());
            }
        }

        // XOVER
        NewsgroupInfo testGroup = new NewsgroupInfo();
        client.selectNewsgroup("alt.test", testGroup);
        long lowArticleNumber = testGroup.getFirstArticleLong();
        long highArticleNumber = lowArticleNumber + 100;
        Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);

        for (Article article : articles) {
            if (article.isDummy()) { // Subject will contain raw response
                System.out.println("Could not parse: " + article.getSubject());
            } else {
                System.out.println(article.getSubject());
            }
        }

        // LIST ACTIVE
        NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
        for (NewsgroupInfo fanGroup : fanGroups) {
            System.out.println(fanGroup.getNewsgroup());
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

public long[] getGroupArticles(String group, long fromArticle, int limit) throws IOException {

    Vector<Long> list;
    long firstToGet, firstArticle, lastArticle;
    NewsgroupInfo groupInfo = new NewsgroupInfo();

    if (!selectNewsgroup(group, groupInfo))
        return null;

    firstArticle = groupInfo.getFirstArticle();
    lastArticle = groupInfo.getLastArticle();

    if (firstArticle == 0 && lastArticle == 0)
        return new long[0];

    // First sync with this group; see the comment below 
    if (fromArticle == -1)
        firstToGet = firstArticle;/*from   w  w  w .j a  va2s  .  c o  m*/

    else {
        if (fromArticle > lastArticle) { // No new articles
            return new long[0];
        } else {
            firstToGet = fromArticle;
        }
    }

    // Now select the first article and start looping until limit or last article reached
    ArticlePointer art = new ArticlePointer();

    list = new Vector<Long>(limit);

    // FIRST CONNECTION TO THE GROUP
    // If this is the first connection we only want the last "limit" articles from the group, 
    // so we ask for the last message and go backwards until we have "limit" articles or 
    // the first one.
    if (fromArticle == -1) {
        if (!selectArticle(lastArticle, art))
            return new long[0];

        for (int i = 0; i < limit; i++) {
            list.insertElementAt((long) art.articleNumber, 0);

            if (art.articleNumber == firstToGet)
                break;

            if (!selectPreviousArticle(art))
                break;
        }
    }

    // NON-FIRST CONNECTION TO THE GROUP
    // For normal non-first connection we start with the last article we got on the previous session and advance from that
    // until limit or last article reached
    else {
        if (!selectArticle(firstToGet, art))
            return new long[0];

        for (int i = 0; i <= limit; i++) {
            list.add((long) art.articleNumber);

            if (art.articleNumber == lastArticle)
                break;

            if (!selectNextArticle(art))
                break;
        }
    }
    int listSize = list.size();
    long[] articleNumbers = new long[listSize];

    for (int i = 0; i < listSize; i++) {
        articleNumbers[i] = list.get(i);
    }

    return articleNumbers;
}

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

public boolean selectNewsGroupConnecting(String group) throws IOException, ServerAuthException {
    mGroup = group;//from ww  w .  j  av  a2s  .co  m
    mGroupID = DBUtils.getGroupIdFromName(group, mContext);
    mBannedThreadsSet = DBUtils.getBannedThreads(group, mContext);

    clientConnectIfNot();

    mGroupInfo = new NewsgroupInfo();

    try {
        return mClient.selectNewsgroup(mGroup, mGroupInfo);
    } catch (IOException e) {
        connect();
        return mClient.selectNewsgroup(mGroup, mGroupInfo);
    }
}

From source file:org.apache.common.net.examples.nntp.ExtendedNNTPOps.java

private void demo(String host, String user, String password) {
    try {//from   ww  w . j  a  v  a  2 s  .  com
        client.connect(host);

        // AUTHINFO USER/AUTHINFO PASS
        if (user != null && password != null) {
            boolean success = client.authenticate(user, password);
            if (success) {
                System.out.println("Authentication succeeded");
            } else {
                System.out.println("Authentication failed, error =" + client.getReplyString());
            }
        }

        // XOVER
        NewsgroupInfo testGroup = new NewsgroupInfo();
        client.selectNewsgroup("alt.test", testGroup);
        long lowArticleNumber = testGroup.getFirstArticleLong();
        long highArticleNumber = lowArticleNumber + 100;
        Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);

        for (Article article : articles) {
            if (article.isDummy()) { // Subject will contain raw response
                System.out.println("Could not parse: " + article.getSubject());
            } else {
                System.out.println(article.getSubject());
            }
        }

        // LIST ACTIVE
        NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
        for (int i = 0; i < fanGroups.length; ++i) {
            System.out.println(fanGroups[i].getNewsgroup());
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.commons.net.examples.nntp.ArticleReader.java

public static void main(String[] args) throws SocketException, IOException {

    if (args.length != 2 && args.length != 3 && args.length != 5) {
        System.out.println(/*from   www. j ava 2  s  .com*/
                "Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
        return;
    }

    String hostname = args[0];
    String newsgroup = args[1];
    // Article specifier can be numeric or Id in form <m.n.o.x@host>
    String articleSpec = args.length >= 3 ? args[2] : null;

    NNTPClient client = new NNTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    client.connect(hostname);

    if (args.length == 5) { // Optional auth
        String user = args[3];
        String password = args[4];
        if (!client.authenticate(user, password)) {
            System.out.println("Authentication failed for user " + user + "!");
            System.exit(1);
        }
    }

    NewsgroupInfo group = new NewsgroupInfo();
    client.selectNewsgroup(newsgroup, group);

    BufferedReader brHdr;
    String line;
    if (articleSpec != null) {
        brHdr = (BufferedReader) client.retrieveArticleHeader(articleSpec);
    } else {
        long articleNum = group.getLastArticleLong();
        brHdr = client.retrieveArticleHeader(articleNum);
    }
    if (brHdr != null) {
        while ((line = brHdr.readLine()) != null) {
            System.out.println(line);
        }
        brHdr.close();
    }
    BufferedReader brBody;
    if (articleSpec != null) {
        brBody = (BufferedReader) client.retrieveArticleBody(articleSpec);
    } else {
        long articleNum = group.getLastArticleLong();
        brBody = client.retrieveArticleBody(articleNum);
    }
    if (brBody != null) {
        while ((line = brBody.readLine()) != null) {
            System.out.println(line);
        }
        brBody.close();
    }
}

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

public static NewsgroupInfo selectNewsgroup(NNTPClient client, NntpNewsGroup newsgroup) {
    String newsgroupName = newsgroup.getUrl().substring(newsgroup.getUrl().lastIndexOf("/") + 1);
    NewsgroupInfo newsgroupInfo = new NewsgroupInfo();
    try {//w  w  w  . ja v  a 2 s.  c o  m
        client.selectNewsgroup(newsgroupName, newsgroupInfo);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println("IOException while selecting newsgroup: '" + newsgroupName + "': " + e.getMessage());
    }
    return newsgroupInfo;
}

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

public static NewsgroupInfo selectNewsgroup(NNTPClient client, NntpNewsGroup newsgroup) {
    String newsgroupName = newsgroup.getNewsGroupName();//newsgroup.getUrl().substring(newsgroup.getUrl().lastIndexOf("/") + 1);
    NewsgroupInfo newsgroupInfo = new NewsgroupInfo();
    try {//from   w  ww  .j a  va  2s.  c  o  m
        client.selectNewsgroup(newsgroupName, newsgroupInfo);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println("IOException while selecting newsgroup: '" + newsgroupName + "': " + e.getMessage());
        e.printStackTrace();
    }
    return newsgroupInfo;
}

From source file:org.rssowl.contrib.internal.nntp.NewsGroupHandler.java

public Pair<IFeed, IConditionalGet> reload(URI link, IProgressMonitor monitor, Map<Object, Object> properties)
        throws CoreException {
    IModelFactory factory = Owl.getModelFactory();

    /* Create a new empty feed from the existing one */
    IFeed feed = factory.createFeed(null, link);

    /* Retrieve last article pointer */
    Integer lastArticleId = getLastArticleId(properties);

    /* Create a new HttpClient */
    NNTPClient client = new NNTPClient();

    try {//  w  w  w  .ja  va 2s  .  co  m

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Connect to Server */
        if (link.getPort() != -1)
            client.connect(link.getHost(), link.getPort());
        else
            client.connect(link.getHost());

        /* Set Timeout */
        setTimeout(client, properties);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Authentication if provided */
        setupAuthentication(link, client);

        /* Check Authentication Required */
        checkAuthenticationRequired(client);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Enable Reader Mode */
        client.sendCommand(MODE_READER);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Select Newsgroup */
        String newsgroup = link.getPath().replace("/", "");
        NewsgroupInfo groupInfo = new NewsgroupInfo();
        boolean selected = client.selectNewsgroup(newsgroup, groupInfo);

        /* Check Authentication Required */
        checkAuthenticationRequired(client);

        /* Check Newsgroup Selected */
        if (!selected)
            throwConnectionException("Unable to select Newsgroup", client);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* First reload: Retrieve an initial amount of News */
        if (lastArticleId == null) {

            /* Set Article Pointer to last Article */
            int status = client.stat(groupInfo.getLastArticle());
            if (status != STATUS_ARTICLE_POINTER_OK)
                throwConnectionException("Unable to retrieve any News", client);

            /* Retrieve initial news */
            for (int i = 0; i < INITIAL_NEWS && !monitor.isCanceled(); i++) {
                createNews(client.retrieveArticle(), feed, monitor);

                /* Goto previous news if provided */
                int result = client.last();
                if (result != STATUS_ARTICLE_POINTER_OK)
                    break;
            }
        }

        /* Subsequent reload: Set pointer to last retrieved News */
        else {

            /* Set Article Pointer to last retrieved News */
            int status = client.stat(lastArticleId);
            if (status != STATUS_ARTICLE_POINTER_OK)
                throwConnectionException("Unable to retrieve any News", client);

            /* Retrieve all the following News */
            while (client.next() == STATUS_ARTICLE_POINTER_OK && !monitor.isCanceled())
                createNews(client.retrieveArticle(), feed, monitor);
        }

        /* Remember last article's ID */
        lastArticleId = groupInfo.getLastArticle();
    }

    /* Wrap Exceptions */
    catch (IOException e) {
        throw new ConnectionException(Activator.createErrorStatus(e.getMessage(), e));
    }

    /* Disconnect */
    finally {
        try {
            if (client.isConnected())
                client.disconnect();
        } catch (IOException e) {
            throw new ConnectionException(Activator.createErrorStatus(e.getMessage(), e));
        }
    }

    /* Create Conditional Get Object */
    IConditionalGet conditionalGet = null;
    if (lastArticleId != null)
        conditionalGet = factory.createConditionalGet(null, link, String.valueOf(lastArticleId));

    return Pair.create(feed, conditionalGet);
}