KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > nntp > ExtendedNNTPOps


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package examples.nntp;
17
18 import java.io.IOException JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20
21 import org.apache.commons.net.nntp.Article;
22 import org.apache.commons.net.nntp.NNTPClient;
23 import org.apache.commons.net.nntp.NewsgroupInfo;
24
25 import examples.PrintCommandListener;
26
27 /**
28  * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
29  *
30  * @author Rory Winston <rwinston@checkfree.com>
31  */

32 public class ExtendedNNTPOps {
33
34     
35     NNTPClient client;
36
37     public ExtendedNNTPOps() {
38         client = new NNTPClient();
39         client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter JavaDoc(System.out)));
40     }
41
42
43     public void demo(String JavaDoc host, String JavaDoc user, String JavaDoc password) {
44         try {
45             client.connect(host);
46
47             // AUTHINFO USER/AUTHINFO PASS
48
boolean success = client.authenticate(user, password);
49             if (success) {
50                 System.out.println("Authentication succeeded");
51             } else {
52                 System.out.println("Authentication failed, error =" + client.getReplyString());
53             }
54
55             // XOVER
56
NewsgroupInfo testGroup = new NewsgroupInfo();
57             client.selectNewsgroup("alt.test", testGroup);
58             int lowArticleNumber = testGroup.getFirstArticle();
59             int highArticleNumber = lowArticleNumber + 100;
60             Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
61
62             for (int i = 0; i < articles.length; ++i) {
63                 System.out.println(articles[i].getSubject());
64             }
65
66             // LIST ACTIVE
67
NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
68             for (int i = 0; i < fanGroups.length; ++i) {
69                 System.out.println(fanGroups[i].getNewsgroup());
70             }
71
72         } catch (IOException JavaDoc e) {
73             e.printStackTrace();
74         }
75     }
76
77     public static void main(String JavaDoc[] args) {
78         ExtendedNNTPOps ops;
79
80         if (args.length != 3) {
81             System.err.println("usage: ExtendedNNTPOps nntpserver username password");
82             System.exit(1);
83         }
84
85         ops = new ExtendedNNTPOps();
86         ops.demo(args[0], args[1], args[2]);
87     }
88
89 }
90
91 /* Emacs configuration
92  * Local variables: **
93  * mode: java **
94  * c-basic-offset: 4 **
95  * indent-tabs-mode: nil **
96  * End: **
97  */

98
Popular Tags