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.FileNotFoundException;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.io.PrintWriter;
26  import java.io.Writer;
27  
28  import org.apache.commons.net.PrintCommandListener;
29  import org.apache.commons.net.io.Util;
30  import org.apache.commons.net.nntp.NNTPClient;
31  import org.apache.commons.net.nntp.NNTPReply;
32  import org.apache.commons.net.nntp.SimpleNNTPHeader;
33  
34  
35  /***
36   * This is an example program using the NNTP package to post an article
37   * to the specified newsgroup(s).  It prompts you for header information and
38   * a filename to post.
39   ***/
40  
41  public final class PostMessage
42  {
43  
44      public static void main(String[] args)
45      {
46          String from, subject, newsgroup, filename, server, organization;
47          String references;
48          BufferedReader stdin;
49          FileReader fileReader = null;
50          SimpleNNTPHeader header;
51          NNTPClient client;
52  
53          if (args.length < 1)
54          {
55              System.err.println("Usage: post newsserver");
56              System.exit(1);
57          }
58  
59          server = args[0];
60  
61          stdin = new BufferedReader(new InputStreamReader(System.in));
62  
63          try
64          {
65              System.out.print("From: ");
66              System.out.flush();
67  
68              from = stdin.readLine();
69  
70              System.out.print("Subject: ");
71              System.out.flush();
72  
73              subject = stdin.readLine();
74  
75              header = new SimpleNNTPHeader(from, subject);
76  
77              System.out.print("Newsgroup: ");
78              System.out.flush();
79  
80              newsgroup = stdin.readLine();
81              header.addNewsgroup(newsgroup);
82  
83              while (true)
84              {
85                  System.out.print("Additional Newsgroup <Hit enter to end>: ");
86                  System.out.flush();
87  
88                  newsgroup = stdin.readLine();
89                  if (newsgroup == null) {
90                      break;
91                  }
92  
93                  newsgroup = newsgroup.trim();
94  
95                  if (newsgroup.length() == 0) {
96                      break;
97                  }
98  
99                  header.addNewsgroup(newsgroup);
100             }
101 
102             System.out.print("Organization: ");
103             System.out.flush();
104 
105             organization = stdin.readLine();
106 
107             System.out.print("References: ");
108             System.out.flush();
109 
110             references = stdin.readLine();
111 
112             if (organization != null && organization.length() > 0) {
113                 header.addHeaderField("Organization", organization);
114             }
115 
116             if (references != null && references.length() > 0) {
117                 header.addHeaderField("References", references);
118             }
119 
120             header.addHeaderField("X-Newsreader", "NetComponents");
121 
122             System.out.print("Filename: ");
123             System.out.flush();
124 
125             filename = stdin.readLine();
126 
127             try
128             {
129                 fileReader = new FileReader(filename);
130             }
131             catch (FileNotFoundException e)
132             {
133                 System.err.println("File not found. " + e.getMessage());
134                 System.exit(1);
135             }
136 
137             client = new NNTPClient();
138             client.addProtocolCommandListener(new PrintCommandListener(
139                                                   new PrintWriter(System.out), true));
140 
141             client.connect(server);
142 
143             if (!NNTPReply.isPositiveCompletion(client.getReplyCode()))
144             {
145                 client.disconnect();
146                 System.err.println("NNTP server refused connection.");
147                 System.exit(1);
148             }
149 
150             if (client.isAllowedToPost())
151             {
152                 Writer writer = client.postArticle();
153 
154                 if (writer != null)
155                 {
156                     writer.write(header.toString());
157                     Util.copyReader(fileReader, writer);
158                     writer.close();
159                     client.completePendingCommand();
160                 }
161             }
162 
163             if (fileReader != null) {
164                 fileReader.close();
165             }
166 
167             client.logout();
168 
169             client.disconnect();
170         }
171         catch (IOException e)
172         {
173             e.printStackTrace();
174             System.exit(1);
175         }
176     }
177 }
178 
179