ListNewsgroups.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.nntp.NNTPClient;
  20. import org.apache.commons.net.nntp.NewsgroupInfo;

  21. /**
  22.  * This is a trivial example using the NNTP package to approximate the UNIX newsgroups command. It merely connects to the specified news server and issues
  23.  * fetches the list of newsgroups stored by the server. On servers that store a lot of newsgroups, this command can take a very long time (listing upwards of
  24.  * 30,000 groups).
  25.  */

  26. public final class ListNewsgroups {

  27.     public static void main(final String[] args) {
  28.         if (args.length < 1) {
  29.             System.err.println("Usage: newsgroups newsserver [pattern]");
  30.             return;
  31.         }

  32.         final NNTPClient client = new NNTPClient();
  33.         final String pattern = args.length >= 2 ? args[1] : "";

  34.         try {
  35.             client.connect(args[0]);

  36.             int j = 0;
  37.             try {
  38.                 for (final String s : client.iterateNewsgroupListing(pattern)) {
  39.                     j++;
  40.                     System.out.println(s);
  41.                 }
  42.             } catch (final IOException e1) {
  43.                 e1.printStackTrace();
  44.             }
  45.             System.out.println(j);

  46.             j = 0;
  47.             for (final NewsgroupInfo n : client.iterateNewsgroups(pattern)) {
  48.                 j++;
  49.                 System.out.println(n.getNewsgroup());
  50.             }
  51.             System.out.println(j);
  52.         } catch (final IOException e) {
  53.             e.printStackTrace();
  54.         } finally {
  55.             try {
  56.                 if (client.isConnected()) {
  57.                     client.disconnect();
  58.                 }
  59.             } catch (final IOException e) {
  60.                 System.err.println("Error disconnecting from server.");
  61.                 e.printStackTrace();
  62.                 System.exit(1);
  63.             }
  64.         }

  65.     }

  66. }