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 019package examples.nntp; 020 021import java.io.IOException; 022import java.io.PrintWriter; 023import java.net.SocketException; 024import org.apache.commons.net.PrintCommandListener; 025import org.apache.commons.net.nntp.Article; 026import org.apache.commons.net.nntp.NNTPClient; 027import org.apache.commons.net.nntp.NewsgroupInfo; 028import org.apache.commons.net.nntp.Threader; 029 030/** 031 * Sample program demonstrating the use of article iteration and threading. 032 */ 033public class MessageThreading { 034 public MessageThreading() { 035 } 036 037 public static void main(String[] args) throws SocketException, IOException { 038 039 if (args.length != 2 && args.length != 4) { 040 System.out.println("Usage: MessageThreading <hostname> <groupname> [<user> <password>]"); 041 return; 042 } 043 044 String hostname = args[0]; 045 String newsgroup = args[1]; 046 047 NNTPClient client = new NNTPClient(); 048 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); 049 client.connect(hostname); 050 051 if (args.length == 4) { // Optional auth 052 String user = args[2]; 053 String password = args[3]; 054 if(!client.authenticate(user, password)) { 055 System.out.println("Authentication failed for user " + user + "!"); 056 System.exit(1); 057 } 058 } 059 060 String fmt[] = client.listOverviewFmt(); 061 if (fmt != null) { 062 System.out.println("LIST OVERVIEW.FMT:"); 063 for(String s : fmt) { 064 System.out.println(s); 065 } 066 } else { 067 System.out.println("Failed to get OVERVIEW.FMT"); 068 } 069 NewsgroupInfo group = new NewsgroupInfo(); 070 client.selectNewsgroup(newsgroup, group); 071 072 long lowArticleNumber = group.getFirstArticleLong(); 073 long highArticleNumber = lowArticleNumber + 5000; 074 075 System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]"); 076 Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber); 077 078 System.out.println("Building message thread tree..."); 079 Threader threader = new Threader(); 080 Article root = (Article)threader.thread(articles); 081 082 Article.printThread(root, 0); 083 } 084 085}