src/org/sonews/util/io/ArticleReader.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 30 Oct 2011 22:13:32 +0100
changeset 112 ca54040b4409
parent 37 74139325d305
permissions -rwxr-xr-x
DrupalAuthInfoCommand → AuthInfoCommand (je to obecná implementace, nezávislá na Drupalu)
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.util.io;
    20 
    21 import java.io.BufferedInputStream;
    22 import java.io.BufferedOutputStream;
    23 import java.io.ByteArrayOutputStream;
    24 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.UnsupportedEncodingException;
    27 import java.net.Socket;
    28 import java.net.UnknownHostException;
    29 import org.sonews.config.Config;
    30 import org.sonews.util.Log;
    31 
    32 /**
    33  * Reads an news article from a NNTP server.
    34  * @author Christian Lins
    35  * @since sonews/0.5.0
    36  */
    37 public class ArticleReader
    38 {
    39 
    40 	private BufferedOutputStream out;
    41 	private BufferedInputStream in;
    42 	private String messageID;
    43 
    44 	public ArticleReader(String host, int port, String messageID)
    45 		throws IOException, UnknownHostException
    46 	{
    47 		this.messageID = messageID;
    48 
    49 		// Connect to NNTP server
    50 		Socket socket = new Socket(host, port);
    51 		this.out = new BufferedOutputStream(socket.getOutputStream());
    52 		this.in = new BufferedInputStream(socket.getInputStream());
    53 		String line = readln(this.in);
    54 		if (!line.startsWith("200 ")) {
    55 			throw new IOException("Invalid hello from server: " + line);
    56 		}
    57 	}
    58 
    59 	private boolean eofArticle(byte[] buf)
    60 	{
    61 		if (buf.length < 4) {
    62 			return false;
    63 		}
    64 
    65 		int l = buf.length - 1;
    66 		return buf[l - 3] == 10 // '*\n'
    67 			&& buf[l - 2] == '.' // '.'
    68 			&& buf[l - 1] == 13 && buf[l] == 10;  // '\r\n'
    69 	}
    70 
    71 	public byte[] getArticleData()
    72 		throws IOException, UnsupportedEncodingException
    73 	{
    74 		long maxSize = Config.inst().get(Config.ARTICLE_MAXSIZE, 1024) * 1024L;
    75 
    76 		try {
    77 			this.out.write(("ARTICLE " + this.messageID + "\r\n").getBytes("UTF-8"));
    78 			this.out.flush();
    79 
    80 			String line = readln(this.in);
    81 			if (line.startsWith("220 ")) {
    82 				ByteArrayOutputStream buf = new ByteArrayOutputStream();
    83 
    84 				while (!eofArticle(buf.toByteArray())) {
    85 					for (int b = in.read(); b != 10; b = in.read()) {
    86 						buf.write(b);
    87 					}
    88 
    89 					buf.write(10);
    90 					if (buf.size() > maxSize) {
    91 						Log.get().warning("Skipping message that is too large: " + buf.size());
    92 						return null;
    93 					}
    94 				}
    95 
    96 				return buf.toByteArray();
    97 			} else {
    98 				Log.get().warning("ArticleReader: " + line);
    99 				return null;
   100 			}
   101 		} catch (IOException ex) {
   102 			throw ex;
   103 		} finally {
   104 			this.out.write("QUIT\r\n".getBytes("UTF-8"));
   105 			this.out.flush();
   106 			this.out.close();
   107 		}
   108 	}
   109 
   110 	private String readln(InputStream in)
   111 		throws IOException
   112 	{
   113 		ByteArrayOutputStream buf = new ByteArrayOutputStream();
   114 		for (int b = in.read(); b != 10 /* \n */; b = in.read()) {
   115 			buf.write(b);
   116 		}
   117 
   118 		return new String(buf.toByteArray());
   119 	}
   120 }