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