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