    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    package examples.nntp;
    019
    020    import java.io.IOException;
    021    import org.apache.commons.net.nntp.NNTPClient;
    022    import 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     * <p>
    031     ***/
    032
    033    public final class ListNewsgroups
    034    {
    035
    036        public final static void main(String[] args)
    037        {
    038            NNTPClient client;
    039            NewsgroupInfo[] list;
    040
    041            if (args.length < 1)
    042            {
    043                System.err.println("Usage: newsgroups newsserver");
    044                System.exit(1);
    045            }
    046
    047            client = new NNTPClient();
    048
    049            try
    050            {
    051                client.connect(args[0]);
    052
    053                list = client.listNewsgroups();
    054
    055                if (list != null)
    056                {
    057                    for (int i = 0; i < list.length; i++)
    058                        System.out.println(list[i].getNewsgroup());
    059                }
    060                else
    061                {
    062                    System.err.println("LIST command failed.");
    063                    System.err.println("Server reply: " + client.getReplyString());
    064                }
    065            }
    066            catch (IOException e)
    067            {
    068                e.printStackTrace();
    069            }
    070            finally
    071            {
    072                try
    073                {
    074                    if (client.isConnected())
    075                        client.disconnect();
    076                }
    077                catch (IOException e)
    078                {
    079                    System.err.println("Error disconnecting from server.");
    080                    e.printStackTrace();
    081                    System.exit(1);
    082                }
    083            }
    084
    085        }
    086
    087    }
    088
    089