|
3 | 3 |
|
4 | 4 | package com.azure.core.amqp.implementation; |
5 | 5 |
|
| 6 | +import com.azure.core.util.CoreUtils; |
| 7 | +import com.azure.core.util.logging.ClientLogger; |
| 8 | + |
6 | 9 | import java.net.URI; |
7 | 10 | import java.net.URISyntaxException; |
8 | 11 | import java.util.Locale; |
|
12 | 15 | * The set of properties that comprise a connection string from the Azure portal. |
13 | 16 | */ |
14 | 17 | public class ConnectionStringProperties { |
| 18 | + private final ClientLogger logger = new ClientLogger(ConnectionStringProperties.class); |
| 19 | + |
15 | 20 | private static final String TOKEN_VALUE_SEPARATOR = "="; |
| 21 | + private static final String ENDPOINT_SCHEME_SB_PREFIX = "sb://"; |
| 22 | + private static final String ENDPOINT_SCHEME_HTTP_PREFIX = "http://"; |
| 23 | + private static final String ENDPOINT_SCHEME_HTTPS_PREFIX = "https://"; |
16 | 24 | private static final String TOKEN_VALUE_PAIR_DELIMITER = ";"; |
17 | 25 | private static final String ENDPOINT = "Endpoint"; |
18 | 26 | private static final String SHARED_ACCESS_KEY_NAME = "SharedAccessKeyName"; |
19 | 27 | private static final String SHARED_ACCESS_KEY = "SharedAccessKey"; |
20 | 28 | private static final String ENTITY_PATH = "EntityPath"; |
21 | 29 | private static final String ERROR_MESSAGE_FORMAT = "Could not parse 'connectionString'. Expected format: " |
22 | 30 | + "'Endpoint={endpoint};SharedAccessKeyName={sharedAccessKeyName};" |
23 | | - + "SharedAccessKey={sharedAccessKey};EntityPath={eventHubName}'. Actual: %s"; |
| 31 | + + "SharedAccessKey={sharedAccessKey};EntityPath={entityPath}'. Actual: %s"; |
| 32 | + private static final String ERROR_MESSAGE_ENDPOINT_FORMAT = "'Endpoint' must be provided in 'connectionString'." |
| 33 | + + " Actual: %s"; |
24 | 34 |
|
25 | 35 | private final URI endpoint; |
26 | 36 | private final String entityPath; |
@@ -60,8 +70,9 @@ public ConnectionStringProperties(String connectionString) { |
60 | 70 | final String value = pair[1].trim(); |
61 | 71 |
|
62 | 72 | if (key.equalsIgnoreCase(ENDPOINT)) { |
| 73 | + final String endpointUri = validateAndUpdateDefaultScheme(value, connectionString); |
63 | 74 | try { |
64 | | - endpoint = new URI(value); |
| 75 | + endpoint = new URI(endpointUri); |
65 | 76 | } catch (URISyntaxException e) { |
66 | 77 | throw new IllegalArgumentException( |
67 | 78 | String.format(Locale.US, "Invalid endpoint: %s", tokenValuePair), e); |
@@ -123,4 +134,25 @@ public String getSharedAccessKeyName() { |
123 | 134 | public String getSharedAccessKey() { |
124 | 135 | return sharedAccessKey; |
125 | 136 | } |
| 137 | + |
| 138 | + /* |
| 139 | + * The function checks for pre existing scheme of "sb://" , "http://" or "https://". If the scheme is not provided |
| 140 | + * in endpoint, it will set the default scheme to "sb://". |
| 141 | + */ |
| 142 | + private String validateAndUpdateDefaultScheme(final String endpoint, final String connectionString) { |
| 143 | + String updatedEndpoint = endpoint.trim(); |
| 144 | + |
| 145 | + if (CoreUtils.isNullOrEmpty(endpoint)) { |
| 146 | + throw logger.logExceptionAsError(new IllegalArgumentException(String.format(Locale.US, |
| 147 | + ERROR_MESSAGE_ENDPOINT_FORMAT, connectionString))); |
| 148 | + |
| 149 | + } |
| 150 | + final String endpointLowerCase = endpoint.toLowerCase(Locale.getDefault()); |
| 151 | + if (!endpointLowerCase.startsWith(ENDPOINT_SCHEME_SB_PREFIX) |
| 152 | + && !endpointLowerCase.startsWith(ENDPOINT_SCHEME_HTTP_PREFIX) |
| 153 | + && !endpointLowerCase.startsWith(ENDPOINT_SCHEME_HTTPS_PREFIX)) { |
| 154 | + updatedEndpoint = ENDPOINT_SCHEME_SB_PREFIX + endpoint; |
| 155 | + } |
| 156 | + return updatedEndpoint; |
| 157 | + } |
126 | 158 | } |
0 commit comments