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 examples.nntp; 019 020import java.io.IOException; 021import org.apache.commons.net.nntp.NNTPClient; 022import org.apache.commons.net.nntp.NewsgroupInfo; 023 024/*** 025 * This is a trivial example using the NNTP package to approximate the 026 * Unix newsgroups command. It merely connects to the specified news 027 * server and issues fetches the list of newsgroups stored by the server. 028 * On servers that store a lot of newsgroups, this command can take a very 029 * long time (listing upwards of 30,000 groups). 030 ***/ 031 032public final class ListNewsgroups 033{ 034 035 public static void main(String[] args) 036 { 037 if (args.length < 1) 038 { 039 System.err.println("Usage: newsgroups newsserver [pattern]"); 040 return; 041 } 042 043 NNTPClient client = new NNTPClient(); 044 String pattern = args.length >= 2 ? args[1] : ""; 045 046 try 047 { 048 client.connect(args[0]); 049 050 int j = 0; 051 try { 052 for(String s : client.iterateNewsgroupListing(pattern)) { 053 j++; 054 System.out.println(s); 055 } 056 } catch (IOException e1) { 057 e1.printStackTrace(); 058 } 059 System.out.println(j); 060 061 j = 0; 062 for(NewsgroupInfo n : client.iterateNewsgroups(pattern)) { 063 j++; 064 System.out.println(n.getNewsgroup()); 065 } 066 System.out.println(j); 067 } 068 catch (IOException e) 069 { 070 e.printStackTrace(); 071 } 072 finally 073 { 074 try 075 { 076 if (client.isConnected()) { 077 client.disconnect(); 078 } 079 } 080 catch (IOException e) 081 { 082 System.err.println("Error disconnecting from server."); 083 e.printStackTrace(); 084 System.exit(1); 085 } 086 } 087 088 } 089 090} 091 092