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  public class ExtendedNNTPOps {
33  
34  
35      NNTPClient client;
36  
37      public ExtendedNNTPOps() {
38          client = new NNTPClient();
39          client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
40      }
41  
42  
43      private void demo(String host, String user, String password) {
44          try {
45              client.connect(host);
46  
47              // AUTHINFO USER/AUTHINFO PASS
48              if (user != null && password != null) {
49                  boolean success = client.authenticate(user, password);
50                  if (success) {
51                      System.out.println("Authentication succeeded");
52                  } else {
53                      System.out.println("Authentication failed, error =" + client.getReplyString());
54                  }
55              }
56  
57              // XOVER
58              NewsgroupInfo testGroup = new NewsgroupInfo();
59              client.selectNewsgroup("alt.test", testGroup);
60              long lowArticleNumber = testGroup.getFirstArticleLong();
61              long  highArticleNumber = lowArticleNumber + 100;
62              Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
63  
64              for (Article article : articles) {
65                  if (article.isDummy()) { // Subject will contain raw response
66                      System.out.println("Could not parse: "+article.getSubject());
67                  } else {
68                      System.out.println(article.getSubject());
69                  }
70              }
71  
72              // LIST ACTIVE
73              NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
74              for (NewsgroupInfo fanGroup : fanGroups)
75              {
76                  System.out.println(fanGroup.getNewsgroup());
77              }
78  
79          } catch (IOException e) {
80              e.printStackTrace();
81          }
82      }
83  
84      public static void main(String[] args) {
85          ExtendedNNTPOps ops;
86  
87          int argc = args.length;
88          if (argc < 1) {
89              System.err.println("usage: ExtendedNNTPOps nntpserver [username password]");
90              System.exit(1);
91          }
92  
93          ops = new ExtendedNNTPOps();
94          ops.demo(args[0], argc >=3 ? args[1] : null, argc >=3 ? args[2] : null);
95      }
96  
97  }
98  
99  /* Emacs configuration
100  * Local variables:        **
101  * mode:             java  **
102  * c-basic-offset:   4     **
103  * indent-tabs-mode: nil   **
104  * End:                    **
105  */