1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.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 public class ExtendedNNTPOps {
33
34
35 private final NNTPClient client;
36
37 public ExtendedNNTPOps() {
38 client = new NNTPClient();
39 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
40 }
41
42
43 private void demo(final String host, final String user, final String password) {
44 try {
45 client.connect(host);
46
47
48 if (user != null && password != null) {
49 final boolean success = client.authenticate(user, password);
50 if (success) {
51 System.out.println("Authentication succeeded");
52 } else {
53 System.out.println("Authentication failed, error =" + client.getReplyString());
54 }
55 }
56
57
58 final NewsgroupInfogroupInfo.html#NewsgroupInfo">NewsgroupInfo testGroup = new NewsgroupInfo();
59 client.selectNewsgroup("alt.test", testGroup);
60 final long lowArticleNumber = testGroup.getFirstArticleLong();
61 final long highArticleNumber = lowArticleNumber + 100;
62 final Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
63
64 for (final Article article : articles) {
65 if (article.isDummy()) {
66 System.out.println("Could not parse: "+article.getSubject());
67 } else {
68 System.out.println(article.getSubject());
69 }
70 }
71
72
73 final NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
74 for (final NewsgroupInfo fanGroup : fanGroups)
75 {
76 System.out.println(fanGroup.getNewsgroup());
77 }
78
79 } catch (final IOException e) {
80 e.printStackTrace();
81 }
82 }
83
84 public static void main(final String[] args) {
85 final ExtendedNNTPOps ops;
86
87 final int argc = args.length;
88 if (argc < 1) {
89 System.err.println("usage: ExtendedNNTPOps nntpserver [username password]");
90 System.exit(1);
91 }
92
93 ops = new ExtendedNNTPOps();
94 ops.demo(args[0], argc >=3 ? args[1] : null, argc >=3 ? args[2] : null);
95 }
96
97 }