org/sonews/util/io/ArticleReader.java
author cli
Thu, 20 Aug 2009 14:31:19 +0200
changeset 12 bb6990c0dd1a
parent 3 2fdc9cc89502
child 16 5a4a41cfc0a3
permissions -rw-r--r--
Merging fixes from sonews/1.0.3
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.BufferedInputStream;
chris@1
    22
import java.io.BufferedOutputStream;
chris@1
    23
import java.io.ByteArrayOutputStream;
chris@1
    24
import java.io.IOException;
chris@1
    25
import java.io.InputStream;
chris@1
    26
import java.io.UnsupportedEncodingException;
chris@1
    27
import java.net.Socket;
chris@1
    28
import java.net.UnknownHostException;
chris@3
    29
import org.sonews.config.Config;
chris@1
    30
import org.sonews.util.Log;
chris@1
    31
chris@1
    32
/**
chris@1
    33
 * Reads an news article from a NNTP server.
chris@1
    34
 * @author Christian Lins
chris@1
    35
 * @since sonews/0.5.0
chris@1
    36
 */
chris@1
    37
public class ArticleReader 
chris@1
    38
{
chris@1
    39
chris@1
    40
  private BufferedOutputStream out;
chris@1
    41
  private BufferedInputStream  in;
chris@1
    42
  private String               messageID;
chris@1
    43
  
chris@1
    44
  public ArticleReader(String host, int port, String messageID)
chris@1
    45
    throws IOException, UnknownHostException
chris@1
    46
  {
chris@1
    47
    this.messageID = messageID;
chris@1
    48
chris@1
    49
    // Connect to NNTP server
chris@1
    50
    Socket socket = new Socket(host, port);
chris@1
    51
    this.out = new BufferedOutputStream(socket.getOutputStream());
chris@1
    52
    this.in  = new BufferedInputStream(socket.getInputStream());
chris@1
    53
    String line = readln(this.in);
chris@1
    54
    if(!line.startsWith("200 "))
chris@1
    55
    {
chris@1
    56
      throw new IOException("Invalid hello from server: " + line);
chris@1
    57
    }
chris@1
    58
  }
chris@1
    59
  
chris@1
    60
  private boolean eofArticle(byte[] buf)
chris@1
    61
  {
chris@1
    62
    if(buf.length < 4)
chris@1
    63
    {
chris@1
    64
      return false;
chris@1
    65
    }
chris@1
    66
    
chris@1
    67
    int l = buf.length - 1;
chris@1
    68
    return buf[l-3] == 10 // '*\n'
chris@1
    69
        && buf[l-2] == '.'                   // '.'
chris@1
    70
        && buf[l-1] == 13 && buf[l] == 10;  // '\r\n'
chris@1
    71
  }
chris@1
    72
  
chris@1
    73
  public byte[] getArticleData()
chris@1
    74
    throws IOException, UnsupportedEncodingException
chris@1
    75
  {
chris@3
    76
    long maxSize = Config.inst().get(Config.ARTICLE_MAXSIZE, 1024) * 1024L;
chris@3
    77
chris@1
    78
    try
chris@1
    79
    {
chris@1
    80
      this.out.write(("ARTICLE " + this.messageID + "\r\n").getBytes("UTF-8"));
chris@1
    81
      this.out.flush();
chris@1
    82
chris@1
    83
      String line = readln(this.in);
chris@1
    84
      if(line.startsWith("220 "))
chris@1
    85
      {
chris@1
    86
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
chris@1
    87
        
chris@1
    88
        while(!eofArticle(buf.toByteArray()))
chris@1
    89
        {
chris@1
    90
          for(int b = in.read(); b != 10; b = in.read())
chris@1
    91
          {
chris@1
    92
            buf.write(b);
chris@1
    93
          }
chris@1
    94
chris@1
    95
          buf.write(10);
chris@3
    96
          if(buf.size() > maxSize)
chris@3
    97
          {
chris@3
    98
            Log.msg("Skipping message that is too large: " + buf.size(), false);
chris@3
    99
            return null;
chris@3
   100
          }
chris@1
   101
        }
chris@1
   102
        
chris@1
   103
        return buf.toByteArray();
chris@1
   104
      }
chris@1
   105
      else
chris@1
   106
      {
chris@1
   107
        Log.msg("ArticleReader: " + line, false);
chris@1
   108
        return null;
chris@1
   109
      }
chris@1
   110
    }
chris@1
   111
    catch(IOException ex)
chris@1
   112
    {
chris@1
   113
      throw ex;
chris@1
   114
    }
chris@1
   115
    finally
chris@1
   116
    {
chris@1
   117
      this.out.write("QUIT\r\n".getBytes("UTF-8"));
chris@1
   118
      this.out.flush();
chris@1
   119
      this.out.close();
chris@1
   120
    }
chris@1
   121
  }
chris@1
   122
  
chris@1
   123
  private String readln(InputStream in)
chris@1
   124
    throws IOException
chris@1
   125
  {
chris@1
   126
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
chris@1
   127
    for(int b = in.read(); b != 10 /* \n */; b = in.read())
chris@1
   128
    {
chris@1
   129
      buf.write(b);
chris@1
   130
    }
chris@1
   131
    
chris@1
   132
    return new String(buf.toByteArray());
chris@1
   133
  }
chris@1
   134
chris@1
   135
}