org/sonews/util/io/ArticleWriter.java
changeset 2 1090e2141798
child 3 2fdc9cc89502
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/util/io/ArticleWriter.java	Wed Jul 01 10:48:22 2009 +0200
     1.3 @@ -0,0 +1,133 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.util.io;
    1.23 +
    1.24 +import java.io.BufferedOutputStream;
    1.25 +import java.io.BufferedReader;
    1.26 +import java.io.IOException;
    1.27 +import java.io.InputStreamReader;
    1.28 +import java.io.UnsupportedEncodingException;
    1.29 +import java.net.Socket;
    1.30 +import java.net.UnknownHostException;
    1.31 +import org.sonews.daemon.storage.Article;
    1.32 +
    1.33 +/**
    1.34 + * Posts an Article to a NNTP server using the POST command.
    1.35 + * @author Christian Lins
    1.36 + * @since sonews/0.5.0
    1.37 + */
    1.38 +public class ArticleWriter 
    1.39 +{
    1.40 +  
    1.41 +  private BufferedOutputStream out;
    1.42 +  private BufferedReader       inr;
    1.43 +
    1.44 +  public ArticleWriter(String host, int port)
    1.45 +    throws IOException, UnknownHostException
    1.46 +  {
    1.47 +    // Connect to NNTP server
    1.48 +    Socket socket = new Socket(host, port);
    1.49 +    this.out = new BufferedOutputStream(socket.getOutputStream());
    1.50 +    this.inr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    1.51 +    String line = inr.readLine();
    1.52 +    if(line == null || !line.startsWith("200 "))
    1.53 +    {
    1.54 +      throw new IOException("Invalid hello from server: " + line);
    1.55 +    }
    1.56 +  }
    1.57 +  
    1.58 +  public void close()
    1.59 +    throws IOException, UnsupportedEncodingException
    1.60 +  {
    1.61 +    this.out.write("QUIT\r\n".getBytes("UTF-8"));
    1.62 +    this.out.flush();
    1.63 +  }
    1.64 +
    1.65 +  protected void finishPOST()
    1.66 +    throws IOException
    1.67 +  {
    1.68 +    this.out.write("\r\n.\r\n".getBytes());
    1.69 +    this.out.flush();
    1.70 +    String line = inr.readLine();
    1.71 +    if(line == null || !line.startsWith("240 "))
    1.72 +    {
    1.73 +      throw new IOException(line);
    1.74 +    }
    1.75 +  }
    1.76 +
    1.77 +  protected void preparePOST()
    1.78 +    throws IOException
    1.79 +  {
    1.80 +    this.out.write("POST\r\n".getBytes("UTF-8"));
    1.81 +    this.out.flush();
    1.82 +
    1.83 +    String line = this.inr.readLine();
    1.84 +    if(line == null || !line.startsWith("340 "))
    1.85 +    {
    1.86 +      throw new IOException(line);
    1.87 +    }
    1.88 +  }
    1.89 +
    1.90 +  public void writeArticle(Article article)
    1.91 +    throws IOException, UnsupportedEncodingException
    1.92 +  {
    1.93 +    byte[] buf = new byte[512];
    1.94 +    ArticleInputStream in = new ArticleInputStream(article);
    1.95 +
    1.96 +    preparePOST();
    1.97 +    
    1.98 +    int len = in.read(buf);
    1.99 +    while(len != -1)
   1.100 +    {
   1.101 +      writeLine(buf, len);
   1.102 +      len = in.read(buf);
   1.103 +    }
   1.104 +
   1.105 +    finishPOST();
   1.106 +  }
   1.107 +
   1.108 +  /**
   1.109 +   * Writes the raw content of an article to the remote server. This method
   1.110 +   * does no charset conversion/handling of any kind so its the preferred
   1.111 +   * method for sending an article to remote peers.
   1.112 +   * @param rawArticle
   1.113 +   * @throws IOException
   1.114 +   */
   1.115 +  public void writeArticle(byte[] rawArticle)
   1.116 +    throws IOException
   1.117 +  {
   1.118 +    preparePOST();
   1.119 +    writeLine(rawArticle, rawArticle.length);
   1.120 +    finishPOST();
   1.121 +  }
   1.122 +
   1.123 +  /**
   1.124 +   * Writes the given buffer to the connect remote server.
   1.125 +   * @param buffer
   1.126 +   * @param len
   1.127 +   * @throws IOException
   1.128 +   */
   1.129 +  protected void writeLine(byte[] buffer, int len)
   1.130 +    throws IOException
   1.131 +  {
   1.132 +    this.out.write(buffer, 0, len);
   1.133 +    this.out.flush();
   1.134 +  }
   1.135 +
   1.136 +}