generated from salesforce/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: more robust handling of connection urls (#7)
Trying to make small steps towards passing around an object that represents the connection string with its various details as opposed to implicitly setting properties on the property bag provided by the client. The breaking change listed here is moving public methods about what urls are handled by this driver out of the `DataCloudConnection : Connection` implementation
- Loading branch information
1 parent
ff56e4c
commit 2e2520f
Showing
13 changed files
with
393 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/main/java/com/salesforce/datacloud/jdbc/core/DataCloudConnectionString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright (c) 2024, Salesforce, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.salesforce.datacloud.jdbc.core; | ||
|
||
import com.salesforce.datacloud.jdbc.exception.DataCloudJDBCException; | ||
import com.salesforce.datacloud.jdbc.util.Messages; | ||
import com.salesforce.datacloud.jdbc.util.StringCompatibility; | ||
import java.net.URI; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Properties; | ||
import java.util.stream.Collectors; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.val; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
@Builder(access = AccessLevel.PRIVATE) | ||
public class DataCloudConnectionString { | ||
public static final String CONNECTION_PROTOCOL = "jdbc:salesforce-datacloud:"; | ||
|
||
public static DataCloudConnectionString of(String url) throws SQLException { | ||
if (!acceptsUrl(url)) { | ||
throw new DataCloudJDBCException(Messages.ILLEGAL_CONNECTION_PROTOCOL); | ||
} | ||
|
||
val database = getDatabaseUrl(url); | ||
val login = getAuthenticationUrl(url); | ||
val parameters = parseParameters(url); | ||
|
||
return DataCloudConnectionString.builder() | ||
.databaseUrl(database) | ||
.loginUrl(login) | ||
.parameters(parameters) | ||
.build(); | ||
} | ||
|
||
@Getter | ||
private final String databaseUrl; | ||
|
||
@Getter | ||
private final String loginUrl; | ||
|
||
private final Map<String, String> parameters; | ||
|
||
public static boolean acceptsUrl(String url) { | ||
return url != null && url.startsWith(CONNECTION_PROTOCOL) && urlDoesNotContainScheme(url); | ||
} | ||
|
||
private static boolean urlDoesNotContainScheme(String url) { | ||
val suffix = url.substring(CONNECTION_PROTOCOL.length()); | ||
return !suffix.startsWith("http://") && !suffix.startsWith("https://"); | ||
} | ||
|
||
/** | ||
* Returns the extracted service url from given jdbc endpoint | ||
* | ||
* @param url of the form jdbc:salesforce-datacloud://login.salesforce.com | ||
* @return service url | ||
* @throws SQLException when given url doesn't belong with required datasource | ||
*/ | ||
static String getAuthenticationUrl(String url) throws SQLException { | ||
if (!acceptsUrl(url)) { | ||
throw new DataCloudJDBCException(Messages.ILLEGAL_CONNECTION_PROTOCOL); | ||
} | ||
|
||
val withoutParameters = withoutParameters(url); | ||
val serviceRootUrl = withoutParameters.substring(CONNECTION_PROTOCOL.length()); | ||
val noTrailingSlash = StringUtils.removeEnd(serviceRootUrl, "/"); | ||
val host = StringUtils.removeStart(noTrailingSlash, "//"); | ||
|
||
return StringCompatibility.isBlank(host) ? host : createURI(host).toString(); | ||
} | ||
|
||
static String getDatabaseUrl(String url) throws SQLException { | ||
if (!acceptsUrl(url)) { | ||
throw new DataCloudJDBCException(Messages.ILLEGAL_CONNECTION_PROTOCOL); | ||
} | ||
|
||
return withoutParameters(url); | ||
} | ||
|
||
private static String withoutParameters(String url) { | ||
return url.split(";")[0]; | ||
} | ||
|
||
private static Map<String, String> parseParameters(String connectionString) { | ||
val parts = connectionString.split(";"); | ||
|
||
return Arrays.stream(parts) | ||
.skip(1) | ||
.filter(pair -> pair.contains("=")) | ||
.map(pair -> { | ||
val split = pair.split("="); | ||
return split.length != 2 ? null : split; | ||
}) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toMap(p -> p[0], p -> p[1])); | ||
} | ||
|
||
private static URI createURI(String host) throws SQLException { | ||
try { | ||
return URI.create("https://" + host); | ||
} catch (IllegalArgumentException e) { | ||
throw new DataCloudJDBCException(Messages.ILLEGAL_CONNECTION_PROTOCOL, e); | ||
} | ||
} | ||
|
||
void withParameters(Properties properties) { | ||
properties.putAll(parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.