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
22 import org.apache.commons.net.examples.PrintCommandListeners;
23 import org.apache.commons.net.nntp.Article;
24 import org.apache.commons.net.nntp.NNTPClient;
25 import org.apache.commons.net.nntp.NewsgroupInfo;
26
27
28
29
30 public class ExtendedNNTPOps {
31
32 public static void main(final String[] args) {
33 final ExtendedNNTPOps ops;
34
35 final int argc = args.length;
36 if (argc < 1) {
37 System.err.println("usage: ExtendedNNTPOps nntpserver [user password]");
38 System.exit(1);
39 }
40
41 ops = new ExtendedNNTPOps();
42 ops.demo(args[0], argc >= 3 ? args[1] : null, argc >= 3 ? args[2] : null);
43 }
44
45 private final NNTPClient client;
46
47 public ExtendedNNTPOps() {
48 client = new NNTPClient();
49 client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener());
50 }
51
52 private void demo(final String host, final String user, final String password) {
53 try {
54 client.connect(host);
55
56
57 if (user != null && password != null) {
58 final boolean success = client.authenticate(user, password);
59 if (success) {
60 System.out.println("Authentication succeeded");
61 } else {
62 System.out.println("Authentication failed, error =" + client.getReplyString());
63 }
64 }
65
66
67 final NewsgroupInfo testGroup = new NewsgroupInfo();
68 client.selectNewsgroup("alt.test", testGroup);
69 final long lowArticleNumber = testGroup.getFirstArticleLong();
70 final long highArticleNumber = lowArticleNumber + 100;
71 final Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
72
73 for (final Article article : articles) {
74 if (article.isDummy()) {
75 System.out.println("Could not parse: " + article.getSubject());
76 } else {
77 System.out.println(article.getSubject());
78 }
79 }
80
81
82 final NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
83 for (final NewsgroupInfo fanGroup : fanGroups) {
84 System.out.println(fanGroup.getNewsgroup());
85 }
86
87 } catch (final IOException e) {
88 e.printStackTrace();
89 }
90 }
91
92 }