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..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 @@ -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,12 @@ * 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 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"; @@ -20,7 +28,9 @@ 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"; private final URI endpoint; private final String entityPath; @@ -60,8 +70,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 +134,25 @@ public String getSharedAccessKeyName() { public String getSharedAccessKey() { return sharedAccessKey; } + + /* + * 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.trim(); + + if (CoreUtils.isNullOrEmpty(endpoint)) { + throw logger.logExceptionAsError(new IllegalArgumentException(String.format(Locale.US, + ERROR_MESSAGE_ENDPOINT_FORMAT, connectionString))); + + } + 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; + } } 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..cb97f6d20f69d 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 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. 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