001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018
019 package examples.nntp;
020
021 import java.io.IOException;
022 import java.io.PrintWriter;
023 import java.net.SocketException;
024 import java.util.List;
025
026 import org.apache.commons.net.PrintCommandListener;
027 import org.apache.commons.net.nntp.Article;
028 import org.apache.commons.net.nntp.NNTPClient;
029 import org.apache.commons.net.nntp.NewsgroupInfo;
030 import org.apache.commons.net.nntp.Threader;
031
032
033 public class MessageThreading {
034 public MessageThreading() {
035 }
036
037 public static void main(String[] args) throws SocketException, IOException {
038
039 if (args.length != 3)
040 usage();
041
042 String hostname = args[0];
043 String user = args[1];
044 String password = args[2];
045
046 NNTPClient client = new NNTPClient();
047 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
048 client.connect(hostname);
049 // optional authentication
050 //
051 // if(!client.authenticate(user, password)) {
052 // System.out.println("Authentication failed for user " + user + "!");
053 // // System.exit(1);
054 // }
055 //
056 NewsgroupInfo group = new NewsgroupInfo();
057 client.selectNewsgroup("alt.test", group);
058
059 long lowArticleNumber = group.getFirstArticle();
060 long highArticleNumber = lowArticleNumber + 5000;
061
062 System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
063 List<Article> articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
064
065 System.out.println("Building message thread tree...");
066 Threader threader = new Threader();
067 Article root = (Article)threader.thread(articles);
068
069 Article.printThread(root, 0);
070 }
071
072
073 public static void usage() {
074 System.out.println("Usage: MessageThreading <hostname> <user> <password>");
075 System.exit(0);
076 }
077 }