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 org.apache.commons.net.examples.nntp;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.net.nntp.NNTPClient;
23  import org.apache.commons.net.nntp.NewsgroupInfo;
24  
25  /**
26   * 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
27   * 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
28   * 30,000 groups).
29   */
30  
31  public final class ListNewsgroups {
32  
33      public static void main(final String[] args) {
34          if (args.length < 1) {
35              System.err.println("Usage: newsgroups newsserver [pattern]");
36              return;
37          }
38  
39          final NNTPClient client = new NNTPClient();
40          final String pattern = args.length >= 2 ? args[1] : "";
41  
42          try {
43              client.connect(args[0]);
44  
45              int j = 0;
46              try {
47                  for (final String s : client.iterateNewsgroupListing(pattern)) {
48                      j++;
49                      System.out.println(s);
50                  }
51              } catch (final IOException e1) {
52                  e1.printStackTrace();
53              }
54              System.out.println(j);
55  
56              j = 0;
57              for (final NewsgroupInfo n : client.iterateNewsgroups(pattern)) {
58                  j++;
59                  System.out.println(n.getNewsgroup());
60              }
61              System.out.println(j);
62          } catch (final IOException e) {
63              e.printStackTrace();
64          } finally {
65              try {
66                  if (client.isConnected()) {
67                      client.disconnect();
68                  }
69              } catch (final IOException e) {
70                  System.err.println("Error disconnecting from server.");
71                  e.printStackTrace();
72                  System.exit(1);
73              }
74          }
75  
76      }
77  
78  }