ArticleReader.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.nntp;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.net.SocketException;

  21. import org.apache.commons.net.PrintCommandListener;
  22. import org.apache.commons.net.io.Util;
  23. import org.apache.commons.net.nntp.NNTPClient;
  24. import org.apache.commons.net.nntp.NewsgroupInfo;

  25. /**
  26.  * Sample program demonstrating the use of article header and body retrieval
  27.  */
  28. public class ArticleReader {

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

  30.         if (args.length != 2 && args.length != 3 && args.length != 5) {
  31.             System.out.println("Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
  32.             return;
  33.         }

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

  38.         final NNTPClient client = new NNTPClient();
  39.         client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
  40.         client.connect(hostname);

  41.         if (args.length == 5) { // Optional auth
  42.             final String user = args[3];
  43.             final String password = args[4];
  44.             if (!client.authenticate(user, password)) {
  45.                 System.out.println("Authentication failed for user " + user + "!");
  46.                 System.exit(1);
  47.             }
  48.         }

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

  51.         final BufferedReader brHdr;
  52.         String line;
  53.         if (articleSpec != null) {
  54.             brHdr = (BufferedReader) client.retrieveArticleHeader(articleSpec);
  55.         } else {
  56.             final long articleNum = group.getLastArticleLong();
  57.             brHdr = client.retrieveArticleHeader(articleNum);
  58.         }
  59.         if (brHdr != null) {
  60.             while ((line = brHdr.readLine()) != null) {
  61.                 System.out.println(line);
  62.             }
  63.             brHdr.close();
  64.         }
  65.         final BufferedReader brBody;
  66.         if (articleSpec != null) {
  67.             brBody = (BufferedReader) client.retrieveArticleBody(articleSpec);
  68.         } else {
  69.             final long articleNum = group.getLastArticleLong();
  70.             brBody = client.retrieveArticleBody(articleNum);
  71.         }
  72.         if (brBody != null) {
  73.             while ((line = brBody.readLine()) != null) {
  74.                 System.out.println(line);
  75.             }
  76.             brBody.close();
  77.         }
  78.     }

  79. }