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