Ignore:
Timestamp:
07/10/12 16:29:35 (12 years ago)
Author:
oschonef
Message:
  • be more robust, when parsing responses
  • add mechanism to send invalid requests (NB: use to provoke errors when testing endpoints for protocol conformance)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUAbstractRequest.java

    r1971 r2024  
    2929    private static final String VERSION_1_2               = "1.2";
    3030    private static final String PARAM_EXTENSION_PREFIX    = "x-";
     31    /** for end-point conformance testing only. never use in production */
     32    public static final String X_MALFORMED_VERSION       =
     33            "x-malformed-version";
     34    /** for end-point conformance testing only. never use in production */
     35    public static final String X_MALFORMED_OPERATION     =
     36            "x-maformed-operation";
     37    /** for end-point conformance testing only. never use in production */
     38    public static final String MALFORMED_OMIT            = "omit";
     39
    3140    protected enum SRUOperation {
    3241        EXPLAIN, SCAN, SEARCH_RETRIEVE
    33     }
     42    } // enum SRUOperation
     43
     44    protected class URIBuilder {
     45        private final StringBuilder sb;
     46        private boolean firstParam = true;
     47
     48        private URIBuilder(String endpointURI) {
     49            this.sb = new StringBuilder(endpointURI);
     50        }
     51
     52
     53        public URIBuilder append(String name, String value) {
     54            if (name == null) {
     55                throw new NullPointerException("name == null");
     56            }
     57            if (name.isEmpty()) {
     58                throw new IllegalArgumentException("name is empty");
     59            }
     60            if (value == null) {
     61                throw new NullPointerException("vale == null");
     62            }
     63            if (value.isEmpty()) {
     64                throw new IllegalArgumentException("value is empty");
     65            }
     66
     67            if (firstParam) {
     68                sb.append('?');
     69                firstParam = false;
     70            } else {
     71                sb.append('&');
     72            }
     73            sb.append(name).append('=').append(value);
     74            return this;
     75        }
     76
     77
     78        public URIBuilder append(String name, int value) {
     79            return append(name, Integer.toString(value));
     80        }
     81
     82
     83        private URI makeURI() {
     84            return URI.create(sb.toString());
     85        }
     86    } // class
    3487    protected final String endpointURI;
    3588    protected SRUVersion version;
    3689    protected Map<String, String> extraRequestData;
     90    private SRUVersion versionPreformed;
    3791
    3892
     
    57111    public SRUVersion getVersion() {
    58112        return version;
     113    }
     114
     115
     116    public SRUVersion getVersionPerformed() {
     117        return versionPreformed;
    59118    }
    60119
     
    106165            throw new NullPointerException("defaultVersion == null");
    107166        }
    108         StringBuilder uri = new StringBuilder(endpointURI);
    109 
    110         // operation
    111         uri.append('?').append(PARAM_OPERATION).append('=');
    112         switch (getOperation()) {
    113         case EXPLAIN:
    114             uri.append(OP_EXPLAIN);
    115             break;
    116         case SCAN:
    117             uri.append(OP_SCAN);
    118             break;
    119         case SEARCH_RETRIEVE:
    120             uri.append(OP_SEARCH_RETRIEVE);
    121             break;
    122         default:
    123             throw new SRUClientException(
    124                     "unsupported operation: " + getOperation());
    125         } // switch
    126 
    127         // version
    128         SRUVersion v = (version != null) ? version : defaultVersion;
    129         uri.append('&').append(PARAM_VERSION).append('=');
    130         switch (v) {
    131         case VERSION_1_1:
    132             uri.append(VERSION_1_1);
    133             break;
    134         case VERSION_1_2:
    135             uri.append(VERSION_1_2);
    136             break;
    137         default:
    138             throw new SRUClientException("unsupported version: " + v);
    139         } // switch
     167        URIBuilder uriBuilder = new URIBuilder(endpointURI);
     168
     169        /*
     170         * append operation parameter
     171         *
     172         * NB: Setting "x-operation-version" as an extra request parameter makes
     173         * the client to send invalid requests. This is intended to use for
     174         * testing endpoints for protocol conformance (i.e. provoke an error)
     175         * and SHOULD NEVER be used in production!
     176         */
     177        final String malformedOperation =
     178                getExtraRequestData(X_MALFORMED_OPERATION);
     179        if (malformedOperation == null) {
     180            switch (getOperation()) {
     181            case EXPLAIN:
     182                uriBuilder.append(PARAM_OPERATION, OP_EXPLAIN);
     183                break;
     184            case SCAN:
     185                uriBuilder.append(PARAM_OPERATION, OP_SCAN);
     186                break;
     187            case SEARCH_RETRIEVE:
     188                uriBuilder.append(PARAM_OPERATION, OP_SEARCH_RETRIEVE);
     189                break;
     190            default:
     191                throw new SRUClientException(
     192                        "unsupported operation: " + getOperation());
     193            } // switch
     194        } else {
     195            if (!malformedOperation.equals(MALFORMED_OMIT)) {
     196                uriBuilder.append(PARAM_OPERATION, malformedOperation);
     197            }
     198        }
     199
     200        /*
     201         * append version parameter
     202         *
     203         * NB: Setting "x-malformed-version" as an extra request parameter makes
     204         * the client to send invalid requests. This is intended to use for
     205         * testing endpoints for protocol conformance (i.e. provoke an error)
     206         * and SHOULD NEVER be used in production!
     207         */
     208        final String malformedVersion =
     209                getExtraRequestData(X_MALFORMED_VERSION);
     210        if (malformedVersion == null) {
     211            versionPreformed = (version != null) ? version : defaultVersion;
     212            switch (versionPreformed) {
     213            case VERSION_1_1:
     214                uriBuilder.append(PARAM_VERSION, VERSION_1_1);
     215                break;
     216            case VERSION_1_2:
     217                uriBuilder.append(PARAM_VERSION, VERSION_1_2);
     218                break;
     219            default:
     220                throw new SRUClientException("unsupported version: " +
     221                        versionPreformed);
     222            } // switch
     223        } else {
     224            if (!malformedVersion.equalsIgnoreCase(MALFORMED_OMIT)) {
     225                uriBuilder.append(PARAM_VERSION, malformedVersion);
     226            }
     227        }
    140228
    141229        // request specific parameters
    142         addParametersToURI(uri);
     230        addParametersToURI(uriBuilder);
    143231
    144232        // extraRequestData
     
    146234            for (Map.Entry<String, String> entry :
    147235                extraRequestData.entrySet()) {
    148                 uri.append('&').append(entry.getKey()).append('=')
    149                         .append(entry.getValue());
    150             }
    151         }
    152         return URI.create(uri.toString());
     236                String key = entry.getKey();
     237                if (key.equals(X_MALFORMED_OPERATION) ||
     238                        key.equals(X_MALFORMED_VERSION)) {
     239                    continue;
     240                }
     241                uriBuilder.append(key, entry.getValue());
     242            }
     243        }
     244        return uriBuilder.makeURI();
    153245    }
    154246
     
    157249
    158250
    159     protected abstract void addParametersToURI(StringBuilder uri)
     251    protected abstract void addParametersToURI(URIBuilder uri)
    160252            throws SRUClientException;
    161253
Note: See TracChangeset for help on using the changeset viewer.