src/org/sonews/util/io/ArticleReader.java
author cli
Tue, 07 Jun 2011 09:23:34 +0200
changeset 43 7d0e65712a95
parent 37 74139325d305
permissions -rwxr-xr-x
Adapt config sample to use hsqldb
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
 */
cli@37
    37
public class ArticleReader
chris@1
    38
{
chris@1
    39
cli@37
    40
	private BufferedOutputStream out;
cli@37
    41
	private BufferedInputStream in;
cli@37
    42
	private String messageID;
chris@1
    43
cli@37
    44
	public ArticleReader(String host, int port, String messageID)
cli@37
    45
		throws IOException, UnknownHostException
cli@37
    46
	{
cli@37
    47
		this.messageID = messageID;
chris@3
    48
cli@37
    49
		// Connect to NNTP server
cli@37
    50
		Socket socket = new Socket(host, port);
cli@37
    51
		this.out = new BufferedOutputStream(socket.getOutputStream());
cli@37
    52
		this.in = new BufferedInputStream(socket.getInputStream());
cli@37
    53
		String line = readln(this.in);
cli@37
    54
		if (!line.startsWith("200 ")) {
cli@37
    55
			throw new IOException("Invalid hello from server: " + line);
cli@37
    56
		}
cli@37
    57
	}
chris@1
    58
cli@37
    59
	private boolean eofArticle(byte[] buf)
cli@37
    60
	{
cli@37
    61
		if (buf.length < 4) {
cli@37
    62
			return false;
cli@37
    63
		}
chris@1
    64
cli@37
    65
		int l = buf.length - 1;
cli@37
    66
		return buf[l - 3] == 10 // '*\n'
cli@37
    67
			&& buf[l - 2] == '.' // '.'
cli@37
    68
			&& buf[l - 1] == 13 && buf[l] == 10;  // '\r\n'
cli@37
    69
	}
chris@1
    70
cli@37
    71
	public byte[] getArticleData()
cli@37
    72
		throws IOException, UnsupportedEncodingException
cli@37
    73
	{
cli@37
    74
		long maxSize = Config.inst().get(Config.ARTICLE_MAXSIZE, 1024) * 1024L;
cli@37
    75
cli@37
    76
		try {
cli@37
    77
			this.out.write(("ARTICLE " + this.messageID + "\r\n").getBytes("UTF-8"));
cli@37
    78
			this.out.flush();
cli@37
    79
cli@37
    80
			String line = readln(this.in);
cli@37
    81
			if (line.startsWith("220 ")) {
cli@37
    82
				ByteArrayOutputStream buf = new ByteArrayOutputStream();
cli@37
    83
cli@37
    84
				while (!eofArticle(buf.toByteArray())) {
cli@37
    85
					for (int b = in.read(); b != 10; b = in.read()) {
cli@37
    86
						buf.write(b);
cli@37
    87
					}
cli@37
    88
cli@37
    89
					buf.write(10);
cli@37
    90
					if (buf.size() > maxSize) {
cli@37
    91
						Log.get().warning("Skipping message that is too large: " + buf.size());
cli@37
    92
						return null;
cli@37
    93
					}
cli@37
    94
				}
cli@37
    95
cli@37
    96
				return buf.toByteArray();
cli@37
    97
			} else {
cli@37
    98
				Log.get().warning("ArticleReader: " + line);
cli@37
    99
				return null;
cli@37
   100
			}
cli@37
   101
		} catch (IOException ex) {
cli@37
   102
			throw ex;
cli@37
   103
		} finally {
cli@37
   104
			this.out.write("QUIT\r\n".getBytes("UTF-8"));
cli@37
   105
			this.out.flush();
cli@37
   106
			this.out.close();
cli@37
   107
		}
cli@37
   108
	}
cli@37
   109
cli@37
   110
	private String readln(InputStream in)
cli@37
   111
		throws IOException
cli@37
   112
	{
cli@37
   113
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
cli@37
   114
		for (int b = in.read(); b != 10 /* \n */; b = in.read()) {
cli@37
   115
			buf.write(b);
cli@37
   116
		}
cli@37
   117
cli@37
   118
		return new String(buf.toByteArray());
cli@37
   119
	}
chris@1
   120
}