1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package examples.nntp;
19
20 import java.io.IOException;
21 import java.io.PrintWriter;
22
23 import org.apache.commons.net.PrintCommandListener;
24 import org.apache.commons.net.nntp.Article;
25 import org.apache.commons.net.nntp.NNTPClient;
26 import org.apache.commons.net.nntp.NewsgroupInfo;
27
28
29
30
31
32
33
34 public class ExtendedNNTPOps {
35
36
37 NNTPClient client;
38
39 public ExtendedNNTPOps() {
40 client = new NNTPClient();
41 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
42 }
43
44
45 public void demo(String host, String user, String password) {
46 try {
47 client.connect(host);
48
49
50 boolean success = client.authenticate(user, password);
51 if (success) {
52 System.out.println("Authentication succeeded");
53 } else {
54 System.out.println("Authentication failed, error =" + client.getReplyString());
55 }
56
57
58 NewsgroupInfo testGroup = new NewsgroupInfo();
59 client.selectNewsgroup("alt.test", testGroup);
60 int lowArticleNumber = testGroup.getFirstArticle();
61 int highArticleNumber = lowArticleNumber + 100;
62 Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
63
64 for (int i = 0; i < articles.length; ++i) {
65 System.out.println(articles[i].getSubject());
66 }
67
68
69 NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
70 for (int i = 0; i < fanGroups.length; ++i) {
71 System.out.println(fanGroups[i].getNewsgroup());
72 }
73
74 } catch (IOException e) {
75 e.printStackTrace();
76 }
77 }
78
79 public static void main(String[] args) {
80 ExtendedNNTPOps ops;
81
82 if (args.length != 3) {
83 System.err.println("usage: ExtendedNNTPOps nntpserver username password");
84 System.exit(1);
85 }
86
87 ops = new ExtendedNNTPOps();
88 ops.demo(args[0], args[1], args[2]);
89 }
90
91 }
92
93
94
95
96
97
98
99