1 16 package org.apache.commons.net.nntp; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.Reader ; 21 import java.io.StringWriter ; 22 import java.io.Writer ; 23 import java.util.StringTokenizer ; 24 import java.util.Vector ; 25 import org.apache.commons.net.io.DotTerminatedMessageReader; 26 import org.apache.commons.net.io.DotTerminatedMessageWriter; 27 import org.apache.commons.net.io.Util; 28 import org.apache.commons.net.MalformedServerReplyException; 29 30 86 87 public class NNTPClient extends NNTP 88 { 89 90 private void __parseArticlePointer(String reply, ArticlePointer pointer) 91 throws MalformedServerReplyException 92 { 93 StringTokenizer tokenizer; 94 95 do 97 { 98 tokenizer = new StringTokenizer (reply); 99 100 if (tokenizer.countTokens() < 3) 101 break; 102 103 tokenizer.nextToken(); 105 try 107 { 108 pointer.articleNumber = Integer.parseInt(tokenizer.nextToken()); 109 } 110 catch (NumberFormatException e) 111 { 112 break; 113 } 114 115 pointer.articleId = tokenizer.nextToken(); 117 return ; 118 } 119 while (false); 120 121 throw new MalformedServerReplyException( 122 "Could not parse article pointer.\nServer reply: " + reply); 123 } 124 125 126 private void __parseGroupReply(String reply, NewsgroupInfo info) 127 throws MalformedServerReplyException 128 { 129 String count, first, last; 130 StringTokenizer tokenizer; 131 132 do 134 { 135 tokenizer = new StringTokenizer (reply); 136 137 if (tokenizer.countTokens() < 5) 138 break; 139 140 tokenizer.nextToken(); 142 count = tokenizer.nextToken(); 144 first = tokenizer.nextToken(); 146 last = tokenizer.nextToken(); 148 info._setNewsgroup(tokenizer.nextToken()); 150 151 try 152 { 153 info._setArticleCount(Integer.parseInt(count)); 154 info._setFirstArticle(Integer.parseInt(first)); 155 info._setLastArticle(Integer.parseInt(last)); 156 } 157 catch (NumberFormatException e) 158 { 159 break; 160 } 161 162 info._setPostingPermission(NewsgroupInfo.UNKNOWN_POSTING_PERMISSION); 163 return ; 164 } 165 while (false); 166 167 throw new MalformedServerReplyException( 168 "Could not parse newsgroup info.\nServer reply: " + reply); 169 } 170 171 172 private NewsgroupInfo __parseNewsgroupListEntry(String entry) 173 { 174 NewsgroupInfo result; 175 StringTokenizer tokenizer; 176 int lastNum, firstNum; 177 String last, first, permission; 178 179 result = new NewsgroupInfo(); 180 tokenizer = new StringTokenizer (entry); 181 182 if (tokenizer.countTokens() < 4) 183 return null; 184 185 result._setNewsgroup(tokenizer.nextToken()); 186 last = tokenizer.nextToken(); 187 first = tokenizer.nextToken(); 188 permission = tokenizer.nextToken(); 189 190 try 191 { 192 lastNum = Integer.parseInt(last); 193 firstNum = Integer.parseInt(first); 194 result._setFirstArticle(firstNum); 195 result._setLastArticle(lastNum); 196 197 if((firstNum == 0) && (lastNum == 0)) 198 result._setArticleCount(0); 199 else 200 result._setArticleCount(lastNum - firstNum + 1); 201 } 202 catch (NumberFormatException e) 203 { 204 return null; 205 } 206 207 switch (permission.charAt(0)) 208 { 209 case 'y': 210 case 'Y': 211 result._setPostingPermission( 212 NewsgroupInfo.PERMITTED_POSTING_PERMISSION); 213 break; 214 case 'n': 215 case 'N': 216 result._setPostingPermission( 217 NewsgroupInfo.PROHIBITED_POSTING_PERMISSION); 218 break; 219 case 'm': 220 case 'M': 221 result._setPostingPermission( 222 NewsgroupInfo.MODERATED_POSTING_PERMISSION); 223 break; 224 default: 225 result._setPostingPermission( 226 NewsgroupInfo.UNKNOWN_POSTING_PERMISSION); 227 break; 228 } 229 230 return result; 231 } 232 233 private NewsgroupInfo[] __readNewsgroupListing() throws IOException 234 { 235 int size; 236 String line; 237 Vector list; 238 BufferedReader reader; 239 NewsgroupInfo tmp, info[]; 240 241 reader = new BufferedReader (new DotTerminatedMessageReader(_reader_)); 242 list = new Vector (2048); 245 246 while ((line = reader.readLine()) != null) 247 { 248 tmp = __parseNewsgroupListEntry(line); 249 if (tmp != null) 250 list.addElement(tmp); 251 else 252 throw new MalformedServerReplyException(line); 253 } 254 255 if ((size = list.size()) < 1) 256 return new NewsgroupInfo[0]; 257 258 info = new NewsgroupInfo[size]; 259 list.copyInto(info); 260 261 return info; 262 } 263 264 265 private Reader __retrieve(int command, 266 String articleId, ArticlePointer pointer) 267 throws IOException 268 { 269 Reader reader; 270 271 if (articleId != null) 272 { 273 if (!NNTPReply.isPositiveCompletion(sendCommand(command, articleId))) 274 return null; 275 } 276 else 277 { 278 if (!NNTPReply.isPositiveCompletion(sendCommand(command))) 279 return null; 280 } 281 282 283 if (pointer != null) 284 __parseArticlePointer(getReplyString(), pointer); 285 286 reader = new DotTerminatedMessageReader(_reader_); 287 return reader; 288 } 289 290 291 private Reader __retrieve(int command, 292 int articleNumber, ArticlePointer pointer) 293 throws IOException 294 { 295 Reader reader; 296 297 if (!NNTPReply.isPositiveCompletion(sendCommand(command, 298 Integer.toString(articleNumber)))) 299 return null; 300 301 if (pointer != null) 302 __parseArticlePointer(getReplyString(), pointer); 303 304 reader = new DotTerminatedMessageReader(_reader_); 305 return reader; 306 } 307 308 309 310 349 public Reader retrieveArticle(String articleId, ArticlePointer pointer) 350 throws IOException 351 { 352 return __retrieve(NNTPCommand.ARTICLE, articleId, pointer); 353 354 } 355 356 357 public Reader retrieveArticle(String articleId) throws IOException 358 { 359 return retrieveArticle(articleId, null); 360 } 361 362 363 public Reader retrieveArticle() throws IOException 364 { 365 return retrieveArticle(null); 366 } 367 368 369 407 public Reader retrieveArticle(int articleNumber, ArticlePointer pointer) 408 throws IOException 409 { 410 return __retrieve(NNTPCommand.ARTICLE, articleNumber, pointer); 411 } 412 413 414 public Reader retrieveArticle(int articleNumber) throws IOException 415 { 416 return retrieveArticle(articleNumber, null); 417 } 418 419 420 421 461 public Reader retrieveArticleHeader(String articleId, ArticlePointer pointer) 462 throws IOException 463 { 464 return __retrieve(NNTPCommand.HEAD, articleId, pointer); 465 466 } 467 468 469 public Reader retrieveArticleHeader(String articleId) throws IOException 470 { 471 return retrieveArticleHeader(articleId, null); 472 } 473 474 475 public Reader retrieveArticleHeader() throws IOException 476 { 477 return retrieveArticleHeader(null); 478 } 479 480 481 519 public Reader retrieveArticleHeader(int articleNumber, 520 ArticlePointer pointer) 521 throws IOException 522 { 523 return __retrieve(NNTPCommand.HEAD, articleNumber, pointer); 524 } 525 526 527 528 public Reader retrieveArticleHeader(int articleNumber) throws IOException 529 { 530 return retrieveArticleHeader(articleNumber, null); 531 } 532 533 534 535 575 public Reader retrieveArticleBody(String articleId, ArticlePointer pointer) 576 throws IOException 577 { 578 return __retrieve(NNTPCommand.BODY, articleId, pointer); 579 580 } 581 582 583 public Reader retrieveArticleBody(String articleId) throws IOException 584 { 585 return retrieveArticleBody(articleId, null); 586 } 587 588 589 public Reader retrieveArticleBody() throws IOException 590 { 591 return retrieveArticleBody(null); 592 } 593 594 595 633 public Reader retrieveArticleBody(int articleNumber, 634 ArticlePointer pointer) 635 throws IOException 636 { 637 return __retrieve(NNTPCommand.BODY, articleNumber, pointer); 638 } 639 640 641 642 public Reader retrieveArticleBody(int articleNumber) throws IOException 643 { 644 return retrieveArticleBody(articleNumber, null); 645 } 646 647 648 666 public boolean selectNewsgroup(String newsgroup, NewsgroupInfo info) 667 throws IOException 668 { 669 if (!NNTPReply.isPositiveCompletion(group(newsgroup))) 670 return false; 671 672 if (info != null) 673 __parseGroupReply(getReplyString(), info); 674 675 return true; 676 } 677 678 679 public boolean selectNewsgroup(String newsgroup) throws IOException 680 { 681 return selectNewsgroup(newsgroup, null); 682 } 683 684 696 public String listHelp() throws IOException 697 { 698 StringWriter help; 699 Reader reader; 700 701 if (!NNTPReply.isInformational(help())) 702 return null; 703 704 help = new StringWriter (); 705 reader = new DotTerminatedMessageReader(_reader_); 706 Util.copyReader(reader, help); 707 reader.close(); 708 help.close(); 709 return help.toString(); 710 } 711 712 713 738 public boolean selectArticle(String articleId, ArticlePointer pointer) 739 throws IOException 740 { 741 if (articleId != null) 742 { 743 if (!NNTPReply.isPositiveCompletion(stat(articleId))) 744 return false; 745 } 746 else 747 { 748 if (!NNTPReply.isPositiveCompletion(stat())) 749 return false; 750 } 751 752 if (pointer != null) 753 __parseArticlePointer(getReplyString(), pointer); 754 755 return true; 756 } 757 758 759 public boolean selectArticle(String articleId) throws IOException 760 { 761 return selectArticle(articleId, null); 762 } 763 764 768 public boolean selectArticle(ArticlePointer pointer) throws IOException 769 { 770 return selectArticle(null, pointer); 771 } 772 773 774 799 public boolean selectArticle(int articleNumber, ArticlePointer pointer) 800 throws IOException 801 { 802 if (!NNTPReply.isPositiveCompletion(stat(articleNumber))) 803 return false; 804 805 if (pointer != null) 806 __parseArticlePointer(getReplyString(), pointer); 807 808 return true; 809 } 810 811 812 813 public boolean selectArticle(int articleNumber) throws IOException 814 { 815 return selectArticle(articleNumber, null); 816 } 817 818 819 843 public boolean selectPreviousArticle(ArticlePointer pointer) 844 throws IOException 845 { 846 if (!NNTPReply.isPositiveCompletion(last())) 847 return false; 848 849 if (pointer != null) 850 __parseArticlePointer(getReplyString(), pointer); 851 852 return true; 853 } 854 855 856 public boolean selectPreviousArticle() throws IOException 857 { 858 return selectPreviousArticle(null); 859 } 860 861 862 886 public boolean selectNextArticle(ArticlePointer pointer) throws IOException 887 { 888 if (!NNTPReply.isPositiveCompletion(next())) 889 return false; 890 891 if (pointer != null) 892 __parseArticlePointer(getReplyString(), pointer); 893 894 return true; 895 } 896 897 898 899 public boolean selectNextArticle() throws IOException 900 { 901 return selectNextArticle(null); 902 } 903 904 905 922 public NewsgroupInfo[] listNewsgroups() throws IOException 923 { 924 if (!NNTPReply.isPositiveCompletion(list())) 925 return null; 926 927 return __readNewsgroupListing(); 928 } 929 930 942 public NewsgroupInfo[] listNewsgroups(String wildmat) throws IOException 943 { 944 if(!NNTPReply.isPositiveCompletion(listActive(wildmat))) 945 return null; 946 return __readNewsgroupListing(); 947 } 948 949 950 969 public NewsgroupInfo[] listNewNewsgroups(NewGroupsOrNewsQuery query) 970 throws IOException 971 { 972 if (!NNTPReply.isPositiveCompletion(newgroups( 973 query.getDate(), query.getTime(), 974 query.isGMT(), query.getDistributions()))) 975 return null; 976 977 return __readNewsgroupListing(); 978 } 979 980 981 1004 public String [] listNewNews(NewGroupsOrNewsQuery query) 1005 throws IOException 1006 { 1007 int size; 1008 String line; 1009 Vector list; 1010 String [] result; 1011 BufferedReader reader; 1012 1013 if (!NNTPReply.isPositiveCompletion(newnews( 1014 query.getNewsgroups(), query.getDate(), query.getTime(), 1015 query.isGMT(), query.getDistributions()))) 1016 return null; 1017 1018 list = new Vector (); 1019 reader = new BufferedReader (new DotTerminatedMessageReader(_reader_)); 1020 1021 while ((line = reader.readLine()) != null) 1022 list.addElement(line); 1023 1024 size = list.size(); 1025 1026 if (size < 1) 1027 return new String [0]; 1028 1029 result = new String [size]; 1030 list.copyInto(result); 1031 1032 return result; 1033 } 1034 1035 1067 public boolean completePendingCommand() throws IOException 1068 { 1069 return NNTPReply.isPositiveCompletion(getReply()); 1070 } 1071 1072 1109 1110 public Writer postArticle() throws IOException 1111 { 1112 if (!NNTPReply.isPositiveIntermediate(post())) 1113 return null; 1114 1115 return new DotTerminatedMessageWriter(_writer_); 1116 } 1117 1118 1119 public Writer forwardArticle(String articleId) throws IOException 1120 { 1121 if (!NNTPReply.isPositiveIntermediate(ihave(articleId))) 1122 return null; 1123 1124 return new DotTerminatedMessageWriter(_writer_); 1125 } 1126 1127 1128 1137 public boolean logout() throws IOException 1138 { 1139 return NNTPReply.isPositiveCompletion(quit()); 1140 } 1141 1142 1143 1153 public boolean authenticate(String username, String password) 1154 throws IOException 1155 { 1156 int replyCode = authinfoUser(username); 1157 1158 if (replyCode == NNTPReply.MORE_AUTH_INFO_REQUIRED) 1159 { 1160 replyCode = authinfoPass(password); 1161 1162 if (replyCode == NNTPReply.AUTHENTICATION_ACCEPTED) 1163 { 1164 _isAllowedToPost = true; 1165 return true; 1166 } 1167 } 1168 return false; 1169 } 1170 1171 1182 private Reader __retrieveArticleInfo(String articleRange) 1183 throws IOException 1184 { 1185 if (!NNTPReply.isPositiveCompletion(xover(articleRange))) 1186 return null; 1187 1188 return new DotTerminatedMessageReader(_reader_); 1189 } 1190 1191 1198 public Reader retrieveArticleInfo(int articleNumber) throws IOException 1199 { 1200 return __retrieveArticleInfo(Integer.toString(articleNumber)); 1201 } 1202 1203 1212 public Reader retrieveArticleInfo(int lowArticleNumber, 1213 int highArticleNumber) 1214 throws IOException 1215 { 1216 return 1217 __retrieveArticleInfo(new String (lowArticleNumber + "-" + 1218 highArticleNumber)); 1219 } 1220 1221 1233 private Reader __retrieveHeader(String header, String articleRange) 1234 throws IOException 1235 { 1236 if (!NNTPReply.isPositiveCompletion(xhdr(header, articleRange))) 1237 return null; 1238 1239 return new DotTerminatedMessageReader(_reader_); 1240 } 1241 1242 1250 public Reader retrieveHeader(String header, int articleNumber) 1251 throws IOException 1252 { 1253 return __retrieveHeader(header, Integer.toString(articleNumber)); 1254 } 1255 1256 1266 public Reader retrieveHeader(String header, int lowArticleNumber, 1267 int highArticleNumber) 1268 throws IOException 1269 { 1270 return 1271 __retrieveHeader(header, 1272 new String (lowArticleNumber + "-" + 1273 highArticleNumber)); 1274 } 1275} 1276 1277 1278 1285 | Popular Tags |