Skip to content

Commit

Permalink
ConnectionString : Default scheme to SB:// in Endpoint if not specifi…
Browse files Browse the repository at this point in the history
…ed (#14600)

* Default scheme to SB:// if no scheme is provided in endpoint
  • Loading branch information
hemanttanwar authored Sep 1, 2020
1 parent 50297c7 commit 4ebe57b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions eng/versioning/version_client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ com.microsoft.azure:spring-cloud-azure-eventhubs-stream-binder;1.2.8-beta.1;1.2.
# unreleased_<groupId>:<artifactId>;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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,15 +15,22 @@
* 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";
private static final String SHARED_ACCESS_KEY = "SharedAccessKey";
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion sdk/eventhubs/azure-messaging-eventhubs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-amqp</artifactId>
<version>1.4.0</version> <!-- {x-version-update;com.azure:azure-core-amqp;dependency} -->
<version>1.5.0-beta.1</version> <!-- {x-version-update;unreleased_com.azure:azure-core-amqp;dependency} -->
</dependency>

<!-- Test dependencies -->
Expand Down
2 changes: 1 addition & 1 deletion sdk/servicebus/azure-messaging-servicebus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-amqp</artifactId>
<version>1.4.0</version> <!-- {x-version-update;com.azure:azure-core-amqp;dependency} -->
<version>1.5.0-beta.1</version> <!-- {x-version-update;unreleased_com.azure:azure-core-amqp;dependency} -->
</dependency>
<dependency>
<groupId>com.azure</groupId>
Expand Down

0 comments on commit 4ebe57b

Please sign in to comment.