1 16 package org.apache.commons.net.nntp; 17 18 import java.io.BufferedReader ; 19 import java.io.BufferedWriter ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.io.OutputStreamWriter ; 23 import org.apache.commons.net.MalformedServerReplyException; 24 import org.apache.commons.net.ProtocolCommandSupport; 25 import org.apache.commons.net.ProtocolCommandListener; 26 import org.apache.commons.net.SocketClient; 27 28 80 81 public class NNTP extends SocketClient 82 { 83 84 public static final int DEFAULT_PORT = 119; 85 86 private static final String __DEFAULT_ENCODING = "ISO-8859-1"; 90 91 private StringBuffer __commandBuffer; 92 93 boolean _isAllowedToPost; 94 int _replyCode; 95 String _replyString; 96 97 102 protected BufferedReader _reader_; 103 104 109 protected BufferedWriter _writer_; 110 111 115 protected ProtocolCommandSupport _commandSupport_; 116 117 122 public NNTP() 123 { 124 setDefaultPort(DEFAULT_PORT); 125 __commandBuffer = new StringBuffer (); 126 _replyString = null; 127 _reader_ = null; 128 _writer_ = null; 129 _isAllowedToPost = false; 130 _commandSupport_ = new ProtocolCommandSupport(this); 131 } 132 133 private void __getReply() throws IOException 134 { 135 _replyString = _reader_.readLine(); 136 137 if (_replyString == null) 138 throw new NNTPConnectionClosedException( 139 "Connection closed without indication."); 140 141 if (_replyString.length() < 3) 144 throw new MalformedServerReplyException( 145 "Truncated server reply: " + _replyString); 146 try 147 { 148 _replyCode = Integer.parseInt(_replyString.substring(0, 3)); 149 } 150 catch (NumberFormatException e) 151 { 152 throw new MalformedServerReplyException( 153 "Could not parse response code.\nServer Reply: " + _replyString); 154 } 155 156 if (_commandSupport_.getListenerCount() > 0) 157 _commandSupport_.fireReplyReceived(_replyCode, _replyString + 158 SocketClient.NETASCII_EOL); 159 160 if (_replyCode == NNTPReply.SERVICE_DISCONTINUED) 161 throw new NNTPConnectionClosedException( 162 "NNTP response 400 received. Server closed connection."); 163 } 164 165 171 protected void _connectAction_() throws IOException 172 { 173 super._connectAction_(); 174 _reader_ = 175 new BufferedReader (new InputStreamReader (_input_, 176 __DEFAULT_ENCODING)); 177 _writer_ = 178 new BufferedWriter (new OutputStreamWriter (_output_, 179 __DEFAULT_ENCODING)); 180 __getReply(); 181 182 _isAllowedToPost = (_replyCode == NNTPReply.SERVER_READY_POSTING_ALLOWED); 183 } 184 185 191 public void addProtocolCommandListener(ProtocolCommandListener listener) 192 { 193 _commandSupport_.addProtocolCommandListener(listener); 194 } 195 196 202 public void removeProtocolCommandListener(ProtocolCommandListener listener) 203 { 204 _commandSupport_.removeProtocolCommandListener(listener); 205 } 206 207 215 public void disconnect() throws IOException 216 { 217 super.disconnect(); 218 _reader_ = null; 219 _writer_ = null; 220 _replyString = null; 221 _isAllowedToPost = false; 222 } 223 224 225 232 public boolean isAllowedToPost() 233 { 234 return _isAllowedToPost; 235 } 236 237 238 257 public int sendCommand(String command, String args) throws IOException 258 { 259 String message; 260 261 __commandBuffer.setLength(0); 262 __commandBuffer.append(command); 263 264 if (args != null) 265 { 266 __commandBuffer.append(' '); 267 __commandBuffer.append(args); 268 } 269 __commandBuffer.append(SocketClient.NETASCII_EOL); 270 271 _writer_.write(message = __commandBuffer.toString()); 272 _writer_.flush(); 273 274 if (_commandSupport_.getListenerCount() > 0) 275 _commandSupport_.fireCommandSent(command, message); 276 277 __getReply(); 278 return _replyCode; 279 } 280 281 282 303 public int sendCommand(int command, String args) throws IOException 304 { 305 return sendCommand(NNTPCommand._commands[command], args); 306 } 307 308 309 327 public int sendCommand(String command) throws IOException 328 { 329 return sendCommand(command, null); 330 } 331 332 333 352 public int sendCommand(int command) throws IOException 353 { 354 return sendCommand(command, null); 355 } 356 357 358 366 public int getReplyCode() 367 { 368 return _replyCode; 369 } 370 371 388 public int getReply() throws IOException 389 { 390 __getReply(); 391 return _replyCode; 392 } 393 394 395 401 public String getReplyString() 402 { 403 return _replyString; 404 } 405 406 407 422 public int article(String messageId) throws IOException 423 { 424 return sendCommand(NNTPCommand.ARTICLE, messageId); 425 } 426 427 442 public int article(int articleNumber) throws IOException 443 { 444 return sendCommand(NNTPCommand.ARTICLE, Integer.toString(articleNumber)); 445 } 446 447 460 public int article() throws IOException 461 { 462 return sendCommand(NNTPCommand.ARTICLE); 463 } 464 465 466 467 482 public int body(String messageId) throws IOException 483 { 484 return sendCommand(NNTPCommand.BODY, messageId); 485 } 486 487 502 public int body(int articleNumber) throws IOException 503 { 504 return sendCommand(NNTPCommand.BODY, Integer.toString(articleNumber)); 505 } 506 507 520 public int body() throws IOException 521 { 522 return sendCommand(NNTPCommand.BODY); 523 } 524 525 526 527 542 public int head(String messageId) throws IOException 543 { 544 return sendCommand(NNTPCommand.HEAD, messageId); 545 } 546 547 562 public int head(int articleNumber) throws IOException 563 { 564 return sendCommand(NNTPCommand.HEAD, Integer.toString(articleNumber)); 565 } 566 567 580 public int head() throws IOException 581 { 582 return sendCommand(NNTPCommand.HEAD); 583 } 584 585 586 587 602 public int stat(String messageId) throws IOException 603 { 604 return sendCommand(NNTPCommand.STAT, messageId); 605 } 606 607 622 public int stat(int articleNumber) throws IOException 623 { 624 return sendCommand(NNTPCommand.STAT, Integer.toString(articleNumber)); 625 } 626 627 640 public int stat() throws IOException 641 { 642 return sendCommand(NNTPCommand.STAT); 643 } 644 645 646 660 public int group(String newsgroup) throws IOException 661 { 662 return sendCommand(NNTPCommand.GROUP, newsgroup); 663 } 664 665 666 679 public int help() throws IOException 680 { 681 return sendCommand(NNTPCommand.HELP); 682 } 683 684 685 700 public int ihave(String messageId) throws IOException 701 { 702 return sendCommand(NNTPCommand.IHAVE, messageId); 703 } 704 705 706 719 public int last() throws IOException 720 { 721 return sendCommand(NNTPCommand.LAST); 722 } 723 724 725 726 739 public int list() throws IOException 740 { 741 return sendCommand(NNTPCommand.LIST); 742 } 743 744 745 746 759 public int next() throws IOException 760 { 761 return sendCommand(NNTPCommand.NEXT); 762 } 763 764 765 785 public int newgroups(String date, String time, boolean GMT, 786 String distributions) throws IOException 787 { 788 StringBuffer buffer = new StringBuffer (); 789 790 buffer.append(date); 791 buffer.append(' '); 792 buffer.append(time); 793 794 if (GMT) 795 { 796 buffer.append(' '); 797 buffer.append("GMT"); 798 } 799 800 if (distributions != null) 801 { 802 buffer.append(" <"); 803 buffer.append(distributions); 804 buffer.append('>'); 805 } 806 807 return sendCommand(NNTPCommand.NEWGROUPS, buffer.toString()); 808 } 809 810 811 833 public int newnews(String newsgroups, String date, String time, boolean GMT, 834 String distributions) throws IOException 835 { 836 StringBuffer buffer = new StringBuffer (); 837 838 buffer.append(newsgroups); 839 buffer.append(' '); 840 buffer.append(date); 841 buffer.append(' '); 842 buffer.append(time); 843 844 if (GMT) 845 { 846 buffer.append(' '); 847 buffer.append("GMT"); 848 } 849 850 if (distributions != null) 851 { 852 buffer.append(" <"); 853 buffer.append(distributions); 854 buffer.append('>'); 855 } 856 857 return sendCommand(NNTPCommand.NEWNEWS, buffer.toString()); 858 } 859 860 861 862 875 public int post() throws IOException 876 { 877 return sendCommand(NNTPCommand.POST); 878 } 879 880 881 882 895 public int quit() throws IOException 896 { 897 return sendCommand(NNTPCommand.QUIT); 898 } 899 900 915 public int authinfoUser(String username) throws IOException { 916 String userParameter = "USER " + username; 917 return sendCommand(NNTPCommand.AUTHINFO, userParameter); 918 } 919 920 937 public int authinfoPass(String password) throws IOException { 938 String passParameter = "PASS " + password; 939 return sendCommand(NNTPCommand.AUTHINFO, passParameter); 940 } 941 942 962 public int xover(String selectedArticles) throws IOException { 963 return sendCommand(NNTPCommand.XOVER, selectedArticles); 964 } 965 966 988 public int xhdr(String header, String selectedArticles) throws IOException { 989 StringBuffer command = new StringBuffer (header); 990 command.append(" "); 991 command.append(selectedArticles); 992 return sendCommand(NNTPCommand.XHDR, command.toString()); 993 } 994 995 1004 public int listActive(String wildmat) throws IOException { 1005 StringBuffer command = new StringBuffer ("ACTIVE "); 1006 command.append(wildmat); 1007 return sendCommand(NNTPCommand.LIST, command.toString()); 1008 } 1009} 1010 1011 1018 | Popular Tags |