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