1 38 39 40 41 package org.xmpp.packet; 42 43 44 45 import org.dom4j.*; 46 47 import org.dom4j.io.XMLWriter; 48 49 import org.dom4j.io.OutputFormat; 50 51 52 53 import java.util.Iterator ; 54 55 import java.io.StringWriter ; 56 57 58 59 70 71 public class PacketError { 72 73 74 75 private static final String ERROR_NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas"; 76 77 78 79 private static DocumentFactory docFactory = DocumentFactory.getInstance(); 80 81 82 83 private Element element; 84 85 86 87 98 99 public PacketError(Condition condition) { 100 101 this.element = docFactory.createElement("error"); 102 103 setCondition(condition); 104 105 setType(condition.getDefaultType()); 106 107 } 108 109 110 111 122 123 public PacketError(Condition condition, Type type) { 124 125 this.element = docFactory.createElement("error"); 126 127 setCondition(condition); 128 129 setType(type); 130 131 } 132 133 134 135 148 149 public PacketError(Condition condition, Type type, String text) { 150 151 this.element = docFactory.createElement("error"); 152 153 setType(type); 154 155 setCondition(condition); 156 157 setText(text, null); 158 159 } 160 161 162 163 178 179 public PacketError(Condition condition, Type type, String text, String lang) { 180 181 this.element = docFactory.createElement("error"); 182 183 setType(type); 184 185 setCondition(condition); 186 187 setText(text, lang); 188 189 } 190 191 192 193 204 205 public PacketError(Element element) { 206 207 this.element = element; 208 209 } 210 211 212 213 224 225 public Type getType() { 226 227 String type = element.attributeValue("type"); 228 229 if (type != null) { 230 231 return Type.fromXMPP(type); 232 233 } 234 235 else { 236 237 return null; 238 239 } 240 241 } 242 243 244 245 256 257 public void setType(Type type) { 258 259 element.addAttribute("type", type==null?null:type.toXMPP()); 260 261 } 262 263 264 265 276 277 public Condition getCondition() { 278 279 for (Iterator i=element.elementIterator(); i.hasNext(); ) { 280 281 Element el = (Element)i.next(); 282 283 if (el.getNamespaceURI().equals(ERROR_NAMESPACE) && 284 285 !el.getName().equals("text")) 286 287 { 288 289 return Condition.fromXMPP(el.getName()); 290 291 } 292 293 } 294 295 297 299 String code = element.attributeValue("code"); 300 301 if (code != null) { 302 303 try { 304 305 return Condition.fromLegacyCode(Integer.parseInt(code)); 306 307 } 308 309 catch (Exception e) { 310 311 313 315 } 316 317 } 318 319 return null; 320 321 } 322 323 324 325 336 337 public void setCondition(Condition condition) { 338 339 if (condition == null) { 340 341 throw new NullPointerException ("Condition cannot be null"); 342 343 } 344 345 347 element.addAttribute("code", Integer.toString(condition.getLegacyCode())); 348 349 350 351 Element conditionElement = null; 352 353 for (Iterator i=element.elementIterator(); i.hasNext(); ) { 354 355 Element el = (Element)i.next(); 356 357 if (el.getNamespaceURI().equals(ERROR_NAMESPACE) && 358 359 !el.getName().equals("text")) 360 361 { 362 363 conditionElement = el; 364 365 } 366 367 } 368 369 if (conditionElement != null) { 370 371 element.remove(conditionElement); 372 373 } 374 375 376 377 conditionElement = docFactory.createElement(condition.toXMPP(), 378 379 ERROR_NAMESPACE); 380 381 element.add(conditionElement); 382 383 } 384 385 386 387 398 399 public String getText() { 400 401 return element.elementText("text"); 402 403 } 404 405 406 407 416 417 public void setText(String text) { 418 419 setText(text, null); 420 421 } 422 423 424 425 440 441 public void setText(String text, String lang) { 442 443 Element textElement = element.element("text"); 444 445 447 if (text == null) { 448 449 if (textElement != null) { 450 451 element.remove(textElement); 452 453 } 454 455 return; 456 457 } 458 459 460 461 if (textElement == null) { 462 463 textElement = docFactory.createElement("text", ERROR_NAMESPACE); 464 465 if (lang != null) { 466 467 textElement.addAttribute(QName.get("lang", "xml", 468 469 "http://www.w3.org/XML/1998/namespace"), lang); 470 471 } 472 473 element.add(textElement); 474 475 } 476 477 textElement.setText(text); 478 479 } 480 481 482 483 494 495 public String getTextLang() { 496 497 Element textElement = element.element("text"); 498 499 if (textElement != null) { 500 501 return textElement.attributeValue(QName.get("lang", "xml", 502 503 "http://www.w3.org/XML/1998/namespace")); 504 505 } 506 507 return null; 508 509 } 510 511 512 513 526 527 public Element getElement() { 528 529 return element; 530 531 } 532 533 534 535 544 545 public String toXML() { 546 547 return element.asXML(); 548 549 } 550 551 552 553 public String toString() { 554 555 StringWriter out = new StringWriter (); 556 557 XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 558 559 try { 560 561 writer.write(element); 562 563 } 564 565 catch (Exception e) { } 566 567 return out.toString(); 568 569 } 570 571 572 573 592 593 public enum Condition { 594 595 596 597 606 607 bad_request("bad-request", Type.modify, 400), 608 609 610 611 620 621 conflict("conflict", Type.cancel, 409), 622 623 624 625 634 635 feature_not_implemented("feature-not-implemented", Type.cancel, 501), 636 637 638 639 646 647 forbidden("forbidden", Type.auth, 403), 648 649 650 651 662 663 gone("gone", Type.modify, 302), 664 665 666 667 676 677 internal_server_error("internal-server-error", Type.wait, 500), 678 679 680 681 688 689 item_not_found("item-not-found", Type.cancel, 404), 690 691 692 693 706 707 jid_malformed("jid-malformed", Type.modify, 400), 708 709 710 711 722 723 not_acceptable("not-acceptable", Type.modify, 406), 724 725 726 727 734 735 not_allowed("not-allowed", Type.cancel, 405), 736 737 738 739 748 749 not_authorized("not-authorized", Type.auth, 401), 750 751 752 753 762 763 payment_required("payment-required", Type.auth, 402), 764 765 766 767 780 781 recipient_unavailable("recipient-unavailable", Type.wait, 404), 782 783 784 785 798 799 redirect("redirect", Type.modify, 302), 800 801 802 803 812 813 registration_required("registration-required", Type.auth, 407), 814 815 816 817 826 827 remote_server_not_found("remote-server-not-found", Type.cancel, 404), 828 829 830 831 842 843 remote_server_timeout("remote-server-timeout", Type.wait, 504), 844 845 846 847 854 855 resource_constraint("resource-constraint", Type.wait, 500), 856 857 858 859 866 867 service_unavailable("service-unavailable", Type.cancel, 503), 868 869 870 871 880 881 subscription_required("subscription-required", Type.auth, 407), 882 883 884 885 902 903 undefined_condition("undefined-condition", Type.wait, 500), 904 905 906 907 916 917 unexpected_condition("unexpected-condition", Type.wait, 400); 918 919 920 921 932 933 public static Condition fromXMPP(String condition) { 934 935 if (condition == null) { 936 937 throw new NullPointerException (); 938 939 } 940 941 condition = condition.toLowerCase(); 942 943 if (bad_request.toXMPP().equals(condition)) { 944 945 return bad_request; 946 947 } 948 949 else if (conflict.toXMPP().equals(condition)) { 950 951 return conflict; 952 953 } 954 955 else if (feature_not_implemented.toXMPP().equals(condition)) { 956 957 return feature_not_implemented; 958 959 } 960 961 else if (forbidden.toXMPP().equals(condition)) { 962 963 return forbidden; 964 965 } 966 967 else if (gone.toXMPP().equals(condition)) { 968 969 return gone; 970 971 } 972 973 else if (internal_server_error.toXMPP().equals(condition)) { 974 975 return internal_server_error; 976 977 } 978 979 else if (item_not_found.toXMPP().equals(condition)) { 980 981 return item_not_found; 982 983 } 984 985 else if (jid_malformed.toXMPP().equals(condition)) { 986 987 return jid_malformed; 988 989 } 990 991 else if (not_acceptable.toXMPP().equals(condition)) { 992 993 return not_acceptable; 994 995 } 996 997 else if (not_allowed.toXMPP().equals(condition)) { 998 999 return not_allowed; 1000 1001 } 1002 1003 else if (not_authorized.toXMPP().equals(condition)) { 1004 1005 return not_authorized; 1006 1007 } 1008 1009 else if (payment_required.toXMPP().equals(condition)) { 1010 1011 return payment_required; 1012 1013 } 1014 1015 else if (recipient_unavailable.toXMPP().equals(condition)) { 1016 1017 return recipient_unavailable; 1018 1019 } 1020 1021 else if (redirect.toXMPP().equals(condition)) { 1022 1023 return redirect; 1024 1025 } 1026 1027 else if (registration_required.toXMPP().equals(condition)) { 1028 1029 return registration_required; 1030 1031 } 1032 1033 else if (remote_server_not_found.toXMPP().equals(condition)) { 1034 1035 return remote_server_not_found; 1036 1037 } 1038 1039 else if (remote_server_timeout.toXMPP().equals(condition)) { 1040 1041 return remote_server_timeout; 1042 1043 } 1044 1045 else if (resource_constraint.toXMPP().equals(condition)) { 1046 1047 return resource_constraint; 1048 1049 } 1050 1051 else if (service_unavailable.toXMPP().equals(condition)) { 1052 1053 return service_unavailable; 1054 1055 } 1056 1057 else if (subscription_required.toXMPP().equals(condition)) { 1058 1059 return subscription_required; 1060 1061 } 1062 1063 else if (undefined_condition.toXMPP().equals(condition)) { 1064 1065 return undefined_condition; 1066 1067 } 1068 1069 else if (unexpected_condition.toXMPP().equals(condition)) { 1070 1071 return unexpected_condition; 1072 1073 } 1074 1075 else { 1076 1077 throw new IllegalArgumentException ("Condition invalid:" + condition); 1078 1079 } 1080 1081 } 1082 1083 1084 1085 public static Condition fromLegacyCode(int code) { 1086 1087 if (bad_request.getLegacyCode() == code) { 1088 1089 return bad_request; 1090 1091 } 1092 1093 else if (conflict.getLegacyCode() == code) { 1094 1095 return conflict; 1096 1097 } 1098 1099 else if (feature_not_implemented.getLegacyCode() == code) { 1100 1101 return feature_not_implemented; 1102 1103 } 1104 1105 else if (forbidden.getLegacyCode() == code) { 1106 1107 return forbidden; 1108 1109 } 1110 1111 else if (gone.getLegacyCode() == code) { 1112 1113 return gone; 1114 1115 } 1116 1117 else if (internal_server_error.getLegacyCode() == code) { 1118 1119 return internal_server_error; 1120 1121 } 1122 1123 else if (item_not_found.getLegacyCode() == code) { 1124 1125 return item_not_found; 1126 1127 } 1128 1129 else if (jid_malformed.getLegacyCode() == code) { 1130 1131 return jid_malformed; 1132 1133 } 1134 1135 else if (not_acceptable.getLegacyCode() == code) { 1136 1137 return not_acceptable; 1138 1139 } 1140 1141 else if (not_allowed.getLegacyCode() == code) { 1142 1143 return not_allowed; 1144 1145 } 1146 1147 else if (not_authorized.getLegacyCode() == code) { 1148 1149 return not_authorized; 1150 1151 } 1152 1153 else if (payment_required.getLegacyCode() == code) { 1154 1155 return payment_required; 1156 1157 } 1158 1159 else if (recipient_unavailable.getLegacyCode() == code) { 1160 1161 return recipient_unavailable; 1162 1163 } 1164 1165 else if (redirect.getLegacyCode() == code) { 1166 1167 return redirect; 1168 1169 } 1170 1171 else if (registration_required.getLegacyCode() == code) { 1172 1173 return registration_required; 1174 1175 } 1176 1177 else if (remote_server_not_found.getLegacyCode() == code) { 1178 1179 return remote_server_not_found; 1180 1181 } 1182 1183 else if (remote_server_timeout.getLegacyCode() == code) { 1184 1185 return remote_server_timeout; 1186 1187 } 1188 1189 else if (resource_constraint.getLegacyCode() == code) { 1190 1191 return resource_constraint; 1192 1193 } 1194 1195 else if (service_unavailable.getLegacyCode() == code) { 1196 1197 return service_unavailable; 1198 1199 } 1200 1201 else if (subscription_required.getLegacyCode() == code) { 1202 1203 return subscription_required; 1204 1205 } 1206 1207 else if (undefined_condition.getLegacyCode() == code) { 1208 1209 return undefined_condition; 1210 1211 } 1212 1213 else if (unexpected_condition.getLegacyCode() == code) { 1214 1215 return unexpected_condition; 1216 1217 } 1218 1219 else { 1220 1221 throw new IllegalArgumentException ("Code invalid:" + code); 1222 1223 } 1224 1225 } 1226 1227 1228 1229 private String value; 1230 1231 private int code; 1232 1233 private Type defaultType; 1234 1235 1236 1237 private Condition(String value, Type defaultType, int code) { 1238 1239 this.value = value; 1240 1241 this.defaultType = defaultType; 1242 1243 this.code = code; 1244 1245 } 1246 1247 1248 1249 1260 1261 public Type getDefaultType() { 1262 1263 return defaultType; 1264 1265 } 1266 1267 1268 1269 1286 1287 public int getLegacyCode() { 1288 1289 return code; 1290 1291 } 1292 1293 1294 1295 1304 1305 public String toXMPP() { 1306 1307 return value; 1308 1309 } 1310 1311 } 1312 1313 1314 1315 1348 1349 public enum Type { 1350 1351 1352 1353 1358 1359 cancel("cancel"), 1360 1361 1362 1363 1372 1373 continue_processing("continue"), 1374 1375 1376 1377 1382 1383 modify("modify"), 1384 1385 1386 1387 1392 1393 auth("auth"), 1394 1395 1396 1397 1402 1403 wait("wait"); 1404 1405 1406 1407 1418 1419 public static Type fromXMPP(String type) { 1420 1421 if (type == null) { 1422 1423 throw new NullPointerException (); 1424 1425 } 1426 1427 type = type.toLowerCase(); 1428 1429 if (cancel.toXMPP().equals(type)) { 1430 1431 return cancel; 1432 1433 } 1434 1435 else if (continue_processing.toXMPP().equals(type)) { 1436 1437 return continue_processing; 1438 1439 } 1440 1441 else if (modify.toXMPP().equals(type)) { 1442 1443 return modify; 1444 1445 } 1446 1447 else if (auth.toXMPP().equals(type)) { 1448 1449 return auth; 1450 1451 } 1452 1453 else if (wait.toXMPP().equals(type)) { 1454 1455 return wait; 1456 1457 } 1458 1459 else { 1460 1461 throw new IllegalArgumentException ("Type invalid:" + type); 1462 1463 } 1464 1465 } 1466 1467 1468 1469 private String value; 1470 1471 1472 1473 private Type(String value) { 1474 1475 this.value = value; 1476 1477 } 1478 1479 1480 1481 1490 1491 public String toXMPP() { 1492 1493 return value; 1494 1495 } 1496 1497 } 1498 1499} | Popular Tags |