Skip to content

Commit

Permalink
move URIBuilder to further facilitate removal of Apache
Browse files Browse the repository at this point in the history
  • Loading branch information
cole committed Sep 28, 2024
1 parent b362b2b commit 83dd697
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
28 changes: 2 additions & 26 deletions data/src/main/java/com/microsoft/azure/kusto/data/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
import com.microsoft.azure.kusto.data.res.JsonResult;
import org.apache.commons.lang3.StringUtils;

import org.apache.http.client.utils.URIBuilder;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

Expand All @@ -37,7 +35,6 @@ class ClientImpl extends BaseClient {
public static final String STREAMING_VERSION = "v1";
private static final String DEFAULT_DATABASE_NAME = "NetDefaultDb";

public static final String FEDERATED_SECURITY_SUFFIX = ";fed=true";
private final TokenProviderBase aadAuthenticationHelper;

private final String clusterUrl;
Expand All @@ -54,29 +51,8 @@ public ClientImpl(ConnectionStringBuilder csb, HttpClientProperties properties)

public ClientImpl(ConnectionStringBuilder csb, HttpClient httpClient) throws URISyntaxException {
super(httpClient);

URI clusterUrlForParsing = new URI(csb.getClusterUrl());
String host = clusterUrlForParsing.getHost();
Objects.requireNonNull(clusterUrlForParsing.getAuthority(), "clusterUri must have uri authority component");
String auth = clusterUrlForParsing.getAuthority().toLowerCase();
if (host == null) {
host = StringUtils.removeEndIgnoreCase(auth, FEDERATED_SECURITY_SUFFIX);
}
URIBuilder uriBuilder = new URIBuilder()
.setScheme(clusterUrlForParsing.getScheme())
.setHost(host);
String path = clusterUrlForParsing.getPath();
if (path != null && !path.isEmpty()) {
path = StringUtils.removeEndIgnoreCase(path, FEDERATED_SECURITY_SUFFIX);
path = StringUtils.removeEndIgnoreCase(path, "/");

uriBuilder.setPath(path);
}

if (clusterUrlForParsing.getPort() != -1) {
uriBuilder.setPort(clusterUrlForParsing.getPort());
}
csb.setClusterUrl(uriBuilder.build().toString());
String clusterURL = UriUtils.createClusterURLFrom(csb.getClusterUrl());
csb.setClusterUrl(clusterURL);

clusterUrl = csb.getClusterUrl();
aadAuthenticationHelper = clusterUrl.toLowerCase().startsWith(CloudInfo.LOCALHOST) ? null : TokenProviderFactory.createTokenProvider(csb, httpClient);
Expand Down
30 changes: 30 additions & 0 deletions data/src/main/java/com/microsoft/azure/kusto/data/UriUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,44 @@
import org.apache.http.client.utils.URIBuilder;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Objects;

public class UriUtils {

public static final String FEDERATED_SECURITY_SUFFIX = ";fed=true";

private UriUtils() {
// Providing hidden constructor to hide default public constructor in utils class
}

public static String createClusterURLFrom(final String clusterURI) throws URISyntaxException {
URI clusterUrlForParsing = new URI(clusterURI);
String host = clusterUrlForParsing.getHost();
Objects.requireNonNull(clusterUrlForParsing.getAuthority(), "clusterUri must have uri authority component");
String auth = clusterUrlForParsing.getAuthority().toLowerCase();
if (host == null) {
host = StringUtils.removeEndIgnoreCase(auth, FEDERATED_SECURITY_SUFFIX);
}
URIBuilder uriBuilder = new URIBuilder()
.setScheme(clusterUrlForParsing.getScheme())
.setHost(host);
String path = clusterUrlForParsing.getPath();
if (path != null && !path.isEmpty()) {
path = StringUtils.removeEndIgnoreCase(path, FEDERATED_SECURITY_SUFFIX);
path = StringUtils.removeEndIgnoreCase(path, "/");

uriBuilder.setPath(path);
}

if (clusterUrlForParsing.getPort() != -1) {
uriBuilder.setPort(clusterUrlForParsing.getPort());
}
return uriBuilder.build().toString();
}

public static String setPathForUri(String uri, String path, boolean ensureTrailingSlash) throws URISyntaxException {
path = StringUtils.prependIfMissing(path, "/");

Expand Down

0 comments on commit 83dd697

Please sign in to comment.