From 907aa171ee85d7e15b0ab8a483c267300c84eb00 Mon Sep 17 00:00:00 2001 From: Hemant Tanwar Date: Fri, 28 Aug 2020 16:58:25 -0700 Subject: [PATCH 1/3] Default scheme to SB:// --- eng/versioning/version_client.txt | 1 + .../ConnectionStringProperties.java | 30 ++++++++++++++++++- .../ConnectionStringPropertiesTest.java | 12 ++++++++ .../azure-messaging-eventhubs/CHANGELOG.md | 1 + .../azure-messaging-eventhubs/pom.xml | 2 +- .../azure-messaging-servicebus/pom.xml | 2 +- 6 files changed, 45 insertions(+), 3 deletions(-) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index 50eadaf0c2c08..1fd444576ea40 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -123,6 +123,7 @@ com.microsoft.azure:spring-cloud-azure-eventhubs-stream-binder;1.2.8-beta.1;1.2. # unreleased_:;dependency-version # note: The unreleased dependencies will not be manipulated with the automatic PR creation code. unreleased_com.azure:azure-core;1.8.0-beta.1 +unreleased_com.azure:azure-core-amqp;1.5.0-beta.1 unreleased_com.azure:azure-messaging-servicebus;7.0.0-beta.5 unreleased_com.azure:azure-security-keyvault-keys;4.3.0-beta.1 diff --git a/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java b/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java index a55dcc4c75cf9..0811a243abc94 100644 --- a/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java +++ b/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java @@ -3,6 +3,9 @@ package com.azure.core.amqp.implementation; +import com.azure.core.util.CoreUtils; +import com.azure.core.util.logging.ClientLogger; + import java.net.URI; import java.net.URISyntaxException; import java.util.Locale; @@ -12,7 +15,11 @@ * The set of properties that comprise a connection string from the Azure portal. */ public class ConnectionStringProperties { + private final ClientLogger logger = new ClientLogger(ConnectionStringProperties.class); + private static final String TOKEN_VALUE_SEPARATOR = "="; + private static final String ENDPOINT_SCHEME_SB_PREFIX = "sb://"; + private static final String ENDPOINT_SCHEME_HTTP_PREFIX = "http://"; private static final String TOKEN_VALUE_PAIR_DELIMITER = ";"; private static final String ENDPOINT = "Endpoint"; private static final String SHARED_ACCESS_KEY_NAME = "SharedAccessKeyName"; @@ -21,6 +28,8 @@ public class ConnectionStringProperties { private static final String ERROR_MESSAGE_FORMAT = "Could not parse 'connectionString'. Expected format: " + "'Endpoint={endpoint};SharedAccessKeyName={sharedAccessKeyName};" + "SharedAccessKey={sharedAccessKey};EntityPath={eventHubName}'. Actual: %s"; + private static final String ERROR_MESSAGE_ENDPOINT_FORMAT = "'Endpoint' must be provided in 'connectionString'." + + " Actual: %s"; private final URI endpoint; private final String entityPath; @@ -60,8 +69,9 @@ public ConnectionStringProperties(String connectionString) { final String value = pair[1].trim(); if (key.equalsIgnoreCase(ENDPOINT)) { + final String endpointUri = validateAndUpdateDefaultScheme(value, connectionString); try { - endpoint = new URI(value); + endpoint = new URI(endpointUri); } catch (URISyntaxException e) { throw new IllegalArgumentException( String.format(Locale.US, "Invalid endpoint: %s", tokenValuePair), e); @@ -123,4 +133,22 @@ public String getSharedAccessKeyName() { public String getSharedAccessKey() { return sharedAccessKey; } + + /* + * The function checks for pre existing scheme of "sb://" and "http://". If the scheme is not provided in + * endpoint, it will set the default scheme to "sb". + */ + private String validateAndUpdateDefaultScheme(final String endpoint, final String connectionString) { + String updatedEndpoint = endpoint; + + if (CoreUtils.isNullOrEmpty(endpoint)) { + throw logger.logExceptionAsError(new IllegalArgumentException(String.format(Locale.US, + ERROR_MESSAGE_ENDPOINT_FORMAT, connectionString))); + + } + if (!endpoint.startsWith(ENDPOINT_SCHEME_SB_PREFIX) && !endpoint.startsWith(ENDPOINT_SCHEME_HTTP_PREFIX)) { + updatedEndpoint = ENDPOINT_SCHEME_SB_PREFIX + endpoint; + } + return updatedEndpoint; + } } diff --git a/sdk/core/azure-core-amqp/src/test/java/com/azure/core/amqp/implementation/ConnectionStringPropertiesTest.java b/sdk/core/azure-core-amqp/src/test/java/com/azure/core/amqp/implementation/ConnectionStringPropertiesTest.java index 3060ee9dd92bc..e203b8c42940d 100644 --- a/sdk/core/azure-core-amqp/src/test/java/com/azure/core/amqp/implementation/ConnectionStringPropertiesTest.java +++ b/sdk/core/azure-core-amqp/src/test/java/com/azure/core/amqp/implementation/ConnectionStringPropertiesTest.java @@ -69,6 +69,18 @@ public void differentEndpointScheme() { Assertions.assertEquals(EVENT_HUB, properties.getEntityPath()); } + @Test + public void noEndpointSchemeDefault() { + // Arrange + final String connectionString = getConnectionString(HOST, EVENT_HUB, SAS_KEY, SAS_VALUE); + + // Act + ConnectionStringProperties properties = new ConnectionStringProperties(connectionString); + + // Assert + Assertions.assertEquals("sb", properties.getEndpoint().getScheme()); + } + /** * Verifies we can create ConnectionStringProperties even if there is an extraneous component. */ diff --git a/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md b/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md index 60a91ceb27c50..52552fcd6dd9e 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md +++ b/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md @@ -1,6 +1,7 @@ # Release History ## 5.2.0-beta.3 (Unreleased) +- Default scheme to 'sb://' if not scheme is set in 'Endpoint'. ## 5.2.0-beta.2 (2020-08-14) - Support for object serializer to send and receive strongly-typed objects. diff --git a/sdk/eventhubs/azure-messaging-eventhubs/pom.xml b/sdk/eventhubs/azure-messaging-eventhubs/pom.xml index 4717d047cc622..a655e18055e14 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/pom.xml +++ b/sdk/eventhubs/azure-messaging-eventhubs/pom.xml @@ -42,7 +42,7 @@ com.azure azure-core-amqp - 1.4.0 + 1.5.0-beta.1 diff --git a/sdk/servicebus/azure-messaging-servicebus/pom.xml b/sdk/servicebus/azure-messaging-servicebus/pom.xml index b16e0d672c0d8..13d7b6f29d9ee 100644 --- a/sdk/servicebus/azure-messaging-servicebus/pom.xml +++ b/sdk/servicebus/azure-messaging-servicebus/pom.xml @@ -47,7 +47,7 @@ com.azure azure-core-amqp - 1.4.0 + 1.5.0-beta.1 com.azure From 7952948e5fda2e04048ae3a926167048eb954ca1 Mon Sep 17 00:00:00 2001 From: Hemant Tanwar Date: Mon, 31 Aug 2020 13:43:39 -0700 Subject: [PATCH 2/3] Fixing test --- .../implementation/ConnectionStringProperties.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java b/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java index 0811a243abc94..7e365aaaf896a 100644 --- a/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java +++ b/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java @@ -20,6 +20,7 @@ public class ConnectionStringProperties { private static final String TOKEN_VALUE_SEPARATOR = "="; private static final String ENDPOINT_SCHEME_SB_PREFIX = "sb://"; private static final String ENDPOINT_SCHEME_HTTP_PREFIX = "http://"; + private static final String ENDPOINT_SCHEME_HTTPS_PREFIX = "https://"; private static final String TOKEN_VALUE_PAIR_DELIMITER = ";"; private static final String ENDPOINT = "Endpoint"; private static final String SHARED_ACCESS_KEY_NAME = "SharedAccessKeyName"; @@ -135,18 +136,21 @@ public String getSharedAccessKey() { } /* - * The function checks for pre existing scheme of "sb://" and "http://". If the scheme is not provided in - * endpoint, it will set the default scheme to "sb". + * The function checks for pre existing scheme of "sb://" , "http://" or "https://". If the scheme is not provided + * in endpoint, it will set the default scheme to "sb://". */ private String validateAndUpdateDefaultScheme(final String endpoint, final String connectionString) { - String updatedEndpoint = endpoint; + String updatedEndpoint = endpoint.trim(); if (CoreUtils.isNullOrEmpty(endpoint)) { throw logger.logExceptionAsError(new IllegalArgumentException(String.format(Locale.US, ERROR_MESSAGE_ENDPOINT_FORMAT, connectionString))); } - if (!endpoint.startsWith(ENDPOINT_SCHEME_SB_PREFIX) && !endpoint.startsWith(ENDPOINT_SCHEME_HTTP_PREFIX)) { + final String endpointLowerCase = endpoint.toLowerCase(Locale.getDefault()); + if (!endpointLowerCase.startsWith(ENDPOINT_SCHEME_SB_PREFIX) + && !endpointLowerCase.startsWith(ENDPOINT_SCHEME_HTTP_PREFIX) + && !endpointLowerCase.startsWith(ENDPOINT_SCHEME_HTTPS_PREFIX)) { updatedEndpoint = ENDPOINT_SCHEME_SB_PREFIX + endpoint; } return updatedEndpoint; From 565c0891b0412794f06ecc8f5c5ed6e3d0ce2246 Mon Sep 17 00:00:00 2001 From: Hemant Tanwar Date: Mon, 31 Aug 2020 15:28:06 -0700 Subject: [PATCH 3/3] REview comments --- .../core/amqp/implementation/ConnectionStringProperties.java | 2 +- sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java b/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java index 7e365aaaf896a..2d2a2f1dc8905 100644 --- a/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java +++ b/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ConnectionStringProperties.java @@ -28,7 +28,7 @@ public class ConnectionStringProperties { private static final String ENTITY_PATH = "EntityPath"; private static final String ERROR_MESSAGE_FORMAT = "Could not parse 'connectionString'. Expected format: " + "'Endpoint={endpoint};SharedAccessKeyName={sharedAccessKeyName};" - + "SharedAccessKey={sharedAccessKey};EntityPath={eventHubName}'. Actual: %s"; + + "SharedAccessKey={sharedAccessKey};EntityPath={entityPath}'. Actual: %s"; private static final String ERROR_MESSAGE_ENDPOINT_FORMAT = "'Endpoint' must be provided in 'connectionString'." + " Actual: %s"; diff --git a/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md b/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md index 52552fcd6dd9e..cb97f6d20f69d 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md +++ b/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md @@ -1,7 +1,7 @@ # Release History ## 5.2.0-beta.3 (Unreleased) -- Default scheme to 'sb://' if not scheme is set in 'Endpoint'. +- Default scheme to 'sb://' if no scheme is set in 'Endpoint'. ## 5.2.0-beta.2 (2020-08-14) - Support for object serializer to send and receive strongly-typed objects.