From aa38d33404829b426699e4723eee62bf97d6ca2f Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 7 Jul 2014 17:00:57 +0100 Subject: [PATCH] Support for setting credentials and vhost in rabbit addresses User can now add credentials, vhost and protocol prefix (amqp://) to any or all of the addresses, extending the format beyond that accepted bu the rabbitmq client, but making it cloud friendly. Only one of the addresses needs those properties and all are optional. Port also defaults to 5672 in an address. --- .../autoconfigure/amqp/RabbitProperties.java | 35 ++++++++++++++++++- .../amqp/RabbitPropertiesTests.java | 28 +++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index dfc6a1e1cc9..63d11b17520 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -70,13 +70,46 @@ public class RabbitProperties { } public void setAddresses(String addresses) { - this.addresses = addresses; + this.addresses = parseAddresses(addresses); } public String getAddresses() { return (this.addresses == null ? this.host + ":" + this.port : this.addresses); } + private String parseAddresses(String addresses) { + StringBuilder result = new StringBuilder(); + for (String address : StringUtils.commaDelimitedListToSet(addresses)) { + address = address.trim(); + if (address.startsWith("amqp://")) { + address = address.substring("amqp://".length()); + } + if (address.contains("@")) { + String[] split = StringUtils.split(address, "@"); + String creds = split[0]; + address = split[1]; + split = StringUtils.split(creds, ":"); + this.username = split[0]; + if (split.length > 0) { + this.password = split[1]; + } + } + int index = address.indexOf("/"); + if (index >= 0 && index < address.length()) { + this.virtualHost = address.substring(index + 1); + address = address.substring(0, index); + } + if (result.length() > 0) { + result.append(","); + } + if (!address.contains(":")) { + address = address + ":" + this.port; + } + result.append(address); + } + return result.length() > 0 ? result.toString() : null; + } + public void setPort(int port) { this.port = port; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java index 30a404cd591..0a9c0058ffb 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java @@ -50,6 +50,34 @@ public class RabbitPropertiesTests { assertEquals(9999, this.properties.getPort()); } + @Test + public void addressesDoubleValuedWithCredentials() { + this.properties.setAddresses("myhost:9999,root:password@otherhost:1111/host"); + assertNull(this.properties.getHost()); + assertEquals(9999, this.properties.getPort()); + assertEquals("root", this.properties.getUsername()); + assertEquals("host", this.properties.getVirtualHost()); + } + + @Test + public void addressesSingleValuedWithCredentials() { + this.properties.setAddresses("amqp://root:password@otherhost:1111/host"); + assertEquals("otherhost", this.properties.getHost()); + assertEquals(1111, this.properties.getPort()); + assertEquals("root", this.properties.getUsername()); + assertEquals("host", this.properties.getVirtualHost()); + } + + @Test + public void addressesSingleValuedWithCredentialsDefaultPort() { + this.properties.setAddresses("amqp://root:password@lemur.cloudamqp.com/host"); + assertEquals("lemur.cloudamqp.com", this.properties.getHost()); + assertEquals(5672, this.properties.getPort()); + assertEquals("root", this.properties.getUsername()); + assertEquals("host", this.properties.getVirtualHost()); + assertEquals("lemur.cloudamqp.com:5672", this.properties.getAddresses()); + } + @Test public void testDefaultVirtualHost() { this.properties.setVirtualHost("/");