Changeset 1957
- Timestamp:
- 05/22/12 21:23:29 (12 years ago)
- Location:
- SRUServer/trunk/src/main/java/eu/clarin/sru/server
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRURequest.java
r1881 r1957 101 101 102 102 /** 103 * Get the <em>query</em> parameter for the request in raw string format.104 * Only available for <em>searchRetrieve</em> requests.105 *106 * @return the raw query or <code>null</code> if not a107 * <em>searchRetrieve</em> request108 */109 public String getRawQuery();110 111 112 /**113 103 * Get the <em>startRecord</em> parameter of this request. Only available 114 104 * for <em>searchRetrieve</em> requests. … … 123 113 * Get the <em>maximumRecords</em> parameter of this request. Only available 124 114 * for <em>searchRetrieve</em> requests. 125 * 126 * @return the maximum number of records or <code>-1</code> if no value was 127 * supplied for this request 115 * If no value was supplied with the request, the server will automatically 116 * set a default value. 117 * 118 * @return the maximum number of records 128 119 */ 129 120 public int getMaximumRecords(); … … 190 181 */ 191 182 public CQLNode getScanClause(); 192 193 194 /**195 * Get the <em>scanClause</em> parameter of this request in raw string196 * format. Only available for <em>scan</em> requests.197 *198 * @return the raw scan clause or <code>null</code> if not a <em>scan</em>199 * request200 */201 public String getRawScanClause();202 183 203 184 -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRURequestImpl.java
r1952 r1957 53 53 private static final String RECORD_PACKING_STRING = "string"; 54 54 private static final String PARAM_EXTENSION_PREFIX = "x-"; 55 private static final String X_UNLIMITED_RESULTSET = "x-unlimited-resultset"; 56 private static final String X_INDENT_RESPONSE = "x-indent-response"; 57 private final SRUServerConfig config; 55 58 private final HttpServletRequest request; 56 59 private List<SRUDiagnostic> diagnostics; … … 186 189 187 190 188 SRURequestImpl( HttpServletRequest request, SRUVersion defaultVersion,189 SRURecordPacking defaultRecordPacking) {191 SRURequestImpl(SRUServerConfig config, HttpServletRequest request) { 192 this.config = config; 190 193 this.request = request; 191 this.version = defaultVersion; 192 this.recordPacking = defaultRecordPacking; 193 } 194 195 196 boolean checkParameters(SRUServerConfig config) { 194 } 195 196 197 boolean checkParameters() { 197 198 // parse mandatory operation parameter 198 199 final String op = getParameter(PARAM_OPERATION, false); … … 386 387 387 388 389 SRUVersion getRawVersion() { 390 return version; 391 } 392 393 394 SRURecordPacking getRawRecordPacking() { 395 return recordPacking; 396 } 397 398 399 String getRawQuery() { 400 return rawQuery; 401 } 402 403 404 int getRawMaximumRecords() { 405 return maximumRecords; 406 } 407 408 409 String getRawScanClause() { 410 return rawScanClause; 411 } 412 413 414 int getIndentResponse() { 415 if (config.allowOverrideIndentResponse()) { 416 String s = getExtraRequestData(X_INDENT_RESPONSE); 417 if (s != null) { 418 try { 419 int x = Integer.parseInt(s); 420 if ((x > -2) && (x < 9)) { 421 return x; 422 } 423 } catch (NumberFormatException e) { 424 /* IGNORE */ 425 } 426 } 427 } 428 return config.getIndentResponse(); 429 } 430 431 388 432 @Override 389 433 public SRUOperation getOperation() { … … 394 438 @Override 395 439 public SRUVersion getVersion() { 396 return version;440 return (version != null) ? version : config.getDefaultVersion(); 397 441 } 398 442 … … 403 447 throw new NullPointerException("version == null"); 404 448 } 405 return this.version.equals(version);449 return getVersion().equals(version); 406 450 } 407 451 … … 418 462 throw new IllegalArgumentException("min > max"); 419 463 } 420 return (min.getVersionNumber() >= version.getVersionNumber()) && 421 (version.getVersionNumber() <= max.getVersionNumber()); 464 final SRUVersion v = getVersion(); 465 return (min.getVersionNumber() >= v.getVersionNumber()) && 466 (v.getVersionNumber() <= max.getVersionNumber()); 422 467 } 423 468 … … 425 470 @Override 426 471 public SRURecordPacking getRecordPacking() { 427 return recordPacking; 472 return (recordPacking != null) 473 ? recordPacking 474 : config.getDeaultRecordPacking(); 428 475 } 429 476 … … 436 483 437 484 @Override 438 public String getRawQuery() {439 return rawQuery;440 }441 442 443 @Override444 485 public int getStartRecord() { 445 486 return startRecord; … … 449 490 @Override 450 491 public int getMaximumRecords() { 451 return maximumRecords; 492 if (config.allowOverrideIndentResponse() && 493 (getExtraRequestData(X_UNLIMITED_RESULTSET) != null)) { 494 return -1; 495 } 496 if (maximumRecords == -1) { 497 return config.getNumberOfRecords(); 498 } else { 499 if (maximumRecords > config.getMaximumRecords()) { 500 return config.getMaximumRecords(); 501 } else { 502 return maximumRecords; 503 } 504 } 452 505 } 453 506 … … 488 541 } 489 542 490 491 @Override492 public String getRawScanClause() {493 return rawScanClause;494 }495 543 496 544 @Override -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java
r1955 r1957 48 48 * public class MySRUServlet extends HttpServlet { 49 49 * private transient SRUServer sruServer; 50 * 51 * 50 * 51 * 52 52 * public void init() throws ServletException { 53 53 * final ServletContext ctx = getServletContext(); … … 58 58 * throw new ServletException("not found, url == null"); 59 59 * } 60 * 60 * 61 61 * // get additional runtime configuration from Servlet context 62 62 * HashMap<String, String> params = new HashMap<String, String>(); … … 69 69 * } 70 70 * } 71 * 71 * 72 72 * SRUServerConfig config = SRUServerConfig.parse(params, 73 73 * url.openStream()); … … 78 78 * } 79 79 * } 80 * 81 * 80 * 81 * 82 82 * protected void doGet(HttpServletRequest request, 83 83 * HttpServletResponse response) throws ServletException, IOException { 84 84 * sruServer.handleRequest(request, response); 85 85 * } 86 * 87 * 86 * 87 * 88 88 * protected void doPost(HttpServletRequest request, 89 89 * HttpServletResponse response) throws ServletException, IOException { … … 92 92 * } 93 93 * </pre> 94 * 94 * 95 95 * @see SRUServerConfig 96 96 * @see SRUSearchEngine … … 123 123 /** 124 124 * Constructor. 125 * 125 * 126 126 * @param config 127 127 * a SRUEndpointConfig object … … 149 149 /** 150 150 * Handle a SRL/CQL request. 151 * 151 * 152 152 * @param request 153 153 * a HttpServletRequest request … … 157 157 public void handleRequest(HttpServletRequest request, 158 158 HttpServletResponse response) { 159 final SRURequestImpl req = new SRURequestImpl(request, 160 config.getDefaultVersion(), config.getDeaultRecordPacking()); 159 final SRURequestImpl req = new SRURequestImpl(config, request); 161 160 try { 162 161 // set response properties … … 168 167 169 168 try { 170 if (req.checkParameters( config)) {169 if (req.checkParameters()) { 171 170 switch (req.getOperation()) { 172 171 case EXPLAIN: … … 229 228 // commence explain ... 230 229 SRUExplainResult result = searchEngine.explain(config, 231 (SRURequest) request, (SRUDiagnosticList)request);230 request, request); 232 231 233 232 // send results … … 272 271 out.writeEndElement(); // "host" element 273 272 out.writeStartElement(SRU_EXPLAIN_NS, "port"); 274 out.writeCharacters( config.getPort());273 out.writeCharacters(Integer.toString(config.getPort())); 275 274 out.writeEndElement(); // "port" element 276 275 out.writeStartElement(SRU_EXPLAIN_NS, "database"); … … 372 371 373 372 // explain/configInfo 373 out.writeStartElement(SRU_EXPLAIN_NS, "configInfo"); 374 // numberOfRecords (default) 375 out.writeStartElement(SRU_EXPLAIN_NS, "default"); 376 out.writeAttribute("type", "numberOfRecords"); 377 out.writeCharacters(Integer.toString(config.getNumberOfRecords())); 378 out.writeEndElement(); // default" element 379 380 // maximumRecords (setting) 381 out.writeStartElement(SRU_EXPLAIN_NS, "setting"); 382 out.writeAttribute("type", "maximumRecords"); 383 out.writeCharacters(Integer.toString(config.getMaximumRecords())); 384 out.writeEndElement(); // "setting" element 385 386 out.writeEndElement(); // "configInfo" element 387 374 388 out.writeEndElement(); // "explain" element 375 389 … … 406 420 // commence scan 407 421 final SRUScanResultSet result = searchEngine.scan(config, 408 (SRURequest) request, (SRUDiagnosticList)request);422 request, request); 409 423 if (result == null) { 410 424 throw new SRUException(SRUConstants.SRU_UNSUPPORTED_OPERATION, … … 508 522 // commence search ... 509 523 final SRUSearchResultSet result = searchEngine.search(config, 510 (SRURequest) request, (SRUDiagnosticList)request);524 request, request); 511 525 if (result == null) { 512 526 throw new SRUException(SRUConstants.SRU_GENERAL_SYSTEM_ERROR, … … 777 791 778 792 private void writeEchoedExplainRequest(SRUXMLStreamWriter out, 779 SRURequest request) throws XMLStreamException,793 SRURequestImpl request) throws XMLStreamException, 780 794 SRUException { 781 795 // echoedSearchRetrieveRequest … … 783 797 784 798 // echoedExplainRequest/version 785 writeVersion(out, request.getVersion()); 799 if (request.getRawVersion() != null) { 800 writeVersion(out, request.getRawVersion()); 801 } 786 802 787 803 // echoedExplainRequest/recordPacking 788 writeRecordPacking(out, request.getRecordPacking()); 804 if (request.getRawRecordPacking() != null) { 805 writeRecordPacking(out, request.getRawRecordPacking()); 806 } 789 807 790 808 // echoedExplainRequest/stylesheet … … 805 823 806 824 private void writeEchoedScanRequest(SRUXMLStreamWriter out, 807 SRURequest request, CQLNode cql) throws XMLStreamException,825 SRURequestImpl request, CQLNode cql) throws XMLStreamException, 808 826 SRUException { 809 827 // echoedScanRequest … … 811 829 812 830 // echoedScanRequest/version 813 writeVersion(out, request.getVersion()); 831 if (request.getRawVersion() != null) { 832 writeVersion(out, request.getRawVersion()); 833 } 814 834 815 835 // echoedScanRequest/scanClause … … 857 877 858 878 private void writeEchoedSearchRetrieveRequest(SRUXMLStreamWriter out, 859 SRURequest request, CQLNode cql) throws XMLStreamException,879 SRURequestImpl request, CQLNode cql) throws XMLStreamException, 860 880 SRUException { 861 881 // echoedSearchRetrieveRequest … … 863 883 864 884 // echoedSearchRetrieveRequest/version 865 writeVersion(out, request.getVersion()); 885 if (request.getRawVersion() != null) { 886 writeVersion(out, request.getRawVersion()); 887 } 866 888 867 889 // echoedSearchRetrieveRequest/query … … 885 907 886 908 // echoedSearchRetrieveRequest/maximumRecords 887 if (request.get MaximumRecords() > 0) {909 if (request.getRawMaximumRecords() > 0) { 888 910 out.writeStartElement(SRU_NS, "maximumRecords"); 889 out.writeCharacters(Integer.toString(request.getMaximumRecords())); 911 out.writeCharacters( 912 Integer.toString(request.getRawMaximumRecords())); 890 913 out.writeEndElement(); // "startRecord" element 891 914 } 892 915 893 916 // echoedSearchRetrieveRequest/recordPacking 894 writeRecordPacking(out, request.getRecordPacking()); 917 if (request.getRawRecordPacking() != null) { 918 writeRecordPacking(out, request.getRawRecordPacking()); 919 } 895 920 896 921 // echoedSearchRetrieveRequest/recordSchema … … 1011 1036 } 1012 1037 1013 1014 private staticint getIndent(SRURequestImpl request) {1015 return -1;1038 1039 private int getIndent(SRURequestImpl request) { 1040 return request.getIndentResponse(); 1016 1041 } 1017 1042 -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServerConfig.java
r1890 r1957 55 55 /** 56 56 * SRU server configuration. 57 * 57 * 58 58 * <p> 59 59 * Example: … … 65 65 * throw new ServletException("not found, url == null"); 66 66 * } 67 * 67 * 68 68 * // other runtime configuration, usually obtained from servlet context 69 69 * HashMap<String, String> params = new HashMap<String, String>(); … … 75 75 * SRUServerConfig config = SRUServerConfig.parse(params, url); 76 76 * </pre> 77 * 77 * 78 78 * <p> 79 79 * The XML configuration file must validate against the "sru-server-config.xsd" … … 83 83 */ 84 84 public final class SRUServerConfig { 85 public static final String SRU_TRANSPORT = "sru.transport"; 86 public static final String SRU_HOST = "sru.host"; 87 public static final String SRU_PORT = "sru.port"; 88 public static final String SRU_DATABASE = "sru.database"; 89 public static final String SRU_ECHO_REQUESTS = "sru.echoRequests"; 85 public static final String SRU_TRANSPORT = "sru.transport"; 86 public static final String SRU_HOST = "sru.host"; 87 public static final String SRU_PORT = "sru.port"; 88 public static final String SRU_DATABASE = "sru.database"; 89 public static final String SRU_NUMBER_OF_RECORDS = "sru.numberOfRecords"; 90 public static final String SRU_MAXIMUM_RECORDS = "sru.maximumRecords"; 91 public static final String SRU_ECHO_REQUESTS = "sru.echoRequests"; 92 public static final String SRU_INDENT_RESPONSE = "sru.indentResponse"; 93 public static final String SRU_ALLOW_OVERRIDE_MAXIMUM_RECORDS = 94 "sru.allowOverrideMaximumRecords"; 95 public static final String SRU_ALLOW_OVERRIDE_INDENT_RESPONSE = 96 "sru.allowOverrideIndentResponse"; 97 public static final int DEFAULT_NUMBER_OF_RECORDS = 100; 98 public static final int DEFAULT_MAXIMUM_RECORDS = 250; 90 99 private static final String CONFIG_FILE_NAMESPACE_URI = 91 100 "http://www.clarin.eu/sru-server/1.0/"; … … 417 426 private final String transport; 418 427 private final String host; 419 private final Stringport;428 private final int port; 420 429 private final String database; 430 private final int numberOfRecords; 431 private final int maximumRecords; 421 432 private final boolean echoRequests; 433 private final int indentResponse; 434 private final boolean allowOverrideMaximumRecords; 435 private final boolean allowOverrideIndentResponse; 422 436 private final String baseUrl; 423 437 private final DatabaseInfo databaseInfo; … … 426 440 427 441 428 private SRUServerConfig(String transport, String host, String port, 429 String database, boolean echoRequests, DatabaseInfo databaseinfo, 430 IndexInfo indexInfo, List<SchemaInfo> schemaInfo) { 431 this.transport = transport; 432 this.host = host; 433 this.port = port; 434 this.database = database; 435 this.databaseInfo = databaseinfo; 436 this.indexInfo = indexInfo; 442 private SRUServerConfig(String transport, String host, int port, 443 String database, int numberOfRecords, int maximumRecords, 444 boolean echoRequests, int indentResponse, 445 boolean allowOverrideMaximumRecords, 446 boolean allowOverrideIndentResponse, 447 DatabaseInfo databaseinfo, IndexInfo indexInfo, 448 List<SchemaInfo> schemaInfo) { 449 this.transport = transport; 450 this.host = host; 451 this.port = port; 452 this.database = database; 453 this.numberOfRecords = numberOfRecords; 454 this.maximumRecords = maximumRecords; 455 this.echoRequests = echoRequests; 456 this.indentResponse = indentResponse; 457 this.allowOverrideMaximumRecords = allowOverrideMaximumRecords; 458 this.allowOverrideIndentResponse = allowOverrideIndentResponse; 459 this.databaseInfo = databaseinfo; 460 this.indexInfo = indexInfo; 437 461 if ((schemaInfo != null) && !schemaInfo.isEmpty()) { 438 462 this.schemaInfo = Collections.unmodifiableList(schemaInfo); … … 440 464 this.schemaInfo = null; 441 465 } 442 this.echoRequests = echoRequests;443 466 444 467 // build baseUrl 445 468 StringBuilder sb = new StringBuilder(); 446 469 sb.append(host); 447 if ( !"80".equals(port)) {470 if (port != 80) { 448 471 sb.append(":").append(port); 449 472 } … … 478 501 479 502 480 public StringgetPort() {503 public int getPort() { 481 504 return port; 482 505 } … … 490 513 public String getBaseUrl() { 491 514 return baseUrl; 515 } 516 517 518 public int getNumberOfRecords() { 519 return numberOfRecords; 520 } 521 522 523 public int getMaximumRecords() { 524 return maximumRecords; 525 } 526 527 528 public int getIndentResponse() { 529 return indentResponse; 530 } 531 532 533 public boolean allowOverrideMaximumRecords() { 534 return allowOverrideMaximumRecords; 535 } 536 537 538 public boolean allowOverrideIndentResponse() { 539 return allowOverrideIndentResponse; 492 540 } 493 541 … … 539 587 * Parse a SRU server XML configuration file and create an configuration 540 588 * object from it. 541 * 589 * 542 590 * @param params 543 591 * additional settings … … 663 711 } 664 712 665 String port = params.get(SRU_PORT); 666 if ((port == null) || port.isEmpty()) { 667 throw new SRUConfigException("parameter \"" + SRU_PORT + 668 "\" is mandatory"); 669 } 670 // sanity check 671 try { 672 int num = Integer.parseInt(port); 673 if ((num < 1) && (num > 65535)) { 674 throw new SRUConfigException("parameter \"" + SRU_PORT + 675 "\" must be between 1 and 65535"); 676 } 677 } catch (NumberFormatException e) { 678 throw new SRUConfigException("parameter \"" + SRU_PORT + 679 "\" must be nummerical"); 680 } 713 int port = parseNumber(params, SRU_PORT, true, -1, 1, 65535); 681 714 682 715 String database = params.get(SRU_DATABASE); … … 686 719 } 687 720 688 // cleanup: remove leading slashed 721 // cleanup: remove leading slashed 689 722 while (database.startsWith("/")) { 690 723 database = database.substring(1); 691 724 } 692 725 693 String s; 694 boolean echoRequests = false; 695 if ((s = params.get(SRU_ECHO_REQUESTS)) != null) { 696 echoRequests = Boolean.valueOf(s).booleanValue(); 697 } 726 727 int numberOfRecords = parseNumber(params, SRU_NUMBER_OF_RECORDS, 728 false, DEFAULT_NUMBER_OF_RECORDS, 1, -1); 729 730 int maximumRecords = parseNumber(params, SRU_MAXIMUM_RECORDS, 731 false, DEFAULT_MAXIMUM_RECORDS, numberOfRecords, -1); 732 733 boolean echoRequests = parseBoolean(params, SRU_ECHO_REQUESTS, 734 false, true); 735 736 int indentResponse = parseNumber(params, SRU_INDENT_RESPONSE, 737 false, -1, -1, 8); 738 739 boolean allowOverrideMaximumRecords = parseBoolean(params, 740 SRU_ALLOW_OVERRIDE_MAXIMUM_RECORDS, false, false); 741 742 boolean allowOverrideIndentResponse = parseBoolean(params, 743 SRU_ALLOW_OVERRIDE_INDENT_RESPONSE, false, false); 698 744 699 745 return new SRUServerConfig(transport, host, port, database, 700 echoRequests, databaseInfo, indexInfo, 746 numberOfRecords, maximumRecords, echoRequests, 747 indentResponse, allowOverrideMaximumRecords, 748 allowOverrideIndentResponse, databaseInfo, indexInfo, 701 749 schemaInfo); 702 750 } catch (IOException e) { … … 710 758 } catch (SRUConfigException e) { 711 759 throw e; 760 } 761 } 762 763 764 private static int parseNumber(Map<String, String> params, String name, 765 boolean mandatory, int defaultValue, int minValue, int maxValue) 766 throws SRUConfigException { 767 String value = params.get(name); 768 if ((value == null) || value.isEmpty()) { 769 if (mandatory) { 770 throw new SRUConfigException("parameter \"" + name + 771 "\" is mandatory"); 772 } else { 773 return defaultValue; 774 } 775 } else { 776 try { 777 int num = Integer.parseInt(value); 778 779 // sanity checks 780 if ((minValue != -1) && (maxValue != -1)) { 781 if ((num < minValue) || (num > maxValue)) { 782 throw new SRUConfigException("parameter \"" + name + 783 "\" must be between " + minValue + " and " + 784 maxValue + ": " + num); 785 } 786 } else { 787 if ((minValue != -1) && (num < minValue)) { 788 throw new SRUConfigException("parameter \"" + name + 789 "\" must be larger than " + minValue + ": " + 790 num); 791 792 } 793 if ((maxValue != -1) && (num > maxValue)) { 794 throw new SRUConfigException("parameter \"" + name + 795 "\" must be smaller than " + maxValue + ": " + 796 num); 797 } 798 } 799 return num; 800 } catch (NumberFormatException e) { 801 throw new SRUConfigException("parameter \"" + name + 802 "\" must be nummerical and less than " + 803 Integer.MAX_VALUE + ": " + value); 804 } 805 } 806 } 807 808 809 private static boolean parseBoolean(Map<String, String> params, 810 String name, boolean mandatory, boolean defaultValue) 811 throws SRUConfigException { 812 String value = params.get(name); 813 if ((value == null) || value.isEmpty()) { 814 if (mandatory) { 815 throw new SRUConfigException("parameter \"" + name + 816 "\" is mandatory"); 817 } else { 818 return defaultValue; 819 } 820 } else { 821 return Boolean.valueOf(value).booleanValue(); 712 822 } 713 823 } … … 804 914 if (set.isEmpty()) { 805 915 throw new SRUConfigException("attribute 'set'" + 806 " on element '/indexInfo/index/map/" + 916 " on element '/indexInfo/index/map/" + 807 917 "name' may not be empty"); 808 918 } … … 819 929 can_sort, maps)); 820 930 } // for 821 931 822 932 // sanity check (/index/map/name/@set exists in any set/@name) 823 933 if (sets != null) { … … 877 987 } 878 988 879 989 880 990 private static List<LocalizedString> buildList(XPath xpath, Document doc, 881 991 String expression) throws SRUConfigException, -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUXMLStreamWriter.java
r1956 r1957 125 125 this.xmlwriter = factory.createXMLStreamWriter(this.writer); 126 126 127 if (indent > 1) {127 if (indent > 0) { 128 128 this.indent = indent; 129 129 this.state = IndentingState.SEEN_NOTHING;
Note: See TracChangeset
for help on using the changeset viewer.