001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package examples.nntp;
017    
018    import java.io.IOException;
019    import org.apache.commons.net.nntp.NNTPClient;
020    import org.apache.commons.net.nntp.NewsgroupInfo;
021    
022    /***
023     * This is a trivial example using the NNTP package to approximate the
024     * Unix newsgroups command.  It merely connects to the specified news
025     * server and issues fetches the list of newsgroups stored by the server.
026     * On servers that store a lot of newsgroups, this command can take a very
027     * long time (listing upwards of 30,000 groups).
028     * <p>
029     ***/
030    
031    public final class newsgroups
032    {
033    
034        public final static void main(String[] args)
035        {
036            NNTPClient client;
037            NewsgroupInfo[] list;
038    
039            if (args.length < 1)
040            {
041                System.err.println("Usage: newsgroups newsserver");
042                System.exit(1);
043            }
044    
045            client = new NNTPClient();
046    
047            try
048            {
049                client.connect(args[0]);
050    
051                list = client.listNewsgroups();
052    
053                if (list != null)
054                {
055                    for (int i = 0; i < list.length; i++)
056                        System.out.println(list[i].getNewsgroup());
057                }
058                else
059                {
060                    System.err.println("LIST command failed.");
061                    System.err.println("Server reply: " + client.getReplyString());
062                }
063            }
064            catch (IOException e)
065            {
066                e.printStackTrace();
067            }
068            finally
069            {
070                try
071                {
072                    if (client.isConnected())
073                        client.disconnect();
074                }
075                catch (IOException e)
076                {
077                    System.err.println("Error disconnecting from server.");
078                    e.printStackTrace();
079                    System.exit(1);
080                }
081            }
082    
083        }
084    
085    }
086    
087