Example usage for org.apache.commons.net.nntp NNTPClient authenticate

List of usage examples for org.apache.commons.net.nntp NNTPClient authenticate

Introduction

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

Prototype

public boolean authenticate(String username, String password) throws IOException 

Source Link

Document

Log into a news server by sending the AUTHINFO USER/AUTHINFO PASS command sequence.

Usage

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

    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.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  w  ww .j  a v a2 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: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  w  w w. j av  a 2 s .  co  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 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 NNTPClient connectToNntpServer(NntpNewsGroup newsgroup) {

    NNTPClient client = new NNTPClient();
    client.setDefaultPort(newsgroup.getPort());
    String serverUrl = newsgroup.getUrl().substring(0, newsgroup.getUrl().lastIndexOf("/"));
    try {//  w  ww.  j  av  a 2  s  .c o m
        client.connect(serverUrl);
        if (newsgroup.getAuthenticationRequired()) {
            client.authenticate(newsgroup.getUsername(), newsgroup.getPassword());
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        System.err.println("SocketException while connecting to NNTP server: '" + newsgroup.getUrl() + "': "
                + e.getMessage());
        //           System.exit(1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println(
                "IOException while connecting to NNTP server: '" + newsgroup.getUrl() + "': " + e.getMessage());
        //           System.exit(1);
    }
    return client;
}

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

public static NNTPClient connectToNntpServer(NntpNewsGroup newsgroup) {

    NNTPClient client = new NNTPClient();
    client.setDefaultPort(newsgroup.getPort());
    String serverUrl = newsgroup.getUrl();

    if (serverUrl.endsWith("/")) {
        serverUrl = newsgroup.getUrl().substring(0, newsgroup.getUrl().lastIndexOf("/"));
    }/*from  w w  w  .  j  a va  2  s  .c  om*/

    try {
        client.connect(serverUrl);
        if (newsgroup.getAuthenticationRequired()) {
            client.authenticate(newsgroup.getUsername(), newsgroup.getPassword());
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        System.err.println("SocketException while connecting to NNTP server: '" + newsgroup.getUrl() + "': "
                + e.getMessage());
        e.printStackTrace();
        //           System.exit(1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println(
                "IOException while connecting to NNTP server: '" + newsgroup.getUrl() + "': " + e.getMessage());
        e.printStackTrace();
        //           System.exit(1);
    }
    return client;
}

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

private void setupAuthentication(URI link, NNTPClient client) throws CredentialsException, IOException {
    ICredentials authCredentials = Owl.getConnectionService().getAuthCredentials(link, null);
    if (authCredentials != null)
        client.authenticate(authCredentials.getUsername(), authCredentials.getPassword());
}