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 java.io.PrintWriter;
022    import java.util.List;
023    
024    import org.apache.commons.net.PrintCommandListener;
025    import org.apache.commons.net.nntp.Article;
026    import org.apache.commons.net.nntp.NNTPClient;
027    import org.apache.commons.net.nntp.NewsgroupInfo;
028    
029    
030    /**
031     * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
032     * 
033     * @author Rory Winston <rwinston@apache.org>
034     */
035    public class ExtendedNNTPOps {
036    
037        
038        NNTPClient client;
039    
040        public ExtendedNNTPOps() {
041            client = new NNTPClient();
042            client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
043        }
044    
045    
046        public void demo(String host, String user, String password) {
047            try {
048                client.connect(host);
049    
050                // AUTHINFO USER/AUTHINFO PASS
051                boolean success = client.authenticate(user, password);
052                if (success) {
053                    System.out.println("Authentication succeeded");
054                } else {
055                    System.out.println("Authentication failed, error =" + client.getReplyString());
056                }
057    
058                // XOVER
059                NewsgroupInfo testGroup = new NewsgroupInfo();
060                client.selectNewsgroup("alt.test", testGroup);
061                long lowArticleNumber = testGroup.getFirstArticle();
062                long  highArticleNumber = lowArticleNumber + 100;
063                List<Article> articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
064    
065                for (Article article : articles) {
066                    System.out.println(article.getSubject());
067                }
068    
069                // LIST ACTIVE
070                NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
071                for (int i = 0; i < fanGroups.length; ++i) {
072                    System.out.println(fanGroups[i].getNewsgroup());
073                }
074    
075            } catch (IOException e) {
076                e.printStackTrace();
077            }
078        }
079    
080        public static void main(String[] args) {
081            ExtendedNNTPOps ops;
082    
083            if (args.length != 3) {
084                System.err.println("usage: ExtendedNNTPOps nntpserver username password");
085                System.exit(1);
086            }
087    
088            ops = new ExtendedNNTPOps();
089            ops.demo(args[0], args[1], args[2]);
090        }
091    
092    }
093    
094    /* Emacs configuration
095     * Local variables:        **
096     * mode:             java  **
097     * c-basic-offset:   4     **
098     * indent-tabs-mode: nil   **
099     * End:                    **
100     */