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