View Javadoc

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  
17  package examples.nntp;
18  
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.net.SocketException;
22  
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  import org.apache.commons.net.nntp.Threader;
27  
28  import examples.PrintCommandListener;
29  
30  public class MessageThreading {
31  	public MessageThreading() {
32  	}
33  	
34  	public static void main(String[] args) throws SocketException, IOException {
35  		
36  		if (args.length != 3)
37  			usage();
38  		
39  		String hostname = args[0];
40  		String user = args[1];
41  		String password = args[2];
42  		
43  		NNTPClient client = new NNTPClient();
44  		client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
45  		client.connect(hostname);
46  		
47  		if(!client.authenticate(user, password)) {
48  			System.out.println("Authentication failed for user " + user + "!");
49  			System.exit(1);
50  		}
51  		
52  		NewsgroupInfo group = new NewsgroupInfo();
53  		client.selectNewsgroup("comp.lang.lisp", group);
54  		
55  		int lowArticleNumber = group.getFirstArticle();
56  		int highArticleNumber = lowArticleNumber + 100;
57  		
58  		System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
59  		Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
60  		
61  		System.out.println("Building message thread tree...");
62  		Threader threader = new Threader();
63  		Article root = (Article)threader.thread(articles);
64  		
65  		Article.printThread(root, 0);	
66  		
67  	}
68  	
69  	
70  	public static void usage() {
71  		System.out.println("Usage: MessageThreading <hostname> <user> <password>");
72  		System.exit(0);
73  	}
74  }