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: */ chris@1: public class ArticleWriter chris@1: { chris@1: chris@1: private BufferedOutputStream out; chris@1: private BufferedReader inr; chris@1: chris@1: public ArticleWriter(String host, int port) chris@1: throws IOException, UnknownHostException chris@1: { chris@1: // Connect to NNTP server chris@1: Socket socket = new Socket(host, port); chris@1: this.out = new BufferedOutputStream(socket.getOutputStream()); chris@1: this.inr = new BufferedReader(new InputStreamReader(socket.getInputStream())); chris@1: String line = inr.readLine(); chris@1: if(line == null || !line.startsWith("200 ")) chris@1: { chris@1: throw new IOException("Invalid hello from server: " + line); chris@1: } chris@1: } chris@1: chris@1: public void close() chris@1: throws IOException, UnsupportedEncodingException chris@1: { chris@1: this.out.write("QUIT\r\n".getBytes("UTF-8")); chris@1: this.out.flush(); chris@1: } chris@1: chris@1: protected void finishPOST() chris@1: throws IOException chris@1: { chris@1: this.out.write("\r\n.\r\n".getBytes()); chris@1: this.out.flush(); chris@1: String line = inr.readLine(); chris@1: if(line == null || !line.startsWith("240 ")) chris@1: { chris@1: throw new IOException(line); chris@1: } chris@1: } chris@1: chris@1: protected void preparePOST() chris@1: throws IOException chris@1: { chris@1: this.out.write("POST\r\n".getBytes("UTF-8")); chris@1: this.out.flush(); chris@1: chris@1: String line = this.inr.readLine(); chris@1: if(line == null || !line.startsWith("340 ")) chris@1: { chris@1: throw new IOException(line); chris@1: } chris@1: } chris@1: chris@1: public void writeArticle(Article article) chris@1: throws IOException, UnsupportedEncodingException chris@1: { chris@1: byte[] buf = new byte[512]; chris@1: ArticleInputStream in = new ArticleInputStream(article); chris@1: chris@1: preparePOST(); chris@1: chris@1: int len = in.read(buf); chris@1: while(len != -1) chris@1: { chris@1: writeLine(buf, len); chris@1: len = in.read(buf); chris@1: } chris@1: chris@1: finishPOST(); chris@1: } chris@1: chris@1: /** chris@1: * Writes the raw content of an article to the remote server. This method chris@1: * does no charset conversion/handling of any kind so its the preferred chris@1: * method for sending an article to remote peers. chris@1: * @param rawArticle chris@1: * @throws IOException chris@1: */ chris@1: public void writeArticle(byte[] rawArticle) chris@1: throws IOException chris@1: { chris@1: preparePOST(); chris@1: writeLine(rawArticle, rawArticle.length); chris@1: finishPOST(); chris@1: } chris@1: chris@1: /** chris@1: * Writes the given buffer to the connect remote server. chris@1: * @param buffer chris@1: * @param len chris@1: * @throws IOException chris@1: */ chris@1: protected void writeLine(byte[] buffer, int len) chris@1: throws IOException chris@1: { chris@1: this.out.write(buffer, 0, len); chris@1: this.out.flush(); chris@1: } chris@1: chris@1: }