- Timestamp:
- 05/27/13 15:54:47 (11 years ago)
- Location:
- SRUClient/trunk/src
- Files:
-
- 2 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClient.java
r2466 r2936 56 56 private List<SRUDiagnostic> diagnostics; 57 57 private DocumentFragment extraResponseData; 58 /* explain */59 private SRURecord record;60 58 /* scan */ 61 59 private List<SRUTerm> terms; … … 64 62 private String resultSetId; 65 63 private int resultSetIdleTime; 64 private int nextRecordPosition; 65 /* explain/searchRetrieve */ 66 66 private List<SRURecord> records; 67 private int nextRecordPosition;68 67 /* statistics */ 69 68 private int totalBytesTransferred; … … 167 166 /** 168 167 * Register a record data parser. 169 * 168 * 170 169 * @param parser 171 170 * a parser instance 172 * @throws SRUClientException173 * if a parser handing the same record schema is already174 * registered175 171 * @throws NullPointerException 176 172 * if any required argument is <code>null</code> 177 173 * @throws IllegalArgumentException 178 * if the supplied parser is invalid 179 * /180 public void registerRecordParser(SRURecordDataParser parser)181 throws SRUClientException{174 * if the supplied parser is invalid or a parser handing the 175 * same record schema is already registered 176 */ 177 public void registerRecordParser(SRURecordDataParser parser) { 182 178 client.registerRecordParser(parser); 183 179 } … … 202 198 try { 203 199 client.explain(request, handler); 200 SRURecord record = null; 201 if ((records != null) && !records.isEmpty()) { 202 record = records.get(0); 203 } 204 204 return new SRUExplainResponse(request, 205 205 diagnostics, … … 313 313 diagnostics = null; 314 314 extraResponseData = null; 315 /* explain */316 record = null;317 315 /* scan */ 318 316 terms = null; … … 321 319 resultSetId = null; 322 320 resultSetIdleTime = -1; 321 nextRecordPosition = -1; 322 /* explain/searchRetrieve */ 323 323 records = null; 324 nextRecordPosition = -1;325 324 /* statistics */ 326 325 totalBytesTransferred = -1; -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainHandler.java
r2466 r2936 76 76 throws XMLStreamException, SRUClientException; 77 77 78 /** 79 * Receive notifications of the start of the enumeration of records in the 80 * response. 81 * 82 * @param numberOfRecords 83 * the number of records or <code>-1</code> if not available 84 * @param resultSetId 85 * the result set id or <code>null</code> if not available 86 * @param resultSetIdleTime 87 * the result set idle time or <code>-1</code> if not available 88 * @throws SRUClientException 89 * any SRU exception, possibly wrapping another exception 90 */ 91 public void onStartRecords(int numberOfRecords, String resultSetId, 92 int resultSetIdleTime) throws SRUClientException; 93 94 95 /** 96 * Receive notifications of the end of the enumeration of records in the 97 * response. 98 * 99 * @param nextRecordPosition 100 * the next record position or <code>-1</code> if not available 101 * @throws SRUClientException 102 * any SRU exception, possibly wrapping another exception 103 */ 104 public void onFinishRecords(int nextRecordPosition) 105 throws SRUClientException; 106 107 108 /** 109 * Receive notification of a record in the result set. 110 * 111 * @param identifier 112 * identifier of the record or <code>null</code> if not available 113 * @param position 114 * position of the record in the result set or <code>-1</code> if 115 * not available 116 * @param data 117 * the parsed record data 118 * @throws SRUClientException 119 * any SRU exception, possibly wrapping another exception 120 * @see SRURecordData 121 * @see SRURecordDataParser 122 */ 123 public void onRecord(String identifier, int position, SRURecordData data) 124 throws SRUClientException; 125 126 127 /** 128 * Receive notification of extra record data. 129 * 130 * @param identifier 131 * identifier of the record or <code>null</code> if not available 132 * @param position 133 * position of the record in the result set or <code>-1</code> if 134 * not available 135 * @param reader 136 * a {@link XMLStreamReader} to parse the extra term data 137 * @throws XMLStreamException 138 * an error occurred while parsing the response 139 * @throws SRUClientException 140 * any SRU exception, possibly wrapping another exception 141 */ 142 public void onExtraRecordData(String identifier, int position, 143 XMLStreamReader reader) throws XMLStreamException, 144 SRUClientException; 145 78 146 } // interface SRUExplainHandler -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRURecordDataParser.java
r2466 r2936 39 39 * @param reader 40 40 * a {@link XMLStreamReader} to parse the record data 41 * @param version 42 * the {@link SRUVersion} that was used to encode the record 41 43 * @return the parsed record 42 44 * @throws XMLStreamException … … 46 48 * @see SRURecordData 47 49 */ 48 public SRURecordData parse(XMLStreamReader reader )50 public SRURecordData parse(XMLStreamReader reader, SRUVersion version) 49 51 throws XMLStreamException, SRUClientException; 50 52 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java
r2879 r2936 84 84 private final HttpClient httpClient; 85 85 private final XmlStreamReaderProxy proxy = new XmlStreamReaderProxy(); 86 86 private final SRUExplainRecordDataParser explainRecordParser = 87 new SRUExplainRecordDataParser(); 87 88 88 89 /** … … 193 194 * @param parser 194 195 * a parser instance 195 * @throws SRUClientException196 * if a parser handing the same record schema is already197 * registered198 196 * @throws NullPointerException 199 197 * if any required argument is <code>null</code> 200 198 * @throws IllegalArgumentException 201 * if the supplied parser is invalid 199 * if the supplied parser is invalid or a parser handing the 200 * same record schema is already registered 202 201 */ 203 public void registerRecordParser(SRURecordDataParser parser) 204 throws SRUClientException { 202 public void registerRecordParser(SRURecordDataParser parser) { 205 203 if (parser == null) { 206 204 throw new NullPointerException("parser == null"); … … 218 216 parsers.put(recordSchema, parser); 219 217 } else { 220 throw new SRUClientException(218 throw new IllegalArgumentException( 221 219 "record data parser already registered: " + recordSchema); 222 220 } … … 247 245 throw new NullPointerException("handler == null"); 248 246 } 249 logger.debug(" explain");247 logger.debug("performing explain request"); 250 248 251 249 final long ts_start = System.nanoTime(); … … 332 330 throw new NullPointerException("handler == null"); 333 331 } 334 logger.debug("searchRetrieve: scanClause = {}", request.getScanClause()); 332 logger.debug("performing scan request: scanClause = {}", 333 request.getScanClause()); 335 334 336 335 final long ts_start = System.nanoTime(); … … 417 416 throw new NullPointerException("handler == null"); 418 417 } 419 logger.debug("searchRetrieve: query = {}", request.getQuery()); 418 logger.debug("performing searchRetrieve request: query = {}", 419 request.getQuery()); 420 420 421 421 final long ts_start = System.nanoTime(); … … 483 483 HttpResponse response = null; 484 484 try { 485 logger.debug(" performing HTTP request: {}", uri.toString());485 logger.debug("submitting HTTP request: {}", uri.toString()); 486 486 try { 487 487 request = new HttpGet(uri); … … 535 535 536 536 private void parseExplainResponse(final SRUXMLStreamReader reader, 537 final SRU AbstractRequest request, final SRUExplainHandler handler)537 final SRUExplainRequest request, final SRUExplainHandler handler) 538 538 throws SRUClientException { 539 539 logger.debug("parsing 'explain' response"); 540 // FIXME: ship explain record data for now 541 // doParseExplainResponse(reader, request, handler, true); 542 doParseExplainResponse(reader, request, handler, false); 543 } 544 545 546 private void doParseExplainResponse(SRUXMLStreamReader reader, 547 SRUAbstractRequest request, SRUExplainHandler handler, 548 boolean parseRecordData) throws SRUClientException { 540 549 try { 541 550 // explainResponse … … 549 558 // explainResponse/record 550 559 reader.readStart(SRU_NS, "record", true); 551 552 String schema = reader.readContent(SRU_NS, "recordSchema", true); 553 554 SRURecordPacking packing = parseRecordPacking(reader, strictMode); 555 556 logger.debug("schema = {}, packing = {}", schema, packing); 557 558 // explainResponse/record/recordData 559 reader.readStart(SRU_NS, "recordData", true); 560 reader.readEnd(SRU_NS, "recordData", true); 561 562 // explainResponse/record/recordPosition 563 if (reader.readStart(SRU_NS, "recordPosition", false)) { 564 reader.readEnd(SRU_NS, "recordPosition", true); 565 } 566 567 // explainResponse/record/extraRecordData 568 if (reader.readStart(SRU_NS, "extraRecordData", false)) { 569 reader.readEnd(SRU_NS, "extraRecordData", true); 570 } 571 572 reader.readEnd(SRU_NS, "record"); 560 if (parseRecordData) { 561 handler.onStartRecords(-1, null, -1); 562 563 SRURecordPacking packing = null; 564 if (!strictMode && reader.peekStart(SRU_NS, "recordPacking")) { 565 packing = parseRecordPacking(reader, false); 566 if (packing != null) { 567 logger.error("element <recordPacking> must apperear after element <recordSchema> within element <record>"); 568 } 569 } 570 String schema = 571 reader.readContent(SRU_NS, "recordSchema", true); 572 if (packing == null) { 573 packing = parseRecordPacking(reader, strictMode); 574 } 575 logger.debug("schema = {}, packing = {}", schema, packing); 576 577 // explainResponse/record/recordData 578 reader.readStart(SRU_NS, "recordData", true); 579 reader.consumeWhitespace(); 580 581 SRURecordData recordData = null; 582 SRUXMLStreamReader recordReader = null; 583 584 if (packing == SRURecordPacking.STRING) { 585 /* 586 * read content into temporary buffer and then use a new XML 587 * reader to parse record data 588 */ 589 final String data = reader.readString(true); 590 InputStream in = new ByteArrayInputStream(data.getBytes()); 591 // FIXME: namespace context? 592 recordReader = createReader(in, false); 593 } else { 594 recordReader = reader; 595 } 596 597 if (!SRUExplainRecordData.RECORD_SCHEMA.equals(schema)) { 598 // FIXME: message 599 throw new SRUClientException("bad record schema"); 600 } 601 try { 602 proxy.reset(recordReader); 603 recordData = explainRecordParser.parse(proxy, version); 604 } catch (XMLStreamException e) { 605 throw new SRUClientException( 606 "error parsing explain record", e); 607 } 608 if (recordData == null) { 609 // FIXME: error message 610 throw new SRUClientException( 611 "error parsing explain record"); 612 } 613 if (packing == SRURecordPacking.STRING) { 614 recordReader.closeCompletly(); 615 } 616 reader.consumeWhitespace(); 617 reader.readEnd(SRU_NS, "recordData", true); 618 619 if (version == SRUVersion.VERSION_1_2) { 620 reader.readContent(SRU_NS, "recordIdentifier", false); 621 } 622 reader.readContent(SRU_NS, "recordPosition", false, -1); 623 624 // notify handler 625 handler.onRecord(null, -1, recordData); 626 627 if (reader.readStart(SRU_NS, "extraRecordData", false)) { 628 reader.consumeWhitespace(); 629 proxy.reset(reader); 630 try { 631 handler.onExtraRecordData(null, -1, proxy); 632 } catch (XMLStreamException e) { 633 throw new SRUClientException("handler " 634 + "triggered error while parsing " 635 + "'extraRecordData'", e); 636 } 637 reader.consumeWhitespace(); 638 reader.readEnd(SRU_NS, "extraRecordData", true); 639 } 640 641 handler.onFinishRecords(-1); 642 643 reader.readEnd(SRU_NS, "record"); 644 } else { 645 /* 646 * do not really parse record and skip everything until <record> 647 * end tag 648 */ 649 reader.readEnd(SRU_NS, "record", true); 650 } 573 651 574 652 // explainResponse/echoedExplainRequest … … 625 703 */ 626 704 if (reader.peekStart(SRU_NS, "explainResponse")) { 627 parseExplainResponse(reader, request, new SRUExplainHandler() {705 doParseExplainResponse(reader, request, new SRUExplainHandler() { 628 706 @Override 629 707 public void onRequestStatistics(int bytes, long millisTotal, … … 643 721 handler.onDiagnostics(diagnostics); 644 722 } 645 }); 723 724 725 @Override 726 public void onStartRecords(int numberOfRecords, 727 String resultSetId, int resultSetIdleTime) 728 throws SRUClientException { 729 } 730 731 732 @Override 733 public void onFinishRecords(int nextRecordPosition) 734 throws SRUClientException { 735 } 736 737 738 @Override 739 public void onRecord(String identifier, int position, 740 SRURecordData data) throws SRUClientException { 741 } 742 743 744 @Override 745 public void onExtraRecordData(String identifier, 746 int position, XMLStreamReader reader) 747 throws XMLStreamException, SRUClientException { 748 } 749 }, false); 646 750 } else { 647 751 logger.debug("parsing 'scanResponse' response"); … … 665 769 666 770 // scanResponse/terms/value 667 String value = reader668 .readContent(SRU_NS, "value", true);771 String value = 772 reader.readContent(SRU_NS, "value", true); 669 773 670 774 // scanResponse/terms/numberOfRecords … … 776 880 */ 777 881 if (reader.peekStart(SRU_NS, "explainResponse")) { 778 parseExplainResponse(reader, request, new SRUExplainHandler() {882 doParseExplainResponse(reader, request, new SRUExplainHandler() { 779 883 @Override 780 884 public void onRequestStatistics(int bytes, long millisTotal, … … 794 898 handler.onDiagnostics(diagnostics); 795 899 } 796 }); 900 901 902 @Override 903 public void onStartRecords(int numberOfRecords, 904 String resultSetId, int resultSetIdleTime) 905 throws SRUClientException { 906 } 907 908 909 @Override 910 public void onFinishRecords(int nextRecordPosition) 911 throws SRUClientException { 912 } 913 914 915 @Override 916 public void onRecord(String identifier, int position, 917 SRURecordData data) throws SRUClientException { 918 } 919 920 921 @Override 922 public void onExtraRecordData(String identifier, 923 int position, XMLStreamReader reader) 924 throws XMLStreamException, SRUClientException { 925 } 926 }, false); 797 927 } else { 798 928 logger.debug("parsing 'searchRetrieve' response"); … … 824 954 // searchRetrieveResponse/results 825 955 if (numberOfRecords > 0) { 826 reader.readStart(SRU_NS, "records", true); 827 828 // searchRetrieveResponse/records/record 829 boolean first = true; 830 while (reader.readStart(SRU_NS, "record", first)) { 831 if (first) { 832 first = false; 833 handler.onStartRecords(numberOfRecords, 834 resultSetId, resultSetIdleTime); 835 } 836 837 String schema = reader.readContent(SRU_NS, 838 "recordSchema", true); 839 840 SRURecordPacking packing = 841 parseRecordPacking(reader, strictMode); 842 843 logger.debug("schema = {}, packing = {}, " + 844 "requested packing = {}", 845 new Object[] { schema, packing, 846 request.getRecordPacking() }); 847 848 if ((request.getRecordPacking() != null) && 849 (packing != request.getRecordPacking())) { 850 final SRURecordPacking p = 851 request.getRecordPacking(); 852 logger.error("requested '{}' record packing, but " + 853 "server responded with '{}' record packing", 854 p.getStringValue(), 855 packing.getStringValue()); 856 if (strictMode) { 857 throw new SRUClientException("requested '" + 858 p.getStringValue() + 859 "' record packing, but server " + 860 "responded with '" + 861 packing.getStringValue() + 862 "' record packing"); 956 /* 957 * some endpoints set numberOfRecords but do not serialize 958 * any records, e.g if requested record schema is not 959 * supported 960 */ 961 int recordCount = 0; 962 if (reader.readStart(SRU_NS, "records", false)) { 963 // searchRetrieveResponse/records/record 964 boolean first = true; 965 while (reader.readStart(SRU_NS, "record", first)) { 966 if (first) { 967 first = false; 968 handler.onStartRecords(numberOfRecords, 969 resultSetId, resultSetIdleTime); 863 970 } 864 } 865 866 // searchRetrieveResponse/record/recordData 867 reader.readStart(SRU_NS, "recordData", true); 868 reader.consumeWhitespace(); 869 870 SRURecordData recordData = null; 871 SRUDiagnostic surrogate = null; 872 SRUXMLStreamReader recordReader = null; 873 874 if (packing == SRURecordPacking.STRING) { 971 875 972 /* 876 * read content into temporary buffer and then use 877 * a new XML reader to parse record data 973 * common error: recordPacking before recordSchema 878 974 */ 879 final String data = reader.readString(true); 880 InputStream in = 881 new ByteArrayInputStream(data.getBytes()); 882 // FIXME: namespace context? 883 recordReader = createReader(in, false); 884 } else { 885 recordReader = reader; 886 } 887 888 if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) { 889 surrogate = parseDiagnostic(recordReader, 890 true, strictMode); 891 } else { 892 SRURecordDataParser parser = findParser(schema); 893 if (parser != null) { 975 SRURecordPacking packing = null; 976 if (!strictMode && 977 reader.peekStart(SRU_NS, "recordPacking")) { 978 packing = parseRecordPacking(reader, false); 979 if (packing != null) { 980 logger.error("element <recordPacking> " + 981 "must appear after element " + 982 "<recordSchema> within " + 983 "element <record>"); 984 } 985 } 986 String schema = reader.readContent(SRU_NS, 987 "recordSchema", true); 988 if (packing == null) { 989 packing = parseRecordPacking(reader, strictMode); 990 } 991 992 logger.debug("schema = {}, packing = {}, " + 993 "requested packing = {}", 994 new Object[] { schema, packing, 995 request.getRecordPacking() }); 996 997 if ((request.getRecordPacking() != null) && 998 (packing != request.getRecordPacking())) { 999 final SRURecordPacking p = 1000 request.getRecordPacking(); 1001 logger.error("requested '{}' record packing, " + 1002 "but server responded with '{}' " + 1003 "record packing", 1004 p.getStringValue(), 1005 packing.getStringValue()); 1006 if (strictMode) { 1007 throw new SRUClientException("requested '" + 1008 p.getStringValue() + 1009 "' record packing, but server " + 1010 "responded with '" + 1011 packing.getStringValue() + 1012 "' record packing"); 1013 } 1014 } 1015 1016 // searchRetrieveResponse/record/recordData 1017 reader.readStart(SRU_NS, "recordData", true); 1018 reader.consumeWhitespace(); 1019 1020 SRURecordData recordData = null; 1021 SRUDiagnostic surrogate = null; 1022 SRUXMLStreamReader recordReader = null; 1023 1024 if (packing == SRURecordPacking.STRING) { 1025 /* 1026 * read content into temporary buffer and then 1027 * use a new XML reader to parse record data 1028 */ 1029 final String data = reader.readString(true); 1030 InputStream in = new ByteArrayInputStream( 1031 data.getBytes()); 1032 // FIXME: namespace context? 1033 recordReader = createReader(in, false); 1034 } else { 1035 recordReader = reader; 1036 } 1037 1038 if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) { 1039 surrogate = parseDiagnostic(recordReader, true, 1040 strictMode); 1041 } else { 1042 SRURecordDataParser parser = findParser(schema); 1043 if (parser != null) { 1044 try { 1045 proxy.reset(recordReader); 1046 recordData = 1047 parser.parse(proxy, version); 1048 } catch (XMLStreamException e) { 1049 throw new SRUClientException( 1050 "error parsing record", e); 1051 } 1052 if (recordData == null) { 1053 logger.debug("record parser did not parse " 1054 + "record correctly and returned " 1055 + "null; injecting client side " 1056 + "surrogate diagnostic"); 1057 surrogate = new SRUDiagnostic( 1058 SRUClientDiagnostics.DIAG_RECORD_PARSER_NULL, 1059 null, 1060 "Record parser for schema '" + 1061 schema + 1062 "' did not " + 1063 "parse record correctly " + 1064 "and errornously " + 1065 "returned null."); 1066 } 1067 } else { 1068 /* 1069 * no record parser found, inject a 1070 * surrogate diagnostic 1071 */ 1072 logger.debug( 1073 "no record data parser found " 1074 + "for schema '{}'; injecting client " 1075 + "side surrogate diagnostic", 1076 schema); 1077 surrogate = new SRUDiagnostic( 1078 SRUClientDiagnostics.DIAG_NO_RECORD_PARSER, 1079 schema, 1080 "No record data parser for schema '" + 1081 schema + "' found."); 1082 } 1083 } 1084 1085 if (packing == SRURecordPacking.STRING) { 1086 recordReader.closeCompletly(); 1087 } 1088 1089 reader.consumeWhitespace(); 1090 reader.readEnd(SRU_NS, "recordData", true); 1091 1092 String identifier = null; 1093 if (version == SRUVersion.VERSION_1_2) { 1094 identifier = reader.readContent(SRU_NS, 1095 "recordIdentifier", false); 1096 } 1097 1098 int position = reader.readContent(SRU_NS, 1099 "recordPosition", false, -1); 1100 1101 logger.debug("recordIdentifier = {}, " + 1102 "recordPosition = {}", 1103 identifier, position); 1104 1105 // notify handler 1106 if (surrogate != null) { 1107 handler.onSurrogateRecord(identifier, 1108 position, surrogate); 1109 } else { 1110 if (recordData != null) { 1111 handler.onRecord(identifier, 1112 position, recordData); 1113 } 1114 } 1115 1116 if (reader.readStart(SRU_NS, 1117 "extraRecordData", false)) { 1118 reader.consumeWhitespace(); 1119 proxy.reset(reader); 894 1120 try { 895 proxy.reset(recordReader);896 recordData = parser.parse(proxy);1121 handler.onExtraRecordData(identifier, 1122 position, proxy); 897 1123 } catch (XMLStreamException e) { 898 throw new SRUClientException( 899 "error parsing record", e); 1124 throw new SRUClientException("handler " + 1125 "triggered error while parsing " + 1126 "'extraRecordData'", e); 900 1127 } 901 if (recordData == null) { 902 logger.debug("record parser did not parse " + 903 "record correctly and returned " + 904 "null; injecting client side " + 905 "surrogate diagnostic"); 906 surrogate = new SRUDiagnostic( 907 SRUClientDiagnostics.DIAG_RECORD_PARSER_NULL, 908 null, "Record parser for schema '" + 909 schema + "' did not " + 910 "parse record correctly " + 911 "and errornously " + 912 "returned null."); 913 } 914 } else { 915 /* 916 * no record parser found, inject a 917 * surrogate diagnostic 918 */ 919 logger.debug("no record data parser found " + 920 "for schema '{}'; injecting client " + 921 "side surrogate diagnostic", schema); 922 surrogate = new SRUDiagnostic( 923 SRUClientDiagnostics.DIAG_NO_RECORD_PARSER, 924 schema, 925 "No record data parser for schema '" + 926 schema + "' found."); 1128 reader.consumeWhitespace(); 1129 reader.readEnd(SRU_NS, "extraRecordData", true); 927 1130 } 928 } 929 930 if (packing == SRURecordPacking.STRING) { 931 recordReader.closeCompletly(); 932 } 933 934 reader.consumeWhitespace(); 935 reader.readEnd(SRU_NS, "recordData", true); 936 937 String identifier = null; 938 if (version == SRUVersion.VERSION_1_2) { 939 identifier = reader.readContent(SRU_NS, 940 "recordIdentifier", false); 941 } 942 943 int position = reader.readContent(SRU_NS, 944 "recordPosition", false, -1); 945 946 logger.debug("recordIdentifier = {}, recordPosition = {}", 947 identifier, position); 948 949 // notify handler 950 if (surrogate != null) { 951 handler.onSurrogateRecord(identifier, 952 position, surrogate); 953 } else { 954 if (recordData != null) { 955 handler.onRecord(identifier, 956 position, recordData); 957 } 958 } 959 960 if (reader.readStart(SRU_NS, "extraRecordData", false)) { 961 reader.consumeWhitespace(); 962 proxy.reset(reader); 963 try { 964 handler.onExtraRecordData(identifier, 965 position, proxy); 966 } catch (XMLStreamException e) { 967 throw new SRUClientException("handler " 968 + "triggered error while parsing " 969 + "'extraRecordData'", e); 970 } 971 reader.consumeWhitespace(); 972 reader.readEnd(SRU_NS, "extraRecordData", true); 973 } 974 975 reader.readEnd(SRU_NS, "record"); 976 } // while 977 reader.readEnd(SRU_NS, "records"); 1131 1132 reader.readEnd(SRU_NS, "record"); 1133 recordCount++; 1134 } // while 1135 reader.readEnd(SRU_NS, "records"); 1136 } 1137 if (recordCount == 0) { 1138 logger.error("endpoint declared {} results, but response contained no <record> elements (behavior may violate SRU specification)", numberOfRecords); 1139 } else if ((request.getMaximumRecords() != -1) && (recordCount > request.getMaximumRecords())) { 1140 logger.error("endpoint did not honour 'maximumRecords' request parameter and responded with {} records instead of a maximum of {}", recordCount, request.getMaximumRecords()); 1141 } 978 1142 } else { 979 1143 /* 980 * provide a better error format, if 1144 * provide a better error format, if endpoints responds with 1145 * an empty <records> element 981 1146 */ 982 1147 if (reader.readStart(SRU_NS, "records", false)) { … … 1000 1165 } else { 1001 1166 logger.error("endpoint declared 0 results, but " + 1002 "response contained an" + bad +1167 "response contained " + bad + 1003 1168 " record(s)"); 1004 1169 if (strictMode) { … … 1108 1273 1109 1274 private static SRUDiagnostic parseDiagnostic(SRUXMLStreamReader reader, 1110 boolean required, boolean st ictMode) throws XMLStreamException,1275 boolean required, boolean strictMode) throws XMLStreamException, 1111 1276 SRUClientException { 1112 1277 if (reader.readStart(SRU_DIAGNOSIC_NS, "diagnostic", required)) { … … 1115 1280 String uri = reader.readContent(SRU_DIAGNOSIC_NS, "uri", true); 1116 1281 1117 // diagnostic/details 1118 String details = reader.readContent(SRU_DIAGNOSIC_NS, "details", 1119 false, stictMode); 1282 String details = null; 1283 String message = null; 1284 if (strictMode) { 1285 // diagnostic/details 1286 details = reader.readContent(SRU_DIAGNOSIC_NS, "details", 1287 false, true); 1288 1289 // diagnostic/message 1290 message = reader.readContent(SRU_DIAGNOSIC_NS, "message", 1291 false, true); 1292 } else { 1293 /* 1294 * common error: diagnostic/details and diagnostic/message may 1295 * appear in any order 1296 */ 1297 if (reader.peekStart(SRU_DIAGNOSIC_NS, "details")) { 1298 details = reader.readContent(SRU_DIAGNOSIC_NS, "details", 1299 false, false); 1300 message = reader.readContent(SRU_DIAGNOSIC_NS, "message", 1301 false, false); 1302 } else { 1303 message = reader.readContent(SRU_DIAGNOSIC_NS, "message", 1304 false, false); 1305 details = reader.readContent(SRU_DIAGNOSIC_NS, "details", 1306 false, false); 1307 if ((message != null) && (details != null)) { 1308 logger.error("element <message> and element " + 1309 "<details> within element <diagnostic> " + 1310 "appeared in wrong order"); 1311 } 1312 } 1313 } 1314 1120 1315 if ((details != null) && details.isEmpty()) { 1121 1316 details = null; 1122 logger. warn("omitting empty element <details> " +1317 logger.debug("omitting empty element <details> " + 1123 1318 "within element <diagnostic>"); 1124 1319 } 1125 1126 // diagnostic/message1127 String message = reader.readContent(SRU_DIAGNOSIC_NS, "message",1128 false, stictMode);1129 1320 if ((message != null) && message.isEmpty()) { 1130 1321 message = null; 1131 logger. warn("omitting empty element <message> " +1322 logger.debug("omitting empty element <message> " + 1132 1323 "within element <diagnostic>"); 1133 1324 } -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUThreadedClient.java
r2870 r2936 107 107 /** 108 108 * Register a record data parser. 109 * 109 * 110 110 * @param parser 111 111 * a parser instance 112 * @throws SRUClientException113 * if a parser handing the same record schema is already114 * registered115 112 * @throws NullPointerException 116 113 * if any required argument is <code>null</code> 117 114 * @throws IllegalArgumentException 118 * if the supplied parser is invalid 119 * /120 public void registerRecordParser(SRURecordDataParser parser)121 throws SRUClientException{115 * if the supplied parser is invalid or a parser handing the 116 * same record schema is already registered 117 */ 118 public void registerRecordParser(SRURecordDataParser parser) { 122 119 if (parser == null) { 123 120 throw new NullPointerException("parser == null"); … … 133 130 134 131 if (parsers.putIfAbsent(recordSchema, parser) != null) { 135 throw new SRUClientException(132 throw new IllegalArgumentException( 136 133 "record data parser already registered: " + recordSchema); 137 134 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/XmlStreamReaderUtils.java
r2466 r2936 32 32 } 33 33 34 public static boolean readStart(XMLStreamReader reader, String namespaceURI, String localName, boolean required) 34 35 public static boolean readStart(XMLStreamReader reader, 36 String namespaceURI, String localName, boolean required) 35 37 throws XMLStreamException { 36 38 return readStart(reader, namespaceURI, localName, required, false); … … 38 40 39 41 40 public static boolean readStart(XMLStreamReader reader, String namespaceURI, String localName, boolean required, 42 public static boolean readStart(XMLStreamReader reader, 43 String namespaceURI, String localName, boolean required, 41 44 boolean attributes) throws XMLStreamException { 42 45 // System.err.println("readStart (" + localName + ", required = " + … … 80 83 81 84 82 public static void readEnd(XMLStreamReader reader, String namespaceURI, String localName)83 throws XMLStreamException {85 public static void readEnd(XMLStreamReader reader, String namespaceURI, 86 String localName) throws XMLStreamException { 84 87 readEnd(reader, namespaceURI, localName, false); 85 88 } 86 89 87 90 88 public static void readEnd(XMLStreamReader reader, String namespaceURI, String localName, boolean skipContent)89 throws XMLStreamException {91 public static void readEnd(XMLStreamReader reader, String namespaceURI, 92 String localName, boolean skipContent) throws XMLStreamException { 90 93 // System.err.println("readEnd (" + localName + ") @ " + dumpState() + 91 94 // ", skipContent = " + skipContent); … … 135 138 136 139 137 public static String readContent(XMLStreamReader reader, String namespaceURI, String localName, boolean required) 140 public static String readContent(XMLStreamReader reader, 141 String namespaceURI, String localName, boolean required) 138 142 throws XMLStreamException { 139 143 String result = null; … … 146 150 147 151 148 public static int readContent(XMLStreamReader reader, String namespaceURI, String localName, boolean required, 149 int defaultValue) throws XMLStreamException { 152 public static int readContent(XMLStreamReader reader, String namespaceURI, 153 String localName, boolean required, int defaultValue) 154 throws XMLStreamException { 150 155 if (readStart(reader, namespaceURI, localName, required)) { 151 156 String s = readString(reader, true); … … 162 167 163 168 164 public static String readString(XMLStreamReader reader, boolean required) throws XMLStreamException { 169 public static String readString(XMLStreamReader reader, boolean required) 170 throws XMLStreamException { 165 171 // System.err.println("readString @ " + toReadable(reader)); 166 172 String s = null; … … 181 187 182 188 183 public static String readAttributeValue(XMLStreamReader reader, String namespaceURI, String localName) 189 public static String readAttributeValue(XMLStreamReader reader, 190 String namespaceURI, String localName, boolean required) 184 191 throws XMLStreamException { 185 192 if (!reader.isStartElement()) { … … 189 196 String attr = reader.getAttributeValue(namespaceURI, localName); 190 197 if (attr != null) { 191 attr = attr.trim().intern(); 198 attr = attr.trim(); 199 if (attr.isEmpty()) { 200 attr = null; 201 } 202 } 203 if ((attr == null) && required) { 204 throw new XMLStreamException("expected non-empty attribute '" + 205 new QName(namespaceURI, localName) + "' on element '" + 206 reader.getName() + "'", reader.getLocation()); 192 207 } 193 208 return attr; … … 195 210 196 211 197 public static String readNamespaceURI(XMLStreamReader reader) throws XMLStreamException { 212 public static String readAttributeValue(XMLStreamReader reader, 213 String namespaceURI, String localName) throws XMLStreamException { 214 return readAttributeValue(reader, namespaceURI, localName, false); 215 } 216 217 218 public static String readNamespaceURI(XMLStreamReader reader) 219 throws XMLStreamException { 198 220 if (!reader.isStartElement()) { 199 221 throw new XMLStreamException("not at a start elment event", … … 204 226 205 227 206 public static String peekElementLocalName(XMLStreamReader reader) throws XMLStreamException { 228 public static String peekElementLocalName(XMLStreamReader reader) 229 throws XMLStreamException { 207 230 if (!reader.isStartElement()) { 208 231 throw new XMLStreamException("not at a start elment event", … … 213 236 214 237 215 public static void consumeStart(XMLStreamReader reader) throws XMLStreamException { 238 public static void consumeStart(XMLStreamReader reader) 239 throws XMLStreamException { 216 240 if (!reader.isStartElement()) { 217 241 throw new XMLStreamException("not at a start elment event", … … 222 246 223 247 224 public static void consumeWhitespace(XMLStreamReader reader) throws XMLStreamException { 248 public static void consumeWhitespace(XMLStreamReader reader) 249 throws XMLStreamException { 225 250 while (reader.isWhiteSpace() && reader.hasNext()) { 226 251 reader.next(); -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/fcs/ClarinFCSRecordParser.java
r2467 r2936 27 27 import eu.clarin.sru.client.SRURecordData; 28 28 import eu.clarin.sru.client.SRURecordDataParser; 29 import eu.clarin.sru.client.SRUVersion; 29 30 import eu.clarin.sru.client.XmlStreamReaderUtils; 30 31 … … 53 54 54 55 @Override 55 public SRURecordData parse(XMLStreamReader reader )56 public SRURecordData parse(XMLStreamReader reader, SRUVersion version) 56 57 throws XMLStreamException, SRUClientException { 57 58 logger.debug("parsing CLARIN-FCS record"); -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestClient.java
r2467 r2936 33 33 SRUClient client = new SRUClient(); 34 34 35 try { 36 client.registerRecordParser(new ClarinFCSRecordParser()); 37 } catch (SRUClientException e) { 38 logger.error("error adding record parser", e); 39 System.exit(1); 40 } 35 // register record data parsers 36 client.registerRecordParser(new ClarinFCSRecordParser()); 41 37 42 38 // explain -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestSimpleClient.java
r2467 r2936 33 33 if (args.length > 0) { 34 34 logger.info("initializing client ..."); 35 SRUSimpleClient client = new SRUSimpleClient(SRUVersion.VERSION_1_2); 36 try { 37 client.registerRecordParser(new ClarinFCSRecordParser()); 38 } catch (SRUClientException e) { 39 logger.error("error adding record parser", e); 40 System.exit(1); 41 } 35 SRUSimpleClient client = new SRUSimpleClient(); 36 37 // register record data parsers 38 client.registerRecordParser(new ClarinFCSRecordParser()); 42 39 43 40 /* … … 110 107 (ClarinFCSRecordData) data; 111 108 TestUtils.dumpResource(record.getResource()); 109 } else if (SRUExplainRecordData.RECORD_SCHEMA 110 .equals(data.getRecordSchema())) { 111 TestUtils.dumpExplainRecordData(data); 112 112 } 113 113 } -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClient.java
r2870 r2936 35 35 SRUThreadedClient client = new SRUThreadedClient(); 36 36 37 try { 38 client.registerRecordParser(new ClarinFCSRecordParser()); 39 } catch (SRUClientException e) { 40 logger.error("error adding record parser", e); 41 System.exit(1); 42 } 37 // register record data parsers 38 client.registerRecordParser(new ClarinFCSRecordParser()); 43 39 44 40 try { -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClientCallback.java
r2467 r2936 34 34 SRUThreadedClient client = new SRUThreadedClient(); 35 35 36 try { 37 client.registerRecordParser(new ClarinFCSRecordParser()); 38 } catch (SRUClientException e) { 39 logger.error("error adding record parser", e); 40 System.exit(1); 41 } 36 // register record data parsers 37 client.registerRecordParser(new ClarinFCSRecordParser()); 42 38 43 39 try { -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestUtils.java
r2467 r2936 42 42 public static SRUScanRequest makeScanRequest(String baseURI) { 43 43 SRUScanRequest request = new SRUScanRequest(baseURI); 44 request.setScanClause("fcs.resource ");44 request.setScanClause("fcs.resource = root"); 45 45 request.setExtraRequestData("x-clarin-resource-info", "true"); 46 46 return request; … … 75 75 SRURecord record = response.getRecord(); 76 76 logger.info("schema = {}", record.getRecordSchema()); 77 if (record.isRecordSchema(SRUExplainRecordData.RECORD_SCHEMA)) { 78 dumpExplainRecordData(record.getRecordData()); 79 } 77 80 } 78 81 } … … 145 148 146 149 150 public static void dumpExplainRecordData(SRURecordData recordData) { 151 if (SRUExplainRecordData.RECORD_SCHEMA.equals(recordData.getRecordSchema())) { 152 SRUExplainRecordData data = (SRUExplainRecordData) recordData; 153 logger.info("host={}, port={}, database={}", new Object[] { 154 data.getServerInfo().getHost(), 155 data.getServerInfo().getPort(), 156 data.getServerInfo().getDatabase() 157 }); 158 159 } 160 } 161 162 147 163 public static void dumpResource(Resource resource) { 148 164 logger.info("CLARIN-FCS: pid={}, ref={}",
Note: See TracChangeset
for help on using the changeset viewer.