24
24
import org .slf4j .Logger ;
25
25
import org .slf4j .LoggerFactory ;
26
26
27
+ import java .util .Map .Entry ;
28
+ import java .util .function .BiConsumer ;
27
29
import javax .net .SocketFactory ;
28
30
import javax .net .ssl .SSLContext ;
29
31
import javax .net .ssl .SSLSocketFactory ;
@@ -386,6 +388,36 @@ private static String uriDecode(String s) {
386
388
}
387
389
}
388
390
391
+ private static final Map <String , BiConsumer <String , ConnectionFactory >> URI_QUERY_PARAMETER_HANDLERS =
392
+ new HashMap <String , BiConsumer <String , ConnectionFactory >>() {
393
+ {
394
+ put ("heartbeat" , (value , cf ) -> {
395
+ try {
396
+ int heartbeatInt = Integer .parseInt (value );
397
+ cf .setRequestedHeartbeat (heartbeatInt );
398
+ } catch (NumberFormatException e ) {
399
+ throw new IllegalArgumentException ("Requested heartbeat must an integer" );
400
+ }
401
+ });
402
+ put ("connection_timeout" , (value , cf ) -> {
403
+ try {
404
+ int connectionTimeoutInt = Integer .parseInt (value );
405
+ cf .setConnectionTimeout (connectionTimeoutInt );
406
+ } catch (NumberFormatException e ) {
407
+ throw new IllegalArgumentException ("TCP connection timeout must an integer" );
408
+ }
409
+ });
410
+ put ("channel_max" , (value , cf ) -> {
411
+ try {
412
+ int channelMaxInt = Integer .parseInt (value );
413
+ cf .setRequestedChannelMax (channelMaxInt );
414
+ } catch (NumberFormatException e ) {
415
+ throw new IllegalArgumentException ("Requested channel max must an integer" );
416
+ }
417
+ });
418
+ }
419
+ };
420
+
389
421
/**
390
422
* Convenience method for setting some fields from query parameters
391
423
* Will handle only a subset of the query parameters supported by the
@@ -395,7 +427,6 @@ private static String uriDecode(String s) {
395
427
*/
396
428
private void setQuery (String rawQuery ) {
397
429
Map <String , String > parameters = new HashMap <>();
398
-
399
430
// parsing the query parameters
400
431
try {
401
432
for (String param : rawQuery .split ("&" )) {
@@ -408,43 +439,31 @@ private void setQuery(String rawQuery) {
408
439
parameters .put (key , value );
409
440
}
410
441
} catch (IOException e ) {
411
- throw new RuntimeException ("Cannot parse the query parameters" , e );
442
+ throw new IllegalArgumentException ("Cannot parse the query parameters" , e );
412
443
}
413
444
414
- // heartbeat
415
- String heartbeat = parameters .get ("heartbeat" );
416
- if (heartbeat != null ) {
417
- try {
418
- int heartbeatInt = Integer .parseInt (heartbeat );
419
- setRequestedHeartbeat (heartbeatInt );
420
- } catch (NumberFormatException e ) {
421
- throw new IllegalArgumentException ("Requested heartbeat must an integer" );
422
- }
423
- }
424
-
425
- // connection_timeout
426
- String connectionTimeout = parameters .get ("connection_timeout" );
427
- if (connectionTimeout != null ) {
428
- try {
429
- int connectionTimeoutInt = Integer .parseInt (connectionTimeout );
430
- setConnectionTimeout (connectionTimeoutInt );
431
- } catch (NumberFormatException e ) {
432
- throw new IllegalArgumentException ("TCP connection timeout must an integer" );
433
- }
434
- }
435
-
436
- // channel_max
437
- String channelMax = parameters .get ("channel_max" );
438
- if (channelMax != null ) {
439
- try {
440
- int channelMaxInt = Integer .parseInt (channelMax );
441
- setRequestedChannelMax (channelMaxInt );
442
- } catch (NumberFormatException e ) {
443
- throw new IllegalArgumentException ("Requested channel max must an integer" );
445
+ for (Entry <String , String > entry : parameters .entrySet ()) {
446
+ BiConsumer <String , ConnectionFactory > handler = URI_QUERY_PARAMETER_HANDLERS
447
+ .get (entry .getKey ());
448
+ if (handler != null ) {
449
+ handler .accept (entry .getValue (), this );
450
+ } else {
451
+ processUriQueryParameter (entry .getKey (), entry .getValue ());
444
452
}
445
453
}
446
454
}
447
455
456
+ /**
457
+ * Hook to process query parameters not handled natively.
458
+ * Handled natively: <code>heartbeat</code>, <code>connection_timeout</code>,
459
+ * <code>channel_max</code>.
460
+ * @param key
461
+ * @param value
462
+ */
463
+ protected void processUriQueryParameter (String key , String value ) {
464
+
465
+ }
466
+
448
467
/**
449
468
* Retrieve the requested maximum channel number
450
469
* @return the initially requested maximum channel number; zero for unlimited
0 commit comments