KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > nntp > post


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package examples.nntp;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.FileReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25 import org.apache.commons.net.io.Util;
26 import org.apache.commons.net.nntp.NNTPClient;
27 import org.apache.commons.net.nntp.NNTPReply;
28 import org.apache.commons.net.nntp.SimpleNNTPHeader;
29
30 import examples.PrintCommandListener;
31
32 /***
33  * This is an example program using the NNTP package to post an article
34  * to the specified newsgroup(s). It prompts you for header information and
35  * a filename to post.
36  * <p>
37  ***/

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