source: SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClientConfig.java @ 7266

Last change on this file since 7266 was 7266, checked in by Oliver Schonefeld, 2 years ago
  • re-factor to make SRUOperation available
  • add experimental support for processing authenticated requests
  • Property svn:eol-style set to native
File size: 12.0 KB
Line 
1/**
2 * This software is copyright (c) 2012-2016 by
3 *  - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
4 * This is free software. You can redistribute it
5 * and/or modify it under the terms described in
6 * the GNU General Public License v3 of which you
7 * should have received a copy. Otherwise you can download
8 * it from
9 *
10 *   http://www.gnu.org/licenses/gpl-3.0.txt
11 *
12 * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
13 *
14 * @license http://www.gnu.org/licenses/gpl-3.0.txt
15 *  GNU General Public License v3
16 */
17package eu.clarin.sru.client;
18
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.List;
22
23import org.apache.http.client.protocol.HttpClientContext;
24import org.apache.http.impl.client.CloseableHttpClient;
25
26/**
27 * A class for encapsulating the configuration of an SRU client.
28 */
29public class SRUClientConfig {
30    /** default version the client will use, if not otherwise specified */
31    public static final SRUVersion DEFAULT_SRU_VERSION = SRUVersion.VERSION_1_2;
32    /** default connect timeout to be used, if not otherwise specified */
33    public static final int DEFAULT_CONNECT_TIMEOUT = -1;
34    /** default socket timeout to be used, if not otherwise specified */
35    public static final int DEFAULT_SOCKET_TIMEOUT  = -1;
36    private final SRUVersion defaultVersion;
37    private final int connectTimeout;
38    private final int socketTimeout;
39    private final CloseableHttpClient httpClient;
40    private final HttpClientContext httpContext;
41    private final SRURequestAuthenticator requestAuthenticator;
42    private final int threadCount;
43    private final List<SRURecordDataParser> recordParsers;
44    private final List<SRUExtraResponseDataParser> extraDataParsers;
45
46
47    /**
48     * Get default SRU version to be used.
49     *
50     * @return the default SRU version to be used.
51     */
52    public SRUVersion getDefaultVersion() {
53        return defaultVersion;
54    }
55
56
57    /**
58     * Get the connect timeout. This value is ignored if a customized HTTP
59     * client is provided.
60     *
61     * @return the connect timeout
62     */
63    public int getConnectTimeout() {
64        return connectTimeout;
65    }
66
67
68    /**
69     * Get the socket timeout. This value is ignored if a customized HTTP client
70     * is provided.
71     *
72     * @return the connect timeout
73     */
74    public int getSocketTimeout() {
75        return socketTimeout;
76    }
77
78
79    /**
80     * Get the customized HTTP client which is to be used.
81     *
82     * @return a configured HTTP client instance or <code>null</code>
83     */
84    public CloseableHttpClient getCustomizedHttpClient() {
85        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;
96    }
97
98
99    /**
100     * Get the HTTP client context which is to be used. Only relevant, if a
101     * customized HTTP client is set, see {{@link #getCustomizedHttpClient()}.
102     *
103     * @return a HTTP client context instance or <code>null</code>
104     */
105    public HttpClientContext getHttpClientContext() {
106        return httpContext;
107    }
108
109
110    /**
111     * Get the number of worker threads. This value is only relevant for the
112     * {@link SRUThreadedClient}.
113     *
114     * @return the number of worker threads
115     */
116    public int getThreadCount() {
117        return threadCount;
118    }
119
120
121    /**
122     * Get the list of record data parsers to be used.
123     *
124     * @return the list of record data parsers.
125     */
126    public List<SRURecordDataParser> getRecordDataParsers() {
127        return recordParsers;
128    }
129
130
131    /**
132     * Get the list of extra response data parsers to be used.
133     *
134     * @return the list of extra response data parsers.
135     */
136    public List<SRUExtraResponseDataParser> getExtraResponseDataParsers() {
137        return extraDataParsers;
138    }
139
140
141    private SRUClientConfig(Builder builder) {
142        if (builder == null) {
143            throw new NullPointerException("builder == null");
144        }
145        this.defaultVersion   = builder.defaultVersion;
146        this.connectTimeout   = builder.connectTimeout;
147        this.socketTimeout    = builder.socketTimeout;
148        if (builder.httpClient != null) {
149            this.httpClient  = builder.httpClient;
150            this.httpContext = builder.httpContext;
151        } else {
152            this.httpClient  = null;
153            this.httpContext = null;
154        }
155        this.requestAuthenticator = builder.requestAuthenticator;
156        this.threadCount      = builder.threadCount;
157        if (builder.recordParsers != null) {
158            this.recordParsers =
159                    Collections.unmodifiableList(builder.recordParsers);
160        } else {
161            this.recordParsers = null;
162        }
163        if (builder.extraDataParsers != null) {
164            this.extraDataParsers =
165                    Collections.unmodifiableList(builder.extraDataParsers);
166        } else {
167            this.extraDataParsers = null;
168        }
169    }
170
171
172    /**
173     * A class that implements the builder pattern to create
174     * {@link SRUClientConfig} instances.
175     *
176     * @see SRUClientConfig
177     */
178    public static class Builder {
179        private SRUVersion defaultVersion = DEFAULT_SRU_VERSION;
180        private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
181        private int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
182        private CloseableHttpClient httpClient = null;
183        private HttpClientContext httpContext = null;
184        private SRURequestAuthenticator requestAuthenticator;
185        private int threadCount =
186                Runtime.getRuntime().availableProcessors() * 2;
187        private List<SRURecordDataParser> recordParsers =
188                new ArrayList<SRURecordDataParser>();
189        private List<SRUExtraResponseDataParser> extraDataParsers = null;
190
191
192        /**
193         * Constructor.
194         *
195         */
196        public Builder() {
197        }
198
199
200        /**
201         * Set the default SRU version to be used.
202         *
203         * @param defaultVersion
204         *            the default SRU version to be used
205         * @return this {@link Builder} instance
206         * @throws NullPointerException
207         *             if a mandatory argument is <code>null</code>
208         */
209        public Builder setDefaultVersion(SRUVersion defaultVersion) {
210            if (defaultVersion == null) {
211                throw new NullPointerException("defaultVersion == null");
212            }
213            this.defaultVersion = defaultVersion;
214            return this;
215        }
216
217
218        /**
219         * Set the timeout in milliseconds until a connection is established.
220         * <p>
221         * A timeout value of <code>0</code> is interpreted as an infinite
222         * timeout; <code>-1</code> is interpreted as system default.
223         * </p>
224         *
225         * @param connectTimeout
226         *            the timeout in milliseconds
227         * @return this {@link Builder} instance
228         */
229        public Builder setConnectTimeout(int connectTimeout) {
230            if (connectTimeout < -1) {
231                throw new IllegalArgumentException("connectTimeout < -1");
232            }
233            this.connectTimeout = connectTimeout;
234            return this;
235        }
236
237
238        /**
239         * Set the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,
240         * which is the timeout for waiting for data.
241         * <p>
242         * A timeout value of <code>0</code> is interpreted as an infinite
243         * timeout; <code>-1</code> is interpreted as system default.
244         * </p>
245         *
246         * @param socketTimeout
247         *            the socket timeout in milliseconds
248         * @return this {@link Builder} instance
249         */
250        public Builder setSocketTimeout(int socketTimeout) {
251            if (socketTimeout < -1) {
252                throw new IllegalArgumentException("socketTimeout < -1");
253            }
254            this.socketTimeout = socketTimeout;
255            return this;
256        }
257
258
259        /**
260         * Set a customized HTTP client which is to be used.
261         *
262         * @param httpClient
263         *            a configured HTTP client instance
264         * @return this {@link Builder} instance
265         */
266        public Builder setCustomizedHttpClient(CloseableHttpClient httpClient) {
267            this.httpClient = httpClient;
268            return this;
269        }
270
271
272        /**
273         * Optionally set the HTTP context which is to be used by the customized
274         * HTTP client.
275         *
276         * @param httpContext
277         *            a HTTP context instance
278         * @return this {@link Builder} instance
279         */
280        public Builder setHttpContext(HttpClientContext httpContext) {
281            this.httpContext = httpContext;
282            return this;
283        }
284
285
286        public Builder setRequestAuthenticator(
287                SRURequestAuthenticator requestAuthenticator) {
288            this.requestAuthenticator = requestAuthenticator;
289            return this;
290        }
291
292
293        /**
294         * Set the number of worker threads. This value is only relevant for the
295         * {@link SRUThreadedClient}.
296         *
297         * @param threadCount
298         *            the number of worker threads
299         * @return this {@link Builder} instance
300         */
301        public Builder setThreadCount(int threadCount) {
302            if (threadCount < 1) {
303                throw new IllegalArgumentException("threadCount < 1");
304            }
305            this.threadCount = threadCount;
306            return this;
307        }
308
309
310        /**
311         * Add a record data parser instance to the list of record data parsers
312         *
313         * @param parser
314         *            the record data parser to be added
315         * @return this {@link Builder} instance
316         * @throws IllegalArgumentException
317         *             if registering of the parser fails
318         * @see SRURecordDataParser
319         */
320        public Builder addRecordDataParser(SRURecordDataParser parser) {
321            if (parser == null) {
322                throw new NullPointerException("parser == null");
323            }
324            final String recordSchema = parser.getRecordSchema();
325            if (recordSchema == null) {
326                throw new NullPointerException(
327                        "parser.getRecordSchema() == null");
328            }
329            if (recordSchema.isEmpty()) {
330                throw new IllegalArgumentException(
331                        "parser.getRecordSchema() returns empty string");
332            }
333
334            for (SRURecordDataParser p : recordParsers) {
335                if (p.getRecordSchema().equals(recordSchema)) {
336                    throw new IllegalArgumentException(
337                            "record data parser already registered: " +
338                                    recordSchema);
339                }
340            }
341            recordParsers.add(parser);
342            return this;
343        }
344
345
346        /**
347         * Add an extra response data parser instance to the list of extra
348         * response data parsers
349         *
350         * @param parser
351         *            the extra response data parser to be added
352         * @return this {@link Builder} instance
353         * @throws IllegalArgumentException
354         *             if registering of the parser fails
355         * @see SRUExtraResponseDataParser
356         */
357        public Builder addExtraResponseDataParser(SRUExtraResponseDataParser parser) {
358            if (parser == null) {
359                throw new NullPointerException("parser == null");
360            }
361            if (extraDataParsers == null) {
362                extraDataParsers = new ArrayList<SRUExtraResponseDataParser>();
363            }
364            extraDataParsers.add(parser);
365            return this;
366        }
367
368
369        /**
370         * Create a configuration instance object for configuring SRU clients
371         *
372         * @return a immutable configuration instance
373         */
374        public SRUClientConfig build() {
375            return new SRUClientConfig(this);
376        }
377    } // inner class Builder
378
379} // class SRUClientConfig
Note: See TracBrowser for help on using the repository browser.