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.BufferedReader;
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.net.SocketException;
24  
25  import org.apache.commons.net.PrintCommandListener;
26  import org.apache.commons.net.nntp.NNTPClient;
27  import org.apache.commons.net.nntp.NewsgroupInfo;
28  
29  /**
30   * Sample program demonstrating the use of article header and body retrieval
31   */
32  public class ArticleReader {
33  
34      public static void main(String[] args) throws SocketException, IOException {
35  
36          if (args.length != 2 && args.length != 3 && args.length != 5) {
37              System.out.println("Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
38              return;
39          }
40  
41          String hostname = args[0];
42          String newsgroup = args[1];
43          // Article specifier can be numeric or Id in form <m.n.o.x@host>
44          String articleSpec = args.length >= 3 ? args[2] : null;
45  
46          NNTPClient client = new NNTPClient();
47          client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
48          client.connect(hostname);
49  
50          if (args.length == 5) { // Optional auth
51              String user = args[3];
52              String password = args[4];
53              if(!client.authenticate(user, password)) {
54                  System.out.println("Authentication failed for user " + user + "!");
55                  System.exit(1);
56              }
57          }
58  
59          NewsgroupInfo group = new NewsgroupInfo();
60          client.selectNewsgroup(newsgroup, group);
61  
62          BufferedReader brHdr;
63          String line;
64          if (articleSpec != null) {
65              brHdr = (BufferedReader) client.retrieveArticleHeader(articleSpec);
66          } else {
67              long articleNum = group.getLastArticleLong();
68              brHdr = client.retrieveArticleHeader(articleNum);
69          }
70          if (brHdr != null) {
71              while((line=brHdr.readLine()) != null) {
72                  System.out.println(line);
73              }
74              brHdr.close();
75          }
76          BufferedReader brBody;
77          if (articleSpec != null) {
78              brBody = (BufferedReader) client.retrieveArticleBody(articleSpec);
79          } else {
80              long articleNum = group.getLastArticleLong();
81              brBody = client.retrieveArticleBody(articleNum);
82          }
83          if (brBody != null) {
84              while((line=brBody.readLine()) != null) {
85                  System.out.println(line);
86              }
87              brBody.close();
88          }
89      }
90  
91  }