org/sonews/util/io/ArticleWriter.java
author cli
Wed, 26 Aug 2009 17:04:04 +0200
changeset 24 2ff819fa5be1
parent 18 7e527fdf0fa8
permissions -rw-r--r--
Some changes to storage interface.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.util.io;
chris@1
    20
chris@1
    21
import java.io.BufferedOutputStream;
chris@1
    22
import java.io.BufferedReader;
chris@1
    23
import java.io.IOException;
chris@1
    24
import java.io.InputStreamReader;
chris@1
    25
import java.io.UnsupportedEncodingException;
chris@1
    26
import java.net.Socket;
chris@1
    27
import java.net.UnknownHostException;
chris@3
    28
import org.sonews.storage.Article;
chris@1
    29
chris@1
    30
/**
chris@1
    31
 * Posts an Article to a NNTP server using the POST command.
chris@1
    32
 * @author Christian Lins
chris@1
    33
 * @since sonews/0.5.0
chris@1
    34
 */
chris@1
    35
public class ArticleWriter 
chris@1
    36
{
chris@1
    37
  
chris@1
    38
  private BufferedOutputStream out;
chris@1
    39
  private BufferedReader       inr;
chris@1
    40
chris@1
    41
  public ArticleWriter(String host, int port)
chris@1
    42
    throws IOException, UnknownHostException
chris@1
    43
  {
chris@1
    44
    // Connect to NNTP server
chris@1
    45
    Socket socket = new Socket(host, port);
chris@1
    46
    this.out = new BufferedOutputStream(socket.getOutputStream());
chris@1
    47
    this.inr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
chris@1
    48
    String line = inr.readLine();
chris@1
    49
    if(line == null || !line.startsWith("200 "))
chris@1
    50
    {
chris@1
    51
      throw new IOException("Invalid hello from server: " + line);
chris@1
    52
    }
chris@1
    53
  }
chris@1
    54
  
chris@1
    55
  public void close()
chris@1
    56
    throws IOException, UnsupportedEncodingException
chris@1
    57
  {
chris@1
    58
    this.out.write("QUIT\r\n".getBytes("UTF-8"));
chris@1
    59
    this.out.flush();
chris@1
    60
  }
chris@1
    61
chris@1
    62
  protected void finishPOST()
chris@1
    63
    throws IOException
chris@1
    64
  {
chris@1
    65
    this.out.write("\r\n.\r\n".getBytes());
chris@1
    66
    this.out.flush();
chris@1
    67
    String line = inr.readLine();
cli@18
    68
    if(line == null || !line.startsWith("240 ") || !line.startsWith("441 "))
chris@1
    69
    {
chris@1
    70
      throw new IOException(line);
chris@1
    71
    }
chris@1
    72
  }
chris@1
    73
chris@1
    74
  protected void preparePOST()
chris@1
    75
    throws IOException
chris@1
    76
  {
chris@1
    77
    this.out.write("POST\r\n".getBytes("UTF-8"));
chris@1
    78
    this.out.flush();
chris@1
    79
chris@1
    80
    String line = this.inr.readLine();
chris@1
    81
    if(line == null || !line.startsWith("340 "))
chris@1
    82
    {
chris@1
    83
      throw new IOException(line);
chris@1
    84
    }
chris@1
    85
  }
chris@1
    86
chris@1
    87
  public void writeArticle(Article article)
chris@1
    88
    throws IOException, UnsupportedEncodingException
chris@1
    89
  {
chris@1
    90
    byte[] buf = new byte[512];
chris@1
    91
    ArticleInputStream in = new ArticleInputStream(article);
chris@1
    92
chris@1
    93
    preparePOST();
chris@1
    94
    
chris@1
    95
    int len = in.read(buf);
chris@1
    96
    while(len != -1)
chris@1
    97
    {
chris@1
    98
      writeLine(buf, len);
chris@1
    99
      len = in.read(buf);
chris@1
   100
    }
chris@1
   101
chris@1
   102
    finishPOST();
chris@1
   103
  }
chris@1
   104
chris@1
   105
  /**
chris@1
   106
   * Writes the raw content of an article to the remote server. This method
chris@1
   107
   * does no charset conversion/handling of any kind so its the preferred
chris@1
   108
   * method for sending an article to remote peers.
chris@1
   109
   * @param rawArticle
chris@1
   110
   * @throws IOException
chris@1
   111
   */
chris@1
   112
  public void writeArticle(byte[] rawArticle)
chris@1
   113
    throws IOException
chris@1
   114
  {
chris@1
   115
    preparePOST();
chris@1
   116
    writeLine(rawArticle, rawArticle.length);
chris@1
   117
    finishPOST();
chris@1
   118
  }
chris@1
   119
chris@1
   120
  /**
chris@1
   121
   * Writes the given buffer to the connect remote server.
chris@1
   122
   * @param buffer
chris@1
   123
   * @param len
chris@1
   124
   * @throws IOException
chris@1
   125
   */
chris@1
   126
  protected void writeLine(byte[] buffer, int len)
chris@1
   127
    throws IOException
chris@1
   128
  {
chris@1
   129
    this.out.write(buffer, 0, len);
chris@1
   130
    this.out.flush();
chris@1
   131
  }
chris@1
   132
chris@1
   133
}