Changeset 7191


Ignore:
Timestamp:
10/11/18 11:42:21 (6 years ago)
Author:
Oliver Schonefeld
Message:
  • add feature to allow for using a customized HTTP client and optionally a HTTP client context
Location:
SRUClient/trunk/src/main/java/eu/clarin/sru/client
Files:
2 edited

Legend:

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

    r6938 r7191  
    2121import java.util.List;
    2222
     23import org.apache.http.client.protocol.HttpClientContext;
     24import org.apache.http.impl.client.CloseableHttpClient;
     25
    2326/**
    2427 * A class for encapsulating the configuration of an SRU client.
     
    3437    private final int connectTimeout;
    3538    private final int socketTimeout;
     39    private final CloseableHttpClient httpClient;
     40    private final HttpClientContext httpContext;
    3641    private final int threadCount;
    3742    private final List<SRURecordDataParser> recordParsers;
     
    5055
    5156    /**
    52      * Get the connect timeout.
     57     * Get the connect timeout. This value is ignored if a customized HTTP
     58     * client is provided.
    5359     *
    5460     * @return the connect timeout
     
    6066
    6167    /**
    62      * Get the socket timeout.
     68     * Get the socket timeout. This value is ignored if a customized HTTP client
     69     * is provided.
    6370     *
    6471     * @return the connect timeout
     
    6673    public int getSocketTimeout() {
    6774        return socketTimeout;
     75    }
     76
     77
     78    /**
     79     * Get the customized HTTP client which is to be used.
     80     *
     81     * @return a configured HTTP client instance or <code>null</code>
     82     */
     83    public CloseableHttpClient getCustomizedHttpClient() {
     84        return httpClient;
     85    }
     86
     87
     88    /**
     89     * Get the HTTP client context which is to be used. Only relevant, if a
     90     * customized HTTP client is set, see {{@link #getCustomizedHttpClient()}.
     91     *
     92     * @return a HTTP client context instance or <code>null</code>
     93     */
     94    public HttpClientContext getHttpClientContext() {
     95        return httpContext;
    6896    }
    6997
     
    104132            throw new NullPointerException("builder == null");
    105133        }
    106         this.defaultVersion   = builder.getDefaultVersion();
    107         this.connectTimeout   = builder.getConnectTimeout();
    108         this.socketTimeout    = builder.getSocketTimeout();
    109         this.threadCount      = builder.getThreadCount();
    110         this.recordParsers    = builder.getRecordDataParsers();
    111         this.extraDataParsers = builder.getExtraResponseDataParsers();
     134        this.defaultVersion   = builder.defaultVersion;
     135        this.connectTimeout   = builder.connectTimeout;
     136        this.socketTimeout    = builder.socketTimeout;
     137        if (builder.httpClient != null) {
     138            this.httpClient  = builder.httpClient;
     139            this.httpContext = builder.httpContext;
     140        } else {
     141            this.httpClient  = null;
     142            this.httpContext = null;
     143        }
     144        this.threadCount      = builder.threadCount;
     145        if (builder.recordParsers != null) {
     146            this.recordParsers =
     147                    Collections.unmodifiableList(builder.recordParsers);
     148        } else {
     149            this.recordParsers = null;
     150        }
     151        if (builder.extraDataParsers != null) {
     152            this.extraDataParsers =
     153                    Collections.unmodifiableList(builder.extraDataParsers);
     154        } else {
     155            this.extraDataParsers = null;
     156        }
     157    }
     158
     159
     160    /**
     161     * Get a new builder for creating a new {@link SRUClientConfig} instance.
     162     *
     163     * @return a builder instance
     164     */
     165    public static Builder builder() {
     166        return new Builder();
    112167    }
    113168
     
    121176    public static class Builder {
    122177        private SRUVersion defaultVersion = DEFAULT_SRU_VERSION;
    123         private int connectTimeout        = DEFAULT_CONNECT_TIMEOUT;
    124         private int socketTimeout         = DEFAULT_SOCKET_TIMEOUT;
    125         private int threadCount           =
     178        private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
     179        private int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
     180        private CloseableHttpClient httpClient = null;
     181        private HttpClientContext httpContext = null;
     182        private int threadCount =
    126183                Runtime.getRuntime().availableProcessors() * 2;
    127184        private List<SRURecordDataParser> recordParsers =
     
    139196
    140197        /**
    141          * Get the default SRU version to be used
    142          *
    143          * @return the defaultSRU version to be used
    144          */
    145         public SRUVersion getDefaultVersion() {
    146             return defaultVersion;
    147         }
    148 
    149 
    150         /**
    151198         * Set the default SRU version to be used.
    152199         *
     
    163210            this.defaultVersion = defaultVersion;
    164211            return this;
    165         }
    166 
    167 
    168         /**
    169          * Get the connect timeout.
    170          *
    171          * @return the connect timeout.
    172          */
    173         public int getConnectTimeout() {
    174             return connectTimeout;
    175212        }
    176213
     
    193230            this.connectTimeout = connectTimeout;
    194231            return this;
    195         }
    196 
    197 
    198         /**
    199          * Get the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,
    200          * which is the timeout for waiting for data.
    201          *
    202          * @return the socket timeout in milliseconds
    203          */
    204         public int getSocketTimeout() {
    205             return socketTimeout;
    206232        }
    207233
     
    229255
    230256        /**
    231          * Get the number of worker threads. This value is only relevant for the
    232          * {@link SRUThreadedClient}.
    233          *
    234          * @return the number of worker threads
    235          */
    236         public int getThreadCount() {
    237             return threadCount;
     257         * Set a customized HTTP client which is to be used.
     258         *
     259         * @param httpClient
     260         *            a configured HTTP client instance
     261         * @return this {@link Builder} instance
     262         */
     263        public Builder setCustomizedHttpClient(CloseableHttpClient httpClient) {
     264            this.httpClient = httpClient;
     265            return this;
     266        }
     267
     268
     269        /**
     270         * Optionally set the HTTP context which is to be used by the customized
     271         * HTTP client.
     272         *
     273         * @param httpContext
     274         *            a HTTP context instance
     275         * @return this {@link Builder} instance
     276         */
     277        public Builder setHttpContext(HttpClientContext httpContext) {
     278            this.httpContext = httpContext;
     279            return this;
    238280        }
    239281
     
    253295            this.threadCount = threadCount;
    254296            return this;
    255         }
    256 
    257 
    258         /**
    259          * Get the list of record data parsers.
    260          *
    261          * @return the list of record data parsers
    262          * @see SRURecordDataParser
    263          */
    264         public List<SRURecordDataParser> getRecordDataParsers() {
    265             return Collections.unmodifiableList(recordParsers);
    266         }
    267 
    268 
    269         /**
    270          * Get the list of extra response data parsers.
    271          *
    272          * @return the list of extra response data parsers
    273          * @see SRUExtraResponseDataParser
    274          */
    275         public List<SRUExtraResponseDataParser> getExtraResponseDataParsers() {
    276             if (extraDataParsers != null) {
    277                 return Collections.unmodifiableList(extraDataParsers);
    278             } else {
    279                 return null;
    280             }
    281297        }
    282298
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java

    r7188 r7191  
    4444import org.apache.http.impl.client.HttpClients;
    4545import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
     46import org.apache.http.protocol.HttpContext;
    4647import org.slf4j.Logger;
    4748import org.slf4j.LoggerFactory;
     
    8384    private final Map<String, SRURecordDataParser> parsers;
    8485    private final CloseableHttpClient httpClient;
     86    private final HttpContext httpContext;
    8587    private final XmlStreamReaderProxy proxy = new XmlStreamReaderProxy();
    8688    private final SRUExplainRecordDataParser explainRecordParser =
     
    123125        }
    124126
    125         // create HTTP client
    126         httpClient = createHttpClient(config.getConnectTimeout(),
    127                                       config.getSocketTimeout());
     127        final CloseableHttpClient client = config.getCustomizedHttpClient();
     128        if (client != null) {
     129            // use customized http client
     130            this.httpClient = client;
     131            this.httpContext = config.getHttpClientContext();
     132        } else {
     133            // create HTTP client
     134            httpClient = createHttpClient(config.getConnectTimeout(),
     135                    config.getSocketTimeout());
     136            httpContext = null;
     137        }
    128138    }
    129139
     
    405415            try {
    406416                HttpGet request = new HttpGet(uri);
    407                 response        = httpClient.execute(request);
     417                response = httpClient.execute(request, httpContext);
    408418                StatusLine status = response.getStatusLine();
    409419                if (status.getStatusCode() != HttpStatus.SC_OK) {
     
    14371447                .setSocketTimeout(socketTimeout)
    14381448                .setConnectionRequestTimeout(0) /* infinite */
    1439                 .setStaleConnectionCheckEnabled(false)
    14401449                .build();
    14411450
Note: See TracChangeset for help on using the changeset viewer.