source: FCSSimpleClient/trunk/src/main/java/eu/clarin/sru/client/fcs/ClarinFCSClientBuilder.java @ 7274

Last change on this file since 7274 was 7274, checked in by Oliver Schonefeld, 2 years ago
  • update copyright
  • Property svn:eol-style set to native
File size: 11.5 KB
Line 
1/**
2 * This software is copyright (c) 2012-2022 by
3 *  - Leibniz-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 Leibniz-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.fcs;
18
19import java.util.ArrayList;
20import java.util.List;
21
22import eu.clarin.sru.client.SRURequestAuthenticator;
23import eu.clarin.sru.client.SRUClient;
24import eu.clarin.sru.client.SRUClientConfig;
25import eu.clarin.sru.client.SRUExtraResponseDataParser;
26import eu.clarin.sru.client.SRUSimpleClient;
27import eu.clarin.sru.client.SRUThreadedClient;
28import eu.clarin.sru.client.SRUVersion;
29
30
31/**
32 * A class that implements the builder pattern for creating SRU client instances
33 * that are configured to be used for CLARIN-FCS.
34 *
35 */
36public class ClarinFCSClientBuilder {
37    private static final boolean DEFAULT_UNKNOWN_AS_DOM =
38            false;
39    private static final SRUVersion DEFAULT_SRU_VERSION =
40            SRUVersion.VERSION_1_2;
41    private List<DataViewParser> parsers = new ArrayList<DataViewParser>();
42    private List<SRUExtraResponseDataParser> extraDataParsers =
43            new ArrayList<SRUExtraResponseDataParser>();
44    private SRUVersion defaultVersion = DEFAULT_SRU_VERSION;
45    private boolean unknownAsDom = DEFAULT_UNKNOWN_AS_DOM;
46    private boolean legacySupport = false;
47    private boolean fullLegacyCompatMode = false;
48    private int connectTimeout = SRUClientConfig.DEFAULT_CONNECT_TIMEOUT;
49    private int socketTimeout = SRUClientConfig.DEFAULT_SOCKET_TIMEOUT;
50    private SRURequestAuthenticator requestAuthStrategy;
51   
52
53    /**
54     * Constructor.
55     *
56     * @param unknownAsDom
57     *            if <code>true</code> unknown data views are parsed into a DOM
58     *
59     */
60    public ClarinFCSClientBuilder(boolean unknownAsDom) {
61        this.unknownAsDom = unknownAsDom;
62    }
63
64
65    /**
66     * Constructor.
67     */
68    public ClarinFCSClientBuilder() {
69        this(DEFAULT_UNKNOWN_AS_DOM);
70    }
71
72
73    /**
74     * Add the recommended default set of data record view parsers.
75     *
76     * @return this {@link ClarinFCSClientBuilder} instance
77     */
78    public ClarinFCSClientBuilder addDefaultDataViewParsers() {
79        doRegisterDataViewParser(parsers, new DataViewParserHits());
80        doRegisterDataViewParser(parsers, new DataViewParserAdvanced());
81        return this;
82    }
83
84
85    /**
86     * Configure client to parse unknown Data Views into a DOM representation.
87     *
88     * @return this {@link ClarinFCSClientBuilder} instance
89     * @see DataViewParserGenericDOM
90     * @see DataViewGenericDOM
91     */
92    public ClarinFCSClientBuilder unknownDataViewAsDOM() {
93        unknownAsDom = true;
94        return this;
95    }
96
97
98    /**
99     * Configure client to parse unknown Data Views into a String
100     * representation.
101     *
102     * @return this {@link ClarinFCSClientBuilder} instance
103     * @see DataViewParserGenericString
104     * @see DataViewGenericString
105     */
106    public ClarinFCSClientBuilder unknownDataViewAsString() {
107        unknownAsDom = false;
108        return this;
109    }
110
111
112    /**
113     * Set default SRU version to be used.
114     *
115     * @param defaultVersion
116     *            the default SRU version to be used
117     *
118     * @return this {@link ClarinFCSClientBuilder} instance
119     */
120    public ClarinFCSClientBuilder setDefaultSRUVersion(
121            final SRUVersion defaultVersion) {
122        if (defaultVersion == null) {
123            throw new NullPointerException("defaultVersion == null");
124        }
125        this.defaultVersion = defaultVersion;
126        return this;
127    }
128
129
130    /**
131     * Configure client to enable support for legacy CLARIN-FCS endpoints.
132     *
133     * @return this {@link ClarinFCSClientBuilder} instance
134     */
135    public ClarinFCSClientBuilder enableLegacySupport() {
136        legacySupport = true;
137        return this;
138    }
139
140
141    /**
142     * Configure client to disable support for legacy CLARIN-FCS endpoints.
143     *
144     * @return this {@link ClarinFCSClientBuilder} instance
145     */
146    public ClarinFCSClientBuilder disableLegacySupport() {
147        legacySupport = false;
148        return this;
149    }
150
151
152    /**
153     * Configure client to support legacy CLARIN-FCS endpoints in full
154     * compatibility mode. Automatically enables support for legacy CLARIN-FCS.
155     * <br>
156     * NB: This feature should not be used in production!
157     *
158     * @return this {@link ClarinFCSClientBuilder} instance
159     */
160    public ClarinFCSClientBuilder enableFullLegacyCompatMode() {
161        enableLegacySupport();
162        fullLegacyCompatMode = true;
163        return this;
164    }
165
166
167    /**
168     * Configure client to disable support for legacy CLARIN_FCS in full
169     * compatibility mode. <br>
170     * NB: Support for legacy CLARIN-FCS will <em>not</em> be disabled
171     * automatically!
172     *
173     * @return this {@link ClarinFCSClientBuilder} instance
174     */
175    public ClarinFCSClientBuilder disableFullLegacyCompatMode() {
176        fullLegacyCompatMode = false;
177        return this;
178    }
179
180
181    /**
182     * Get the timeout in milliseconds until a connection is established.
183     *
184     * @return this connect timeout in milliseconds
185     */
186    public int getConnectTimeout() {
187        return connectTimeout;
188    }
189
190
191    /**
192     * Set the timeout in milliseconds until a connection is established.
193     * <p>
194     * A timeout value of <code>0</code> is interpreted as an infinite timeout;
195     * <code>-1</code> is interpreted as system default.
196     * </p>
197     *
198     * @param connectTimeout
199     *            the connect timeout in milliseconds
200     * @return this {@link ClarinFCSClientBuilder} instance
201     */
202    public ClarinFCSClientBuilder setConnectTimeout(int connectTimeout) {
203        if (connectTimeout < -1) {
204            throw new IllegalArgumentException("connectTimeout < -1");
205        }
206        this.connectTimeout = connectTimeout;
207        return this;
208    }
209
210
211    /**
212     * Get the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,
213     * which is the timeout for waiting for data.
214     *
215     * @return socketTimeout
216     *            the socket timeout in milliseconds
217     */
218    public int getSocketTimeout() {
219        return socketTimeout;
220    }
221
222
223    /**
224     * Set the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds, which
225     * is the timeout for waiting for data.
226     * <p>
227     * A timeout value of <code>0</code> is interpreted as an infinite timeout;
228     * <code>-1</code> is interpreted as system default.
229     * </p>
230     *
231     * @param socketTimeout
232     *            the socket timeout in milliseconds
233     * @return this {@link ClarinFCSClientBuilder} instance
234     */
235    public ClarinFCSClientBuilder setSocketTimeout(int socketTimeout) {
236        if (socketTimeout < -1) {
237            throw new IllegalArgumentException("socketTimeout < -1");
238        }
239        this.socketTimeout = socketTimeout;
240        return this;
241    }
242
243
244    public ClarinFCSClientBuilder setRequestAuthenticator(
245            SRURequestAuthenticator requestAuthStrategy) {
246        this.requestAuthStrategy = requestAuthStrategy;
247        return this;
248    }
249
250
251    /**
252     * Register a Data View parser.
253     *
254     * @param parser
255     *            the data view parser to be registered
256     * @return this {@link ClarinFCSClientBuilder} instance
257     * @throws IllegalArgumentException
258     *             if an error occurred while registering the data view parser
259     * @see DataViewParser
260     */
261    public ClarinFCSClientBuilder registerDataViewParser(DataViewParser parser) {
262        if (parser == null) {
263            throw new NullPointerException("parser == null");
264        }
265        if ((parser instanceof DataViewParserGenericDOM) ||
266                (parser instanceof DataViewParserGenericString)) {
267            throw new IllegalArgumentException("parsers of type '" +
268                    parser.getClass().getName() +
269                    "' cannot be added manually");
270        }
271
272        if (!doRegisterDataViewParser(parsers, parser)) {
273            throw new IllegalArgumentException("parser of type '" +
274                    parser.getClass().getName() + "' was already registered");
275        }
276        return this;
277    }
278
279
280    /**
281     * Register an extra response data parser.
282     *
283     * @param parser
284     *            the extra response data parser to be registered
285     * @return this {@link ClarinFCSClientBuilder} instance
286     * @throws IllegalArgumentException
287     *             if an error occurred while registering the extra response
288     *             data parser
289     * @see SRUExtraResponseDataParser
290     */
291    public ClarinFCSClientBuilder registerExtraResponseDataParser(
292            SRUExtraResponseDataParser parser) {
293        if (parser == null) {
294            throw new NullPointerException("parser == null");
295        }
296        extraDataParsers.add(parser);
297        return this;
298    }
299
300
301    /**
302     * Create a {@link SRUSimpleClient} instance.
303     *
304     * @return a configured {@link SRUSimpleClient} instance
305     */
306    public SRUSimpleClient buildSimpleClient() {
307        return new SRUSimpleClient(makeClientConfig());
308    }
309
310
311    /**
312     * Create a {@link SRUClient} instance.
313     *
314     * @return a configured {@link SRUClient} instance
315     */
316    public SRUClient buildClient() {
317        return new SRUClient(makeClientConfig());
318    }
319
320
321    /**
322     * Create a {@link SRUThreadedClient} instance.
323     *
324     * @return a configured {@link SRUThreadedClient} instance
325     */
326    public SRUThreadedClient buildThreadedClient() {
327        return new SRUThreadedClient(makeClientConfig());
328    }
329
330
331    @SuppressWarnings("deprecation")
332    private SRUClientConfig makeClientConfig() {
333        final SRUClientConfig.Builder builder = new SRUClientConfig.Builder();
334        builder
335            .setDefaultVersion(defaultVersion)
336            .setConnectTimeout(connectTimeout)
337            .setSocketTimeout(socketTimeout)
338            .setRequestAuthenticator(requestAuthStrategy);
339        final List<DataViewParser> p = finalizeDataViewParsers();
340        builder.addRecordDataParser(new ClarinFCSRecordDataParser(p));
341        if (legacySupport) {
342            builder.addRecordDataParser(new LegacyClarinFCSRecordDataParser(p, fullLegacyCompatMode));
343        }
344        if ((extraDataParsers != null) && !extraDataParsers.isEmpty()) {
345            for (SRUExtraResponseDataParser parser : extraDataParsers) {
346                builder.addExtraResponseDataParser(parser);
347            }
348        }
349        return builder.build();
350    }
351
352
353    @SuppressWarnings("deprecation")
354    private List<DataViewParser> finalizeDataViewParsers() {
355        final List<DataViewParser> result =
356                new ArrayList<DataViewParser>(parsers.size() +
357                        (legacySupport ? 2 : 1));
358        result.addAll(parsers);
359        if (unknownAsDom) {
360            result.add(new DataViewParserGenericDOM());
361        } else {
362            result.add(new DataViewParserGenericString());
363        }
364        if (legacySupport) {
365            result.add(new DataViewParserKWIC(fullLegacyCompatMode));
366        }
367        return result;
368    }
369
370
371    private static boolean doRegisterDataViewParser(
372            List<DataViewParser> parsers, DataViewParser parser) {
373        if (parsers.contains(parser)) {
374            return false;
375        } else {
376            parsers.add(parser);
377            return true;
378        }
379    }
380
381} // class ClarinFCSClientBuilder
Note: See TracBrowser for help on using the repository browser.