View Javadoc

    1   /*
    2    * Copyright 2001-2005 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package examples.nntp;
    17
    18  import java.io.IOException;
    19  import org.apache.commons.net.nntp.NNTPClient;
    20  import org.apache.commons.net.nntp.NewsgroupInfo;
    21
    22  /****
    23   * This is a trivial example using the NNTP package to approximate the
    24   * Unix newsgroups command.  It merely connects to the specified news
    25   * server and issues fetches the list of newsgroups stored by the server.
    26   * On servers that store a lot of newsgroups, this command can take a very
    27   * long time (listing upwards of 30,000 groups).
    28   * <p>
    29   ***/
    30
    31  public final class newsgroups
    32  {
    33
    34      public final static void main(String[] args)
    35      {
    36          NNTPClient client;
    37          NewsgroupInfo[] list;
    38
    39          if (args.length < 1)
    40          {
    41              System.err.println("Usage: newsgroups newsserver");
    42              System.exit(1);
    43          }
    44
    45          client = new NNTPClient();
    46
    47          try
    48          {
    49              client.connect(args[0]);
    50
    51              list = client.listNewsgroups();
    52
    53              if (list != null)
    54              {
    55                  for (int i = 0; i < list.length; i++)
    56                      System.out.println(list[i].getNewsgroup());
    57              }
    58              else
    59              {
    60                  System.err.println("LIST command failed.");
    61                  System.err.println("Server reply: " + client.getReplyString());
    62              }
    63          }
    64          catch (IOException e)
    65          {
    66              e.printStackTrace();
    67          }
    68          finally
    69          {
    70              try
    71              {
    72                  if (client.isConnected())
    73                      client.disconnect();
    74              }
    75              catch (IOException e)
    76              {
    77                  System.err.println("Error disconnecting from server.");
    78                  e.printStackTrace();
    79                  System.exit(1);
    80              }
    81          }
    82
    83      }
    84
    85  }
    86
    87