chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.util.io; chris@1: chris@1: import java.io.BufferedOutputStream; chris@1: import java.io.BufferedReader; chris@1: import java.io.IOException; chris@1: import java.io.InputStreamReader; chris@1: import java.io.UnsupportedEncodingException; chris@1: import java.net.Socket; chris@1: import java.net.UnknownHostException; chris@3: import org.sonews.storage.Article; chris@1: chris@1: /** chris@1: * Posts an Article to a NNTP server using the POST command. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ cli@37: public class ArticleWriter chris@1: { chris@1: cli@37: private BufferedOutputStream out; cli@37: private BufferedReader inr; chris@1: cli@37: public ArticleWriter(String host, int port) cli@37: throws IOException, UnknownHostException cli@37: { cli@37: // Connect to NNTP server cli@37: Socket socket = new Socket(host, port); cli@37: this.out = new BufferedOutputStream(socket.getOutputStream()); cli@37: this.inr = new BufferedReader(new InputStreamReader(socket.getInputStream())); cli@37: String line = inr.readLine(); cli@37: if (line == null || !line.startsWith("200 ")) { cli@37: throw new IOException("Invalid hello from server: " + line); cli@37: } cli@37: } chris@1: cli@37: public void close() cli@37: throws IOException, UnsupportedEncodingException cli@37: { cli@37: this.out.write("QUIT\r\n".getBytes("UTF-8")); cli@37: this.out.flush(); cli@37: } chris@1: cli@37: protected void finishPOST() cli@37: throws IOException cli@37: { cli@37: this.out.write("\r\n.\r\n".getBytes()); cli@37: this.out.flush(); cli@37: String line = inr.readLine(); cli@37: if (line == null || !line.startsWith("240 ") || !line.startsWith("441 ")) { cli@37: throw new IOException(line); cli@37: } cli@37: } chris@1: cli@37: protected void preparePOST() cli@37: throws IOException cli@37: { cli@37: this.out.write("POST\r\n".getBytes("UTF-8")); cli@37: this.out.flush(); chris@1: cli@37: String line = this.inr.readLine(); cli@37: if (line == null || !line.startsWith("340 ")) { cli@37: throw new IOException(line); cli@37: } cli@37: } chris@1: cli@37: public void writeArticle(Article article) cli@37: throws IOException, UnsupportedEncodingException cli@37: { cli@37: byte[] buf = new byte[512]; cli@37: ArticleInputStream in = new ArticleInputStream(article); chris@1: cli@37: preparePOST(); chris@1: cli@37: int len = in.read(buf); cli@37: while (len != -1) { cli@37: writeLine(buf, len); cli@37: len = in.read(buf); cli@37: } chris@1: cli@37: finishPOST(); cli@37: } cli@37: cli@37: /** cli@37: * Writes the raw content of an article to the remote server. This method cli@37: * does no charset conversion/handling of any kind so its the preferred cli@37: * method for sending an article to remote peers. cli@37: * @param rawArticle cli@37: * @throws IOException cli@37: */ cli@37: public void writeArticle(byte[] rawArticle) cli@37: throws IOException cli@37: { cli@37: preparePOST(); cli@37: writeLine(rawArticle, rawArticle.length); cli@37: finishPOST(); cli@37: } cli@37: cli@37: /** cli@37: * Writes the given buffer to the connect remote server. cli@37: * @param buffer cli@37: * @param len cli@37: * @throws IOException cli@37: */ cli@37: protected void writeLine(byte[] buffer, int len) cli@37: throws IOException cli@37: { cli@37: this.out.write(buffer, 0, len); cli@37: this.out.flush(); cli@37: } chris@1: }