src/org/sonews/util/io/ArticleWriter.java
author cli
Sun Aug 29 18:17:37 2010 +0200 (2010-08-29)
changeset 37 74139325d305
parent 35 ed84c8bdd87b
permissions -rw-r--r--
Switch intent style to Original K&R / Linux / Kernel.
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
 */
cli@37
    35
public class ArticleWriter
chris@1
    36
{
chris@1
    37
cli@37
    38
	private BufferedOutputStream out;
cli@37
    39
	private BufferedReader inr;
chris@1
    40
cli@37
    41
	public ArticleWriter(String host, int port)
cli@37
    42
		throws IOException, UnknownHostException
cli@37
    43
	{
cli@37
    44
		// Connect to NNTP server
cli@37
    45
		Socket socket = new Socket(host, port);
cli@37
    46
		this.out = new BufferedOutputStream(socket.getOutputStream());
cli@37
    47
		this.inr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
cli@37
    48
		String line = inr.readLine();
cli@37
    49
		if (line == null || !line.startsWith("200 ")) {
cli@37
    50
			throw new IOException("Invalid hello from server: " + line);
cli@37
    51
		}
cli@37
    52
	}
chris@1
    53
cli@37
    54
	public void close()
cli@37
    55
		throws IOException, UnsupportedEncodingException
cli@37
    56
	{
cli@37
    57
		this.out.write("QUIT\r\n".getBytes("UTF-8"));
cli@37
    58
		this.out.flush();
cli@37
    59
	}
chris@1
    60
cli@37
    61
	protected void finishPOST()
cli@37
    62
		throws IOException
cli@37
    63
	{
cli@37
    64
		this.out.write("\r\n.\r\n".getBytes());
cli@37
    65
		this.out.flush();
cli@37
    66
		String line = inr.readLine();
cli@37
    67
		if (line == null || !line.startsWith("240 ") || !line.startsWith("441 ")) {
cli@37
    68
			throw new IOException(line);
cli@37
    69
		}
cli@37
    70
	}
chris@1
    71
cli@37
    72
	protected void preparePOST()
cli@37
    73
		throws IOException
cli@37
    74
	{
cli@37
    75
		this.out.write("POST\r\n".getBytes("UTF-8"));
cli@37
    76
		this.out.flush();
chris@1
    77
cli@37
    78
		String line = this.inr.readLine();
cli@37
    79
		if (line == null || !line.startsWith("340 ")) {
cli@37
    80
			throw new IOException(line);
cli@37
    81
		}
cli@37
    82
	}
chris@1
    83
cli@37
    84
	public void writeArticle(Article article)
cli@37
    85
		throws IOException, UnsupportedEncodingException
cli@37
    86
	{
cli@37
    87
		byte[] buf = new byte[512];
cli@37
    88
		ArticleInputStream in = new ArticleInputStream(article);
chris@1
    89
cli@37
    90
		preparePOST();
chris@1
    91
cli@37
    92
		int len = in.read(buf);
cli@37
    93
		while (len != -1) {
cli@37
    94
			writeLine(buf, len);
cli@37
    95
			len = in.read(buf);
cli@37
    96
		}
chris@1
    97
cli@37
    98
		finishPOST();
cli@37
    99
	}
cli@37
   100
cli@37
   101
	/**
cli@37
   102
	 * Writes the raw content of an article to the remote server. This method
cli@37
   103
	 * does no charset conversion/handling of any kind so its the preferred
cli@37
   104
	 * method for sending an article to remote peers.
cli@37
   105
	 * @param rawArticle
cli@37
   106
	 * @throws IOException
cli@37
   107
	 */
cli@37
   108
	public void writeArticle(byte[] rawArticle)
cli@37
   109
		throws IOException
cli@37
   110
	{
cli@37
   111
		preparePOST();
cli@37
   112
		writeLine(rawArticle, rawArticle.length);
cli@37
   113
		finishPOST();
cli@37
   114
	}
cli@37
   115
cli@37
   116
	/**
cli@37
   117
	 * Writes the given buffer to the connect remote server.
cli@37
   118
	 * @param buffer
cli@37
   119
	 * @param len
cli@37
   120
	 * @throws IOException
cli@37
   121
	 */
cli@37
   122
	protected void writeLine(byte[] buffer, int len)
cli@37
   123
		throws IOException
cli@37
   124
	{
cli@37
   125
		this.out.write(buffer, 0, len);
cli@37
   126
		this.out.flush();
cli@37
   127
	}
chris@1
   128
}