| 1 48 49 package com.caucho.burlap.io; 50 51 import com.caucho.hessian.io.Deserializer; 52 import com.caucho.hessian.io.HessianRemoteResolver; 53 import com.caucho.hessian.io.SerializerFactory; 54 55 import java.io.ByteArrayOutputStream ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.io.Reader ; 59 import java.lang.reflect.Field ; 60 import java.util.ArrayList ; 61 import java.util.Calendar ; 62 import java.util.Date ; 63 import java.util.HashMap ; 64 import java.util.TimeZone ; 65 66 82 public class BurlapInput extends AbstractBurlapInput { 83 private static int []base64Decode; 84 85 public final static int TAG_EOF = -1; 86 87 public final static int TAG_NULL = 0; 88 public final static int TAG_BOOLEAN = 1; 89 public final static int TAG_INT = 2; 90 public final static int TAG_LONG = 3; 91 public final static int TAG_DOUBLE = 4; 92 public final static int TAG_DATE = 5; 93 public final static int TAG_STRING = 6; 94 public final static int TAG_XML = 7; 95 public final static int TAG_BASE64 = 8; 96 public final static int TAG_MAP = 9; 97 public final static int TAG_LIST = 10; 98 public final static int TAG_TYPE = 11; 99 public final static int TAG_LENGTH = 12; 100 101 public final static int TAG_REF = 13; 102 public final static int TAG_REMOTE = 14; 103 104 public final static int TAG_CALL = 15; 105 public final static int TAG_REPLY = 16; 106 public final static int TAG_FAULT = 17; 107 public final static int TAG_METHOD = 18; 108 public final static int TAG_HEADER = 19; 109 110 public final static int TAG_NULL_END = TAG_NULL + 100; 111 public final static int TAG_BOOLEAN_END = TAG_BOOLEAN + 100; 112 public final static int TAG_INT_END = TAG_INT + 100; 113 public final static int TAG_LONG_END = TAG_LONG + 100; 114 public final static int TAG_DOUBLE_END = TAG_DOUBLE + 100; 115 public final static int TAG_DATE_END = TAG_DATE + 100; 116 public final static int TAG_STRING_END = TAG_STRING + 100; 117 public final static int TAG_XML_END = TAG_XML + 100; 118 public final static int TAG_BASE64_END = TAG_BASE64 + 100; 119 public final static int TAG_MAP_END = TAG_MAP + 100; 120 public final static int TAG_LIST_END = TAG_LIST + 100; 121 public final static int TAG_TYPE_END = TAG_TYPE + 100; 122 public final static int TAG_LENGTH_END = TAG_LENGTH + 100; 123 124 public final static int TAG_REF_END = TAG_REF + 100; 125 public final static int TAG_REMOTE_END = TAG_REMOTE + 100; 126 127 public final static int TAG_CALL_END = TAG_CALL + 100; 128 public final static int TAG_REPLY_END = TAG_REPLY + 100; 129 public final static int TAG_FAULT_END = TAG_FAULT + 100; 130 public final static int TAG_METHOD_END = TAG_METHOD + 100; 131 public final static int TAG_HEADER_END = TAG_HEADER + 100; 132 133 private static HashMap _tagMap; 134 135 private static Field _detailMessageField; 136 137 protected SerializerFactory _serializerFactory; 138 139 protected ArrayList _refs; 140 141 private InputStream _is; 143 protected int _peek = -1; 145 146 private String _method; 148 149 private int _peekTag; 150 151 private Throwable _replyFault; 152 153 protected StringBuffer _sbuf = new StringBuffer (); 154 protected StringBuffer _entityBuffer = new StringBuffer (); 155 156 protected Calendar _utcCalendar; 157 protected Calendar _localCalendar; 158 159 162 public BurlapInput() 163 { 164 } 165 166 172 public BurlapInput(InputStream is) 173 { 174 init(is); 175 } 176 177 180 public void setSerializerFactory(SerializerFactory factory) 181 { 182 _serializerFactory = factory; 183 } 184 185 188 public SerializerFactory getSerializerFactory() 189 { 190 return _serializerFactory; 191 } 192 193 196 public void init(InputStream is) 197 { 198 _is = is; 199 _method = null; 200 _peek = -1; 201 _peekTag = -1; 202 _refs = null; 203 _replyFault = null; 204 205 if (_serializerFactory == null) 206 _serializerFactory = new SerializerFactory(); 207 } 208 209 212 public String getMethod() 213 { 214 return _method; 215 } 216 217 220 public Throwable getReplyFault() 221 { 222 return _replyFault; 223 } 224 225 233 public void startCall() 234 throws IOException  235 { 236 readCall(); 237 238 while ((readHeader() != null)) 239 readObject(); 240 241 readMethod(); 242 } 243 244 253 public int readCall() 254 throws IOException  255 { 256 expectTag(TAG_CALL); 257 258 int major = 1; 259 int minor = 0; 260 261 return (major << 16) + minor; 262 } 263 264 271 public String readMethod() 272 throws IOException  273 { 274 expectTag(TAG_METHOD); 275 276 _method = parseString(); 277 278 expectTag(TAG_METHOD_END); 279 280 return _method; 281 } 282 283 292 public void completeCall() 293 throws IOException  294 { 295 expectTag(TAG_CALL_END); 296 } 297 298 302 public Object readReply(Class expectedClass) 303 throws Throwable  304 { 305 expectTag(TAG_REPLY); 306 307 int tag = parseTag(); 308 309 if (tag == TAG_FAULT) 310 throw prepareFault(); 311 else { 312 _peekTag = tag; 313 Object value = readObject(expectedClass); 314 315 expectTag(TAG_REPLY_END); 316 317 return value; 318 } 319 } 320 321 331 public void startReply() 332 throws Throwable  333 { 334 expectTag(TAG_REPLY); 335 336 int tag = parseTag(); 337 if (tag == TAG_FAULT) 338 throw prepareFault(); 339 else 340 _peekTag = tag; 341 } 342 343 346 private Throwable prepareFault() 347 throws IOException  348 { 349 HashMap fault = readFault(); 350 351 Object detail = fault.get("detail"); 352 String message = (String ) fault.get("message"); 353 354 if (detail instanceof Throwable ) { 355 _replyFault = (Throwable ) detail; 356 357 if (message != null && _detailMessageField != null) { 358 try { 359 _detailMessageField.set(_replyFault, message); 360 } catch (Throwable e) { 361 } 362 } 363 364 return _replyFault; 365 } 366 367 else { 368 String code = (String ) fault.get("code"); 369 370 _replyFault = new BurlapServiceException(message, code, detail); 371 372 return _replyFault; 373 } 374 } 375 376 385 public void completeReply() 386 throws IOException  387 { 388 expectTag(TAG_REPLY_END); 389 } 390 391 398 public String readHeader() 399 throws IOException  400 { 401 int tag = parseTag(); 402 403 if (tag == TAG_HEADER) { 404 _sbuf.setLength(0); 405 String value = parseString(_sbuf).toString(); 406 expectTag(TAG_HEADER_END); 407 return value; 408 } 409 410 _peekTag = tag; 411 412 return null; 413 } 414 415 422 public void readNull() 423 throws IOException  424 { 425 int tag = parseTag(); 426 427 switch (tag) { 428 case TAG_NULL: 429 expectTag(TAG_NULL_END); 430 return; 431 432 default: 433 throw expectedTag("null", tag); 434 } 435 } 436 437 445 public boolean readBoolean() 446 throws IOException  447 { 448 int tag = parseTag(); 449 450 boolean value; 451 452 switch (tag) { 453 case TAG_NULL: 454 value = false; 455 expectTag(TAG_NULL_END); 456 return value; 457 458 case TAG_BOOLEAN: 459 value = parseInt() != 0; 460 expectTag(TAG_BOOLEAN_END); 461 return value; 462 463 case TAG_INT: 464 value = parseInt() != 0; 465 expectTag(TAG_INT_END); 466 return value; 467 468 case TAG_LONG: 469 value = parseLong() != 0; 470 expectTag(TAG_LONG_END); 471 return value; 472 473 case TAG_DOUBLE: 474 value = parseDouble() != 0; 475 expectTag(TAG_DOUBLE_END); 476 return value; 477 478 default: 479 throw expectedTag("boolean", tag); 480 } 481 } 482 483 490 public byte readByte() 491 throws IOException  492 { 493 return (byte) readInt(); 494 } 495 496 503 public short readShort() 504 throws IOException  505 { 506 return (short) readInt(); 507 } 508 509 516 public int readInt() 517 throws IOException  518 { 519 int tag = parseTag(); 520 521 int value; 522 523 switch (tag) { 524 case TAG_NULL: 525 value = 0; 526 expectTag(TAG_NULL_END); 527 return value; 528 529 case TAG_BOOLEAN: 530 value = parseInt(); 531 expectTag(TAG_BOOLEAN_END); 532 return value; 533 534 case TAG_INT: 535 value = parseInt(); 536 expectTag(TAG_INT_END); 537 return value; 538 539 case TAG_LONG: 540 value = (int) parseLong(); 541 expectTag(TAG_LONG_END); 542 return value; 543 544 case TAG_DOUBLE: 545 value = (int) parseDouble(); 546 expectTag(TAG_DOUBLE_END); 547 return value; 548 549 default: 550 throw expectedTag("int", tag); 551 } 552 } 553 554 561 public long readLong() 562 throws IOException  563 { 564 int tag = parseTag(); 565 566 long value; 567 568 switch (tag) { 569 case TAG_NULL: 570 value = 0; 571 expectTag(TAG_NULL_END); 572 return value; 573 574 case TAG_BOOLEAN: 575 value = parseInt(); 576 expectTag(TAG_BOOLEAN_END); 577 return value; 578 579 case TAG_INT: 580 value = parseInt(); 581 expectTag(TAG_INT_END); 582 return value; 583 584 case TAG_LONG: 585 value = parseLong(); 586 expectTag(TAG_LONG_END); 587 return value; 588 589 case TAG_DOUBLE: 590 value = (long) parseDouble(); 591 expectTag(TAG_DOUBLE_END); 592 return value; 593 594 default: 595 throw expectedTag("long", tag); 596 } 597 } 598 599 606 public float readFloat() 607 throws IOException  608 { 609 return (float) readDouble(); 610 } 611 612 619 public double readDouble() 620 throws IOException  621 { 622 int tag = parseTag(); 623 624 double value; 625 626 switch (tag) { 627 case TAG_NULL: 628 value = 0; 629 expectTag(TAG_NULL_END); 630 return value; 631 632 case TAG_BOOLEAN: 633 value = parseInt(); 634 expectTag(TAG_BOOLEAN_END); 635 return value; 636 637 case TAG_INT: 638 value = parseInt(); 639 expectTag(TAG_INT_END); 640 return value; 641 642 case TAG_LONG: 643 value = parseLong(); 644 expectTag(TAG_LONG_END); 645 return value; 646 647 case TAG_DOUBLE: 648 value = parseDouble(); 649 expectTag(TAG_DOUBLE_END); 650 return value; 651 652 default: 653 throw expectedTag("double", tag); 654 } 655 } 656 657 664 public long readUTCDate() 665 throws IOException  666 { 667 int tag = parseTag(); 668 669 if (tag != TAG_DATE) 670 throw error("expected date"); 671 672 if (_utcCalendar == null) 673 _utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 674 675 long value = parseDate(_utcCalendar); 676 677 expectTag(TAG_DATE_END); 678 679 return value; 680 } 681 682 689 public long readLocalDate() 690 throws IOException  691 { 692 int tag = parseTag(); 693 694 if (tag != TAG_DATE) 695 throw error("expected date"); 696 697 if (_localCalendar == null) 698 _localCalendar = Calendar.getInstance(); 699 700 long value = parseDate(_localCalendar); 701 702 expectTag(TAG_DATE_END); 703 704 return value; 705 } 706 707 714 public String readString() 715 throws IOException  716 { 717 int tag = parseTag(); 718 719 String value; 720 721 switch (tag) { 722 case TAG_NULL: 723 expectTag(TAG_NULL_END); 724 return null; 725 726 case TAG_STRING: 727 _sbuf.setLength(0); 728 value = parseString(_sbuf).toString(); 729 expectTag(TAG_STRING_END); 730 return value; 731 732 case TAG_XML: 733 _sbuf.setLength(0); 734 value = parseString(_sbuf).toString(); 735 expectTag(TAG_XML_END); 736 return value; 737 738 default: 739 throw expectedTag("string", tag); 740 } 741 } 742 743 750 public org.w3c.dom.Node readNode() 751 throws IOException  752 { 753 int tag = read(); 754 755 switch (tag) { 756 case 'N': 757 return null; 758 759 case 'S': 760 case 's': 761 case 'X': 762 case 'x': 763 throw error("can't cope"); 764 765 default: 766 throw expectedTag("string", tag); 767 } 768 } 769 770 777 public byte []readBytes() 778 throws IOException  779 { 780 int tag = parseTag(); 781 782 switch (tag) { 783 case TAG_NULL: 784 expectTag(TAG_NULL_END); 785 return null; 786 787 case TAG_BASE64: 788 byte []data = parseBytes(); 789 expectTag(TAG_BASE64_END); 790 791 return data; 792 793 default: 794 throw expectedTag("bytes", tag); 795 } 796 } 797 798 805 public int readLength() 806 throws IOException  807 { 808 int tag = parseTag(); 809 810 if (tag != TAG_LENGTH) { 811 _peekTag = tag; 812 return -1; 813 } 814 815 int value = parseInt(); 816 817 expectTag(TAG_LENGTH_END); 818 819 return value; 820 } 821 822 825 private HashMap readFault() 826 throws IOException  827 { 828 HashMap map = new HashMap (); 829 830 int code = parseTag(); 831 for (; code >= 0 && code != TAG_FAULT_END; code = parseTag()) { 832 _peekTag = code; 833 834 Object key = readObject(); 835 Object value = readObject(); 836 837 if (key != null && value != null) 838 map.put(key, value); 839 } 840 841 if (code != TAG_FAULT_END) 842 throw expectedTag("fault", code); 843 844 return map; 845 } 846 847 850 public Object readObject(Class cl) 851 throws IOException  852 { 853 if (cl == null || cl.equals(Object .class)) 854 return readObject(); 855 856 int tag = parseTag(); 857 858 switch (tag) { 859 case TAG_NULL: 860 expectTag(TAG_NULL_END); 861 return null; 862 863 case TAG_MAP: 864 { 865 String type = readType(); 866 Deserializer reader; 867 reader = _serializerFactory.getObjectDeserializer(type); 868 869 if (cl != reader.getType() && cl.isAssignableFrom(reader.getType())) 870 return reader.readMap(this); 871 872 reader = _serializerFactory.getDeserializer(cl); 873 874 return reader.readMap(this); 875 } 876 877 case TAG_LIST: 878 { 879 String type = readType(); 880 int length = readLength(); 881 882 Deserializer reader; 883 reader = _serializerFactory.getObjectDeserializer(type); 884 885 if (cl != reader.getType() && cl.isAssignableFrom(reader.getType())) 886 return reader.readList(this, length); 887 888 reader = _serializerFactory.getDeserializer(cl); 889 890 return reader.readList(this, length); 891 } 892 893 case TAG_REF: 894 { 895 int ref = parseInt(); 896 897 expectTag(TAG_REF_END); 898 899 return _refs.get(ref); 900 } 901 902 case TAG_REMOTE: 903 { 904 String type = readType(); 905 String url = readString(); 906 907 expectTag(TAG_REMOTE_END); 908 909 Object remote = resolveRemote(type, url); 910 911 return remote; 912 } 913 } 914 915 _peekTag = tag; 916 917 Object value = _serializerFactory.getDeserializer(cl).readObject(this); 918 919 return value; 920 } 921 922 926 public Object readObject() 927 throws IOException  928 { 929 int tag = parseTag(); 930 931 switch (tag) { 932 case TAG_NULL: 933 expectTag(TAG_NULL_END); 934 return null; 935 936 case TAG_BOOLEAN: 937 { 938 int value = parseInt(); 939 expectTag(TAG_BOOLEAN_END); 940 return new Boolean (value != 0); 941 } 942 943 case TAG_INT: 944 { 945 int value = parseInt(); 946 expectTag(TAG_INT_END); 947 return new Integer (value); 948 } 949 950 case TAG_LONG: 951 { 952 long value = parseLong(); 953 expectTag(TAG_LONG_END); 954 return new Long (value); 955 } 956 957 case TAG_DOUBLE: 958 { 959 double value = parseDouble(); 960 expectTag(TAG_DOUBLE_END); 961 return new Double (value); 962 } 963 964 case TAG_DATE: 965 { 966 long value = parseDate(); 967 expectTag(TAG_DATE_END); 968 return new Date (value); 969 } 970 971 case TAG_XML: 972 { 973 return parseXML(); 974 } 975 976 case TAG_STRING: 977 { 978 _sbuf.setLength(0); 979 980 String value = parseString(_sbuf).toString(); 981 982 expectTag(TAG_STRING_END); 983 984 return value; 985 } 986 987 case TAG_BASE64: 988 { 989 byte []data = parseBytes(); 990 991 expectTag(TAG_BASE64_END); 992 993 return data; 994 } 995 996 case TAG_LIST: 997 { 998 String type = readType(); 999 int length = readLength(); 1000 1001 return _serializerFactory.readList(this, length, type); 1002 } 1003 1004 case TAG_MAP: 1005 { 1006 String type = readType(); 1007 Deserializer deserializer; 1008 deserializer = _serializerFactory.getObjectDeserializer(type); 1009 1010 return deserializer.readMap(this); 1011 } 1012 1013 case TAG_REF: 1014 { 1015 int ref = parseInt(); 1016 1017 expectTag(TAG_REF_END); 1018 1019 return _refs.get(ref); 1020 } 1021 1022 case TAG_REMOTE: 1023 { 1024 String type = readType(); 1025 String url = readString(); 1026 1027 expectTag(TAG_REMOTE_END); 1028 1029 return resolveRemote(type, url); 1030 } 1031 1032 default: 1033 throw error("unknown code:" + tagName(tag)); 1034 } 1035 } 1036 1037 1040 public Object readRemote() 1041 throws IOException  1042 { 1043 String type = readType(); 1044 String url = readString(); 1045 1046 return resolveRemote(type, url); 1047 } 1048 1049 1052 public Object readRef() 1053 throws IOException  1054 { 1055 return _refs.get(parseInt()); 1056 } 1057 1058 1061 public int readListStart() 1062 throws IOException  1063 { 1064 return parseTag(); 1065 } 1066 1067 1070 public int readMapStart() 1071 throws IOException  1072 { 1073 return parseTag(); 1074 } 1075 1076 1079 public boolean isEnd() 1080 throws IOException  1081 { 1082 int code = parseTag(); 1083 1084 _peekTag = code; 1085 1086 return (code < 0 || code >= 100); 1087 } 1088 1089 1092 public void readEnd() 1093 throws IOException  1094 { 1095 int code = parseTag(); 1096 1097 if (code < 100) 1098 throw error("unknown code:" + (char) code); 1099 } 1100 1101 1104 public void readMapEnd() 1105 throws IOException  1106 { 1107 expectTag(TAG_MAP_END); 1108 } 1109 1110 1113 public void readListEnd() 1114 throws IOException  1115 { 1116 expectTag(TAG_LIST_END); 1117 } 1118 1119 |