View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package examples.nntp;
19  
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  
23  import org.apache.commons.net.PrintCommandListener;
24  import org.apache.commons.net.nntp.Article;
25  import org.apache.commons.net.nntp.NNTPClient;
26  import org.apache.commons.net.nntp.NewsgroupInfo;
27  
28  
29  /**
30   * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
31   * 
32   * @author Rory Winston <rwinston@checkfree.com>
33   */
34  public class ExtendedNNTPOps {
35  
36      
37      NNTPClient client;
38  
39      public ExtendedNNTPOps() {
40          client = new NNTPClient();
41          client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
42      }
43  
44  
45      public void demo(String host, String user, String password) {
46          try {
47              client.connect(host);
48  
49              // AUTHINFO USER/AUTHINFO PASS
50              boolean success = client.authenticate(user, password);
51              if (success) {
52                  System.out.println("Authentication succeeded");
53              } else {
54                  System.out.println("Authentication failed, error =" + client.getReplyString());
55              }
56  
57              // XOVER
58              NewsgroupInfo testGroup = new NewsgroupInfo();
59              client.selectNewsgroup("alt.test", testGroup);
60              int lowArticleNumber = testGroup.getFirstArticle();
61              int highArticleNumber = lowArticleNumber + 100;
62              Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
63  
64              for (int i = 0; i < articles.length; ++i) {
65                  System.out.println(articles[i].getSubject());
66              }
67  
68              // LIST ACTIVE
69              NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
70              for (int i = 0; i < fanGroups.length; ++i) {
71                  System.out.println(fanGroups[i].getNewsgroup());
72              }
73  
74          } catch (IOException e) {
75              e.printStackTrace();
76          }
77      }
78  
79      public static void main(String[] args) {
80          ExtendedNNTPOps ops;
81  
82          if (args.length != 3) {
83              System.err.println("usage: ExtendedNNTPOps nntpserver username password");
84              System.exit(1);
85          }
86  
87          ops = new ExtendedNNTPOps();
88          ops.demo(args[0], args[1], args[2]);
89      }
90  
91  }
92  
93  /* Emacs configuration
94   * Local variables:        **
95   * mode:             java  **
96   * c-basic-offset:   4     **
97   * indent-tabs-mode: nil   **
98   * End:                    **
99   */