- Timestamp:
- 01/12/16 21:03:47 (8 years ago)
- Location:
- SRUClient/trunk/src
- Files:
-
- 12 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUAbstractRequest.java
r6076 r6903 34 34 */ 35 35 abstract class SRUAbstractRequest { 36 static final String PARAM_OPERATION = "operation"; 37 static final String PARAM_VERSION = "version"; 38 static final String PARAM_RECORD_PACKING = "recordPacking"; 39 static final String PARAM_STYLESHEET = "stylesheet"; 40 static final String PARAM_QUERY = "query"; 41 static final String PARAM_START_RECORD = "startRecord"; 42 static final String PARAM_MAXIMUM_RECORDS = "maximumRecords"; 43 static final String PARAM_RECORD_SCHEMA = "recordSchema"; 44 static final String PARAM_RECORD_X_PATH = "recordXPath"; 45 static final String PARAM_RESULT_SET_TTL = "resultSetTTL"; 46 static final String PARAM_SORT_KEYS = "sortKeys"; 47 static final String PARAM_SCAN_CLAUSE = "scanClause"; 48 static final String PARAM_RESPONSE_POSITION = "responsePosition"; 49 static final String PARAM_MAXIMUM_TERMS = "maximumTerms"; 50 static final String RECORD_PACKING_XML = "xml"; 51 static final String RECORD_PACKING_STRING = "string"; 52 private static final String OP_EXPLAIN = "explain"; 53 private static final String OP_SCAN = "scan"; 54 private static final String OP_SEARCH_RETRIEVE = "searchRetrieve"; 55 private static final String VERSION_1_1 = "1.1"; 56 private static final String VERSION_1_2 = "1.2"; 57 private static final String PARAM_EXTENSION_PREFIX = "x-"; 36 /* general / explain related parameter names */ 37 protected static final String PARAM_OPERATION = "operation"; 38 protected static final String PARAM_VERSION = "version"; 39 protected static final String PARAM_STYLESHEET = "stylesheet"; 40 protected static final String PARAM_RENDER_BY = "renderedBy"; 41 protected static final String PARAM_HTTP_ACCEPT = "httpAccept"; 42 protected static final String PARAM_RESPONSE_TYPE = "responseType"; 43 /* searchRetrieve related parameter names */ 44 protected static final String PARAM_QUERY = "query"; 45 protected static final String PARAM_QUERY_TYPE = "queryType"; 46 protected static final String PARAM_START_RECORD = "startRecord"; 47 protected static final String PARAM_MAXIMUM_RECORDS = "maximumRecords"; 48 protected static final String PARAM_RECORD_XML_ESCAPING = "recordXMLEscaping"; 49 protected static final String PARAM_RECORD_PACKING = "recordPacking"; 50 protected static final String PARAM_RECORD_SCHEMA = "recordSchema"; 51 protected static final String PARAM_RECORD_X_PATH = "recordXPath"; 52 protected static final String PARAM_RESULT_SET_TTL = "resultSetTTL"; 53 protected static final String PARAM_SORT_KEYS = "sortKeys"; 54 /* scan related parameter names */ 55 protected static final String PARAM_SCAN_CLAUSE = "scanClause"; 56 protected static final String PARAM_RESPONSE_POSITION = "responsePosition"; 57 protected static final String PARAM_MAXIMUM_TERMS = "maximumTerms"; 58 /* operations */ 59 protected static final String OP_EXPLAIN = "explain"; 60 protected static final String OP_SCAN = "scan"; 61 protected static final String OP_SEARCH_RETRIEVE = "searchRetrieve"; 62 protected static final String VERSION_1_1 = "1.1"; 63 protected static final String VERSION_1_2 = "1.2"; 64 /* various parameter values */ 65 protected static final String RECORD_XML_ESCAPING_XML = "xml"; 66 protected static final String RECORD_XML_ESCPAING_STRING = "string"; 67 protected static final String RECORD_PACKING_PACKED = "packed"; 68 protected static final String RECORD_PACKING_UNPACKED = "unpacked"; 69 protected static final String RENDER_BY_CLIENT = "client"; 70 protected static final String RENDER_BY_SERVER = "server"; 71 protected static final String PARAM_EXTENSION_PREFIX = "x-"; 58 72 /** for end-point conformance testing only. never use in production. */ 59 73 public static final String X_MALFORMED_OPERATION = … … 220 234 * Get the version for this request. 221 235 * 222 * @return version for this request or <code>null</code> of client default236 * @return version for this request or <code>null</code> if client default 223 237 * is used 224 238 */ … … 347 361 new URIHelper(new URIBuilder(baseURI)); 348 362 349 /* 350 * append operation parameter 351 * 352 * NB: Setting "x-malformed-operation" as an extra request parameter 353 * makes the client send invalid requests. This is intended to 354 * use for testing SRU servers for protocol conformance (i.e. 355 * provoke an error) and SHOULD NEVER be used in production! 356 */ 357 final String malformedOperation = 358 getExtraRequestData(X_MALFORMED_OPERATION); 359 if (malformedOperation == null) { 360 switch (getOperation()) { 361 case EXPLAIN: 362 uriBuilder.append(PARAM_OPERATION, OP_EXPLAIN); 363 break; 364 case SCAN: 365 uriBuilder.append(PARAM_OPERATION, OP_SCAN); 366 break; 367 case SEARCH_RETRIEVE: 368 uriBuilder.append(PARAM_OPERATION, OP_SEARCH_RETRIEVE); 369 break; 370 default: 371 throw new SRUClientException("unsupported operation: " + 372 getOperation()); 373 } // switch 374 } else { 375 if (!malformedOperation.equals(MALFORMED_OMIT)) { 376 uriBuilder.append(PARAM_OPERATION, malformedOperation); 363 /* store the version, we use for this request */ 364 versionRequested = (version != null) ? version : defaultVersion; 365 366 switch (versionRequested) { 367 case VERSION_1_1: 368 /* $FALL-THROUGH$ */ 369 case VERSION_1_2: 370 /* 371 * append operation parameter 372 * 373 * NB: Setting "x-malformed-operation" as an extra request parameter 374 * makes the client send invalid requests. This is intended to 375 * use for testing SRU servers for protocol conformance (i.e. 376 * provoke an error) and SHOULD NEVER be used in production! 377 */ 378 final String malformedOperation = 379 getExtraRequestData(X_MALFORMED_OPERATION); 380 if (malformedOperation == null) { 381 switch (getOperation()) { 382 case EXPLAIN: 383 uriBuilder.append(PARAM_OPERATION, OP_EXPLAIN); 384 break; 385 case SCAN: 386 uriBuilder.append(PARAM_OPERATION, OP_SCAN); 387 break; 388 case SEARCH_RETRIEVE: 389 uriBuilder.append(PARAM_OPERATION, OP_SEARCH_RETRIEVE); 390 break; 391 default: 392 } // switch 393 } else { 394 if (!malformedOperation.equals(MALFORMED_OMIT)) { 395 uriBuilder.append(PARAM_OPERATION, malformedOperation); 396 } 377 397 } 378 } 379 380 /*381 * append version parameter382 *383 * NB: Setting "x-malformed-version" as an extra request parameter384 * makes the client send invalid requests. This is intended to385 * use for testing SRU servers for protocol conformance (i.e.386 * provoke an error) and SHOULD NEVER be used in production!387 */388 final String malformedVersion =389 getExtraRequestData(X_MALFORMED_VERSION);390 if (malformedVersion == null) {391 versionRequested = (version != null) ? version : defaultVersion;392 switch (versionRequested) {393 case VERSION_1_1:394 uriBuilder.append(PARAM_VERSION, VERSION_1_1);395 break;396 case VERSION_1_2:397 uriBuilder.append(PARAM_VERSION, VERSION_1_2);398 break;399 default:400 throw new SRUClientException("unsupported version: " +401 versionRequested);402 } // switch403 } else{404 if (!malformedVersion.equalsIgnoreCase(MALFORMED_OMIT)) {405 uriBuilder.append(PARAM_VERSION, malformedVersion);398 399 /* 400 * append version parameter 401 * 402 * NB: Setting "x-malformed-version" as an extra request parameter 403 * makes the client send invalid requests. This is intended to 404 * use for testing SRU servers for protocol conformance (i.e. 405 * provoke an error) and SHOULD NEVER be used in production! 406 */ 407 final String malformedVersion = 408 getExtraRequestData(X_MALFORMED_VERSION); 409 if (malformedVersion == null) { 410 versionRequested = (version != null) ? version : defaultVersion; 411 switch (versionRequested) { 412 case VERSION_1_1: 413 uriBuilder.append(PARAM_VERSION, VERSION_1_1); 414 break; 415 case VERSION_1_2: 416 uriBuilder.append(PARAM_VERSION, VERSION_1_2); 417 break; 418 default: 419 throw new SRUClientException("unsupported version: " + 420 versionRequested); 421 } // switch 422 } else { 423 if (!malformedVersion.equalsIgnoreCase(MALFORMED_OMIT)) { 424 uriBuilder.append(PARAM_VERSION, malformedVersion); 425 } 406 426 } 427 break; 428 case VERSION_2_0: 429 if (getExtraRequestData(X_MALFORMED_OPERATION) != null) { 430 throw new SRUClientException("parameter '" + 431 X_MALFORMED_OPERATION + 432 "' is not supported when using version 2.0"); 433 } 434 if (getExtraRequestData(X_MALFORMED_VERSION) != null) { 435 throw new SRUClientException("parameter '" + 436 X_MALFORMED_VERSION + 437 "' is not supported when using version 2.0"); 438 } 439 break; 440 default: 441 throw new SRUClientException("unsupported version: " + 442 versionRequested); 407 443 } 408 444 409 445 // request specific parameters 410 addParametersToURI(uriBuilder );446 addParametersToURI(uriBuilder, versionRequested); 411 447 412 448 // extraRequestData … … 418 454 /* 419 455 * make sure, we skip the client-internal parameters 420 * to generate invalid requests ...456 * used to generate invalid requests ... 421 457 */ 422 458 if (!key.startsWith(MALFORMED_KEY_PREFIX)) { … … 442 478 443 479 444 abstract void addParametersToURI(URIHelper uriBuilder )480 abstract void addParametersToURI(URIHelper uriBuilder, SRUVersion version) 445 481 throws SRUClientException; 446 482 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClientConstants.java
r5750 r6903 25 25 public static final String RECORD_DATA_PARSER_SCHEMA_ANY = "*"; 26 26 27 /** constant for CQL query type */ 28 public static final String QUERY_TYPE_CQL = "cql"; 29 27 30 } // class SRUClientConstants -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRecordDataParser.java
r5750 r6903 52 52 private static final String VERSION_1_1 = "1.1"; 53 53 private static final String VERSION_1_2 = "1.2"; 54 private static final String VERSION_2 = "2"; 55 private static final String VERSION_2_0 = "2.0"; 54 56 private static final String TRANSPORT_HTTP = "http"; 55 57 private static final String TRANSPORT_HTTPS = "https"; … … 180 182 } else if (VERSION_1_2.equals(s)) { 181 183 version = SRUVersion.VERSION_1_2; 184 } else if (VERSION_2.equals(s) || VERSION_2_0.equals(s)) { 185 version = SRUVersion.VERSION_2_0; 182 186 } else { 183 187 throw new SRUClientException("invalid or unsupported value '" + 184 s + " 'for attribute 'version' on element '" +188 s + " 'for attribute 'version' on element '" + 185 189 reader.getName() + "'"); 186 190 } -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRequest.java
r5750 r6903 26 26 */ 27 27 public class SRUExplainRequest extends SRUAbstractRequest { 28 private SRURecord Packing recordPacking;28 private SRURecordXmlEscaping recordXmlEscaping; 29 29 private boolean parseRecordDataEnabled = false; 30 30 … … 52 52 53 53 /** 54 * Set the requested record packing. 54 * Get the <em>recordXmlEscpaing</em> (SRU 2.0) or <em>recordPacking</em> 55 * (SRU 1.1 and SRU 1.2) parameter of this request. 55 56 * 56 * @param recordPacking 57 * the requested record packing 58 * @see SRURecordPacking 57 * @return the requested record packing 58 * @see SRURecordXmlEscaping 59 59 */ 60 public void setRecordPacking(SRURecordPacking recordPacking) { 61 if (recordPacking == null) { 62 throw new NullPointerException("recordPacking == null"); 63 } 64 this.recordPacking = recordPacking; 60 public SRURecordXmlEscaping getRecordXmlEscaping() { 61 return recordXmlEscaping; 65 62 } 66 63 67 64 68 65 /** 69 * Get the requested record packing. 66 * Set the <em>recordXmlEscpaing</em> (SRU 2.0) or <em>recordPacking</em> 67 * (SRU 1.1 and SRU 1.2) parameter of this request. 70 68 * 71 * @return the requested record packing 72 * @see SRURecordPacking 69 * @param getRecordXmlEscaping 70 * the requested record XML escaping 71 * @see SRURecordXmlEscaping 73 72 */ 74 public SRURecordPacking getRecordPacking() { 75 return recordPacking; 73 public void setRecordXmlEscaping(SRURecordXmlEscaping getRecordXmlEscaping) { 74 if (getRecordXmlEscaping == null) { 75 throw new NullPointerException("getRecordXmlEscaping == null"); 76 } 77 this.recordXmlEscaping = getRecordXmlEscaping; 78 } 79 80 81 /** 82 * Check, whether the record data of a explain response (ZeeRex record) 83 * shall be parsed or not. 84 * 85 * @return <code>true</code> if parsing is enabled, <code>false</code> 86 * otherwise 87 */ 88 public boolean isParseRecordDataEnabled() { 89 return parseRecordDataEnabled; 76 90 } 77 91 … … 90 104 91 105 92 /**93 * Check, whether the record data of a explain response (ZeeRex record)94 * shall be parsed or not.95 *96 * @return <code>true</code> if parsing is enabled, <code>false</code>97 * otherwise98 */99 public boolean isParseRecordDataEnabled() {100 return parseRecordDataEnabled;101 }102 103 104 106 @Override 105 107 SRUOperation getOperation() { … … 109 111 110 112 @Override 111 void addParametersToURI(URIHelper uriHelper) throws SRUClientException { 112 // recordPacking 113 if (recordPacking != null) { 114 switch (recordPacking) { 115 case XML: 116 uriHelper.append(PARAM_RECORD_PACKING, RECORD_PACKING_XML); 113 void addParametersToURI(URIHelper uriHelper, SRUVersion version) 114 throws SRUClientException { 115 // recordXMLEscaping / recordPacking 116 if (recordXmlEscaping != null) { 117 switch (version) { 118 case VERSION_1_1: 119 /* $FALL-THROUGH$ */ 120 case VERSION_1_2: 121 switch (recordXmlEscaping) { 122 case XML: 123 uriHelper.append(PARAM_RECORD_PACKING, 124 RECORD_XML_ESCAPING_XML); 125 break; 126 case STRING: 127 uriHelper.append(PARAM_RECORD_PACKING, 128 RECORD_XML_ESCPAING_STRING); 129 break; 130 default: 131 throw new SRUClientException("unsupported record packing: " + 132 recordXmlEscaping); 133 } // switch 117 134 break; 118 case STRING: 119 uriHelper.append(PARAM_RECORD_PACKING, RECORD_PACKING_STRING); 135 case VERSION_2_0: 136 switch (recordXmlEscaping) { 137 case XML: 138 uriHelper.append(PARAM_RECORD_XML_ESCAPING, 139 RECORD_XML_ESCAPING_XML); 140 break; 141 case STRING: 142 uriHelper.append(PARAM_RECORD_XML_ESCAPING, 143 RECORD_XML_ESCPAING_STRING); 144 break; 145 default: 146 throw new SRUClientException( 147 "unsupported record packing: " + 148 recordXmlEscaping); 149 } 120 150 break; 121 151 default: 122 throw new SRUClientException("unsupported record packing: " +123 recordPacking);124 } // switch152 /* cannot happen */ 153 break; 154 } 125 155 } 126 156 } -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRURecordPacking.java
r5750 r6903 1 1 /** 2 * This software is copyright (c) 201 2-2014by2 * This software is copyright (c) 2011-2015 by 3 3 * - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) 4 4 * This is free software. You can redistribute it … … 18 18 19 19 /** 20 * SRU record packing20 * SRU 2.0 record packing. 21 21 */ 22 22 public enum SRURecordPacking { 23 23 /** 24 * XML record packing 24 * The client requests that the server should supply records strictly 25 * according to the requested schema. 25 26 */ 26 XML { 27 @Override 28 String getStringValue() { 29 return "xml"; 30 } 31 }, 27 PACKED, 32 28 33 29 /** 34 * String record packing 30 * The server is free to allow the location of application data to vary 31 * within the record. 35 32 */ 36 STRING { 37 @Override 38 String getStringValue() { 39 return "string"; 40 } 41 }; 42 43 abstract String getStringValue(); 44 33 UNPACKED 45 34 } // enum SRURecordPacking -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRURecordXmlEscaping.java
r6079 r6903 20 20 * SRU record packing 21 21 */ 22 public enum SRURecord Packing {22 public enum SRURecordXmlEscaping { 23 23 /** 24 24 * XML record packing -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUScanRequest.java
r5750 r6903 158 158 159 159 @Override 160 void addParametersToURI(URIHelper uriHelper) throws SRUClientException { 160 void addParametersToURI(URIHelper uriHelper, SRUVersion version) 161 throws SRUClientException { 161 162 // scanClause 162 163 final String malformedScan = -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSearchRetrieveRequest.java
r5750 r6903 43 43 "x-malformed-maximumRecords"; 44 44 /** for end-point conformance testing only. never use in production. */ 45 public static final String X_MALFORMED_RECORD_ PACKING =45 public static final String X_MALFORMED_RECORD_XML_ESCAPING = 46 46 "x-malformed-recordPacking"; 47 private String queryType; 47 48 private String query; 48 49 private int startRecord = -1; 49 50 private int maximumRecords = -1; 51 private SRURecordXmlEscaping recordXmlEscaping; 50 52 private SRURecordPacking recordPacking; 51 53 private String recordSchema; … … 72 74 public SRUSearchRetrieveRequest(String baseURI) { 73 75 super(baseURI); 76 } 77 78 79 /** 80 * (SRU 2.0) Get the value of the <em>queryType</em> argument for this 81 * request. 82 * 83 * @return the value for the <em>queryType</em> argument or 84 * <code>null</code> of none was set 85 */ 86 public String getQueryType() { 87 return queryType; 88 } 89 90 91 /** 92 * (SRU 2.0) Set the value of the <em>queryType</em> argument for this 93 * request. 94 * 95 * @param queryType 96 * the value for the <em>queryType</em> argument 97 * @throws NullPointerException 98 * if any required argument is <code>null</code> 99 * @throws IllegalArgumentException 100 * if any argument is invalid 101 */ 102 public void setQueryType(String queryType) { 103 if (queryType == null) { 104 throw new NullPointerException("queryType == null"); 105 } 106 if (queryType.isEmpty()) { 107 throw new IllegalArgumentException("queryType is an empty string"); 108 } 109 for (int i = 0; i < queryType.length(); i++) { 110 final char ch = queryType.charAt(i); 111 if (!((ch >= 'a' && ch <= 'z') || 112 (ch >= 'A' && ch <= 'Z') || 113 (ch >= '0' && ch <= '9') || 114 ((i > 0) && ((ch == '-') || ch == '_')))) { 115 throw new IllegalArgumentException( 116 "queryType contains illegal characters"); 117 } 118 } 119 this.queryType = queryType; 74 120 } 75 121 … … 188 234 189 235 /** 190 * Get the value of the <em>recordSchema</em> argument for this request. 191 * 192 * @return the value for the <em>recordSchema</em> argument or 193 * <code>null</code> of none was set 236 * Get the <em>recordXmlEscpaing</em> (SRU 2.0) or <em>recordPacking</em> 237 * (SRU 1.1 and SRU 1.2) parameter of this request. 238 * 239 * @return the requested record XML escaping 240 * @see SRURecordXmlEscaping 241 */ 242 public SRURecordXmlEscaping getRecordXmlEscaping() { 243 return recordXmlEscaping; 244 } 245 246 247 /** 248 * Set the <em>recordXmlEscpaing</em> (SRU 2.0) or <em>recordPacking</em> 249 * (SRU 1.1 and SRU 1.2) parameter of this request. 250 * 251 * @param recordXmlEscaping 252 * the requested record XML escaping 253 * @see SRURecordXmlEscaping 254 */ 255 public void setRecordXmlEscaping(SRURecordXmlEscaping recordXmlEscaping) { 256 if (recordXmlEscaping == null) { 257 throw new NullPointerException("recordXmlEscaping == null"); 258 } 259 this.recordXmlEscaping = recordXmlEscaping; 260 } 261 262 263 /** 264 * Get the <em>recordPacking</em> (SRU 2.0) parameter of this request. 265 * 266 * @return the requested record packing 267 * @see SRURecordPacking 194 268 */ 195 269 public SRURecordPacking getRecordPacking() { … … 199 273 200 274 /** 201 * Set the value of the <em>recordPacking</em> argument for this request. 202 * 203 * @param recordPacking 204 * the value for the <em>recordPacking</em> argument 205 * @throws NullPointerException 206 * if any required argument is <code>null</code> 275 * Set the <em>recordPacking</em> (SRU 2.0) parameter of this request. 276 * 277 * @param getRecordXmlEscaping 278 * the requested record XML escaping 279 * @see SRURecordXmlEscaping 207 280 */ 208 281 public void setRecordPacking(SRURecordPacking recordPacking) { … … 245 318 246 319 @Override 247 void addParametersToURI(URIHelper uriHelper) throws SRUClientException { 320 void addParametersToURI(URIHelper uriHelper, SRUVersion version) 321 throws SRUClientException { 248 322 /* 249 323 * append query argument (mandatory) … … 268 342 269 343 /* 344 * append queryType parameter (SRU 2.0, optional) 345 */ 346 if ((version == SRUVersion.VERSION_2_0) && (queryType != null)) { 347 if (!SRUClientConstants.QUERY_TYPE_CQL.equals(queryType)) { 348 uriHelper.append(PARAM_QUERY_TYPE, queryType); 349 } 350 } 351 352 /* 270 353 * append startRecord argument (optional) 271 354 * … … 309 392 310 393 /* 311 * append record Packing argument (optional)394 * append record XML escaping argument (optional) 312 395 * 313 396 * NB: Setting "x-malformed-recordPacking" as an extra request … … 316 399 * (i.e. provoke an error) and SHOULD NEVER be used in production! 317 400 */ 318 final String malformedRecord Packing =319 getExtraRequestData(X_MALFORMED_RECORD_ PACKING);320 if (malformedRecord Packing == null) {321 if (record Packing != null) {322 switch (record Packing) {401 final String malformedRecordXmlEscaping = 402 getExtraRequestData(X_MALFORMED_RECORD_XML_ESCAPING); 403 if (malformedRecordXmlEscaping == null) { 404 if (recordXmlEscaping != null) { 405 switch (recordXmlEscaping) { 323 406 case XML: 324 uriHelper.append(PARAM_RECORD_PACKING, RECORD_PACKING_XML); 407 if (version == SRUVersion.VERSION_2_0) { 408 uriHelper.append(PARAM_RECORD_XML_ESCAPING, 409 RECORD_XML_ESCAPING_XML); 410 } else { 411 uriHelper.append(PARAM_RECORD_PACKING, 412 RECORD_XML_ESCAPING_XML); 413 } 325 414 break; 326 415 case STRING: 327 uriHelper.append(PARAM_RECORD_PACKING, 328 RECORD_PACKING_STRING); 416 if (version == SRUVersion.VERSION_2_0) { 417 uriHelper.append(PARAM_RECORD_XML_ESCAPING, 418 RECORD_XML_ESCPAING_STRING); 419 } else { 420 uriHelper.append(PARAM_RECORD_PACKING, 421 RECORD_XML_ESCPAING_STRING); 422 } 329 423 break; 330 424 default: 331 425 throw new SRUClientException( 332 "unsupported record packing: " + recordPacking);426 "unsupported record XML escpaing: " + recordXmlEscaping); 333 427 } // switch 334 428 } 335 429 } else { 336 if (!malformedRecordPacking.equalsIgnoreCase(MALFORMED_OMIT)) { 337 uriHelper.append(PARAM_RECORD_PACKING, malformedRecordPacking); 338 } 430 if (!malformedRecordXmlEscaping.equalsIgnoreCase(MALFORMED_OMIT)) { 431 if (version == SRUVersion.VERSION_2_0) { 432 uriHelper.append(PARAM_RECORD_XML_ESCAPING, 433 malformedRecordXmlEscaping); 434 } else { 435 uriHelper.append(PARAM_RECORD_PACKING, 436 malformedRecordXmlEscaping); 437 } 438 } 439 } 440 441 /* 442 * (SRU 2.0) append recordPacking argument (optional) 443 */ 444 if ((version == SRUVersion.VERSION_2_0) && (recordPacking != null)) { 445 String s = null; 446 switch (recordPacking) { 447 case PACKED: 448 s = "packed"; 449 break; 450 case UNPACKED: 451 s = "unpacked"; 452 break; 453 } 454 uriHelper.append(PARAM_RECORD_PACKING, s); 339 455 } 340 456 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java
r6077 r6903 69 69 private static final String USER_AGENT = "SRU-Client/1.0.0"; 70 70 /** default version the client will use, if not otherwise specified */ 71 private static final String SRU_NS =72 "http://www.loc.gov/zing/srw/";73 private static final String SRU_DIAGNOSIC_NS =74 "http://www.loc.gov/zing/srw/diagnostic/";71 // private static final String SRU_NS = 72 // "http://www.loc.gov/zing/srw/"; 73 // private static final String SRU_DIAGNOSIC_NS = 74 // "http://www.loc.gov/zing/srw/diagnostic/"; 75 75 private static final String SRU_DIAGNOSTIC_RECORD_SCHEMA = 76 76 "info:srw/schema/1/diagnostics-v1.1"; 77 77 private static final String VERSION_1_1 = "1.1"; 78 78 private static final String VERSION_1_2 = "1.2"; 79 private static final String RECORD_PACKING_XML = "xml"; 80 private static final String RECORD_PACKING_STRING = "string"; 79 private static final String VERSION_2_0 = "2.0"; 80 private static final String RECORD_PACKING_PACKED = "packed"; 81 private static final String RECORD_PACKING_UNPACKED = "unpacked"; 82 private static final String RECORD_ESCAPING_XML = "xml"; 83 private static final String RECORD_ESCAPING_STRING = "string"; 81 84 private static final Logger logger = 82 85 LoggerFactory.getLogger(SRUSimpleClient.class); … … 454 457 logger.debug("parsing 'explain' response (mode = {})", 455 458 (request.isStrictMode() ? "strict" : "non-strict")); 459 460 /* detect response namespaces */ 461 final SRUNamespaces ns = 462 detectNamespace(reader, request.getRequestedVersion()); 463 456 464 /* 457 465 * Eventually, SRUClient should always parse explain record data. … … 462 470 logger.debug("parsing of explain record data skipped"); 463 471 } 464 doParseExplainResponse(reader, request, handler, parse);472 doParseExplainResponse(reader, ns, request, handler, parse); 465 473 } 466 474 467 475 468 476 private void doParseExplainResponse(SRUXMLStreamReader reader, 469 SRUAbstractRequest request, SRUExplainHandler handler, 470 boolean parseRecordData) throws SRUClientException { 477 SRUNamespaces ns, SRUAbstractRequest request, 478 SRUExplainHandler handler, boolean parseRecordData) 479 throws SRUClientException { 471 480 try { 472 481 final boolean strictMode = request.isStrictMode(); 473 482 474 483 // explainResponse 475 reader.readStart( SRU_NS, "explainResponse", true);484 reader.readStart(ns.sruNS(), "explainResponse", true); 476 485 477 486 // explainResponse/version 478 SRUVersion version = parseVersion(reader);487 final SRUVersion version = parseVersion(reader, ns.sruNS()); 479 488 logger.debug("version = {}, requested = {}", version, 480 489 request.getRequestedVersion()); 481 490 482 491 // explainResponse/record 483 reader.readStart( SRU_NS, "record", true);492 reader.readStart(ns.sruNS(), "record", true); 484 493 if (parseRecordData) { 485 494 handler.onStartRecords(-1, null, -1); 486 495 487 SRURecordPacking packing = null; 488 if (!strictMode && reader.peekStart(SRU_NS, "recordPacking")) { 489 packing = parseRecordPacking(reader, false); 490 if (packing != null) { 496 /* 497 * common error: recordEscaping / recordPacking (SRU 1.2) in 498 * wrong order 499 */ 500 SRURecordXmlEscaping recordXmlEscaping = null; 501 if (!strictMode && reader.peekStart(ns.sruNS(), "recordPacking")) { 502 recordXmlEscaping = parseRecordXmlEscaping(reader, ns, 503 request.getRequestedVersion(), false); 504 if (recordXmlEscaping != null) { 491 505 logger.error("element <recordPacking> must apperear " + 492 506 "after element <recordSchema> within " + … … 494 508 } 495 509 } 510 496 511 String schema = 497 reader.readContent(SRU_NS, "recordSchema", true); 498 if (packing == null) { 499 packing = parseRecordPacking(reader, strictMode); 500 } 501 logger.debug("schema = {}, packing = {}", schema, packing); 512 reader.readContent(ns.sruNS(), "recordSchema", true); 513 514 // (SRU 2.0) recordPacking (optional) 515 // XXX: what to do with it? 516 SRURecordPacking recordPacking = null; 517 if (version == SRUVersion.VERSION_2_0) { 518 recordPacking = parseRecordPacking(reader, 519 ns.sruNS(), strictMode); 520 } 521 522 if (recordXmlEscaping == null) { 523 recordXmlEscaping = parseRecordXmlEscaping(reader, ns, 524 request.getRequestedVersion(), strictMode); 525 } 526 logger.debug("schema = {}, escaping = {}, packing = {}", 527 schema, recordXmlEscaping, recordPacking); 502 528 503 529 // explainResponse/record/recordData 504 reader.readStart( SRU_NS, "recordData", true);530 reader.readStart(ns.sruNS(), "recordData", true); 505 531 reader.consumeWhitespace(); 506 532 … … 508 534 SRUXMLStreamReader recordReader = null; 509 535 510 if ( packing == SRURecordPacking.STRING) {536 if (recordXmlEscaping == SRURecordXmlEscaping.STRING) { 511 537 /* 512 538 * read content into temporary buffer and then use a new XML … … 538 564 * string record packing 539 565 */ 540 if ( packing == SRURecordPacking.STRING) {566 if (recordXmlEscaping == SRURecordXmlEscaping.STRING) { 541 567 recordReader.closeCompletly(); 542 568 } … … 547 573 } 548 574 reader.consumeWhitespace(); 549 reader.readEnd( SRU_NS, "recordData", true);575 reader.readEnd(ns.sruNS(), "recordData", true); 550 576 551 577 if (version == SRUVersion.VERSION_1_2) { 552 reader.readContent( SRU_NS, "recordIdentifier", false);553 } 554 reader.readContent( SRU_NS, "recordPosition", false, -1);578 reader.readContent(ns.sruNS(), "recordIdentifier", false); 579 } 580 reader.readContent(ns.sruNS(), "recordPosition", false, -1); 555 581 556 582 // notify handler 557 583 handler.onRecord(null, -1, recordData); 558 584 559 if (reader.readStart( SRU_NS, "extraRecordData", false)) {585 if (reader.readStart(ns.sruNS(), "extraRecordData", false)) { 560 586 reader.consumeWhitespace(); 561 587 proxy.reset(reader); … … 569 595 } 570 596 reader.consumeWhitespace(); 571 reader.readEnd( SRU_NS, "extraRecordData", true);597 reader.readEnd(ns.sruNS(), "extraRecordData", true); 572 598 } 573 599 574 600 handler.onFinishRecords(-1); 575 601 576 reader.readEnd( SRU_NS, "record");602 reader.readEnd(ns.sruNS(), "record"); 577 603 } else { 578 604 /* … … 580 606 * until <record> end tag 581 607 */ 582 reader.readEnd( SRU_NS, "record", true);608 reader.readEnd(ns.sruNS(), "record", true); 583 609 } 584 610 585 611 // explainResponse/echoedExplainRequest 586 if (reader.readStart( SRU_NS, "echoedExplainRequest", false)) {587 reader.readEnd( SRU_NS, "echoedExplainRequest", true);612 if (reader.readStart(ns.sruNS(), "echoedExplainRequest", false)) { 613 reader.readEnd(ns.sruNS(), "echoedExplainRequest", true); 588 614 } 589 615 … … 604 630 // explainResponse/diagnostics 605 631 final List<SRUDiagnostic> diagnostics = 606 parseDiagnostics(reader, strictMode);632 parseDiagnostics(reader, ns, ns.scanNS(), strictMode); 607 633 if (diagnostics != null) { 608 634 handler.onDiagnostics(diagnostics); … … 610 636 611 637 // explainResponse/extraResponseData 612 if (reader.readStart( SRU_NS, "extraResponseData", false)) {638 if (reader.readStart(ns.sruNS(), "extraResponseData", false)) { 613 639 reader.consumeWhitespace(); 614 640 proxy.reset(reader); … … 621 647 } 622 648 reader.consumeWhitespace(); 623 reader.readEnd( SRU_NS, "extraResponseData", true);624 } 625 626 reader.readEnd( SRU_NS, "explainResponse");649 reader.readEnd(ns.sruNS(), "extraResponseData", true); 650 } 651 652 reader.readEnd(ns.sruNS(), "explainResponse"); 627 653 } catch (XMLStreamException e) { 628 654 throw new SRUClientException(e.getMessage(), e); … … 635 661 throws SRUClientException { 636 662 try { 663 /* detect response namespaces */ 664 final SRUNamespaces ns = 665 detectNamespace(reader, request.getRequestedVersion()); 666 637 667 /* 638 668 * if the endpoint cannot determine the operation, it should create 639 669 * a explain response. 640 670 */ 641 if (reader.peekStart( SRU_NS, "explainResponse")) {642 doParseExplainResponse(reader, request, new SRUExplainHandler() {671 if (reader.peekStart(ns.sruNS(), "explainResponse")) { 672 doParseExplainResponse(reader, ns, request, new SRUExplainHandler() { 643 673 @Override 644 674 public void onRequestStatistics(int bytes, long millisTotal, … … 691 721 (strictMode ? "strict" : "non-strict")); 692 722 693 694 723 // scanResponse 695 reader.readStart( SRU_NS, "scanResponse", true);724 reader.readStart(ns.scanNS(), "scanResponse", true); 696 725 697 726 // scanResponse/version 698 SRUVersion version = parseVersion(reader);727 final SRUVersion version = parseVersion(reader, ns.scanNS()); 699 728 logger.debug("version = {}, requested = {}", version, 700 729 request.getRequestedVersion()); 701 730 702 731 // scanResponse/terms 703 if (reader.readStart( SRU_NS, "terms", false)) {732 if (reader.readStart(ns.scanNS(), "terms", false)) { 704 733 boolean first = true; 705 while (reader.readStart( SRU_NS, "term", first)) {734 while (reader.readStart(ns.scanNS(), "term", first)) { 706 735 if (first) { 707 736 first = false; … … 711 740 // scanResponse/terms/value 712 741 String value = 713 reader.readContent( SRU_NS, "value", true);742 reader.readContent(ns.scanNS(), "value", true); 714 743 715 744 // scanResponse/terms/numberOfRecords 716 int numberOfRecords = reader.readContent( SRU_NS,745 int numberOfRecords = reader.readContent(ns.scanNS(), 717 746 "numberOfRecords", false, -1); 718 747 719 748 // scanResponse/terms/displayTerm 720 String displayTerm = reader.readContent( SRU_NS,749 String displayTerm = reader.readContent(ns.scanNS(), 721 750 "displayTerm", false); 722 751 723 752 // scanResponse/terms/whereInList 724 String s = reader.readContent( SRU_NS,753 String s = reader.readContent(ns.scanNS(), 725 754 "whereInList", false); 726 755 SRUWhereInList whereInList = null; … … 746 775 747 776 // scanResponse/terms/extraTermData 748 if (reader.readStart( SRU_NS, "extraTermData", first)) {777 if (reader.readStart(ns.scanNS(), "extraTermData", first)) { 749 778 reader.consumeWhitespace(); 750 779 proxy.reset(reader); … … 757 786 } 758 787 reader.consumeWhitespace(); 759 reader.readEnd( SRU_NS, "extraTermData", true);788 reader.readEnd(ns.scanNS(), "extraTermData", true); 760 789 } 761 reader.readEnd( SRU_NS, "term", true);790 reader.readEnd(ns.scanNS(), "term", true); 762 791 763 792 } // while 764 reader.readEnd( SRU_NS, "terms");793 reader.readEnd(ns.scanNS(), "terms"); 765 794 handler.onFinishTerms(); 766 795 } 767 796 768 797 // scanResponse/echoedScanRequest 769 if (reader.readStart( SRU_NS, "echoedScanRequest", false)) {770 reader.readEnd( SRU_NS, "echoedScanRequest", true);798 if (reader.readStart(ns.scanNS(), "echoedScanRequest", false)) { 799 reader.readEnd(ns.scanNS(), "echoedScanRequest", true); 771 800 } 772 801 … … 784 813 // scanResponse/diagnostics 785 814 final List<SRUDiagnostic> diagnostics = 786 parseDiagnostics(reader, strictMode);815 parseDiagnostics(reader, ns, ns.scanNS(), strictMode); 787 816 if (diagnostics != null) { 788 817 handler.onDiagnostics(diagnostics); … … 790 819 791 820 // scanResponse/extraResponseData 792 if (reader.readStart( SRU_NS, "extraResponseData", false)) {821 if (reader.readStart(ns.scanNS(), "extraResponseData", false)) { 793 822 reader.consumeWhitespace(); 794 823 proxy.reset(reader); … … 801 830 } 802 831 reader.consumeWhitespace(); 803 reader.readEnd( SRU_NS, "extraResponseData", true);804 } 805 806 reader.readEnd( SRU_NS, "scanResponse");832 reader.readEnd(ns.scanNS(), "extraResponseData", true); 833 } 834 835 reader.readEnd(ns.scanNS(), "scanResponse"); 807 836 } 808 837 } catch (XMLStreamException e) { … … 816 845 final SRUSearchRetrieveHandler handler) throws SRUClientException { 817 846 try { 847 /* detect response namespaces */ 848 final SRUNamespaces ns = 849 detectNamespace(reader, request.getRequestedVersion()); 850 818 851 /* 819 852 * if the endpoint cannot determine the operation, it should create 820 853 * a explain response. 821 854 */ 822 if (reader.peekStart( SRU_NS, "explainResponse")) {823 doParseExplainResponse(reader, request, new SRUExplainHandler() {855 if (reader.peekStart(ns.sruNS(), "explainResponse")) { 856 doParseExplainResponse(reader, ns, request, new SRUExplainHandler() { 824 857 @Override 825 858 public void onRequestStatistics(int bytes, long millisTotal, … … 873 906 874 907 // searchRetrieveResponse 875 reader.readStart( SRU_NS, "searchRetrieveResponse", true);908 reader.readStart(ns.sruNS(), "searchRetrieveResponse", true); 876 909 877 910 // searchRetrieveResponse/version 878 SRUVersion version = parseVersion(reader);911 final SRUVersion version = parseVersion(reader, ns.sruNS()); 879 912 logger.debug("version = {}, requested = {}", version, 880 913 request.getRequestedVersion()); 881 914 882 915 // searchRetrieveResponse/numberOfRecords 883 int numberOfRecords = reader.readContent( SRU_NS,916 int numberOfRecords = reader.readContent(ns.sruNS(), 884 917 "numberOfRecords", true, -1); 885 918 886 919 // searchRetrieveResponse/resultSetId 887 String resultSetId = reader.readContent( SRU_NS,920 String resultSetId = reader.readContent(ns.sruNS(), 888 921 "resultSetId", false); 889 922 890 923 // searchRetrieveResponse/resultSetIdleTime 891 int resultSetIdleTime = reader.readContent( SRU_NS,924 int resultSetIdleTime = reader.readContent(ns.sruNS(), 892 925 "resultSetIdleTime", false, -1); 893 926 … … 904 937 */ 905 938 int recordCount = 0; 906 if (reader.readStart( SRU_NS, "records", false)) {939 if (reader.readStart(ns.sruNS(), "records", false)) { 907 940 // searchRetrieveResponse/records/record 908 941 boolean first = true; 909 while (reader.readStart( SRU_NS, "record", first)) {942 while (reader.readStart(ns.sruNS(), "record", first)) { 910 943 if (first) { 911 944 first = false; … … 915 948 916 949 /* 917 * common error: recordPacking before recordSchema 950 * common error: recordEscaping / recordPacking 951 * (SRU 1.2) in wrong order 918 952 */ 919 SRURecord Packing packing = null;953 SRURecordXmlEscaping recordXmlEscaping = null; 920 954 if (!strictMode && 921 reader.peekStart(SRU_NS, "recordPacking")) { 922 packing = parseRecordPacking(reader, false); 923 if (packing != null) { 955 reader.peekStart(ns.sruNS(), "recordPacking")) { 956 recordXmlEscaping = 957 parseRecordXmlEscaping(reader, ns, 958 version, false); 959 if (recordXmlEscaping != null) { 924 960 logger.error("element <recordPacking> " + 925 961 "must appear after element " + … … 928 964 } 929 965 } 930 String schema = reader.readContent(SRU_NS, 966 967 String schema = reader.readContent(ns.sruNS(), 931 968 "recordSchema", true); 932 if (packing == null) { 933 packing = parseRecordPacking(reader, strictMode); 969 970 // (SRU 2.0) recordPacking (optional) 971 // XXX: what to do with it? 972 SRURecordPacking recordPacking = null; 973 if (version == SRUVersion.VERSION_2_0) { 974 recordPacking = parseRecordPacking(reader, 975 ns.sruNS(), strictMode); 934 976 } 935 977 936 logger.debug("schema = {}, packing = {}, " + 978 if (recordXmlEscaping == null) { 979 recordXmlEscaping = 980 parseRecordXmlEscaping(reader, ns, 981 version, strictMode); 982 } 983 984 logger.debug("schema = {}, escpaing = {}, " + 985 "packing = {}, requested escaping = {}, " + 937 986 "requested packing = {}", 938 schema, packing, 987 schema, recordXmlEscaping, recordPacking, 988 request.getRecordXmlEscaping(), 939 989 request.getRecordPacking()); 940 990 941 if ((request.getRecord Packing() != null) &&942 ( packing != request.getRecordPacking())) {943 final SRURecord Packing p =944 request.getRecord Packing();945 logger.error("requested '{}' record packing, " +991 if ((request.getRecordXmlEscaping() != null) && 992 (recordXmlEscaping != request.getRecordXmlEscaping())) { 993 final SRURecordXmlEscaping p = 994 request.getRecordXmlEscaping(); 995 logger.error("requested '{}' record XML escaping, " + 946 996 "but server responded with '{}' " + 947 "record packing",997 "record XML escaping", 948 998 p.getStringValue(), 949 packing.getStringValue());999 recordXmlEscaping.getStringValue()); 950 1000 if (strictMode) { 951 1001 throw new SRUClientException("requested '" + … … 953 1003 "' record packing, but server " + 954 1004 "responded with '" + 955 packing.getStringValue() +1005 recordXmlEscaping.getStringValue() + 956 1006 "' record packing"); 957 1007 } … … 959 1009 960 1010 // searchRetrieveResponse/record/recordData 961 reader.readStart( SRU_NS, "recordData", true);1011 reader.readStart(ns.sruNS(), "recordData", true); 962 1012 reader.consumeWhitespace(); 963 1013 … … 966 1016 SRUXMLStreamReader recordReader = null; 967 1017 968 if ( packing == SRURecordPacking.STRING) {1018 if (recordXmlEscaping == SRURecordXmlEscaping.STRING) { 969 1019 /* 970 1020 * read content into temporary buffer and then … … 981 1031 982 1032 if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) { 983 surrogate = parseDiagnostic(recordReader, true,984 strictMode);1033 surrogate = parseDiagnostic(recordReader, ns, 1034 true, strictMode); 985 1035 } else { 986 1036 SRURecordDataParser parser = findParser(schema); … … 998 1048 * packing 999 1049 */ 1000 if ( packing == SRURecordPacking.STRING) {1050 if (recordXmlEscaping == SRURecordXmlEscaping.STRING) { 1001 1051 recordReader.closeCompletly(); 1002 1052 } … … 1036 1086 1037 1087 reader.consumeWhitespace(); 1038 reader.readEnd( SRU_NS, "recordData", true);1088 reader.readEnd(ns.sruNS(), "recordData", true); 1039 1089 1040 1090 String identifier = null; 1041 1091 if (version == SRUVersion.VERSION_1_2) { 1042 identifier = reader.readContent( SRU_NS,1092 identifier = reader.readContent(ns.sruNS(), 1043 1093 "recordIdentifier", false); 1044 1094 } 1045 1095 1046 int position = reader.readContent( SRU_NS,1096 int position = reader.readContent(ns.sruNS(), 1047 1097 "recordPosition", false, -1); 1048 1098 … … 1062 1112 } 1063 1113 1064 if (reader.readStart( SRU_NS,1114 if (reader.readStart(ns.sruNS(), 1065 1115 "extraRecordData", false)) { 1066 1116 reader.consumeWhitespace(); … … 1075 1125 } 1076 1126 reader.consumeWhitespace(); 1077 reader.readEnd( SRU_NS, "extraRecordData", true);1127 reader.readEnd(ns.sruNS(), "extraRecordData", true); 1078 1128 } 1079 1129 1080 reader.readEnd( SRU_NS, "record");1130 reader.readEnd(ns.sruNS(), "record"); 1081 1131 recordCount++; 1082 1132 } // while 1083 reader.readEnd( SRU_NS, "records");1133 reader.readEnd(ns.sruNS(), "records"); 1084 1134 } 1085 1135 if (recordCount == 0) { … … 1093 1143 * an empty <records> element 1094 1144 */ 1095 if (reader.readStart( SRU_NS, "records", false)) {1145 if (reader.readStart(ns.sruNS(), "records", false)) { 1096 1146 int bad = 0; 1097 while (reader.readStart( SRU_NS, "record", false)) {1147 while (reader.readStart(ns.sruNS(), "record", false)) { 1098 1148 bad++; 1099 reader.readEnd( SRU_NS, "record", true);1149 reader.readEnd(ns.sruNS(), "record", true); 1100 1150 } 1101 reader.readEnd( SRU_NS, "records", true);1151 reader.readEnd(ns.sruNS(), "records", true); 1102 1152 if (bad == 0) { 1103 1153 logger.error("endpoint declared 0 results, but " + … … 1126 1176 } 1127 1177 1128 int nextRecordPosition = reader.readContent( SRU_NS,1178 int nextRecordPosition = reader.readContent(ns.sruNS(), 1129 1179 "nextRecordPosition", false, -1); 1130 1180 logger.debug("nextRecordPosition = {}", nextRecordPosition); … … 1132 1182 1133 1183 // searchRetrieveResponse/echoedSearchRetrieveResponse 1134 if (reader.readStart( SRU_NS,1184 if (reader.readStart(ns.sruNS(), 1135 1185 "echoedSearchRetrieveRequest", false)) { 1136 reader.readEnd( SRU_NS, "echoedSearchRetrieveRequest", true);1186 reader.readEnd(ns.sruNS(), "echoedSearchRetrieveRequest", true); 1137 1187 } 1138 1188 … … 1156 1206 // searchRetrieveResponse/diagnostics 1157 1207 final List<SRUDiagnostic> diagnostics = 1158 parseDiagnostics(reader, strictMode);1208 parseDiagnostics(reader, ns, ns.toString(), strictMode); 1159 1209 if (diagnostics != null) { 1160 1210 handler.onDiagnostics(diagnostics); … … 1162 1212 1163 1213 // explainResponse/extraResponseData 1164 if (reader.readStart( SRU_NS, "extraResponseData", false)) {1214 if (reader.readStart(ns.sruNS(), "extraResponseData", false)) { 1165 1215 reader.consumeWhitespace(); 1166 1216 proxy.reset(reader); … … 1172 1222 } 1173 1223 reader.consumeWhitespace(); 1174 reader.readEnd(SRU_NS, "extraResponseData", true); 1175 } 1176 1177 reader.readEnd(SRU_NS, "searchRetrieveResponse"); 1224 reader.readEnd(ns.sruNS(), "extraResponseData", true); 1225 } 1226 1227 if (version == SRUVersion.VERSION_2_0) { 1228 // SRU (2.0) arbitrary stuff 1229 // SRU (2.0) resultSetTTL (replaces resultSetIdleTime) 1230 // SRU (2.0) resultCountPrecision 1231 if (reader.readStart(ns.sruNS(), "resultCountPrecision", false)) { 1232 reader.readEnd(ns.sruNS(), "resultCountPrecision", true); 1233 } 1234 // SRU (2.0) facetedResults 1235 // SRU (2.0) searchResultAnalysis 1236 } 1237 reader.readEnd(ns.sruNS(), "searchRetrieveResponse"); 1178 1238 } 1179 1239 } catch (XMLStreamException e) { … … 1183 1243 1184 1244 1185 private static SRUVersion parseVersion(SRUXMLStreamReader reader )1186 throws XMLStreamException, SRUClientException {1187 final String v = reader.readContent( SRU_NS, "version", true);1245 private static SRUVersion parseVersion(SRUXMLStreamReader reader, 1246 String envelopNs) throws XMLStreamException, SRUClientException { 1247 final String v = reader.readContent(envelopNs, "version", true); 1188 1248 if (VERSION_1_1.equals(v)) { 1189 1249 return SRUVersion.VERSION_1_1; 1190 1250 } else if (VERSION_1_2.equals(v)) { 1191 1251 return SRUVersion.VERSION_1_2; 1252 } else if (VERSION_2_0.equals(v)) { 1253 return SRUVersion.VERSION_2_0; 1192 1254 } else { 1193 1255 throw new SRUClientException("invalid value '" + v + … … 1199 1261 1200 1262 private static List<SRUDiagnostic> parseDiagnostics( 1201 SRUXMLStreamReader reader, boolean strictMode)1263 SRUXMLStreamReader reader, SRUNamespaces ns, String responseNs, boolean strictMode) 1202 1264 throws XMLStreamException, SRUClientException { 1203 if (reader.readStart( SRU_NS, "diagnostics", false)) {1265 if (reader.readStart(responseNs, "diagnostics", false)) { 1204 1266 List<SRUDiagnostic> diagnostics = null; 1205 1267 1206 1268 SRUDiagnostic diagnostic = null; 1207 while ((diagnostic = parseDiagnostic(reader, 1269 while ((diagnostic = parseDiagnostic(reader, ns, 1208 1270 (diagnostics == null), strictMode)) != null) { 1209 1271 if (diagnostics == null) { … … 1212 1274 diagnostics.add(diagnostic); 1213 1275 } // while 1214 reader.readEnd( SRU_NS, "diagnostics");1276 reader.readEnd(responseNs, "diagnostics"); 1215 1277 return diagnostics; 1216 1278 } else { … … 1221 1283 1222 1284 private static SRUDiagnostic parseDiagnostic(SRUXMLStreamReader reader, 1223 boolean required, boolean strictMode) throws XMLStreamException,1285 SRUNamespaces ns, boolean required, boolean strictMode) throws XMLStreamException, 1224 1286 SRUClientException { 1225 if (reader.readStart( SRU_DIAGNOSIC_NS, "diagnostic", required)) {1287 if (reader.readStart(ns.diagnosticNS(), "diagnostic", required)) { 1226 1288 1227 1289 // diagnostic/uri 1228 String uri = reader.readContent( SRU_DIAGNOSIC_NS, "uri", true);1290 String uri = reader.readContent(ns.diagnosticNS(), "uri", true); 1229 1291 1230 1292 String details = null; … … 1232 1294 if (strictMode) { 1233 1295 // diagnostic/details 1234 details = reader.readContent( SRU_DIAGNOSIC_NS, "details",1296 details = reader.readContent(ns.diagnosticNS(), "details", 1235 1297 false, true); 1236 1298 1237 1299 // diagnostic/message 1238 message = reader.readContent( SRU_DIAGNOSIC_NS, "message",1300 message = reader.readContent(ns.diagnosticNS(), "message", 1239 1301 false, true); 1240 1302 } else { … … 1243 1305 * appear in any order 1244 1306 */ 1245 if (reader.peekStart( SRU_DIAGNOSIC_NS, "details")) {1246 details = reader.readContent( SRU_DIAGNOSIC_NS, "details",1307 if (reader.peekStart(ns.diagnosticNS(), "details")) { 1308 details = reader.readContent(ns.diagnosticNS(), "details", 1247 1309 false, false); 1248 message = reader.readContent( SRU_DIAGNOSIC_NS, "message",1310 message = reader.readContent(ns.diagnosticNS(), "message", 1249 1311 false, false); 1250 1312 } else { 1251 message = reader.readContent( SRU_DIAGNOSIC_NS, "message",1313 message = reader.readContent(ns.diagnosticNS(), "message", 1252 1314 false, false); 1253 details = reader.readContent( SRU_DIAGNOSIC_NS, "details",1315 details = reader.readContent(ns.diagnosticNS(), "details", 1254 1316 false, false); 1255 1317 if ((message != null) && (details != null)) { … … 1272 1334 } 1273 1335 1274 reader.readEnd( SRU_DIAGNOSIC_NS, "diagnostic");1336 reader.readEnd(ns.diagnosticNS(), "diagnostic"); 1275 1337 1276 1338 logger.debug("diagnostic: uri={}, detail={}, message={}", … … 1284 1346 1285 1347 private static SRURecordPacking parseRecordPacking( 1286 SRUXMLStreamReader reader, boolean strictMode) 1348 SRUXMLStreamReader reader, String envelopNs, boolean strictMode) 1349 throws XMLStreamException, SRUClientException { 1350 final String s = reader.readContent(envelopNs, "recordPacking", false); 1351 if (s != null) { 1352 if (RECORD_PACKING_PACKED.equals(s)) { 1353 return SRURecordPacking.PACKED; 1354 } else if (RECORD_PACKING_UNPACKED.equals(s)) { 1355 return SRURecordPacking.UNPACKED; 1356 } else if (!strictMode && RECORD_PACKING_PACKED.equalsIgnoreCase(s)) { 1357 logger.error("invalid value '{}' for '<recordPacking>', should be '{}'", 1358 s, RECORD_PACKING_PACKED); 1359 return SRURecordPacking.PACKED; 1360 } else if (!strictMode && RECORD_PACKING_UNPACKED.equalsIgnoreCase(s)) { 1361 logger.error("invalid value '{}' for '<recordPacking>', should be '{}'", 1362 s, RECORD_PACKING_UNPACKED); 1363 return SRURecordPacking.UNPACKED; 1364 } else { 1365 throw new SRUClientException("invalid value '" + s + 1366 "' for '<recordPacking>' (valid values are: '" + 1367 RECORD_PACKING_PACKED + "' and '" + RECORD_PACKING_UNPACKED + 1368 "')"); 1369 } 1370 } 1371 return null; 1372 } 1373 1374 1375 private static SRURecordXmlEscaping parseRecordXmlEscaping( 1376 SRUXMLStreamReader reader, SRUNamespaces ns, SRUVersion version, boolean strictMode) 1287 1377 throws XMLStreamException, SRUClientException { 1288 final String v = reader.readContent(SRU_NS, "recordPacking", true); 1289 1290 if (RECORD_PACKING_XML.equals(v)) { 1291 return SRURecordPacking.XML; 1292 } else if (RECORD_PACKING_STRING.equals(v)) { 1293 return SRURecordPacking.STRING; 1294 } else if (!strictMode && RECORD_PACKING_XML.equalsIgnoreCase(v)) { 1295 logger.error("invalid value '{}' for record packing, should be '{}'", 1296 v, RECORD_PACKING_XML); 1297 return SRURecordPacking.XML; 1298 } else if (!strictMode && RECORD_PACKING_STRING.equalsIgnoreCase(v)) { 1299 logger.error("invalid value '{}' for record packing, should be '{}'", 1300 v, RECORD_PACKING_STRING); 1301 return SRURecordPacking.STRING; 1302 1378 1379 final String name = (version == SRUVersion.VERSION_2_0) 1380 ? "recordXMLEscaping" : "recordPacking"; 1381 final String s = reader.readContent(ns.sruNS(), name, true); 1382 1383 if (RECORD_ESCAPING_XML.equals(s)) { 1384 return SRURecordXmlEscaping.XML; 1385 } else if (RECORD_ESCAPING_STRING.equals(s)) { 1386 return SRURecordXmlEscaping.STRING; 1387 } else if (!strictMode && RECORD_ESCAPING_XML.equalsIgnoreCase(s)) { 1388 logger.error("invalid value '{}' for '<{}>', should be '{}'", 1389 s, name, RECORD_ESCAPING_XML); 1390 return SRURecordXmlEscaping.XML; 1391 } else if (!strictMode && RECORD_ESCAPING_STRING.equalsIgnoreCase(s)) { 1392 logger.error("invalid value '{}' for '<{}>', should be '{}'", 1393 s, name, RECORD_ESCAPING_STRING); 1394 return SRURecordXmlEscaping.STRING; 1303 1395 } else { 1304 throw new SRUClientException("invalid value '" + v+1305 "' for record packing(valid values are: '" +1306 RECORD_ PACKING_XML + "' and '" + RECORD_PACKING_STRING +1396 throw new SRUClientException("invalid value '" + s + 1397 "' for '<" + name + ">' (valid values are: '" + 1398 RECORD_ESCAPING_XML + "' and '" + RECORD_ESCAPING_STRING + 1307 1399 "')"); 1308 1400 } … … 1358 1450 } 1359 1451 1452 1453 private static SRUNamespaces detectNamespace(XMLStreamReader reader, 1454 SRUVersion requestedVersion) 1455 throws SRUClientException { 1456 try { 1457 // skip to first start tag 1458 reader.nextTag(); 1459 1460 final String namespaceURI = reader.getNamespaceURI(); 1461 logger.debug("found namespace URI '{}', requested version = {}", 1462 namespaceURI, requestedVersion); 1463 1464 if (NAMESPACES_LEGACY_LOC.foo(namespaceURI)) { 1465 return NAMESPACES_LEGACY_LOC; 1466 } else if (NAMESPACES_OASIS.foo(namespaceURI)) { 1467 return NAMESPACES_OASIS; 1468 } else { 1469 throw new SRUClientException( 1470 "invalid namespace '" + reader.getNamespaceURI() + "'"); 1471 } 1472 } catch (XMLStreamException e) { 1473 throw new SRUClientException("error detecting namespace", e); 1474 } 1475 } 1476 1477 1478 private interface SRUNamespaces { 1479 public String sruNS(); 1480 1481 public String scanNS(); 1482 1483 public String diagnosticNS(); 1484 1485 public boolean foo(String namespaceURI); 1486 1487 } // interface SRUNamespace 1488 1489 1490 private static final SRUNamespaces NAMESPACES_LEGACY_LOC = new SRUNamespaces() { 1491 private static final String SRU_NS = 1492 "http://www.loc.gov/zing/srw/"; 1493 private static final String SRU_DIAGNOSIC_NS = 1494 "http://www.loc.gov/zing/srw/diagnostic/"; 1495 1496 1497 @Override 1498 public String sruNS() { 1499 return SRU_NS; 1500 } 1501 1502 1503 @Override 1504 public String scanNS() { 1505 return SRU_NS; 1506 } 1507 1508 1509 @Override 1510 public String diagnosticNS() { 1511 return SRU_DIAGNOSIC_NS; 1512 } 1513 1514 1515 @Override 1516 public boolean foo(String namespaceURI) { 1517 return SRU_NS.equals(namespaceURI); 1518 } 1519 }; 1520 1521 1522 private static final SRUNamespaces NAMESPACES_OASIS = new SRUNamespaces() { 1523 private static final String SRU_NS = 1524 "http://docs.oasis-open.org/ns/search-ws/sruResponse"; 1525 private static final String SRU_SCAN_NS = 1526 "http://docs.oasis-open.org/ns/search-ws/scan"; 1527 private static final String SRU_DIAGNOSIC_NS = 1528 "http://docs.oasis-open.org/ns/search-ws/diagnostic"; 1529 1530 1531 @Override 1532 public String sruNS() { 1533 return SRU_NS; 1534 } 1535 1536 1537 @Override 1538 public String scanNS() { 1539 return SRU_SCAN_NS; 1540 1541 } 1542 1543 1544 @Override 1545 public String diagnosticNS() { 1546 return SRU_DIAGNOSIC_NS; 1547 } 1548 1549 1550 @Override 1551 public boolean foo(String namespaceURI) { 1552 return SRU_NS.equals(namespaceURI) || SRU_SCAN_NS.equals(namespaceURI); 1553 } 1554 }; 1555 1360 1556 } // class SRUSimpleClient -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUVersion.java
r5750 r6903 29 29 * SRU/CQL version 1.2 30 30 */ 31 VERSION_1_2 31 VERSION_1_2, 32 33 /** 34 * SRU/CQL version 2.0 35 */ 36 VERSION_2_0; 32 37 33 38 } // enum SRUVersion -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/fcs/ClarinFCSEndpointDescriptionParser.java
r5804 r6903 105 105 throws XMLStreamException, SRUClientException { 106 106 final int version = parseVersion(reader); 107 if (version != EXPECTED_VERSION) { 107 // FIXME: actually handle other version 108 if ((version != EXPECTED_VERSION) && (version != 2)) { 108 109 throw new SRUClientException("Attribute 'version' of " + 109 110 "element '<EndpointDescription>' must be of value '1'"); … … 201 202 MIMETYPE_HITS_DATAVIEW + 202 203 "') to conform to CLARIN-FCS specification"); 204 } 205 206 // SupportedLayers 207 // FIXME: actually do something useful 208 if (XmlStreamReaderUtils.readStart(reader, ED_NS_URI, 209 "SupportedLayers", false)) { 210 XmlStreamReaderUtils.readEnd(reader, ED_NS_URI, 211 "SupportedLayers", true); 203 212 } 204 213 … … 315 324 logger.debug("DataViews: {}", dataviews); 316 325 326 327 // FIXME: actually do something useful 328 if (XmlStreamReaderUtils.readStart(reader, ED_NS_URI, "AvailableLayers", false, true)) { 329 final String ls = XmlStreamReaderUtils.readAttributeValue(reader, null, "ref", true); 330 reader.next(); // consume start tag 331 XmlStreamReaderUtils.readEnd(reader, ED_NS_URI, "AvailableLayers"); 332 logger.debug("Layers: {}", dataviews); 333 } 334 335 317 336 List<ResourceInfo> subResources = null; 318 337 if (XmlStreamReaderUtils.peekStart(reader, -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestClient.java
r5804 r6903 35 35 SRUClient client = new ClarinFCSClientBuilder() 36 36 .addDefaultDataViewParsers() 37 .setDefaultSRUVersion(SRUVersion.VERSION_2_0) 37 38 .unknownDataViewAsString() 38 39 .enableLegacySupport() -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestUtils.java
r5799 r6903 65 65 // request.setRecordSchema(ClarinFCSRecordData.LEGACY_RECORD_SCHEMA); 66 66 request.setMaximumRecords(5); 67 request.setRecordPacking(SRURecordPacking.XML); 67 request.setRecordXmlEscaping(SRURecordXmlEscaping.XML); 68 request.setRecordPacking(SRURecordPacking.PACKED); 68 69 request.setExtraRequestData("x-indent-response", "4"); 69 70 return request;
Note: See TracChangeset
for help on using the changeset viewer.