View Javadoc
1 package org.apache.commons.net.nntp; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Commons" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.BufferedReader; 58 import java.io.BufferedWriter; 59 import java.io.IOException; 60 import java.io.InputStreamReader; 61 import java.io.OutputStreamWriter; 62 import org.apache.commons.net.MalformedServerReplyException; 63 import org.apache.commons.net.ProtocolCommandSupport; 64 import org.apache.commons.net.ProtocolCommandListener; 65 import org.apache.commons.net.SocketClient; 66 67 /**** 68 * The NNTP class is not meant to be used by itself and is provided 69 * only so that you may easily implement your own NNTP client if 70 * you so desire. If you have no need to perform your own implementation, 71 * you should use <a href="org.apache.commons.net.nntp.NNTPClient.html">NNTPClient</a>. 72 * The NNTP class is made public to provide access to various NNTP constants 73 * and to make it easier for adventurous programmers (or those with special 74 * needs) to interact with the NNTP protocol and implement their own clients. 75 * A set of methods with names corresponding to the NNTP command names are 76 * provided to facilitate this interaction. 77 * <p> 78 * You should keep in mind that the NNTP server may choose to prematurely 79 * close a connection if the client has been idle for longer than a 80 * given time period or if the server is being shutdown by the operator or 81 * some other reason. The NNTP class will detect a 82 * premature NNTP server connection closing when it receives a 83 * <a href="org.apache.commons.net.nntp.NNTPReply.html#SERVICE_DISCONTINUED"> 84 * NNTPReply.SERVICE_DISCONTINUED </a> response to a command. 85 * When that occurs, the NNTP class method encountering that reply will throw 86 * an <a href="org.apache.commons.net.nntp.NNTPConnectionClosedException.html"> 87 * NNTPConnectionClosedException </a>. 88 * <code>NNTPConectionClosedException</code> 89 * is a subclass of <code> IOException </code> and therefore need not be 90 * caught separately, but if you are going to catch it separately, its 91 * catch block must appear before the more general <code> IOException </code> 92 * catch block. When you encounter an 93 * <a href="org.apache.commons.net.nntp.NNTPConnectionClosedException.html"> 94 * NNTPConnectionClosedException </a>, you must disconnect the connection with 95 * <a href="#disconnect"> disconnect() </a> to properly clean up the 96 * system resources used by NNTP. Before disconnecting, you may check the 97 * last reply code and text with 98 * <a href="#getReplyCode"> getReplyCode </a> and 99 * <a href="#getReplyString"> getReplyString </a>. 100 * <p> 101 * Rather than list it separately for each method, we mention here that 102 * every method communicating with the server and throwing an IOException 103 * can also throw a 104 * <a href="org.apache.commons.net.MalformedServerReplyException.html"> 105 * MalformedServerReplyException </a>, which is a subclass 106 * of IOException. A MalformedServerReplyException will be thrown when 107 * the reply received from the server deviates enough from the protocol 108 * specification that it cannot be interpreted in a useful manner despite 109 * attempts to be as lenient as possible. 110 * <p> 111 * <p> 112 * @author Daniel F. Savarese 113 * @see NNTPClient 114 * @see NNTPConnectionClosedException 115 * @see org.apache.commons.net.MalformedServerReplyException 116 ***/ 117 118 public class NNTP extends SocketClient 119 { 120 /**** The default NNTP port. Its value is 119 according to RFC 977. ***/ 121 public static final int DEFAULT_PORT = 119; 122 123 private StringBuffer __commandBuffer; 124 125 boolean _isAllowedToPost; 126 BufferedWriter _writer; 127 BufferedReader _reader; 128 int _replyCode; 129 String _replyString; 130 131 /**** 132 * A ProtocolCommandSupport object used to manage the registering of 133 * ProtocolCommandListeners and te firing of ProtocolCommandEvents. 134 ***/ 135 protected ProtocolCommandSupport _commandSupport_; 136 137 /**** 138 * The default NNTP constructor. Sets the default port to 139 * <code>DEFAULT_PORT</code> and initializes internal data structures 140 * for saving NNTP reply information. 141 ***/ 142 public NNTP() 143 { 144 setDefaultPort(DEFAULT_PORT); 145 __commandBuffer = new StringBuffer(); 146 _replyString = null; 147 _reader = null; 148 _writer = null; 149 _isAllowedToPost = false; 150 _commandSupport_ = new ProtocolCommandSupport(this); 151 } 152 153 private void __getReply() throws IOException 154 { 155 _replyString = _reader.readLine(); 156 157 if (_replyString == null) 158 throw new NNTPConnectionClosedException( 159 "Connection closed without indication."); 160 161 // In case we run into an anomaly we don't want fatal index exceptions 162 // to be thrown. 163 if (_replyString.length() < 3) 164 throw new MalformedServerReplyException( 165 "Truncated server reply: " + _replyString); 166 try 167 { 168 _replyCode = Integer.parseInt(_replyString.substring(0, 3)); 169 } 170 catch (NumberFormatException e) 171 { 172 throw new MalformedServerReplyException( 173 "Could not parse response code.\nServer Reply: " + _replyString); 174 } 175 176 if (_commandSupport_.getListenerCount() > 0) 177 _commandSupport_.fireReplyReceived(_replyCode, _replyString + 178 SocketClient.NETASCII_EOL); 179 180 if (_replyCode == NNTPReply.SERVICE_DISCONTINUED) 181 throw new NNTPConnectionClosedException( 182 "NNTP response 400 received. Server closed connection."); 183 } 184 185 /**** 186 * Initiates control connections and gets initial reply, determining 187 * if the client is allowed to post to the server. 188 ***/ 189 protected void _connectAction_() throws IOException 190 { 191 super._connectAction_(); 192 _reader = 193 new BufferedReader(new InputStreamReader(_input_)); 194 _writer = 195 new BufferedWriter(new OutputStreamWriter(_output_)); 196 __getReply(); 197 198 _isAllowedToPost = (_replyCode == NNTPReply.SERVER_READY_POSTING_ALLOWED); 199 } 200 201 /**** 202 * Adds a ProtocolCommandListener. Delegates this task to 203 * <a href="#_commandSupport_"> _commandSupport_ </a>. 204 * <p> 205 * @param listener The ProtocolCommandListener to add. 206 ***/ 207 public void addProtocolCommandListener(ProtocolCommandListener listener) 208 { 209 _commandSupport_.addProtocolCommandListener(listener); 210 } 211 212 /**** 213 * Removes a ProtocolCommandListener. Delegates this task to 214 * <a href="#_commandSupport_"> _commandSupport_ </a>. 215 * <p> 216 * @param listener The ProtocolCommandListener to remove. 217 ***/ 218 public void removeProtocolCommandListener(ProtocolCommandListener listener) 219 { 220 _commandSupport_.removeProtocolCommandListener(listener); 221 } 222 223 /**** 224 * Closes the connection to the NNTP server and sets to null 225 * some internal data so that the memory may be reclaimed by the 226 * garbage collector. The reply text and code information from the 227 * last command is voided so that the memory it used may be reclaimed. 228 * <p> 229 * @exception IOException If an error occurs while disconnecting. 230 ***/ 231 public void disconnect() throws IOException 232 { 233 super.disconnect(); 234 _reader = null; 235 _writer = null; 236 _replyString = null; 237 _isAllowedToPost = false; 238 } 239 240 241 /**** 242 * Indicates whether or not the client is allowed to post articles to 243 * the server it is currently connected to. 244 * <p> 245 * @return True if the client can post articles to the server, false 246 * otherwise. 247 ***/ 248 public boolean isAllowedToPost() 249 { 250 return _isAllowedToPost; 251 } 252 253 254 /**** 255 * Sends an NNTP command to the server, waits for a reply and returns the 256 * numerical response code. After invocation, for more detailed 257 * information, the actual reply text can be accessed by calling 258 * <a href="#getReplyString"> getReplyString </a>. 259 * <p> 260 * @param command The text representation of the NNTP command to send. 261 * @param args The arguments to the NNTP command. If this parameter is 262 * set to null, then the command is sent with no argument. 263 * @return The integer value of the NNTP reply code returned by the server 264 * in response to the command. 265 * @exception NNTPConnectionClosedException 266 * If the NNTP server prematurely closes the connection as a result 267 * of the client being idle or some other reason causing the server 268 * to send NNTP reply code 400. This exception may be caught either 269 * as an IOException or independently as itself. 270 * @exception IOException If an I/O error occurs while either sending the 271 * command or receiving the server reply. 272 ***/ 273 public int sendCommand(String command, String args) throws IOException 274 { 275 String message; 276 277 __commandBuffer.setLength(0); 278 __commandBuffer.append(command); 279 280 if (args != null) 281 { 282 __commandBuffer.append(' '); 283 __commandBuffer.append(args); 284 } 285 __commandBuffer.append(SocketClient.NETASCII_EOL); 286 287 _writer.write(message = __commandBuffer.toString()); 288 _writer.flush(); 289 290 if (_commandSupport_.getListenerCount() > 0) 291 _commandSupport_.fireCommandSent(command, message); 292 293 __getReply(); 294 return _replyCode; 295 } 296 297 298 /**** 299 * Sends an NNTP command to the server, waits for a reply and returns the 300 * numerical response code. After invocation, for more detailed 301 * information, the actual reply text can be accessed by calling 302 * <a href="#getReplyString"> getReplyString </a>. 303 * <p> 304 * @param command The NNTPCommand constant corresponding to the NNTP command 305 * to send. 306 * @param args The arguments to the NNTP command. If this parameter is 307 * set to null, then the command is sent with no argument. 308 * @return The integer value of the NNTP reply code returned by the server 309 * in response to the command. 310 * in response to the command. 311 * @exception NNTPConnectionClosedException 312 * If the NNTP server prematurely closes the connection as a result 313 * of the client being idle or some other reason causing the server 314 * to send NNTP reply code 400. This exception may be caught either 315 * as an IOException or independently as itself. 316 * @exception IOException If an I/O error occurs while either sending the 317 * command or receiving the server reply. 318 ***/ 319 public int sendCommand(int command, String args) throws IOException 320 { 321 return sendCommand(NNTPCommand._commands[command], args); 322 } 323 324 325 /**** 326 * Sends an NNTP command with no arguments to the server, waits for a 327 * reply and returns the numerical response code. After invocation, for 328 * more detailed information, the actual reply text can be accessed by 329 * calling <a href="#getReplyString"> getReplyString </a>. 330 * <p> 331 * @param command The text representation of the NNTP command to send. 332 * @return The integer value of the NNTP reply code returned by the server 333 * in response to the command. 334 * in response to the command. 335 * @exception NNTPConnectionClosedException 336 * If the NNTP server prematurely closes the connection as a result 337 * of the client being idle or some other reason causing the server 338 * to send NNTP reply code 400. This exception may be caught either 339 * as an IOException or independently as itself. 340 * @exception IOException If an I/O error occurs while either sending the 341 * command or receiving the server reply. 342 ***/ 343 public int sendCommand(String command) throws IOException 344 { 345 return sendCommand(command, null); 346 } 347 348 349 /**** 350 * Sends an NNTP command with no arguments to the server, waits for a 351 * reply and returns the numerical response code. After invocation, for 352 * more detailed information, the actual reply text can be accessed by 353 * calling <a href="#getReplyString"> getReplyString </a>. 354 * <p> 355 * @param command The NNTPCommand constant corresponding to the NNTP command 356 * to send. 357 * @return The integer value of the NNTP reply code returned by the server 358 * in response to the command. 359 * in response to the command. 360 * @exception NNTPConnectionClosedException 361 * If the NNTP server prematurely closes the connection as a result 362 * of the client being idle or some other reason causing the server 363 * to send NNTP reply code 400. This exception may be caught either 364 * as an IOException or independently as itself. 365 * @exception IOException If an I/O error occurs while either sending the 366 * command or receiving the server reply. 367 ***/ 368 public int sendCommand(int command) throws IOException 369 { 370 return sendCommand(command, null); 371 } 372 373 374 /**** 375 * Returns the integer value of the reply code of the last NNTP reply. 376 * You will usually only use this method after you connect to the 377 * NNTP server to check that the connection was successful since 378 * <code> connect </code> is of type void. 379 * <p> 380 * @return The integer value of the reply code of the last NNTP reply. 381 ***/ 382 public int getReplyCode() 383 { 384 return _replyCode; 385 } 386 387 /**** 388 * Fetches a reply from the NNTP server and returns the integer reply 389 * code. After calling this method, the actual reply text can be accessed 390 * from <a href="#getReplyString"> getReplyString </a>. Only use this 391 * method if you are implementing your own NNTP client or if you need to 392 * fetch a secondary response from the NNTP server. 393 * <p> 394 * @return The integer value of the reply code of the fetched NNTP reply. 395 * in response to the command. 396 * @exception NNTPConnectionClosedException 397 * If the NNTP server prematurely closes the connection as a result 398 * of the client being idle or some other reason causing the server 399 * to send NNTP reply code 400. This exception may be caught either 400 * as an IOException or independently as itself. 401 * @exception IOException If an I/O error occurs while 402 * receiving the server reply. 403 ***/ 404 public int getReply() throws IOException 405 { 406 __getReply(); 407 return _replyCode; 408 } 409 410 411 /**** 412 * Returns the entire text of the last NNTP server response exactly 413 * as it was received, not including the end of line marker. 414 * <p> 415 * @return The entire text from the last NNTP response as a String. 416 ***/ 417 public String getReplyString() 418 { 419 return _replyString; 420 } 421 422 423 /**** 424 * A convenience method to send the NNTP ARTICLE command to the server, 425 * receive the initial reply, and return the reply code. 426 * <p> 427 * @param messageId The message identifier of the requested article, 428 * including the encapsulating < and > characters. 429 * @return The reply code received from the server. 430 * @exception NNTPConnectionClosedException 431 * If the NNTP server prematurely closes the connection as a result 432 * of the client being idle or some other reason causing the server 433 * to send NNTP reply code 400. This exception may be caught either 434 * as an IOException or independently as itself. 435 * @exception IOException If an I/O error occurs while either sending the 436 * command or receiving the server reply. 437 ***/ 438 public int article(String messageId) throws IOException 439 { 440 return sendCommand(NNTPCommand.ARTICLE, messageId); 441 } 442 443 /**** 444 * A convenience method to send the NNTP ARTICLE command to the server, 445 * receive the initial reply, and return the reply code. 446 * <p> 447 * @param articleNumber The number of the article to request from the 448 * currently selected newsgroup. 449 * @return The reply code received from the server. 450 * @exception NNTPConnectionClosedException 451 * If the NNTP server prematurely closes the connection as a result 452 * of the client being idle or some other reason causing the server 453 * to send NNTP reply code 400. This exception may be caught either 454 * as an IOException or independently as itself. 455 * @exception IOException If an I/O error occurs while either sending the 456 * command or receiving the server reply. 457 ***/ 458 public int article(int articleNumber) throws IOException 459 { 460 return sendCommand(NNTPCommand.ARTICLE, Integer.toString(articleNumber)); 461 } 462 463 /**** 464 * A convenience method to send the NNTP ARTICLE command to the server, 465 * receive the initial reply, and return the reply code. 466 * <p> 467 * @return The reply code received from the server. 468 * @exception NNTPConnectionClosedException 469 * If the NNTP server prematurely closes the connection as a result 470 * of the client being idle or some other reason causing the server 471 * to send NNTP reply code 400. This exception may be caught either 472 * as an IOException or independently as itself. 473 * @exception IOException If an I/O error occurs while either sending the 474 * command or receiving the server reply. 475 ***/ 476 public int article() throws IOException 477 { 478 return sendCommand(NNTPCommand.ARTICLE); 479 } 480 481 482 483 /**** 484 * A convenience method to send the NNTP BODY command to the server, 485 * receive the initial reply, and return the reply code. 486 * <p> 487 * @param messageId The message identifier of the requested article, 488 * including the encapsulating < and > characters. 489 * @return The reply code received from the server. 490 * @exception NNTPConnectionClosedException 491 * If the NNTP server prematurely closes the connection as a result 492 * of the client being idle or some other reason causing the server 493 * to send NNTP reply code 400. This exception may be caught either 494 * as an IOException or independently as itself. 495 * @exception IOException If an I/O error occurs while either sending the 496 * command or receiving the server reply. 497 ***/ 498 public int body(String messageId) throws IOException 499 { 500 return sendCommand(NNTPCommand.BODY, messageId); 501 } 502 503 /**** 504 * A convenience method to send the NNTP BODY command to the server, 505 * receive the initial reply, and return the reply code. 506 * <p> 507 * @param articleNumber The number of the article to request from the 508 * currently selected newsgroup. 509 * @return The reply code received from the server. 510 * @exception NNTPConnectionClosedException 511 * If the NNTP server prematurely closes the connection as a result 512 * of the client being idle or some other reason causing the server 513 * to send NNTP reply code 400. This exception may be caught either 514 * as an IOException or independently as itself. 515 * @exception IOException If an I/O error occurs while either sending the 516 * command or receiving the server reply. 517 ***/ 518 public int body(int articleNumber) throws IOException 519 { 520 return sendCommand(NNTPCommand.BODY, Integer.toString(articleNumber)); 521 } 522 523 /**** 524 * A convenience method to send the NNTP BODY command to the server, 525 * receive the initial reply, and return the reply code. 526 * <p> 527 * @return The reply code received from the server. 528 * @exception NNTPConnectionClosedException 529 * If the NNTP server prematurely closes the connection as a result 530 * of the client being idle or some other reason causing the server 531 * to send NNTP reply code 400. This exception may be caught either 532 * as an IOException or independently as itself. 533 * @exception IOException If an I/O error occurs while either sending the 534 * command or receiving the server reply. 535 ***/ 536 public int body() throws IOException 537 { 538 return sendCommand(NNTPCommand.BODY); 539 } 540 541 542 543 /**** 544 * A convenience method to send the NNTP HEAD command to the server, 545 * receive the initial reply, and return the reply code. 546 * <p> 547 * @param messageId The message identifier of the requested article, 548 * including the encapsulating < and > characters. 549 * @return The reply code received from the server. 550 * @exception NNTPConnectionClosedException 551 * If the NNTP server prematurely closes the connection as a result 552 * of the client being idle or some other reason causing the server 553 * to send NNTP reply code 400. This exception may be caught either 554 * as an IOException or independently as itself. 555 * @exception IOException If an I/O error occurs while either sending the 556 * command or receiving the server reply. 557 ***/ 558 public int head(String messageId) throws IOException 559 { 560 return sendCommand(NNTPCommand.HEAD, messageId); 561 } 562 563 /**** 564 * A convenience method to send the NNTP HEAD command to the server, 565 * receive the initial reply, and return the reply code. 566 * <p> 567 * @param articleNumber The number of the article to request from the 568 * currently selected newsgroup. 569 * @return The reply code received from the server. 570 * @exception NNTPConnectionClosedException 571 * If the NNTP server prematurely closes the connection as a result 572 * of the client being idle or some other reason causing the server 573 * to send NNTP reply code 400. This exception may be caught either 574 * as an IOException or independently as itself. 575 * @exception IOException If an I/O error occurs while either sending the 576 * command or receiving the server reply. 577 ***/ 578 public int head(int articleNumber) throws IOException 579 { 580 return sendCommand(NNTPCommand.HEAD, Integer.toString(articleNumber)); 581 } 582 583 /**** 584 * A convenience method to send the NNTP HEAD command to the server, 585 * receive the initial reply, and return the reply code. 586 * <p> 587 * @return The reply code received from the server. 588 * @exception NNTPConnectionClosedException 589 * If the NNTP server prematurely closes the connection as a result 590 * of the client being idle or some other reason causing the server 591 * to send NNTP reply code 400. This exception may be caught either 592 * as an IOException or independently as itself. 593 * @exception IOException If an I/O error occurs while either sending the 594 * command or receiving the server reply. 595 ***/ 596 public int head() throws IOException 597 { 598 return sendCommand(NNTPCommand.HEAD); 599 } 600 601 602 603 /**** 604 * A convenience method to send the NNTP STAT command to the server, 605 * receive the initial reply, and return the reply code. 606 * <p> 607 * @param messageId The message identifier of the requested article, 608 * including the encapsulating < and > characters. 609 * @return The reply code received from the server. 610 * @exception NNTPConnectionClosedException 611 * If the NNTP server prematurely closes the connection as a result 612 * of the client being idle or some other reason causing the server 613 * to send NNTP reply code 400. This exception may be caught either 614 * as an IOException or independently as itself. 615 * @exception IOException If an I/O error occurs while either sending the 616 * command or receiving the server reply. 617 ***/ 618 public int stat(String messageId) throws IOException 619 { 620 return sendCommand(NNTPCommand.STAT, messageId); 621 } 622 623 /**** 624 * A convenience method to send the NNTP STAT command to the server, 625 * receive the initial reply, and return the reply code. 626 * <p> 627 * @param articleNumber The number of the article to request from the 628 * currently selected newsgroup. 629 * @return The reply code received from the server. 630 * @exception NNTPConnectionClosedException 631 * If the NNTP server prematurely closes the connection as a result 632 * of the client being idle or some other reason causing the server 633 * to send NNTP reply code 400. This exception may be caught either 634 * as an IOException or independently as itself. 635 * @exception IOException If an I/O error occurs while either sending the 636 * command or receiving the server reply. 637 ***/ 638 public int stat(int articleNumber) throws IOException 639 { 640 return sendCommand(NNTPCommand.STAT, Integer.toString(articleNumber)); 641 } 642 643 /**** 644 * A convenience method to send the NNTP STAT command to the server, 645 * receive the initial reply, and return the reply code. 646 * <p> 647 * @return The reply code received from the server. 648 * @exception NNTPConnectionClosedException 649 * If the NNTP server prematurely closes the connection as a result 650 * of the client being idle or some other reason causing the server 651 * to send NNTP reply code 400. This exception may be caught either 652 * as an IOException or independently as itself. 653 * @exception IOException If an I/O error occurs while either sending the 654 * command or receiving the server reply. 655 ***/ 656 public int stat() throws IOException 657 { 658 return sendCommand(NNTPCommand.STAT); 659 } 660 661 662 /**** 663 * A convenience method to send the NNTP GROUP command to the server, 664 * receive the reply, and return the reply code. 665 * <p> 666 * @param newsgroup The name of the newsgroup to select. 667 * @return The reply code received from the server. 668 * @exception NNTPConnectionClosedException 669 * If the NNTP server prematurely closes the connection as a result 670 * of the client being idle or some other reason causing the server 671 * to send NNTP reply code 400. This exception may be caught either 672 * as an IOException or independently as itself. 673 * @exception IOException If an I/O error occurs while either sending the 674 * command or receiving the server reply. 675 ***/ 676 public int group(String newsgroup) throws IOException 677 { 678 return sendCommand(NNTPCommand.GROUP, newsgroup); 679 } 680 681 682 /**** 683 * A convenience method to send the NNTP HELP command to the server, 684 * receive the reply, and return the reply code. 685 * <p> 686 * @return The reply code received from the server. 687 * @exception NNTPConnectionClosedException 688 * If the NNTP server prematurely closes the connection as a result 689 * of the client being idle or some other reason causing the server 690 * to send NNTP reply code 400. This exception may be caught either 691 * as an IOException or independently as itself. 692 * @exception IOException If an I/O error occurs while either sending the 693 * command or receiving the server reply. 694 ***/ 695 public int help() throws IOException 696 { 697 return sendCommand(NNTPCommand.HELP); 698 } 699 700 701 /**** 702 * A convenience method to send the NNTP IHAVE command to the server, 703 * receive the reply, and return the reply code. 704 * <p> 705 * @param messageId The article identifier, 706 * including the encapsulating < and > characters. 707 * @return The reply code received from the server. 708 * @exception NNTPConnectionClosedException 709 * If the NNTP server prematurely closes the connection as a result 710 * of the client being idle or some other reason causing the server 711 * to send NNTP reply code 400. This exception may be caught either 712 * as an IOException or independently as itself. 713 * @exception IOException If an I/O error occurs while either sending the 714 * command or receiving the server reply. 715 ***/ 716 public int ihave(String messageId) throws IOException 717 { 718 return sendCommand(NNTPCommand.IHAVE, messageId); 719 } 720 721 722 /**** 723 * A convenience method to send the NNTP LAST command to the server, 724 * receive the reply, and return the reply code. 725 * <p> 726 * @return The reply code received from the server. 727 * @exception NNTPConnectionClosedException 728 * If the NNTP server prematurely closes the connection as a result 729 * of the client being idle or some other reason causing the server 730 * to send NNTP reply code 400. This exception may be caught either 731 * as an IOException or independently as itself. 732 * @exception IOException If an I/O error occurs while either sending the 733 * command or receiving the server reply. 734 ***/ 735 public int last() throws IOException 736 { 737 return sendCommand(NNTPCommand.LAST); 738 } 739 740 741 742 /**** 743 * A convenience method to send the NNTP LIST command to the server, 744 * receive the reply, and return the reply code. 745 * <p> 746 * @return The reply code received from the server. 747 * @exception NNTPConnectionClosedException 748 * If the NNTP server prematurely closes the connection as a result 749 * of the client being idle or some other reason causing the server 750 * to send NNTP reply code 400. This exception may be caught either 751 * as an IOException or independently as itself. 752 * @exception IOException If an I/O error occurs while either sending the 753 * command or receiving the server reply. 754 ***/ 755 public int list() throws IOException 756 { 757 return sendCommand(NNTPCommand.LIST); 758 } 759 760 761 762 /**** 763 * A convenience method to send the NNTP NEXT command to the server, 764 * receive the reply, and return the reply code. 765 * <p> 766 * @return The reply code received from the server. 767 * @exception NNTPConnectionClosedException 768 * If the NNTP server prematurely closes the connection as a result 769 * of the client being idle or some other reason causing the server 770 * to send NNTP reply code 400. This exception may be caught either 771 * as an IOException or independently as itself. 772 * @exception IOException If an I/O error occurs while either sending the 773 * command or receiving the server reply. 774 ***/ 775 public int next() throws IOException 776 { 777 return sendCommand(NNTPCommand.NEXT); 778 } 779 780 781 /**** 782 * A convenience method to send the NNTP NEWGROUPS command to the server, 783 * receive the reply, and return the reply code. 784 * <p> 785 * @param date The date after which to check for new groups. 786 * Date format is YYMMDD 787 * @param time The time after which to check for new groups. 788 * Time format is HHMMSS using a 24-hour clock. 789 * @param GMT True if the time is in GMT, false if local server time. 790 * @param distributions Comma-separated distribution list to check for 791 * new groups. Set to null if no distributions. 792 * @return The reply code received from the server. 793 * @exception NNTPConnectionClosedException 794 * If the NNTP server prematurely closes the connection as a result 795 * of the client being idle or some other reason causing the server 796 * to send NNTP reply code 400. This exception may be caught either 797 * as an IOException or independently as itself. 798 * @exception IOException If an I/O error occurs while either sending the 799 * command or receiving the server reply. 800 ***/ 801 public int newgroups(String date, String time, boolean GMT, 802 String distributions) throws IOException 803 { 804 StringBuffer buffer = new StringBuffer(); 805 806 buffer.append(date); 807 buffer.append(' '); 808 buffer.append(time); 809 810 if (GMT) 811 { 812 buffer.append(' '); 813 buffer.append("GMT"); 814 } 815 816 if (distributions != null) 817 { 818 buffer.append(" <"); 819 buffer.append(distributions); 820 buffer.append('>'); 821 } 822 823 return sendCommand(NNTPCommand.NEWGROUPS, buffer.toString()); 824 } 825 826 827 /**** 828 * A convenience method to send the NNTP NEWGROUPS command to the server, 829 * receive the reply, and return the reply code. 830 * <p> 831 * @param newsgroups A comma-separated list of newsgroups to check for new 832 * news. 833 * @param date The date after which to check for new news. 834 * Date format is YYMMDD 835 * @param time The time after which to check for new news. 836 * Time format is HHMMSS using a 24-hour clock. 837 * @param GMT True if the time is in GMT, false if local server time. 838 * @param distributions Comma-separated distribution list to check for 839 * new news. Set to null if no distributions. 840 * @return The reply code received from the server. 841 * @exception NNTPConnectionClosedException 842 * If the NNTP server prematurely closes the connection as a result 843 * of the client being idle or some other reason causing the server 844 * to send NNTP reply code 400. This exception may be caught either 845 * as an IOException or independently as itself. 846 * @exception IOException If an I/O error occurs while either sending the 847 * command or receiving the server reply. 848 ***/ 849 public int newnews(String newsgroups, String date, String time, boolean GMT, 850 String distributions) throws IOException 851 { 852 StringBuffer buffer = new StringBuffer(); 853 854 buffer.append(newsgroups); 855 buffer.append(' '); 856 buffer.append(date); 857 buffer.append(' '); 858 buffer.append(time); 859 860 if (GMT) 861 { 862 buffer.append(' '); 863 buffer.append("GMT"); 864 } 865 866 if (distributions != null) 867 { 868 buffer.append(" <"); 869 buffer.append(distributions); 870 buffer.append('>'); 871 } 872 873 return sendCommand(NNTPCommand.NEWNEWS, buffer.toString()); 874 } 875 876 877 878 /**** 879 * A convenience method to send the NNTP POST command to the server, 880 * receive the reply, and return the reply code. 881 * <p> 882 * @return The reply code received from the server. 883 * @exception NNTPConnectionClosedException 884 * If the NNTP server prematurely closes the connection as a result 885 * of the client being idle or some other reason causing the server 886 * to send NNTP reply code 400. This exception may be caught either 887 * as an IOException or independently as itself. 888 * @exception IOException If an I/O error occurs while either sending the 889 * command or receiving the server reply. 890 ***/ 891 public int post() throws IOException 892 { 893 return sendCommand(NNTPCommand.POST); 894 } 895 896 897 898 /**** 899 * A convenience method to send the NNTP QUIT command to the server, 900 * receive the reply, and return the reply code. 901 * <p> 902 * @return The reply code received from the server. 903 * @exception NNTPConnectionClosedException 904 * If the NNTP server prematurely closes the connection as a result 905 * of the client being idle or some other reason causing the server 906 * to send NNTP reply code 400. This exception may be caught either 907 * as an IOException or independently as itself. 908 * @exception IOException If an I/O error occurs while either sending the 909 * command or receiving the server reply. 910 ***/ 911 public int quit() throws IOException 912 { 913 return sendCommand(NNTPCommand.QUIT); 914 } 915 916 }

This page was automatically generated by Maven