001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.examples.nntp;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.PrintWriter;
023import java.net.SocketException;
024
025import org.apache.commons.net.PrintCommandListener;
026import org.apache.commons.net.nntp.NNTPClient;
027import org.apache.commons.net.nntp.NewsgroupInfo;
028
029/**
030 * Sample program demonstrating the use of article header and body retrieval
031 */
032public class ArticleReader {
033
034    public static void main(final String[] args) throws SocketException, IOException {
035
036        if (args.length != 2 && args.length != 3 && args.length != 5) {
037            System.out.println("Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
038            return;
039        }
040
041        final String hostname = args[0];
042        final String newsgroup = args[1];
043        // Article specifier can be numeric or Id in form <m.n.o.x@host>
044        final String articleSpec = args.length >= 3 ? args[2] : null;
045
046        final NNTPClient client = new NNTPClient();
047        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
048        client.connect(hostname);
049
050        if (args.length == 5) { // Optional auth
051            final String user = args[3];
052            final String password = args[4];
053            if (!client.authenticate(user, password)) {
054                System.out.println("Authentication failed for user " + user + "!");
055                System.exit(1);
056            }
057        }
058
059        final NewsgroupInfo group = new NewsgroupInfo();
060        client.selectNewsgroup(newsgroup, group);
061
062        final BufferedReader brHdr;
063        String line;
064        if (articleSpec != null) {
065            brHdr = (BufferedReader) client.retrieveArticleHeader(articleSpec);
066        } else {
067            final long articleNum = group.getLastArticleLong();
068            brHdr = client.retrieveArticleHeader(articleNum);
069        }
070        if (brHdr != null) {
071            while ((line = brHdr.readLine()) != null) {
072                System.out.println(line);
073            }
074            brHdr.close();
075        }
076        final BufferedReader brBody;
077        if (articleSpec != null) {
078            brBody = (BufferedReader) client.retrieveArticleBody(articleSpec);
079        } else {
080            final long articleNum = group.getLastArticleLong();
081            brBody = client.retrieveArticleBody(articleNum);
082        }
083        if (brBody != null) {
084            while ((line = brBody.readLine()) != null) {
085                System.out.println(line);
086            }
087            brBody.close();
088        }
089    }
090
091}