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), true));
42 }
43
44
45 private void demo(String host, String user, String password) {
46 try {
47 client.connect(host);
48
49
50 if (user != null && password != null) {
51 boolean success = client.authenticate(user, password);
52 if (success) {
53 System.out.println("Authentication succeeded");
54 } else {
55 System.out.println("Authentication failed, error =" + client.getReplyString());
56 }
57 }
58
59
60 NewsgroupInfo testGroup = new NewsgroupInfo();
61 client.selectNewsgroup("alt.test", testGroup);
62 long lowArticleNumber = testGroup.getFirstArticleLong();
63 long highArticleNumber = lowArticleNumber + 100;
64 Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
65
66 for (Article article : articles) {
67 if (article.isDummy()) {
68 System.out.println("Could not parse: "+article.getSubject());
69 } else {
70 System.out.println(article.getSubject());
71 }
72 }
73
74
75 NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
76 for (NewsgroupInfo fanGroup : fanGroups)
77 {
78 System.out.println(fanGroup.getNewsgroup());
79 }
80
81 } catch (IOException e) {
82 e.printStackTrace();
83 }
84 }
85
86 public static void main(String[] args) {
87 ExtendedNNTPOps ops;
88
89 int argc = args.length;
90 if (argc < 1) {
91 System.err.println("usage: ExtendedNNTPOps nntpserver [username password]");
92 System.exit(1);
93 }
94
95 ops = new ExtendedNNTPOps();
96 ops.demo(args[0], argc >=3 ? args[1] : null, argc >=3 ? args[2] : null);
97 }
98
99 }
100
101
102
103
104
105
106
107