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