View Javadoc

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  
18  package examples.nntp;
19  
20  import java.io.IOException;
21  import org.apache.commons.net.nntp.NNTPClient;
22  import org.apache.commons.net.nntp.NewsgroupInfo;
23  
24  /***
25   * This is a trivial example using the NNTP package to approximate the
26   * Unix newsgroups command.  It merely connects to the specified news
27   * server and issues fetches the list of newsgroups stored by the server.
28   * On servers that store a lot of newsgroups, this command can take a very
29   * long time (listing upwards of 30,000 groups).
30   * <p>
31   ***/
32  
33  public final class newsgroups
34  {
35  
36      public final static void main(String[] args)
37      {
38          NNTPClient client;
39          NewsgroupInfo[] list;
40  
41          if (args.length < 1)
42          {
43              System.err.println("Usage: newsgroups newsserver");
44              System.exit(1);
45          }
46  
47          client = new NNTPClient();
48  
49          try
50          {
51              client.connect(args[0]);
52  
53              list = client.listNewsgroups();
54  
55              if (list != null)
56              {
57                  for (int i = 0; i < list.length; i++)
58                      System.out.println(list[i].getNewsgroup());
59              }
60              else
61              {
62                  System.err.println("LIST command failed.");
63                  System.err.println("Server reply: " + client.getReplyString());
64              }
65          }
66          catch (IOException e)
67          {
68              e.printStackTrace();
69          }
70          finally
71          {
72              try
73              {
74                  if (client.isConnected())
75                      client.disconnect();
76              }
77              catch (IOException e)
78              {
79                  System.err.println("Error disconnecting from server.");
80                  e.printStackTrace();
81                  System.exit(1);
82              }
83          }
84  
85      }
86  
87  }
88  
89