Changeset 6903


Ignore:
Timestamp:
01/12/16 21:03:47 (8 years ago)
Author:
Oliver Schonefeld
Message:
  • some steps towards SRU 2.0
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  
    3434 */
    3535abstract 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-";
    5872    /** for end-point conformance testing only. never use in production. */
    5973    public static final String X_MALFORMED_OPERATION   =
     
    220234     * Get the version for this request.
    221235     *
    222      * @return version for this request or <code>null</code> of client default
     236     * @return version for this request or <code>null</code> if client default
    223237     *         is used
    224238     */
     
    347361                    new URIHelper(new URIBuilder(baseURI));
    348362
    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                    }
    377397                }
    378             }
    379 
    380             /*
    381              * append version parameter
    382              *
    383              * NB: Setting "x-malformed-version" as an extra request parameter
    384              * makes the client send invalid requests. This is intended to
    385              * 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                 } // switch
    403             } 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                    }
    406426                }
     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);
    407443            }
    408444
    409445            // request specific parameters
    410             addParametersToURI(uriBuilder);
     446            addParametersToURI(uriBuilder, versionRequested);
    411447
    412448            // extraRequestData
     
    418454                    /*
    419455                     * make sure, we skip the client-internal parameters
    420                      * to generate invalid requests ...
     456                     * used to generate invalid requests ...
    421457                     */
    422458                    if (!key.startsWith(MALFORMED_KEY_PREFIX)) {
     
    442478
    443479
    444     abstract void addParametersToURI(URIHelper uriBuilder)
     480    abstract void addParametersToURI(URIHelper uriBuilder, SRUVersion version)
    445481            throws SRUClientException;
    446482
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClientConstants.java

    r5750 r6903  
    2525    public static final String RECORD_DATA_PARSER_SCHEMA_ANY = "*";
    2626
     27    /** constant for CQL query type */
     28    public static final String QUERY_TYPE_CQL = "cql";
     29
    2730} // class SRUClientConstants
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRecordDataParser.java

    r5750 r6903  
    5252    private static final String VERSION_1_1     = "1.1";
    5353    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";
    5456    private static final String TRANSPORT_HTTP  = "http";
    5557    private static final String TRANSPORT_HTTPS = "https";
     
    180182            } else if (VERSION_1_2.equals(s)) {
    181183                version = SRUVersion.VERSION_1_2;
     184            } else if (VERSION_2.equals(s) || VERSION_2_0.equals(s)) {
     185                version = SRUVersion.VERSION_2_0;
    182186            } else {
    183187                throw new SRUClientException("invalid or unsupported value '" +
    184                         s + "'for attribute 'version' on element '" +
     188                        s + " 'for attribute 'version' on element '" +
    185189                        reader.getName() + "'");
    186190            }
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRequest.java

    r5750 r6903  
    2626 */
    2727public class SRUExplainRequest extends SRUAbstractRequest {
    28     private SRURecordPacking recordPacking;
     28    private SRURecordXmlEscaping recordXmlEscaping;
    2929    private boolean parseRecordDataEnabled = false;
    3030
     
    5252
    5353    /**
    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.
    5556     *
    56      * @param recordPacking
    57      *            the requested record packing
    58      * @see SRURecordPacking
     57     * @return the requested record packing
     58     * @see SRURecordXmlEscaping
    5959     */
    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;
    6562    }
    6663
    6764
    6865    /**
    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.
    7068     *
    71      * @return the requested record packing
    72      * @see SRURecordPacking
     69     * @param getRecordXmlEscaping
     70     *            the requested record XML escaping
     71     * @see SRURecordXmlEscaping
    7372     */
    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;
    7690    }
    7791
     
    90104
    91105
    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      *         otherwise
    98      */
    99     public boolean isParseRecordDataEnabled() {
    100         return parseRecordDataEnabled;
    101     }
    102 
    103 
    104106    @Override
    105107    SRUOperation getOperation() {
     
    109111
    110112    @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
    117134                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                }
    120150                break;
    121151            default:
    122                 throw new SRUClientException("unsupported record packing: " +
    123                         recordPacking);
    124             } // switch
     152                /* cannot happen */
     153                break;
     154            }
    125155        }
    126156    }
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRURecordPacking.java

    r5750 r6903  
    11/**
    2  * This software is copyright (c) 2012-2014 by
     2 * This software is copyright (c) 2011-2015 by
    33 *  - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
    44 * This is free software. You can redistribute it
     
    1818
    1919/**
    20  * SRU record packing
     20 * SRU 2.0 record packing.
    2121 */
    2222public enum SRURecordPacking {
    2323    /**
    24      * XML record packing
     24     * The client requests that the server should supply records strictly
     25     * according to the requested schema.
    2526     */
    26     XML {
    27         @Override
    28         String getStringValue() {
    29             return "xml";
    30         }
    31     },
     27    PACKED,
    3228
    3329    /**
    34      * String record packing
     30     * The server is free to allow the location of application data to vary
     31     * within the record.
    3532     */
    36     STRING {
    37         @Override
    38         String getStringValue() {
    39             return "string";
    40         }
    41     };
    42 
    43     abstract String getStringValue();
    44 
     33    UNPACKED
    4534} // enum SRURecordPacking
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRURecordXmlEscaping.java

    r6079 r6903  
    2020 * SRU record packing
    2121 */
    22 public enum SRURecordPacking {
     22public enum SRURecordXmlEscaping {
    2323    /**
    2424     * XML record packing
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUScanRequest.java

    r5750 r6903  
    158158
    159159    @Override
    160     void addParametersToURI(URIHelper uriHelper) throws SRUClientException {
     160    void addParametersToURI(URIHelper uriHelper, SRUVersion version)
     161            throws SRUClientException {
    161162        // scanClause
    162163        final String malformedScan =
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSearchRetrieveRequest.java

    r5750 r6903  
    4343            "x-malformed-maximumRecords";
    4444    /** 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 =
    4646            "x-malformed-recordPacking";
     47    private String queryType;
    4748    private String query;
    4849    private int startRecord = -1;
    4950    private int maximumRecords = -1;
     51    private SRURecordXmlEscaping recordXmlEscaping;
    5052    private SRURecordPacking recordPacking;
    5153    private String recordSchema;
     
    7274    public SRUSearchRetrieveRequest(String baseURI) {
    7375        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;
    74120    }
    75121
     
    188234
    189235    /**
    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
    194268     */
    195269    public SRURecordPacking getRecordPacking() {
     
    199273
    200274    /**
    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
    207280     */
    208281    public void setRecordPacking(SRURecordPacking recordPacking) {
     
    245318
    246319    @Override
    247     void addParametersToURI(URIHelper uriHelper) throws SRUClientException {
     320    void addParametersToURI(URIHelper uriHelper, SRUVersion version)
     321            throws SRUClientException {
    248322        /*
    249323         * append query argument (mandatory)
     
    268342
    269343        /*
     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        /*
    270353         * append startRecord argument (optional)
    271354         *
     
    309392
    310393        /*
    311          * append recordPacking argument (optional)
     394         * append record XML escaping argument (optional)
    312395         *
    313396         * NB: Setting "x-malformed-recordPacking" as an extra request
     
    316399         * (i.e. provoke an error) and SHOULD NEVER be used in production!
    317400         */
    318         final String malformedRecordPacking =
    319                 getExtraRequestData(X_MALFORMED_RECORD_PACKING);
    320         if (malformedRecordPacking == null) {
    321             if (recordPacking != null) {
    322                 switch (recordPacking) {
     401        final String malformedRecordXmlEscaping =
     402                getExtraRequestData(X_MALFORMED_RECORD_XML_ESCAPING);
     403        if (malformedRecordXmlEscaping == null) {
     404            if (recordXmlEscaping != null) {
     405                switch (recordXmlEscaping) {
    323406                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                    }
    325414                    break;
    326415                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                    }
    329423                    break;
    330424                default:
    331425                    throw new SRUClientException(
    332                             "unsupported record packing: " + recordPacking);
     426                            "unsupported record XML escpaing: " + recordXmlEscaping);
    333427                } // switch
    334428            }
    335429        } 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);
    339455        }
    340456
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java

    r6077 r6903  
    6969    private static final String USER_AGENT = "SRU-Client/1.0.0";
    7070    /** 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/";
    7575    private static final String SRU_DIAGNOSTIC_RECORD_SCHEMA =
    7676            "info:srw/schema/1/diagnostics-v1.1";
    7777    private static final String VERSION_1_1 = "1.1";
    7878    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";
    8184    private static final Logger logger =
    8285            LoggerFactory.getLogger(SRUSimpleClient.class);
     
    454457        logger.debug("parsing 'explain' response (mode = {})",
    455458                (request.isStrictMode() ? "strict" : "non-strict"));
     459
     460        /* detect response namespaces */
     461        final SRUNamespaces ns =
     462                detectNamespace(reader, request.getRequestedVersion());
     463
    456464        /*
    457465         * Eventually, SRUClient should always parse explain record data.
     
    462470            logger.debug("parsing of explain record data skipped");
    463471        }
    464         doParseExplainResponse(reader, request, handler, parse);
     472        doParseExplainResponse(reader, ns, request, handler, parse);
    465473    }
    466474
    467475
    468476    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 {
    471480        try {
    472481            final boolean strictMode = request.isStrictMode();
    473482
    474483            // explainResponse
    475             reader.readStart(SRU_NS, "explainResponse", true);
     484            reader.readStart(ns.sruNS(), "explainResponse", true);
    476485
    477486            // explainResponse/version
    478             SRUVersion version = parseVersion(reader);
     487            final SRUVersion version = parseVersion(reader, ns.sruNS());
    479488            logger.debug("version = {}, requested = {}", version,
    480489                    request.getRequestedVersion());
    481490
    482491            // explainResponse/record
    483             reader.readStart(SRU_NS, "record", true);
     492            reader.readStart(ns.sruNS(), "record", true);
    484493            if (parseRecordData) {
    485494                handler.onStartRecords(-1, null, -1);
    486495
    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) {
    491505                        logger.error("element <recordPacking> must apperear " +
    492506                                "after element <recordSchema> within " +
     
    494508                    }
    495509                }
     510
    496511                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);
    502528
    503529                // explainResponse/record/recordData
    504                 reader.readStart(SRU_NS, "recordData", true);
     530                reader.readStart(ns.sruNS(), "recordData", true);
    505531                reader.consumeWhitespace();
    506532
     
    508534                SRUXMLStreamReader recordReader = null;
    509535
    510                 if (packing == SRURecordPacking.STRING) {
     536                if (recordXmlEscaping == SRURecordXmlEscaping.STRING) {
    511537                    /*
    512538                     * read content into temporary buffer and then use a new XML
     
    538564                     * string record packing
    539565                     */
    540                     if (packing == SRURecordPacking.STRING) {
     566                    if (recordXmlEscaping == SRURecordXmlEscaping.STRING) {
    541567                        recordReader.closeCompletly();
    542568                    }
     
    547573                }
    548574                reader.consumeWhitespace();
    549                 reader.readEnd(SRU_NS, "recordData", true);
     575                reader.readEnd(ns.sruNS(), "recordData", true);
    550576
    551577                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);
    555581
    556582                // notify handler
    557583                handler.onRecord(null, -1, recordData);
    558584
    559                 if (reader.readStart(SRU_NS, "extraRecordData", false)) {
     585                if (reader.readStart(ns.sruNS(), "extraRecordData", false)) {
    560586                    reader.consumeWhitespace();
    561587                    proxy.reset(reader);
     
    569595                    }
    570596                    reader.consumeWhitespace();
    571                     reader.readEnd(SRU_NS, "extraRecordData", true);
     597                    reader.readEnd(ns.sruNS(), "extraRecordData", true);
    572598                }
    573599
    574600                handler.onFinishRecords(-1);
    575601
    576                 reader.readEnd(SRU_NS, "record");
     602                reader.readEnd(ns.sruNS(), "record");
    577603            } else {
    578604                /*
     
    580606                 * until <record> end tag
    581607                 */
    582                 reader.readEnd(SRU_NS, "record", true);
     608                reader.readEnd(ns.sruNS(), "record", true);
    583609            }
    584610
    585611            // 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);
    588614            }
    589615
     
    604630            // explainResponse/diagnostics
    605631            final List<SRUDiagnostic> diagnostics =
    606                     parseDiagnostics(reader, strictMode);
     632                    parseDiagnostics(reader, ns, ns.scanNS(), strictMode);
    607633            if (diagnostics != null) {
    608634                handler.onDiagnostics(diagnostics);
     
    610636
    611637            // explainResponse/extraResponseData
    612             if (reader.readStart(SRU_NS, "extraResponseData", false)) {
     638            if (reader.readStart(ns.sruNS(), "extraResponseData", false)) {
    613639                reader.consumeWhitespace();
    614640                proxy.reset(reader);
     
    621647                }
    622648                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");
    627653        } catch (XMLStreamException e) {
    628654            throw new SRUClientException(e.getMessage(), e);
     
    635661            throws SRUClientException {
    636662        try {
     663            /* detect response namespaces */
     664            final SRUNamespaces ns =
     665                    detectNamespace(reader, request.getRequestedVersion());
     666
    637667            /*
    638668             * if the endpoint cannot determine the operation, it should create
    639669             * a explain response.
    640670             */
    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() {
    643673                    @Override
    644674                    public void onRequestStatistics(int bytes, long millisTotal,
     
    691721                        (strictMode ? "strict" : "non-strict"));
    692722
    693 
    694723                // scanResponse
    695                 reader.readStart(SRU_NS, "scanResponse", true);
     724                reader.readStart(ns.scanNS(), "scanResponse", true);
    696725
    697726                // scanResponse/version
    698                 SRUVersion version = parseVersion(reader);
     727                final SRUVersion version = parseVersion(reader, ns.scanNS());
    699728                logger.debug("version = {}, requested = {}", version,
    700729                        request.getRequestedVersion());
    701730
    702731                // scanResponse/terms
    703                 if (reader.readStart(SRU_NS, "terms", false)) {
     732                if (reader.readStart(ns.scanNS(), "terms", false)) {
    704733                    boolean first = true;
    705                     while (reader.readStart(SRU_NS, "term", first)) {
     734                    while (reader.readStart(ns.scanNS(), "term", first)) {
    706735                        if (first) {
    707736                            first = false;
     
    711740                        // scanResponse/terms/value
    712741                        String value =
    713                                 reader.readContent(SRU_NS, "value", true);
     742                                reader.readContent(ns.scanNS(), "value", true);
    714743
    715744                        // scanResponse/terms/numberOfRecords
    716                         int numberOfRecords = reader.readContent(SRU_NS,
     745                        int numberOfRecords = reader.readContent(ns.scanNS(),
    717746                                "numberOfRecords", false, -1);
    718747
    719748                        // scanResponse/terms/displayTerm
    720                         String displayTerm = reader.readContent(SRU_NS,
     749                        String displayTerm = reader.readContent(ns.scanNS(),
    721750                                "displayTerm", false);
    722751
    723752                        // scanResponse/terms/whereInList
    724                         String s = reader.readContent(SRU_NS,
     753                        String s = reader.readContent(ns.scanNS(),
    725754                                "whereInList", false);
    726755                        SRUWhereInList whereInList = null;
     
    746775
    747776                        // scanResponse/terms/extraTermData
    748                         if (reader.readStart(SRU_NS, "extraTermData", first)) {
     777                        if (reader.readStart(ns.scanNS(), "extraTermData", first)) {
    749778                            reader.consumeWhitespace();
    750779                            proxy.reset(reader);
     
    757786                            }
    758787                            reader.consumeWhitespace();
    759                             reader.readEnd(SRU_NS, "extraTermData", true);
     788                            reader.readEnd(ns.scanNS(), "extraTermData", true);
    760789                        }
    761                         reader.readEnd(SRU_NS, "term", true);
     790                        reader.readEnd(ns.scanNS(), "term", true);
    762791
    763792                    } // while
    764                     reader.readEnd(SRU_NS, "terms");
     793                    reader.readEnd(ns.scanNS(), "terms");
    765794                    handler.onFinishTerms();
    766795                }
    767796
    768797                // 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);
    771800                }
    772801
     
    784813                // scanResponse/diagnostics
    785814                final List<SRUDiagnostic> diagnostics =
    786                         parseDiagnostics(reader, strictMode);
     815                        parseDiagnostics(reader, ns, ns.scanNS(), strictMode);
    787816                if (diagnostics != null) {
    788817                    handler.onDiagnostics(diagnostics);
     
    790819
    791820                // scanResponse/extraResponseData
    792                 if (reader.readStart(SRU_NS, "extraResponseData", false)) {
     821                if (reader.readStart(ns.scanNS(), "extraResponseData", false)) {
    793822                    reader.consumeWhitespace();
    794823                    proxy.reset(reader);
     
    801830                    }
    802831                    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");
    807836            }
    808837        } catch (XMLStreamException e) {
     
    816845            final SRUSearchRetrieveHandler handler) throws SRUClientException {
    817846        try {
     847            /* detect response namespaces */
     848            final SRUNamespaces ns =
     849                    detectNamespace(reader, request.getRequestedVersion());
     850
    818851            /*
    819852             * if the endpoint cannot determine the operation, it should create
    820853             * a explain response.
    821854             */
    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() {
    824857                    @Override
    825858                    public void onRequestStatistics(int bytes, long millisTotal,
     
    873906
    874907                // searchRetrieveResponse
    875                 reader.readStart(SRU_NS, "searchRetrieveResponse", true);
     908                reader.readStart(ns.sruNS(), "searchRetrieveResponse", true);
    876909
    877910                // searchRetrieveResponse/version
    878                 SRUVersion version = parseVersion(reader);
     911                final SRUVersion version = parseVersion(reader, ns.sruNS());
    879912                logger.debug("version = {}, requested = {}", version,
    880913                        request.getRequestedVersion());
    881914
    882915                // searchRetrieveResponse/numberOfRecords
    883                 int numberOfRecords = reader.readContent(SRU_NS,
     916                int numberOfRecords = reader.readContent(ns.sruNS(),
    884917                        "numberOfRecords", true, -1);
    885918
    886919                // searchRetrieveResponse/resultSetId
    887                 String resultSetId = reader.readContent(SRU_NS,
     920                String resultSetId = reader.readContent(ns.sruNS(),
    888921                        "resultSetId", false);
    889922
    890923                // searchRetrieveResponse/resultSetIdleTime
    891                 int resultSetIdleTime = reader.readContent(SRU_NS,
     924                int resultSetIdleTime = reader.readContent(ns.sruNS(),
    892925                        "resultSetIdleTime", false, -1);
    893926
     
    904937                     */
    905938                    int recordCount = 0;
    906                     if (reader.readStart(SRU_NS, "records", false)) {
     939                    if (reader.readStart(ns.sruNS(), "records", false)) {
    907940                        // searchRetrieveResponse/records/record
    908941                        boolean first = true;
    909                         while (reader.readStart(SRU_NS, "record", first)) {
     942                        while (reader.readStart(ns.sruNS(), "record", first)) {
    910943                            if (first) {
    911944                                first = false;
     
    915948
    916949                            /*
    917                              * common error: recordPacking before recordSchema
     950                             * common error: recordEscaping / recordPacking
     951                             * (SRU 1.2) in wrong order
    918952                             */
    919                             SRURecordPacking packing = null;
     953                            SRURecordXmlEscaping recordXmlEscaping = null;
    920954                            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) {
    924960                                    logger.error("element <recordPacking> " +
    925961                                            "must appear after element " +
     
    928964                                }
    929965                            }
    930                             String schema = reader.readContent(SRU_NS,
     966
     967                            String schema = reader.readContent(ns.sruNS(),
    931968                                    "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);
    934976                            }
    935977
    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 = {}, " +
    937986                                    "requested packing = {}",
    938                                     schema, packing,
     987                                    schema, recordXmlEscaping, recordPacking,
     988                                    request.getRecordXmlEscaping(),
    939989                                    request.getRecordPacking());
    940990
    941                             if ((request.getRecordPacking() != null) &&
    942                                     (packing != request.getRecordPacking())) {
    943                                 final SRURecordPacking p =
    944                                         request.getRecordPacking();
    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, " +
    946996                                        "but server responded with '{}' " +
    947                                         "record packing",
     997                                        "record XML escaping",
    948998                                        p.getStringValue(),
    949                                         packing.getStringValue());
     999                                        recordXmlEscaping.getStringValue());
    9501000                                if (strictMode) {
    9511001                                    throw new SRUClientException("requested '" +
     
    9531003                                            "' record packing, but server " +
    9541004                                            "responded with '" +
    955                                             packing.getStringValue() +
     1005                                            recordXmlEscaping.getStringValue() +
    9561006                                            "' record packing");
    9571007                                }
     
    9591009
    9601010                            // searchRetrieveResponse/record/recordData
    961                             reader.readStart(SRU_NS, "recordData", true);
     1011                            reader.readStart(ns.sruNS(), "recordData", true);
    9621012                            reader.consumeWhitespace();
    9631013
     
    9661016                            SRUXMLStreamReader recordReader = null;
    9671017
    968                             if (packing == SRURecordPacking.STRING) {
     1018                            if (recordXmlEscaping == SRURecordXmlEscaping.STRING) {
    9691019                                /*
    9701020                                 * read content into temporary buffer and then
     
    9811031
    9821032                            if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) {
    983                                 surrogate = parseDiagnostic(recordReader, true,
    984                                         strictMode);
     1033                                surrogate = parseDiagnostic(recordReader, ns,
     1034                                        true, strictMode);
    9851035                            } else {
    9861036                                SRURecordDataParser parser = findParser(schema);
     
    9981048                                         * packing
    9991049                                         */
    1000                                         if (packing == SRURecordPacking.STRING) {
     1050                                        if (recordXmlEscaping == SRURecordXmlEscaping.STRING) {
    10011051                                            recordReader.closeCompletly();
    10021052                                        }
     
    10361086
    10371087                            reader.consumeWhitespace();
    1038                             reader.readEnd(SRU_NS, "recordData", true);
     1088                            reader.readEnd(ns.sruNS(), "recordData", true);
    10391089
    10401090                            String identifier = null;
    10411091                            if (version == SRUVersion.VERSION_1_2) {
    1042                                 identifier = reader.readContent(SRU_NS,
     1092                                identifier = reader.readContent(ns.sruNS(),
    10431093                                        "recordIdentifier", false);
    10441094                            }
    10451095
    1046                             int position = reader.readContent(SRU_NS,
     1096                            int position = reader.readContent(ns.sruNS(),
    10471097                                    "recordPosition", false, -1);
    10481098
     
    10621112                            }
    10631113
    1064                             if (reader.readStart(SRU_NS,
     1114                            if (reader.readStart(ns.sruNS(),
    10651115                                    "extraRecordData", false)) {
    10661116                                reader.consumeWhitespace();
     
    10751125                                }
    10761126                                reader.consumeWhitespace();
    1077                                 reader.readEnd(SRU_NS, "extraRecordData", true);
     1127                                reader.readEnd(ns.sruNS(), "extraRecordData", true);
    10781128                            }
    10791129
    1080                             reader.readEnd(SRU_NS, "record");
     1130                            reader.readEnd(ns.sruNS(), "record");
    10811131                            recordCount++;
    10821132                        } // while
    1083                         reader.readEnd(SRU_NS, "records");
     1133                        reader.readEnd(ns.sruNS(), "records");
    10841134                    }
    10851135                    if (recordCount == 0) {
     
    10931143                     * an empty <records> element
    10941144                     */
    1095                     if (reader.readStart(SRU_NS, "records", false)) {
     1145                    if (reader.readStart(ns.sruNS(), "records", false)) {
    10961146                        int bad = 0;
    1097                         while (reader.readStart(SRU_NS, "record", false)) {
     1147                        while (reader.readStart(ns.sruNS(), "record", false)) {
    10981148                            bad++;
    1099                             reader.readEnd(SRU_NS, "record", true);
     1149                            reader.readEnd(ns.sruNS(), "record", true);
    11001150                        }
    1101                         reader.readEnd(SRU_NS, "records", true);
     1151                        reader.readEnd(ns.sruNS(), "records", true);
    11021152                        if (bad == 0) {
    11031153                            logger.error("endpoint declared 0 results, but " +
     
    11261176                }
    11271177
    1128                 int nextRecordPosition = reader.readContent(SRU_NS,
     1178                int nextRecordPosition = reader.readContent(ns.sruNS(),
    11291179                        "nextRecordPosition", false, -1);
    11301180                logger.debug("nextRecordPosition = {}", nextRecordPosition);
     
    11321182
    11331183                // searchRetrieveResponse/echoedSearchRetrieveResponse
    1134                 if (reader.readStart(SRU_NS,
     1184                if (reader.readStart(ns.sruNS(),
    11351185                        "echoedSearchRetrieveRequest", false)) {
    1136                     reader.readEnd(SRU_NS, "echoedSearchRetrieveRequest", true);
     1186                    reader.readEnd(ns.sruNS(), "echoedSearchRetrieveRequest", true);
    11371187                }
    11381188
     
    11561206                // searchRetrieveResponse/diagnostics
    11571207                final List<SRUDiagnostic> diagnostics =
    1158                         parseDiagnostics(reader, strictMode);
     1208                        parseDiagnostics(reader, ns, ns.toString(), strictMode);
    11591209                if (diagnostics != null) {
    11601210                    handler.onDiagnostics(diagnostics);
     
    11621212
    11631213                // explainResponse/extraResponseData
    1164                 if (reader.readStart(SRU_NS, "extraResponseData", false)) {
     1214                if (reader.readStart(ns.sruNS(), "extraResponseData", false)) {
    11651215                    reader.consumeWhitespace();
    11661216                    proxy.reset(reader);
     
    11721222                    }
    11731223                    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");
    11781238            }
    11791239        } catch (XMLStreamException e) {
     
    11831243
    11841244
    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);
    11881248        if (VERSION_1_1.equals(v)) {
    11891249            return SRUVersion.VERSION_1_1;
    11901250        } else if (VERSION_1_2.equals(v)) {
    11911251            return SRUVersion.VERSION_1_2;
     1252        } else if (VERSION_2_0.equals(v)) {
     1253            return SRUVersion.VERSION_2_0;
    11921254        } else {
    11931255            throw new SRUClientException("invalid value '" + v +
     
    11991261
    12001262    private static List<SRUDiagnostic> parseDiagnostics(
    1201             SRUXMLStreamReader reader, boolean strictMode)
     1263            SRUXMLStreamReader reader, SRUNamespaces ns, String responseNs, boolean strictMode)
    12021264            throws XMLStreamException, SRUClientException {
    1203         if (reader.readStart(SRU_NS, "diagnostics", false)) {
     1265        if (reader.readStart(responseNs, "diagnostics", false)) {
    12041266            List<SRUDiagnostic> diagnostics = null;
    12051267
    12061268            SRUDiagnostic diagnostic = null;
    1207             while ((diagnostic = parseDiagnostic(reader,
     1269            while ((diagnostic = parseDiagnostic(reader, ns,
    12081270                    (diagnostics == null), strictMode)) != null) {
    12091271                if (diagnostics == null) {
     
    12121274                diagnostics.add(diagnostic);
    12131275            } // while
    1214             reader.readEnd(SRU_NS, "diagnostics");
     1276            reader.readEnd(responseNs, "diagnostics");
    12151277            return diagnostics;
    12161278        } else {
     
    12211283
    12221284    private static SRUDiagnostic parseDiagnostic(SRUXMLStreamReader reader,
    1223             boolean required, boolean strictMode) throws XMLStreamException,
     1285            SRUNamespaces ns, boolean required, boolean strictMode) throws XMLStreamException,
    12241286            SRUClientException {
    1225         if (reader.readStart(SRU_DIAGNOSIC_NS, "diagnostic", required)) {
     1287        if (reader.readStart(ns.diagnosticNS(), "diagnostic", required)) {
    12261288
    12271289            // diagnostic/uri
    1228             String uri = reader.readContent(SRU_DIAGNOSIC_NS, "uri", true);
     1290            String uri = reader.readContent(ns.diagnosticNS(), "uri", true);
    12291291
    12301292            String details = null;
     
    12321294            if (strictMode) {
    12331295                // diagnostic/details
    1234                 details = reader.readContent(SRU_DIAGNOSIC_NS, "details",
     1296                details = reader.readContent(ns.diagnosticNS(), "details",
    12351297                        false, true);
    12361298
    12371299                // diagnostic/message
    1238                 message = reader.readContent(SRU_DIAGNOSIC_NS, "message",
     1300                message = reader.readContent(ns.diagnosticNS(), "message",
    12391301                        false, true);
    12401302            } else {
     
    12431305                 * appear in any order
    12441306                 */
    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",
    12471309                            false, false);
    1248                     message = reader.readContent(SRU_DIAGNOSIC_NS, "message",
     1310                    message = reader.readContent(ns.diagnosticNS(), "message",
    12491311                            false, false);
    12501312                } else {
    1251                     message = reader.readContent(SRU_DIAGNOSIC_NS, "message",
     1313                    message = reader.readContent(ns.diagnosticNS(), "message",
    12521314                            false, false);
    1253                     details = reader.readContent(SRU_DIAGNOSIC_NS, "details",
     1315                    details = reader.readContent(ns.diagnosticNS(), "details",
    12541316                            false, false);
    12551317                    if ((message != null) && (details != null)) {
     
    12721334            }
    12731335
    1274             reader.readEnd(SRU_DIAGNOSIC_NS, "diagnostic");
     1336            reader.readEnd(ns.diagnosticNS(), "diagnostic");
    12751337
    12761338            logger.debug("diagnostic: uri={}, detail={}, message={}",
     
    12841346
    12851347    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)
    12871377            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;
    13031395        } 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 +
    13071399                    "')");
    13081400        }
     
    13581450    }
    13591451
     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
    13601556} // class SRUSimpleClient
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUVersion.java

    r5750 r6903  
    2929     * SRU/CQL version 1.2
    3030     */
    31     VERSION_1_2
     31    VERSION_1_2,
     32
     33    /**
     34     * SRU/CQL version 2.0
     35     */
     36    VERSION_2_0;
    3237
    3338} // enum SRUVersion
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/fcs/ClarinFCSEndpointDescriptionParser.java

    r5804 r6903  
    105105            throws XMLStreamException, SRUClientException {
    106106        final int version = parseVersion(reader);
    107         if (version != EXPECTED_VERSION) {
     107        // FIXME: actually handle other version
     108        if ((version != EXPECTED_VERSION) && (version != 2)) {
    108109            throw new SRUClientException("Attribute 'version' of " +
    109110                    "element '<EndpointDescription>' must be of value '1'");
     
    201202                    MIMETYPE_HITS_DATAVIEW +
    202203                    "') 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);
    203212        }
    204213
     
    315324            logger.debug("DataViews: {}", dataviews);
    316325
     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
    317336            List<ResourceInfo> subResources = null;
    318337            if (XmlStreamReaderUtils.peekStart(reader,
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestClient.java

    r5804 r6903  
    3535            SRUClient client = new ClarinFCSClientBuilder()
    3636                    .addDefaultDataViewParsers()
     37                    .setDefaultSRUVersion(SRUVersion.VERSION_2_0)
    3738                    .unknownDataViewAsString()
    3839                    .enableLegacySupport()
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestUtils.java

    r5799 r6903  
    6565//        request.setRecordSchema(ClarinFCSRecordData.LEGACY_RECORD_SCHEMA);
    6666        request.setMaximumRecords(5);
    67         request.setRecordPacking(SRURecordPacking.XML);
     67        request.setRecordXmlEscaping(SRURecordXmlEscaping.XML);
     68        request.setRecordPacking(SRURecordPacking.PACKED);
    6869        request.setExtraRequestData("x-indent-response", "4");
    6970        return request;
Note: See TracChangeset for help on using the changeset viewer.