Changeset 2880 for SRUClient


Ignore:
Timestamp:
05/07/13 20:22:18 (11 years ago)
Author:
oschonef
Message:
  • ensure that generated URIs are correctly UTF-8 encoded and URI-escaped
Location:
SRUClient/trunk/src/main/java/eu/clarin/sru/client
Files:
4 edited

Legend:

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

    r2466 r2880  
    1818
    1919import java.net.URI;
     20import java.net.URISyntaxException;
    2021import java.util.HashMap;
    2122import java.util.Map;
    2223
     24import org.apache.http.client.utils.URIBuilder;
     25
    2326
    2427
    2528/**
    2629 * Abstract base class for SRU requests.
    27  * 
     30 *
    2831 * @see SRUExplainResponse
    2932 * @see SRUScanResponse
     
    6972
    7073
    71     class URIBuilder {
    72         private final StringBuilder sb;
    73         private boolean firstParam = true;
    74 
    75         private URIBuilder(String endpointURI) {
    76             this.sb = new StringBuilder(endpointURI);
    77         }
    78 
    79 
    80         public URIBuilder append(String name, String value) {
     74    class URIHelper {
     75        private final URIBuilder uriBuilder;
     76
     77
     78        private URIHelper(URIBuilder builder) {
     79            this.uriBuilder = builder;
     80        }
     81
     82
     83        public URIHelper append(String name, String value) {
    8184            if (name == null) {
    8285                throw new NullPointerException("name == null");
     
    9295            }
    9396
    94             if (firstParam) {
    95                 sb.append('?');
    96                 firstParam = false;
    97             } else {
    98                 sb.append('&');
    99             }
    100             sb.append(name).append('=').append(value);
     97            uriBuilder.addParameter(name, value);
    10198            return this;
    10299        }
    103100
    104101
    105         public URIBuilder append(String name, int value) {
     102        public URIHelper append(String name, int value) {
    106103            return append(name, Integer.toString(value));
    107104        }
    108105
    109106
    110         private URI makeURI() {
    111             return URI.create(sb.toString());
    112         }
    113     } // class
     107        private URI makeURI() throws URISyntaxException {
     108            return uriBuilder.build();
     109        }
     110    } // class URIHelper
    114111    /** The URL of the endpoint. */
    115112    protected final String endpointURI;
     
    248245            throw new NullPointerException("defaultVersion == null");
    249246        }
    250         URIBuilder uriBuilder = new URIBuilder(endpointURI);
    251 
    252         /*
    253          * append operation parameter
    254          *
    255          * NB: Setting "x-malformed-operation" as an extra request parameter
    256          * makes the client to send invalid requests. This is intended to use
    257          * for testing endpoints for protocol conformance (i.e. provoke an
    258          * error) and SHOULD NEVER be used in production!
    259          */
    260         final String malformedOperation =
    261                 getExtraRequestData(X_MALFORMED_OPERATION);
    262         if (malformedOperation == null) {
    263             switch (getOperation()) {
    264             case EXPLAIN:
    265                 uriBuilder.append(PARAM_OPERATION, OP_EXPLAIN);
    266                 break;
    267             case SCAN:
    268                 uriBuilder.append(PARAM_OPERATION, OP_SCAN);
    269                 break;
    270             case SEARCH_RETRIEVE:
    271                 uriBuilder.append(PARAM_OPERATION, OP_SEARCH_RETRIEVE);
    272                 break;
    273             default:
    274                 throw new SRUClientException(
    275                         "unsupported operation: " + getOperation());
    276             } // switch
    277         } else {
    278             if (!malformedOperation.equals(MALFORMED_OMIT)) {
    279                 uriBuilder.append(PARAM_OPERATION, malformedOperation);
    280             }
    281         }
    282 
    283         /*
    284          * append version parameter
    285          *
    286          * NB: Setting "x-malformed-version" as an extra request parameter makes
    287          * the client to send invalid requests. This is intended to use for
    288          * testing endpoints for protocol conformance (i.e. provoke an error)
    289          * and SHOULD NEVER be used in production!
    290          */
    291         final String malformedVersion =
    292                 getExtraRequestData(X_MALFORMED_VERSION);
    293         if (malformedVersion == null) {
    294             versionPreformed = (version != null) ? version : defaultVersion;
    295             switch (versionPreformed) {
    296             case VERSION_1_1:
    297                 uriBuilder.append(PARAM_VERSION, VERSION_1_1);
    298                 break;
    299             case VERSION_1_2:
    300                 uriBuilder.append(PARAM_VERSION, VERSION_1_2);
    301                 break;
    302             default:
    303                 throw new SRUClientException("unsupported version: " +
    304                         versionPreformed);
    305             } // switch
    306         } else {
    307             if (!malformedVersion.equalsIgnoreCase(MALFORMED_OMIT)) {
    308                 uriBuilder.append(PARAM_VERSION, malformedVersion);
    309             }
    310         }
    311 
    312         // request specific parameters
    313         addParametersToURI(uriBuilder);
    314 
    315         // extraRequestData
    316         if ((extraRequestData != null) && !extraRequestData.isEmpty()) {
    317             for (Map.Entry<String, String> entry :
    318                 extraRequestData.entrySet()) {
    319                 String key = entry.getKey();
    320                 if (key.startsWith(MALFORMED_KEY_PREFIX)) {
    321                     continue;
     247
     248        try {
     249            final URIHelper uriBuilder =
     250                    new URIHelper(new URIBuilder(endpointURI));
     251
     252            /*
     253             * append operation parameter
     254             *
     255             * NB: Setting "x-malformed-operation" as an extra request parameter
     256             * makes the client to send invalid requests. This is intended to
     257             * use for testing endpoints for protocol conformance (i.e. provoke
     258             * an error) and SHOULD NEVER be used in production!
     259             */
     260            final String malformedOperation =
     261                    getExtraRequestData(X_MALFORMED_OPERATION);
     262            if (malformedOperation == null) {
     263                switch (getOperation()) {
     264                case EXPLAIN:
     265                    uriBuilder.append(PARAM_OPERATION, OP_EXPLAIN);
     266                    break;
     267                case SCAN:
     268                    uriBuilder.append(PARAM_OPERATION, OP_SCAN);
     269                    break;
     270                case SEARCH_RETRIEVE:
     271                    uriBuilder.append(PARAM_OPERATION, OP_SEARCH_RETRIEVE);
     272                    break;
     273                default:
     274                    throw new SRUClientException("unsupported operation: " +
     275                            getOperation());
     276                } // switch
     277            } else {
     278                if (!malformedOperation.equals(MALFORMED_OMIT)) {
     279                    uriBuilder.append(PARAM_OPERATION, malformedOperation);
    322280                }
    323                 uriBuilder.append(key, entry.getValue());
    324             }
    325         }
    326         return uriBuilder.makeURI();
     281            }
     282
     283            /*
     284             * append version parameter
     285             *
     286             * NB: Setting "x-malformed-version" as an extra request parameter
     287             * makes the client to send invalid requests. This is intended to
     288             * use for testing endpoints for protocol conformance (i.e. provoke
     289             * an error) and SHOULD NEVER be used in production!
     290             */
     291            final String malformedVersion =
     292                    getExtraRequestData(X_MALFORMED_VERSION);
     293            if (malformedVersion == null) {
     294                versionPreformed = (version != null) ? version : defaultVersion;
     295                switch (versionPreformed) {
     296                case VERSION_1_1:
     297                    uriBuilder.append(PARAM_VERSION, VERSION_1_1);
     298                    break;
     299                case VERSION_1_2:
     300                    uriBuilder.append(PARAM_VERSION, VERSION_1_2);
     301                    break;
     302                default:
     303                    throw new SRUClientException("unsupported version: " +
     304                            versionPreformed);
     305                } // switch
     306            } else {
     307                if (!malformedVersion.equalsIgnoreCase(MALFORMED_OMIT)) {
     308                    uriBuilder.append(PARAM_VERSION, malformedVersion);
     309                }
     310            }
     311
     312            // request specific parameters
     313            addParametersToURI(uriBuilder);
     314
     315            // extraRequestData
     316            if ((extraRequestData != null) && !extraRequestData.isEmpty()) {
     317                for (Map.Entry<String, String> entry :
     318                    extraRequestData.entrySet()) {
     319                    final String key = entry.getKey();
     320
     321                    /*
     322                     * make sure, we skip the client-internal parameters
     323                     * to generate invalid requests ...
     324                     */
     325                    if (!key.startsWith(MALFORMED_KEY_PREFIX)) {
     326                        uriBuilder.append(key, entry.getValue());
     327                    }
     328                }
     329            }
     330
     331            return uriBuilder.makeURI();
     332        } catch (URISyntaxException e) {
     333            throw new SRUClientException("error while building request URI", e);
     334        }
    327335    }
    328336
     
    335343
    336344
    337     abstract void addParametersToURI(URIBuilder uri) throws SRUClientException;
     345    abstract void addParametersToURI(URIHelper uriBuilder)
     346            throws SRUClientException;
    338347
    339348} // class AbstractSRURequest
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRequest.java

    r2466 r2880  
    1919/**
    2020 * An object for performing a <em>explain</em> operation.
    21  * 
     21 *
    2222 * @see SRUExplainHandler
    2323 * @see <a href="http://www.loc.gov/standards/sru/specs/explain.html">SRU Explain Operation</a>
     
    2929    /**
    3030     * Constructor.
    31      * 
     31     *
    3232     * @param baseURI
    3333     *            the baseURI of the endpoint
     
    4040    /**
    4141     * Set the requested record packing.
    42      * 
     42     *
    4343     * @param recordPacking
    4444     *            the requested record packing
     
    5555    /**
    5656     * Get the requested record packing.
    57      * 
     57     *
    5858     * @return the requested record packing
    5959     * @see SRURecordPacking
     
    7171
    7272    @Override
    73     void addParametersToURI(URIBuilder uriBuilder) throws SRUClientException {
     73    void addParametersToURI(URIHelper uriHelper) throws SRUClientException {
    7474        // recordPacking
    7575        if (recordPacking != null) {
    7676            switch (recordPacking) {
    7777            case XML:
    78                 uriBuilder.append(PARAM_RECORD_PACKING, RECORD_PACKING_XML);
     78                uriHelper.append(PARAM_RECORD_PACKING, RECORD_PACKING_XML);
    7979                break;
    8080            case STRING:
    81                 uriBuilder.append(PARAM_RECORD_PACKING, RECORD_PACKING_STRING);
     81                uriHelper.append(PARAM_RECORD_PACKING, RECORD_PACKING_STRING);
    8282                break;
    8383            default:
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUScanRequest.java

    r2466 r2880  
    2323 *   <li><em>scanClause</em></li>
    2424 * </ul>
    25  * 
     25 *
    2626 * @see SRUScanHandler
    2727 * @see <a href="http://www.loc.gov/standards/sru/specs/scan.html">SRU Scan
     
    4545    /**
    4646     * Constructor.
    47      * 
     47     *
    4848     * @param baseURI
    4949     *            the baseURI of the endpoint
     
    5656    /**
    5757     * Get the value of the <em>scanClause</em> argument for this request.
    58      * 
     58     *
    5959     * @return the value for the <em>scanClause</em> argument or
    6060     *         <code>null</code> of none was set
     
    6767    /**
    6868     * Set the value of the <em>scanClause</em> argument for this request.
    69      * 
     69     *
    7070     * @param scanClause
    7171     *            the value for the <em>scanClause</em> argument
     
    8888    /**
    8989     * Get the value of the <em>responsePosition</em> argument for this request.
    90      * 
     90     *
    9191     * @return the value for the <em>responsePosition</em> argument
    9292     */
     
    9898    /**
    9999     * Set the value of the <em>responsePosition</em> argument for this request.
    100      * 
     100     *
    101101     * @param responsePosition
    102102     *            the value for the <em>responsePosition</em> argument
     
    114114    /**
    115115     * Get the value of the <em>maximumTerms</em> argument for this request.
    116      * 
     116     *
    117117     * @return the value for the <em>maximumTerms</em> argument
    118118     */
     
    124124    /**
    125125     * Set the value of the <em>maximumTerms</em> argument for this request.
    126      * 
     126     *
    127127     * @param maximumTerms
    128128     *            the value for the <em>maximumTerms</em> argument
     
    145145
    146146    @Override
    147     void addParametersToURI(URIBuilder uriBuilder) throws SRUClientException {
     147    void addParametersToURI(URIHelper uriHelper) throws SRUClientException {
    148148        // scanClause
    149149        final String malformedScan =
     
    154154                        "mandatory argument 'scanClause' not set or empty");
    155155            }
    156             uriBuilder.append(PARAM_SCAN_CLAUSE, scanClause);
     156            uriHelper.append(PARAM_SCAN_CLAUSE, scanClause);
    157157        } else {
    158158            if (!malformedScan.equalsIgnoreCase(MALFORMED_OMIT)) {
    159                 uriBuilder.append(PARAM_VERSION, malformedScan);
     159                uriHelper.append(PARAM_VERSION, malformedScan);
    160160            }
    161161        }
     
    166166        if (malformedResponsePosition == null) {
    167167            if (responsePosition > -1) {
    168                 uriBuilder.append(PARAM_RESPONSE_POSITION, responsePosition);
     168                uriHelper.append(PARAM_RESPONSE_POSITION, responsePosition);
    169169            }
    170170        } else {
    171171            if (!malformedResponsePosition.equalsIgnoreCase(MALFORMED_OMIT)) {
    172                 uriBuilder.append(PARAM_RESPONSE_POSITION,
     172                uriHelper.append(PARAM_RESPONSE_POSITION,
    173173                        malformedResponsePosition);
    174174            }
     
    180180        if (malformedMaximumTerms == null) {
    181181            if (maximumTerms > -1) {
    182                 uriBuilder.append(PARAM_MAXIMUM_TERMS, maximumTerms);
     182                uriHelper.append(PARAM_MAXIMUM_TERMS, maximumTerms);
    183183            }
    184184        } else {
    185185            if (!malformedMaximumTerms.equalsIgnoreCase(MALFORMED_OMIT)) {
    186                 uriBuilder.append(PARAM_MAXIMUM_TERMS, malformedMaximumTerms);
     186                uriHelper.append(PARAM_MAXIMUM_TERMS, malformedMaximumTerms);
    187187            }
    188188        }
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSearchRetrieveRequest.java

    r2466 r2880  
    232232
    233233    @Override
    234     void addParametersToURI(URIBuilder uriBuilder) throws SRUClientException {
     234    void addParametersToURI(URIHelper uriHelper) throws SRUClientException {
    235235        /*
    236236         * append query argument (mandatory)
     
    247247                        "mandatory argument 'query' not set or empty");
    248248            }
    249             uriBuilder.append(PARAM_QUERY, query);
     249            uriHelper.append(PARAM_QUERY, query);
    250250        } else {
    251251            if (!malformedQuery.equalsIgnoreCase(MALFORMED_OMIT)) {
    252                 uriBuilder.append(PARAM_QUERY, malformedQuery);
     252                uriHelper.append(PARAM_QUERY, malformedQuery);
    253253            }
    254254        }
     
    266266        if (malformedStartRecord == null) {
    267267            if (startRecord > 0) {
    268                 uriBuilder.append(PARAM_START_RECORD, startRecord);
     268                uriHelper.append(PARAM_START_RECORD, startRecord);
    269269            }
    270270        } else {
    271271            if (!malformedStartRecord.equalsIgnoreCase(MALFORMED_OMIT)) {
    272                 uriBuilder.append(PARAM_START_RECORD, malformedStartRecord);
     272                uriHelper.append(PARAM_START_RECORD, malformedStartRecord);
    273273            }
    274274        }
     
    286286        if (malformedMaxiumRecords == null) {
    287287            if (maximumRecords > -1) {
    288                 uriBuilder.append(PARAM_MAXIMUM_RECORDS, maximumRecords);
     288                uriHelper.append(PARAM_MAXIMUM_RECORDS, maximumRecords);
    289289            }
    290290        } else {
    291291            if (!malformedMaxiumRecords.equalsIgnoreCase(MALFORMED_OMIT)) {
    292                 uriBuilder.append(PARAM_MAXIMUM_RECORDS,
     292                uriHelper.append(PARAM_MAXIMUM_RECORDS,
    293293                        malformedMaxiumRecords);
    294294            }
     
    309309                switch (recordPacking) {
    310310                case XML:
    311                     uriBuilder.append(PARAM_RECORD_PACKING, RECORD_PACKING_XML);
     311                    uriHelper.append(PARAM_RECORD_PACKING, RECORD_PACKING_XML);
    312312                    break;
    313313                case STRING:
    314                     uriBuilder.append(PARAM_RECORD_PACKING,
     314                    uriHelper.append(PARAM_RECORD_PACKING,
    315315                            RECORD_PACKING_STRING);
    316316                    break;
     
    322322        } else {
    323323            if (!malformedRecordPacking.equalsIgnoreCase(MALFORMED_OMIT)) {
    324                 uriBuilder.append(PARAM_RECORD_PACKING, malformedRecordPacking);
     324                uriHelper.append(PARAM_RECORD_PACKING, malformedRecordPacking);
    325325            }
    326326        }
     
    330330         */
    331331        if (recordSchema != null) {
    332             uriBuilder.append(PARAM_RECORD_SCHEMA, recordSchema);
     332            uriHelper.append(PARAM_RECORD_SCHEMA, recordSchema);
    333333        }
    334334
     
    337337         */
    338338        if (resultSetTTL > -1) {
    339             uriBuilder.append(PARAM_RESULT_SET_TTL, resultSetTTL);
     339            uriHelper.append(PARAM_RESULT_SET_TTL, resultSetTTL);
    340340        }
    341341    }
Note: See TracChangeset for help on using the changeset viewer.