|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.net.SocketClient org.apache.commons.net.nntp.NNTP org.apache.commons.net.nntp.NNTPClient
public class NNTPClient
NNTPClient encapsulates all the functionality necessary to post and
retrieve articles from an NNTP server. As with all classes derived
from SocketClient
,
you must first connect to the server with
connect
before doing anything, and finally
disconnect()
after you're completely finished interacting with the server.
Remember that the
isAllowedToPost()
method is defined in
NNTP
.
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
NNTPReply.SERVICE_DISCONTINUED
response to a command.
When that occurs, the NNTP class method encountering that reply will throw
an NNTPConnectionClosedException
.
NNTPConectionClosedException
is a subclass of IOException
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 IOException
catch block. When you encounter an
NNTPConnectionClosedException
, you must disconnect the connection with
disconnect()
to properly clean up the
system resources used by NNTP. Before disconnecting, you may check the
last reply code and text with
getReplyCode
and
getReplyString
.
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
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.
NNTP
,
NNTPConnectionClosedException
,
MalformedServerReplyException
Field Summary |
---|
Fields inherited from class org.apache.commons.net.nntp.NNTP |
---|
_commandSupport_, _reader_, _writer_, DEFAULT_PORT |
Fields inherited from class org.apache.commons.net.SocketClient |
---|
_defaultPort_, _input_, _isConnected_, _output_, _socket_, _socketFactory_, _timeout_, NETASCII_EOL |
Constructor Summary | |
---|---|
NNTPClient()
|
Method Summary | |
---|---|
boolean |
authenticate(String username,
String password)
Log into a news server by sending the AUTHINFO USER/AUTHINFO PASS command sequence. |
boolean |
completePendingCommand()
There are a few NNTPClient methods that do not complete the entire sequence of NNTP commands to complete a transaction. |
Writer |
forwardArticle(String articleId)
|
String |
listHelp()
List the command help from the server. |
String[] |
listNewNews(NewGroupsOrNewsQuery query)
List all new articles added to the NNTP server since a particular date subject to the conditions of the specified query. |
NewsgroupInfo[] |
listNewNewsgroups(NewGroupsOrNewsQuery query)
List all new newsgroups added to the NNTP server since a particular date subject to the conditions of the specified query. |
NewsgroupInfo[] |
listNewsgroups()
List all newsgroups served by the NNTP server. |
NewsgroupInfo[] |
listNewsgroups(String wildmat)
An overloaded listNewsgroups() command that allows us to specify with a pattern what groups we want to list. |
boolean |
logout()
Logs out of the news server gracefully by sending the QUIT command. |
Writer |
postArticle()
Post an article to the NNTP server. |
Reader |
retrieveArticle()
Same as retrieveArticle(null) |
Reader |
retrieveArticle(int articleNumber)
Same as retrieveArticle(articleNumber, null) |
Reader |
retrieveArticle(int articleNumber,
ArticlePointer pointer)
Retrieves an article from the currently selected newsgroup. |
Reader |
retrieveArticle(String articleId)
Same as retrieveArticle(articleId, null) |
Reader |
retrieveArticle(String articleId,
ArticlePointer pointer)
Retrieves an article from the NNTP server. |
Reader |
retrieveArticleBody()
Same as retrieveArticleBody(null) |
Reader |
retrieveArticleBody(int articleNumber)
Same as retrieveArticleBody(articleNumber, null) |
Reader |
retrieveArticleBody(int articleNumber,
ArticlePointer pointer)
Retrieves an article body from the currently selected newsgroup. |
Reader |
retrieveArticleBody(String articleId)
Same as retrieveArticleBody(articleId, null) |
Reader |
retrieveArticleBody(String articleId,
ArticlePointer pointer)
Retrieves an article body from the NNTP server. |
Reader |
retrieveArticleHeader()
Same as retrieveArticleHeader(null) |
Reader |
retrieveArticleHeader(int articleNumber)
Same as retrieveArticleHeader(articleNumber, null) |
Reader |
retrieveArticleHeader(int articleNumber,
ArticlePointer pointer)
Retrieves an article header from the currently selected newsgroup. |
Reader |
retrieveArticleHeader(String articleId)
Same as retrieveArticleHeader(articleId, null) |
Reader |
retrieveArticleHeader(String articleId,
ArticlePointer pointer)
Retrieves an article header from the NNTP server. |
Reader |
retrieveArticleInfo(int articleNumber)
Return article headers for a specified post. |
Reader |
retrieveArticleInfo(int lowArticleNumber,
int highArticleNumber)
Return article headers for all articles between lowArticleNumber and highArticleNumber, inclusively. |
Reader |
retrieveHeader(String header,
int articleNumber)
Return an article header for a specified post. |
Reader |
retrieveHeader(String header,
int lowArticleNumber,
int highArticleNumber)
Return an article header for all articles between lowArticleNumber and highArticleNumber, inclusively. |
boolean |
selectArticle(ArticlePointer pointer)
Same as selectArticle(null, articleId) . |
boolean |
selectArticle(int articleNumber)
Same as selectArticle(articleNumber, null) |
boolean |
selectArticle(int articleNumber,
ArticlePointer pointer)
Select an article in the currently selected newsgroup by its number. |
boolean |
selectArticle(String articleId)
Same as selectArticle(articleId, null) |
boolean |
selectArticle(String articleId,
ArticlePointer pointer)
Select an article by its unique identifier (including enclosing < and >) and return its article number and id through the pointer parameter. |
boolean |
selectNewsgroup(String newsgroup)
Same as selectNewsgroup(newsgroup, null) |
boolean |
selectNewsgroup(String newsgroup,
NewsgroupInfo info)
Select the specified newsgroup to be the target of for future article retrieval and posting operations. |
boolean |
selectNextArticle()
Same as selectNextArticle(null) |
boolean |
selectNextArticle(ArticlePointer pointer)
Select the article following the currently selected article in the currently selected newsgroup and return its number and unique id through the pointer parameter. |
boolean |
selectPreviousArticle()
Same as selectPreviousArticle(null) |
boolean |
selectPreviousArticle(ArticlePointer pointer)
Select the article preceeding the currently selected article in the currently selected newsgroup and return its number and unique id through the pointer parameter. |
Methods inherited from class org.apache.commons.net.nntp.NNTP |
---|
_connectAction_, addProtocolCommandListener, article, article, article, authinfoPass, authinfoUser, body, body, body, disconnect, getReply, getReplyCode, getReplyString, group, head, head, head, help, ihave, isAllowedToPost, last, list, listActive, newgroups, newnews, next, post, quit, removeProtocolCommandListener, sendCommand, sendCommand, sendCommand, sendCommand, stat, stat, stat, xhdr, xover |
Methods inherited from class org.apache.commons.net.SocketClient |
---|
connect, connect, connect, connect, connect, connect, getDefaultPort, getDefaultTimeout, getLocalAddress, getLocalPort, getRemoteAddress, getRemotePort, getSoLinger, getSoTimeout, getTcpNoDelay, isConnected, setDefaultPort, setDefaultTimeout, setSocketFactory, setSoLinger, setSoTimeout, setTcpNoDelay, verifyRemote |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public NNTPClient()
Method Detail |
---|
public Reader retrieveArticle(String articleId, ArticlePointer pointer) throws IOException
articleId
field of the ArticlePointer cannot always be trusted because some
NNTP servers do not correctly follow the RFC 977 reply format.
A DotTerminatedMessageReader is returned from which the article can be read. If the article does not exist, null is returned.
You must not issue any commands to the NNTP server (i.e., call any other methods) until you finish reading the message from the returned Reader instance. The NNTP protocol uses the same stream for issuing commands as it does for returning results. Therefore the returned Reader actually reads directly from the NNTP connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
articleId
- The unique article identifier of the article to
retrieve. If this parameter is null, the currently selected
article is retrieved.pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Reader retrieveArticle(String articleId) throws IOException
retrieveArticle(articleId, null)
IOException
public Reader retrieveArticle() throws IOException
retrieveArticle(null)
IOException
public Reader retrieveArticle(int articleNumber, ArticlePointer pointer) throws IOException
articleId
field of the ArticlePointer cannot always be trusted because some
NNTP servers do not correctly follow the RFC 977 reply format.
A DotTerminatedMessageReader is returned from which the article can be read. If the article does not exist, null is returned.
You must not issue any commands to the NNTP server (i.e., call any other methods) until you finish reading the message from the returned Reader instance. The NNTP protocol uses the same stream for issuing commands as it does for returning results. Therefore the returned Reader actually reads directly from the NNTP connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
articleNumber
- The number of the the article to
retrieve.pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Reader retrieveArticle(int articleNumber) throws IOException
retrieveArticle(articleNumber, null)
IOException
public Reader retrieveArticleHeader(String articleId, ArticlePointer pointer) throws IOException
articleId
field of the ArticlePointer cannot always be trusted because some
NNTP servers do not correctly follow the RFC 977 reply format.
A DotTerminatedMessageReader is returned from which the article can be read. If the article does not exist, null is returned.
You must not issue any commands to the NNTP server (i.e., call any other methods) until you finish reading the message from the returned Reader instance. The NNTP protocol uses the same stream for issuing commands as it does for returning results. Therefore the returned Reader actually reads directly from the NNTP connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
articleId
- The unique article identifier of the article whose
header is being retrieved. If this parameter is null, the
header of the currently selected article is retrieved.pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Reader retrieveArticleHeader(String articleId) throws IOException
retrieveArticleHeader(articleId, null)
IOException
public Reader retrieveArticleHeader() throws IOException
retrieveArticleHeader(null)
IOException
public Reader retrieveArticleHeader(int articleNumber, ArticlePointer pointer) throws IOException
articleId
field of the ArticlePointer cannot always be trusted because some
NNTP servers do not correctly follow the RFC 977 reply format.
A DotTerminatedMessageReader is returned from which the article can be read. If the article does not exist, null is returned.
You must not issue any commands to the NNTP server (i.e., call any other methods) until you finish reading the message from the returned Reader instance. The NNTP protocol uses the same stream for issuing commands as it does for returning results. Therefore the returned Reader actually reads directly from the NNTP connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
articleNumber
- The number of the the article whose header is
being retrieved.pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Reader retrieveArticleHeader(int articleNumber) throws IOException
retrieveArticleHeader(articleNumber, null)
IOException
public Reader retrieveArticleBody(String articleId, ArticlePointer pointer) throws IOException
articleId
field of the ArticlePointer cannot always be trusted because some
NNTP servers do not correctly follow the RFC 977 reply format.
A DotTerminatedMessageReader is returned from which the article can be read. If the article does not exist, null is returned.
You must not issue any commands to the NNTP server (i.e., call any other methods) until you finish reading the message from the returned Reader instance. The NNTP protocol uses the same stream for issuing commands as it does for returning results. Therefore the returned Reader actually reads directly from the NNTP connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
articleId
- The unique article identifier of the article whose
body is being retrieved. If this parameter is null, the
body of the currently selected article is retrieved.pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Reader retrieveArticleBody(String articleId) throws IOException
retrieveArticleBody(articleId, null)
IOException
public Reader retrieveArticleBody() throws IOException
retrieveArticleBody(null)
IOException
public Reader retrieveArticleBody(int articleNumber, ArticlePointer pointer) throws IOException
articleId
field of the ArticlePointer cannot always be trusted because some
NNTP servers do not correctly follow the RFC 977 reply format.
A DotTerminatedMessageReader is returned from which the article can be read. If the article does not exist, null is returned.
You must not issue any commands to the NNTP server (i.e., call any other methods) until you finish reading the message from the returned Reader instance. The NNTP protocol uses the same stream for issuing commands as it does for returning results. Therefore the returned Reader actually reads directly from the NNTP connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
articleNumber
- The number of the the article whose body is
being retrieved.pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Reader retrieveArticleBody(int articleNumber) throws IOException
retrieveArticleBody(articleNumber, null)
IOException
public boolean selectNewsgroup(String newsgroup, NewsgroupInfo info) throws IOException
newsgroup
- The newsgroup to select.info
- A parameter through which the newsgroup information of
the selected newsgroup contained in the server reply is returned.
Set this to null if you do not desire this information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean selectNewsgroup(String newsgroup) throws IOException
selectNewsgroup(newsgroup, null)
IOException
public String listHelp() throws IOException
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean selectArticle(String articleId, ArticlePointer pointer) throws IOException
articleId
- The unique article identifier of the article that
is being selectedd. If this parameter is null, the
body of the current article is selectedpointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean selectArticle(String articleId) throws IOException
selectArticle(articleId, null)
IOException
public boolean selectArticle(ArticlePointer pointer) throws IOException
selectArticle(null, articleId)
. Useful
for retrieving the current article number.
IOException
public boolean selectArticle(int articleNumber, ArticlePointer pointer) throws IOException
articleNumber
- The number of the article to select from the
currently selected newsgroup.pointer
- A parameter through which to return the article's
number and unique id. Although the articleId field cannot always
be trusted because of server deviations from RFC 977 reply formats,
we haven't found a server that misformats this information in response
to this particular command. You may set this parameter to null if
you do not desire to retrieve the returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean selectArticle(int articleNumber) throws IOException
selectArticle(articleNumber, null)
IOException
public boolean selectPreviousArticle(ArticlePointer pointer) throws IOException
selectArticle(pointer.articleNumber, pointer)
immediately
afterward.
pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean selectPreviousArticle() throws IOException
selectPreviousArticle(null)
IOException
public boolean selectNextArticle(ArticlePointer pointer) throws IOException
selectArticle(pointer.articleNumber, pointer)
immediately
afterward.
pointer
- A parameter through which to return the article's
number and unique id. The articleId field cannot always be trusted
because of server deviations from RFC 977 reply formats. You may
set this parameter to null if you do not desire to retrieve the
returned article information.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean selectNextArticle() throws IOException
selectNextArticle(null)
IOException
public NewsgroupInfo[] listNewsgroups() throws IOException
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public NewsgroupInfo[] listNewsgroups(String wildmat) throws IOException
wildmat
- a pseudo-regex pattern (cf. RFC 2980)
IOException
public NewsgroupInfo[] listNewNewsgroups(NewGroupsOrNewsQuery query) throws IOException
query
- The query restricting how to search for new newsgroups.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public String[] listNewNews(NewGroupsOrNewsQuery query) throws IOException
query
- The query restricting how to search for new news. You
must add at least one newsgroup to the query.
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean completePendingCommand() throws IOException
For example
writer = client.postArticle(); if(writer == null) // failure return false; header = new SimpleNNTPHeader("foobar@foo.com", "Just testing"); header.addNewsgroup("alt.test"); writer.write(header.toString()); writer.write("This is just a test"); writer.close(); if(!client.completePendingCommand()) // failure return false;
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.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Writer postArticle() throws IOException
isAllowedToPost()
before trying to post. However, a posting
attempt can fail due to malformed headers.
You must not issue any commands to the NNTP server (i.e., call any (other methods) until you finish writing to the returned Writer instance and close it. The NNTP protocol uses the same stream for issuing commands as it does for returning results. Therefore the returned Writer actually writes directly to the NNTP connection. After you close the writer, you can execute new commands. If you do not follow these requirements your program will not work properly.
Different NNTP servers will require different header formats, but
you can use the provided
SimpleNNTPHeader
class to construct the bare minimum acceptable header for most
news readers. To construct more complicated headers you should
refer to RFC 822. When the Java Mail API is finalized, you will be
able to use it to compose fully compliant Internet text messages.
The DotTerminatedMessageWriter takes care of doubling line-leading
dots and ending the message with a single dot upon closing, so all
you have to worry about is writing the header and the message.
Upon closing the returned Writer, you need to call
completePendingCommand()
to finalize the posting and verify its success or failure from
the server reply.
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public Writer forwardArticle(String articleId) throws IOException
IOException
public boolean logout() throws IOException
IOException
- If an I/O error occurs while either sending a
command to the server or receiving a reply from the server.public boolean authenticate(String username, String password) throws IOException
username
- a valid usernamepassword
- the corresponding password
IOException
public Reader retrieveArticleInfo(int articleNumber) throws IOException
articleNumber
- the article to retrieve headers for
IOException
public Reader retrieveArticleInfo(int lowArticleNumber, int highArticleNumber) throws IOException
lowArticleNumber
- highArticleNumber
-
IOException
public Reader retrieveHeader(String header, int articleNumber) throws IOException
header
- the header to retrievearticleNumber
- the article to retrieve the header for
IOException
public Reader retrieveHeader(String header, int lowArticleNumber, int highArticleNumber) throws IOException
header
- lowArticleNumber
- highArticleNumber
-
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |