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

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

Introduction

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

Prototype

public void addProtocolCommandListener(ProtocolCommandListener listener) 

Source Link

Document

Adds a ProtocolCommandListener.

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. jav a2  s .  c  o  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.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  a  v  a2 s  .c om
                "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.post.java

public final static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;/*from   w w w.  j a v  a  2 s .com*/
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0)
                break;

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0)
            header.addHeaderField("Organization", organization);

        if (references != null && organization.length() > 0)
            header.addHeaderField("References", references);

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        fileReader.close();

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:examples.nntp.PostMessage.java

public static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;/*  www.  j a v a  2  s .c o  m*/
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0) {
                break;
            }

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0) {
            header.addHeaderField("Organization", organization);
        }

        if (references != null && references.length() > 0) {
            header.addHeaderField("References", references);
        }

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

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

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        if (fileReader != null) {
            fileReader.close();
        }

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

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

public final static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;//from  w ww  . j a  v a 2 s  .com
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0) {
                break;
            }

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0) {
            header.addHeaderField("Organization", organization);
        }

        if (references != null && references.length() > 0) {
            header.addHeaderField("References", references);
        }

        header.addHeaderField("ProcessExecutorHelper-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

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

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        if (fileReader != null) {
            fileReader.close();
        }

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

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  ww.  j  a  v  a 2s.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.apache.commons.net.examples.nntp.PostMessage.java

public static void main(String[] args) {
    String from, subject, newsgroup, fileName, server, organization;
    String references;//ww  w . ja va  2s . co m
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            newsgroup = stdin.readLine();
            if (newsgroup == null) {
                break;
            }

            newsgroup = newsgroup.trim();

            if (newsgroup.length() == 0) {
                break;
            }

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0) {
            header.addHeaderField("Organization", organization);
        }

        if (references != null && references.length() > 0) {
            header.addHeaderField("References", references);
        }

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        fileName = stdin.readLine();

        try {
            fileReader = new FileReader(fileName);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

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

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        if (fileReader != null) {
            fileReader.close();
        }

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}