[5750] | 1 | /** |
---|
[7274] | 2 | * This software is copyright (c) 2012-2022 by |
---|
| 3 | * - Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
[5750] | 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 | * |
---|
[7274] | 12 | * @copyright Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
[5750] | 13 | * |
---|
| 14 | * @license http://www.gnu.org/licenses/gpl-3.0.txt |
---|
| 15 | * GNU General Public License v3 |
---|
| 16 | */ |
---|
[5743] | 17 | package eu.clarin.sru.client.fcs; |
---|
| 18 | |
---|
| 19 | import java.util.ArrayList; |
---|
| 20 | import java.util.List; |
---|
| 21 | |
---|
[7268] | 22 | import eu.clarin.sru.client.SRURequestAuthenticator; |
---|
[5743] | 23 | import eu.clarin.sru.client.SRUClient; |
---|
[5748] | 24 | import eu.clarin.sru.client.SRUClientConfig; |
---|
[5798] | 25 | import eu.clarin.sru.client.SRUExtraResponseDataParser; |
---|
[5743] | 26 | import eu.clarin.sru.client.SRUSimpleClient; |
---|
| 27 | import eu.clarin.sru.client.SRUThreadedClient; |
---|
| 28 | import eu.clarin.sru.client.SRUVersion; |
---|
| 29 | |
---|
| 30 | |
---|
[5748] | 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 | */ |
---|
[5743] | 36 | public 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>(); |
---|
[5798] | 42 | private List<SRUExtraResponseDataParser> extraDataParsers = |
---|
| 43 | new ArrayList<SRUExtraResponseDataParser>(); |
---|
[5748] | 44 | private SRUVersion defaultVersion = DEFAULT_SRU_VERSION; |
---|
[7079] | 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; |
---|
[7268] | 50 | private SRURequestAuthenticator requestAuthStrategy; |
---|
| 51 | |
---|
[5743] | 52 | |
---|
| 53 | /** |
---|
| 54 | * Constructor. |
---|
[6938] | 55 | * |
---|
[6912] | 56 | * @param unknownAsDom |
---|
| 57 | * if <code>true</code> unknown data views are parsed into a DOM |
---|
[6938] | 58 | * |
---|
[5743] | 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 | |
---|
[5748] | 73 | /** |
---|
| 74 | * Add the recommended default set of data record view parsers. |
---|
| 75 | * |
---|
| 76 | * @return this {@link ClarinFCSClientBuilder} instance |
---|
| 77 | */ |
---|
| 78 | public ClarinFCSClientBuilder addDefaultDataViewParsers() { |
---|
[5743] | 79 | doRegisterDataViewParser(parsers, new DataViewParserHits()); |
---|
[6933] | 80 | doRegisterDataViewParser(parsers, new DataViewParserAdvanced()); |
---|
[5743] | 81 | return this; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
[5748] | 85 | /** |
---|
[5749] | 86 | * Configure client to parse unknown Data Views into a DOM representation. |
---|
[5748] | 87 | * |
---|
| 88 | * @return this {@link ClarinFCSClientBuilder} instance |
---|
| 89 | * @see DataViewParserGenericDOM |
---|
[5749] | 90 | * @see DataViewGenericDOM |
---|
[5748] | 91 | */ |
---|
| 92 | public ClarinFCSClientBuilder unknownDataViewAsDOM() { |
---|
[5743] | 93 | unknownAsDom = true; |
---|
| 94 | return this; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
[5748] | 98 | /** |
---|
[5749] | 99 | * Configure client to parse unknown Data Views into a String |
---|
| 100 | * representation. |
---|
[5748] | 101 | * |
---|
| 102 | * @return this {@link ClarinFCSClientBuilder} instance |
---|
| 103 | * @see DataViewParserGenericString |
---|
[5749] | 104 | * @see DataViewGenericString |
---|
[5748] | 105 | */ |
---|
| 106 | public ClarinFCSClientBuilder unknownDataViewAsString() { |
---|
[5743] | 107 | unknownAsDom = false; |
---|
| 108 | return this; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
[5748] | 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 | /** |
---|
[5749] | 131 | * Configure client to enable support for legacy CLARIN-FCS endpoints. |
---|
[5748] | 132 | * |
---|
| 133 | * @return this {@link ClarinFCSClientBuilder} instance |
---|
| 134 | */ |
---|
| 135 | public ClarinFCSClientBuilder enableLegacySupport() { |
---|
| 136 | legacySupport = true; |
---|
| 137 | return this; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | /** |
---|
[5749] | 142 | * Configure client to disable support for legacy CLARIN-FCS endpoints. |
---|
[5748] | 143 | * |
---|
| 144 | * @return this {@link ClarinFCSClientBuilder} instance |
---|
| 145 | */ |
---|
| 146 | public ClarinFCSClientBuilder disableLegacySupport() { |
---|
| 147 | legacySupport = false; |
---|
| 148 | return this; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | /** |
---|
[7079] | 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 | /** |
---|
[5748] | 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 | |
---|
[7268] | 244 | public ClarinFCSClientBuilder setRequestAuthenticator( |
---|
| 245 | SRURequestAuthenticator requestAuthStrategy) { |
---|
| 246 | this.requestAuthStrategy = requestAuthStrategy; |
---|
| 247 | return this; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | |
---|
[5748] | 251 | /** |
---|
[5749] | 252 | * Register a Data View parser. |
---|
[5748] | 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 |
---|
[5749] | 259 | * @see DataViewParser |
---|
[5748] | 260 | */ |
---|
[5743] | 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() + |
---|
[5749] | 269 | "' cannot be added manually"); |
---|
[5743] | 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 | |
---|
[5748] | 280 | /** |
---|
[5804] | 281 | * Register an extra response data parser. |
---|
[5798] | 282 | * |
---|
| 283 | * @param parser |
---|
| 284 | * the extra response data parser to be registered |
---|
| 285 | * @return this {@link ClarinFCSClientBuilder} instance |
---|
| 286 | * @throws IllegalArgumentException |
---|
[5804] | 287 | * if an error occurred while registering the extra response |
---|
| 288 | * data parser |
---|
[5798] | 289 | * @see SRUExtraResponseDataParser |
---|
| 290 | */ |
---|
[5804] | 291 | public ClarinFCSClientBuilder registerExtraResponseDataParser( |
---|
| 292 | SRUExtraResponseDataParser parser) { |
---|
[5798] | 293 | if (parser == null) { |
---|
| 294 | throw new NullPointerException("parser == null"); |
---|
| 295 | } |
---|
| 296 | extraDataParsers.add(parser); |
---|
| 297 | return this; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | /** |
---|
[5748] | 302 | * Create a {@link SRUSimpleClient} instance. |
---|
| 303 | * |
---|
| 304 | * @return a configured {@link SRUSimpleClient} instance |
---|
| 305 | */ |
---|
| 306 | public SRUSimpleClient buildSimpleClient() { |
---|
| 307 | return new SRUSimpleClient(makeClientConfig()); |
---|
[5743] | 308 | } |
---|
| 309 | |
---|
| 310 | |
---|
[5748] | 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()); |
---|
[5743] | 318 | } |
---|
| 319 | |
---|
| 320 | |
---|
[5748] | 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()); |
---|
[5743] | 328 | } |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | @SuppressWarnings("deprecation") |
---|
[5748] | 332 | private SRUClientConfig makeClientConfig() { |
---|
| 333 | final SRUClientConfig.Builder builder = new SRUClientConfig.Builder(); |
---|
| 334 | builder |
---|
| 335 | .setDefaultVersion(defaultVersion) |
---|
| 336 | .setConnectTimeout(connectTimeout) |
---|
[7268] | 337 | .setSocketTimeout(socketTimeout) |
---|
| 338 | .setRequestAuthenticator(requestAuthStrategy); |
---|
[5743] | 339 | final List<DataViewParser> p = finalizeDataViewParsers(); |
---|
[5748] | 340 | builder.addRecordDataParser(new ClarinFCSRecordDataParser(p)); |
---|
[5743] | 341 | if (legacySupport) { |
---|
[7079] | 342 | builder.addRecordDataParser(new LegacyClarinFCSRecordDataParser(p, fullLegacyCompatMode)); |
---|
[5743] | 343 | } |
---|
[5798] | 344 | if ((extraDataParsers != null) && !extraDataParsers.isEmpty()) { |
---|
| 345 | for (SRUExtraResponseDataParser parser : extraDataParsers) { |
---|
| 346 | builder.addExtraResponseDataParser(parser); |
---|
| 347 | } |
---|
| 348 | } |
---|
[5748] | 349 | return builder.build(); |
---|
[5743] | 350 | } |
---|
| 351 | |
---|
| 352 | |
---|
[5749] | 353 | @SuppressWarnings("deprecation") |
---|
[5743] | 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) { |
---|
[7079] | 365 | result.add(new DataViewParserKWIC(fullLegacyCompatMode)); |
---|
[5743] | 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 |
---|