ExtendedNNTPOps.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.IOException;

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

  24. /**
  25.  * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
  26.  */
  27. public class ExtendedNNTPOps {

  28.     public static void main(final String[] args) {
  29.         final ExtendedNNTPOps ops;

  30.         final int argc = args.length;
  31.         if (argc < 1) {
  32.             System.err.println("usage: ExtendedNNTPOps nntpserver [user password]");
  33.             System.exit(1);
  34.         }

  35.         ops = new ExtendedNNTPOps();
  36.         ops.demo(args[0], argc >= 3 ? args[1] : null, argc >= 3 ? args[2] : null);
  37.     }

  38.     private final NNTPClient client;

  39.     public ExtendedNNTPOps() {
  40.         client = new NNTPClient();
  41.         client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
  42.     }

  43.     private void demo(final String host, final String user, final String password) {
  44.         try {
  45.             client.connect(host);

  46.             // AUTHINFO USER/AUTHINFO PASS
  47.             if (user != null && password != null) {
  48.                 final boolean success = client.authenticate(user, password);
  49.                 if (success) {
  50.                     System.out.println("Authentication succeeded");
  51.                 } else {
  52.                     System.out.println("Authentication failed, error =" + client.getReplyString());
  53.                 }
  54.             }

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

  61.             for (final Article article : articles) {
  62.                 if (article.isDummy()) { // Subject will contain raw response
  63.                     System.out.println("Could not parse: " + article.getSubject());
  64.                 } else {
  65.                     System.out.println(article.getSubject());
  66.                 }
  67.             }

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

  73.         } catch (final IOException e) {
  74.             e.printStackTrace();
  75.         }
  76.     }

  77. }