nntp.java
来自「apache推出的net包」· Java 代码 · 共 1,018 行 · 第 1/3 页
JAVA
1,018 行
/* * Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.net.nntp;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import org.apache.commons.net.MalformedServerReplyException;import org.apache.commons.net.ProtocolCommandSupport;import org.apache.commons.net.ProtocolCommandListener;import org.apache.commons.net.SocketClient;/*** * The NNTP class is not meant to be used by itself and is provided * only so that you may easily implement your own NNTP client if * you so desire. If you have no need to perform your own implementation, * you should use {@link org.apache.commons.net.nntp.NNTPClient}. * The NNTP class is made public to provide access to various NNTP constants * and to make it easier for adventurous programmers (or those with special * needs) to interact with the NNTP protocol and implement their own clients. * A set of methods with names corresponding to the NNTP command names are * provided to facilitate this interaction. * <p> * You should keep in mind that the NNTP server may choose to prematurely * close a connection if the client has been idle for longer than a * given time period or if the server is being shutdown by the operator or * some other reason. The NNTP class will detect a * premature NNTP server connection closing when it receives a * {@link org.apache.commons.net.nntp.NNTPReply#SERVICE_DISCONTINUED NNTPReply.SERVICE_DISCONTINUED } * response to a command. * When that occurs, the NNTP class method encountering that reply will throw * an {@link org.apache.commons.net.nntp.NNTPConnectionClosedException} * . * <code>NNTPConectionClosedException</code> * is a subclass of <code> IOException </code> and therefore need not be * caught separately, but if you are going to catch it separately, its * catch block must appear before the more general <code> IOException </code> * catch block. When you encounter an * {@link org.apache.commons.net.nntp.NNTPConnectionClosedException} * , you must disconnect the connection with * {@link #disconnect disconnect() } to properly clean up the * system resources used by NNTP. Before disconnecting, you may check the * last reply code and text with * {@link #getReplyCode getReplyCode } and * {@link #getReplyString getReplyString }. * <p> * Rather than list it separately for each method, we mention here that * every method communicating with the server and throwing an IOException * can also throw a * {@link org.apache.commons.net.MalformedServerReplyException} * , which is a subclass * of IOException. A MalformedServerReplyException will be thrown when * the reply received from the server deviates enough from the protocol * specification that it cannot be interpreted in a useful manner despite * attempts to be as lenient as possible. * <p> * <p> * @author Daniel F. Savarese * @author Rory Winston * @author Ted Wise * @see NNTPClient * @see NNTPConnectionClosedException * @see org.apache.commons.net.MalformedServerReplyException ***/public class NNTP extends SocketClient{ /*** The default NNTP port. Its value is 119 according to RFC 977. ***/ public static final int DEFAULT_PORT = 119; // We have to ensure that the protocol communication is in ASCII // but we use ISO-8859-1 just in case 8-bit characters cross // the wire. private static final String __DEFAULT_ENCODING = "ISO-8859-1"; private StringBuffer __commandBuffer; boolean _isAllowedToPost; int _replyCode; String _replyString; /** * Wraps {@link SocketClient#_input_} * to communicate with server. Initialized by {@link #_connectAction_}. * All server reads should be done through this variable. */ protected BufferedReader _reader_; /** * Wraps {@link SocketClient#_output_} * to communicate with server. Initialized by {@link #_connectAction_}. * All server reads should be done through this variable. */ protected BufferedWriter _writer_; /*** * A ProtocolCommandSupport object used to manage the registering of * ProtocolCommandListeners and te firing of ProtocolCommandEvents. ***/ protected ProtocolCommandSupport _commandSupport_; /*** * The default NNTP constructor. Sets the default port to * <code>DEFAULT_PORT</code> and initializes internal data structures * for saving NNTP reply information. ***/ public NNTP() { setDefaultPort(DEFAULT_PORT); __commandBuffer = new StringBuffer(); _replyString = null; _reader_ = null; _writer_ = null; _isAllowedToPost = false; _commandSupport_ = new ProtocolCommandSupport(this); } private void __getReply() throws IOException { _replyString = _reader_.readLine(); if (_replyString == null) throw new NNTPConnectionClosedException( "Connection closed without indication."); // In case we run into an anomaly we don't want fatal index exceptions // to be thrown. if (_replyString.length() < 3) throw new MalformedServerReplyException( "Truncated server reply: " + _replyString); try { _replyCode = Integer.parseInt(_replyString.substring(0, 3)); } catch (NumberFormatException e) { throw new MalformedServerReplyException( "Could not parse response code.\nServer Reply: " + _replyString); } if (_commandSupport_.getListenerCount() > 0) _commandSupport_.fireReplyReceived(_replyCode, _replyString + SocketClient.NETASCII_EOL); if (_replyCode == NNTPReply.SERVICE_DISCONTINUED) throw new NNTPConnectionClosedException( "NNTP response 400 received. Server closed connection."); } /*** * Initiates control connections and gets initial reply, determining * if the client is allowed to post to the server. Initializes * {@link #_reader_} and {@link #_writer_} to wrap * {@link SocketClient#_input_} and {@link SocketClient#_output_}. ***/ protected void _connectAction_() throws IOException { super._connectAction_(); _reader_ = new BufferedReader(new InputStreamReader(_input_, __DEFAULT_ENCODING)); _writer_ = new BufferedWriter(new OutputStreamWriter(_output_, __DEFAULT_ENCODING)); __getReply(); _isAllowedToPost = (_replyCode == NNTPReply.SERVER_READY_POSTING_ALLOWED); } /*** * Adds a ProtocolCommandListener. Delegates this task to * {@link #_commandSupport_ _commandSupport_ }. * <p> * @param listener The ProtocolCommandListener to add. ***/ public void addProtocolCommandListener(ProtocolCommandListener listener) { _commandSupport_.addProtocolCommandListener(listener); } /*** * Removes a ProtocolCommandListener. Delegates this task to * {@link #_commandSupport_ _commandSupport_ }. * <p> * @param listener The ProtocolCommandListener to remove. ***/ public void removeProtocolCommandListener(ProtocolCommandListener listener) { _commandSupport_.removeProtocolCommandListener(listener); } /*** * Closes the connection to the NNTP server and sets to null * some internal data so that the memory may be reclaimed by the * garbage collector. The reply text and code information from the * last command is voided so that the memory it used may be reclaimed. * <p> * @exception IOException If an error occurs while disconnecting. ***/ public void disconnect() throws IOException { super.disconnect(); _reader_ = null; _writer_ = null; _replyString = null; _isAllowedToPost = false; } /*** * Indicates whether or not the client is allowed to post articles to * the server it is currently connected to. * <p> * @return True if the client can post articles to the server, false * otherwise. ***/ public boolean isAllowedToPost() { return _isAllowedToPost; } /*** * Sends an NNTP command to the server, waits for a reply and returns the * numerical response code. After invocation, for more detailed * information, the actual reply text can be accessed by calling * {@link #getReplyString getReplyString }. * <p> * @param command The text representation of the NNTP command to send. * @param args The arguments to the NNTP command. If this parameter is * set to null, then the command is sent with no argument. * @return The integer value of the NNTP reply code returned by the server * in response to the command. * @exception NNTPConnectionClosedException * If the NNTP server prematurely closes the connection as a result * of the client being idle or some other reason causing the server * to send NNTP reply code 400. This exception may be caught either * as an IOException or independently as itself. * @exception IOException If an I/O error occurs while either sending the * command or receiving the server reply. ***/ public int sendCommand(String command, String args) throws IOException { String message; __commandBuffer.setLength(0); __commandBuffer.append(command); if (args != null) { __commandBuffer.append(' '); __commandBuffer.append(args); } __commandBuffer.append(SocketClient.NETASCII_EOL); _writer_.write(message = __commandBuffer.toString()); _writer_.flush(); if (_commandSupport_.getListenerCount() > 0) _commandSupport_.fireCommandSent(command, message); __getReply(); return _replyCode; } /*** * Sends an NNTP command to the server, waits for a reply and returns the * numerical response code. After invocation, for more detailed * information, the actual reply text can be accessed by calling * {@link #getReplyString getReplyString }. * <p> * @param command The NNTPCommand constant corresponding to the NNTP command * to send. * @param args The arguments to the NNTP command. If this parameter is * set to null, then the command is sent with no argument. * @return The integer value of the NNTP reply code returned by the server * in response to the command. * in response to the command. * @exception NNTPConnectionClosedException * If the NNTP server prematurely closes the connection as a result * of the client being idle or some other reason causing the server * to send NNTP reply code 400. This exception may be caught either * as an IOException or independently as itself. * @exception IOException If an I/O error occurs while either sending the * command or receiving the server reply. ***/ public int sendCommand(int command, String args) throws IOException { return sendCommand(NNTPCommand._commands[command], args); } /*** * Sends an NNTP command with no arguments to the server, waits for a * reply and returns the numerical response code. After invocation, for * more detailed information, the actual reply text can be accessed by * calling {@link #getReplyString getReplyString }. * <p> * @param command The text representation of the NNTP command to send. * @return The integer value of the NNTP reply code returned by the server * in response to the command. * in response to the command. * @exception NNTPConnectionClosedException * If the NNTP server prematurely closes the connection as a result * of the client being idle or some other reason causing the server * to send NNTP reply code 400. This exception may be caught either * as an IOException or independently as itself. * @exception IOException If an I/O error occurs while either sending the * command or receiving the server reply. ***/ public int sendCommand(String command) throws IOException { return sendCommand(command, null); } /*** * Sends an NNTP command with no arguments to the server, waits for a * reply and returns the numerical response code. After invocation, for * more detailed information, the actual reply text can be accessed by * calling {@link #getReplyString getReplyString }. * <p> * @param command The NNTPCommand constant corresponding to the NNTP command * to send.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?