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