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   ***/
31  
32  public final class ListNewsgroups
33  {
34  
35      public static void main(String[] args)
36      {
37          if (args.length < 1)
38          {
39              System.err.println("Usage: newsgroups newsserver [pattern]");
40              return;
41          }
42  
43          NNTPClient client = new NNTPClient();
44          String pattern = args.length >= 2 ? args[1] : "";
45  
46          try
47          {
48              client.connect(args[0]);
49  
50              int j = 0;
51              try {
52                  for(String s : client.iterateNewsgroupListing(pattern)) {
53                      j++;
54                      System.out.println(s);
55                  }
56              } catch (IOException e1) {
57                  e1.printStackTrace();
58              }
59              System.out.println(j);
60  
61              j = 0;
62              for(NewsgroupInfo n : client.iterateNewsgroups(pattern)) {
63                  j++;
64                  System.out.println(n.getNewsgroup());
65              }
66              System.out.println(j);
67          }
68          catch (IOException e)
69          {
70              e.printStackTrace();
71          }
72          finally
73          {
74              try
75              {
76                  if (client.isConnected()) {
77                      client.disconnect();
78                  }
79              }
80              catch (IOException e)
81              {
82                  System.err.println("Error disconnecting from server.");
83                  e.printStackTrace();
84                  System.exit(1);
85              }
86          }
87  
88      }
89  
90  }
91  
92