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