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