01: /*
02: * Copyright 2001-2005 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: public final static void main(String[] args) {
34: NNTPClient client;
35: NewsgroupInfo[] list;
36:
37: if (args.length < 1) {
38: System.err.println("Usage: newsgroups newsserver");
39: System.exit(1);
40: }
41:
42: client = new NNTPClient();
43:
44: try {
45: client.connect(args[0]);
46:
47: list = client.listNewsgroups();
48:
49: if (list != null) {
50: for (int i = 0; i < list.length; i++)
51: System.out.println(list[i].getNewsgroup());
52: } else {
53: System.err.println("LIST command failed.");
54: System.err.println("Server reply: "
55: + client.getReplyString());
56: }
57: } catch (IOException e) {
58: e.printStackTrace();
59: } finally {
60: try {
61: if (client.isConnected())
62: client.disconnect();
63: } catch (IOException e) {
64: System.err.println("Error disconnecting from server.");
65: e.printStackTrace();
66: System.exit(1);
67: }
68: }
69:
70: }
71:
72: }
|