Changeset 7266


Ignore:
Timestamp:
01/05/22 22:26:59 (2 years ago)
Author:
Oliver Schonefeld
Message:
  • re-factor to make SRUOperation available
  • add experimental support for processing authenticated requests
Location:
SRUClient/trunk/src/main/java/eu/clarin/sru/client
Files:
2 added
6 edited

Legend:

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

    r6938 r7266  
    8181
    8282
    83     enum SRUOperation {
    84         EXPLAIN, SCAN, SEARCH_RETRIEVE
    85     } // enum SRUOperation
    86 
    87 
    8883    protected static class URIHelper {
    8984        private final URIBuilder uriBuilder;
     
    477472     * @return a operation constant for this request
    478473     */
    479     abstract SRUOperation getOperation();
     474    public abstract SRUOperation getOperation();
    480475
    481476
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClientConfig.java

    r7194 r7266  
    3939    private final CloseableHttpClient httpClient;
    4040    private final HttpClientContext httpContext;
     41    private final SRURequestAuthenticator requestAuthenticator;
    4142    private final int threadCount;
    4243    private final List<SRURecordDataParser> recordParsers;
     
    8384    public CloseableHttpClient getCustomizedHttpClient() {
    8485        return httpClient;
     86    }
     87
     88
     89    /**
     90     * Get the request authenticator
     91     *
     92     * @return the configured request authenticator or <code>null</code>
     93     */
     94    public SRURequestAuthenticator getRequestAuthenticator() {
     95        return requestAuthenticator;
    8596    }
    8697
     
    142153            this.httpContext = null;
    143154        }
     155        this.requestAuthenticator = builder.requestAuthenticator;
    144156        this.threadCount      = builder.threadCount;
    145157        if (builder.recordParsers != null) {
     
    170182        private CloseableHttpClient httpClient = null;
    171183        private HttpClientContext httpContext = null;
     184        private SRURequestAuthenticator requestAuthenticator;
    172185        private int threadCount =
    173186                Runtime.getRuntime().availableProcessors() * 2;
     
    271284
    272285
     286        public Builder setRequestAuthenticator(
     287                SRURequestAuthenticator requestAuthenticator) {
     288            this.requestAuthenticator = requestAuthenticator;
     289            return this;
     290        }
     291
     292
    273293        /**
    274294         * Set the number of worker threads. This value is only relevant for the
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRequest.java

    r6938 r7266  
    105105
    106106    @Override
    107     SRUOperation getOperation() {
     107    public SRUOperation getOperation() {
    108108        return SRUOperation.EXPLAIN;
    109109    }
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUScanRequest.java

    r6938 r7266  
    152152
    153153    @Override
    154     SRUOperation getOperation() {
     154    public SRUOperation getOperation() {
    155155        return SRUOperation.SCAN;
    156156    }
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSearchRetrieveRequest.java

    r6938 r7266  
    313313
    314314    @Override
    315     SRUOperation getOperation() {
     315    public SRUOperation getOperation() {
    316316        return SRUOperation.SEARCH_RETRIEVE;
    317317    }
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java

    r7191 r7266  
    8585    private final CloseableHttpClient httpClient;
    8686    private final HttpContext httpContext;
     87    private final SRURequestAuthenticator requestAuthenticator;
    8788    private final XmlStreamReaderProxy proxy = new XmlStreamReaderProxy();
    8889    private final SRUExplainRecordDataParser explainRecordParser =
     
    124125            }
    125126        }
    126 
    127127        final CloseableHttpClient client = config.getCustomizedHttpClient();
    128128        if (client != null) {
     
    136136            httpContext = null;
    137137        }
     138        this.requestAuthenticator = config.getRequestAuthenticator();
    138139    }
    139140
     
    168169        // create URI and perform request
    169170        final URI uri = request.makeURI(defaultVersion);
    170         CloseableHttpResponse response = executeRequest(uri);
     171        CloseableHttpResponse response = executeRequest(uri, request);
    171172        InputStream stream             = null;
    172173        SRUXMLStreamReader reader      = null;
     
    258259        // create URI and perform request
    259260        final URI uri = request.makeURI(defaultVersion);
    260         CloseableHttpResponse response = executeRequest(uri);
     261        CloseableHttpResponse response = executeRequest(uri, request);
    261262        InputStream stream             = null;
    262263        SRUXMLStreamReader reader      = null;
     
    347348        // create URI and perform request
    348349        final URI uri = request.makeURI(defaultVersion);
    349         CloseableHttpResponse response = executeRequest(uri);
     350        CloseableHttpResponse response = executeRequest(uri, request);
    350351        InputStream stream             = null;
    351352        SRUXMLStreamReader reader      = null;
     
    407408
    408409
    409     private CloseableHttpResponse executeRequest(URI uri)
    410             throws SRUClientException {
     410    private CloseableHttpResponse executeRequest(URI requestUri,
     411            SRUAbstractRequest sruRequest) throws SRUClientException {
    411412        CloseableHttpResponse response = null;
    412413        boolean forceClose             = true;
    413414        try {
    414             logger.debug("submitting HTTP request: {}", uri.toString());
    415415            try {
    416                 HttpGet request = new HttpGet(uri);
     416                HttpGet request = new HttpGet(requestUri);
     417                if (requestAuthenticator != null) {
     418                    String value = requestAuthenticator.createAuthenticationHeaderValue(
     419                            sruRequest.getOperation(),
     420                            sruRequest.getBaseURI().toString());
     421                    if (value != null) {
     422                        value = value.trim();
     423                        if (!value.isEmpty()) {
     424                            logger.debug("adding HTTP authentication header with value: {}", value);
     425                            request.addHeader("Authentication", value);
     426                        } else {
     427                            logger.error("request authenticator returned an empty header value!");
     428                        }
     429                    }
     430                }
     431                logger.debug("submitting HTTP request: {}", requestUri.toString());
    417432                response = httpClient.execute(request, httpContext);
    418433                StatusLine status = response.getStatusLine();
    419434                if (status.getStatusCode() != HttpStatus.SC_OK) {
    420435                    if (status.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
    421                         throw new SRUClientException("not found: " + uri);
     436                        throw new SRUClientException("not found: " + requestUri);
    422437                    } else {
    423438                        throw new SRUClientException("unexpected status: " +
     
    431446            } catch (UnknownHostException e) {
    432447                throw new SRUClientException(
    433                         "unknown host: " + uri.getHost(), e);
     448                        "unknown host: " + requestUri.getHost(), e);
    434449            } catch (IOException e) {
    435450                String msg = null;
     
    12791294                    (diagnostics == null), strictMode)) != null) {
    12801295                if (diagnostics == null) {
    1281                     diagnostics = new ArrayList<SRUDiagnostic>();
     1296                    diagnostics = new ArrayList<>();
    12821297                }
    12831298                diagnostics.add(diagnostic);
Note: See TracChangeset for help on using the changeset viewer.