Skip to content

Commit

Permalink
Removes Apache HTTP URIBuilder (#397)
Browse files Browse the repository at this point in the history
* removes apache http URIBuilder

* reverts to ParseException
  • Loading branch information
georgebanasios authored Nov 14, 2024
1 parent ab29102 commit d1db64a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ based on the size, format and compression of data. This will also allow users to
to enable. Default changed to 0.
- [BREAKING] Added exception to signature of ResourceAlgorithms.postToQueueWithRetries.
- [BREAKING] Removed maxConnectionsPerRoute as it was not easily provided by azure-core.
- [BREAKING] IPv6 addresses must now be enclosed in brackets ( [] ) within URLs.
- Removed the dependency on Apache HTTP client URIBuilder for URL parsing.

## [5.2.0] - 2024-08-27
### Fixed
Expand Down
19 changes: 11 additions & 8 deletions data/src/main/java/com/microsoft/azure/kusto/data/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,32 @@
import com.microsoft.azure.kusto.data.auth.TokenProviderBase;
import com.microsoft.azure.kusto.data.auth.TokenProviderFactory;
import com.microsoft.azure.kusto.data.auth.endpoints.KustoTrustedEndpoints;
import com.microsoft.azure.kusto.data.http.*;
import com.microsoft.azure.kusto.data.instrumentation.*;
import com.microsoft.azure.kusto.data.req.KustoRequest;
import com.microsoft.azure.kusto.data.req.KustoRequestContext;
import com.microsoft.azure.kusto.data.res.JsonResult;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import com.microsoft.azure.kusto.data.exceptions.ExceptionsUtils;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import com.microsoft.azure.kusto.data.http.HttpClientFactory;
import com.microsoft.azure.kusto.data.http.HttpClientProperties;
import com.microsoft.azure.kusto.data.http.HttpRequestBuilder;
import com.microsoft.azure.kusto.data.http.HttpTracing;
import com.microsoft.azure.kusto.data.http.UncloseableStream;
import com.microsoft.azure.kusto.data.instrumentation.MonitoredActivity;
import com.microsoft.azure.kusto.data.instrumentation.SupplierOneException;
import com.microsoft.azure.kusto.data.instrumentation.SupplierTwoExceptions;
import com.microsoft.azure.kusto.data.instrumentation.TraceableAttributes;
import com.microsoft.azure.kusto.data.req.KustoRequest;
import com.microsoft.azure.kusto.data.req.KustoRequestContext;
import com.microsoft.azure.kusto.data.res.JsonResult;
import org.apache.commons.lang3.StringUtils;

import org.apache.http.ParseException;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

class ClientImpl extends BaseClient {
Expand Down
30 changes: 18 additions & 12 deletions data/src/main/java/com/microsoft/azure/kusto/data/UriUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.microsoft.azure.kusto.data;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;

import java.io.File;
import java.net.URI;
Expand All @@ -25,30 +24,37 @@ public static String createClusterURLFrom(final String clusterURI) throws URISyn
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();
String clusterUri = String.format("%s://%s%s%s",
clusterUrlForParsing.getScheme(),
host,
clusterUrlForParsing.getPort() != -1 ? ":" + clusterUrlForParsing.getPort() : StringUtils.EMPTY,
path);
return new URI(clusterUri).toString();
}

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

String pathString = new URIBuilder(uri).setPath(path).build().toString();
URI baseUri = new URI(uri);

URI newUri = new URI(
baseUri.getScheme(),
baseUri.getAuthority(),
path,
baseUri.getQuery(),
baseUri.getFragment());
String pathString = newUri.toString();
if (ensureTrailingSlash) {
pathString = StringUtils.appendIfMissing(pathString, "/");
}

return pathString;
}

Expand All @@ -57,7 +63,7 @@ public static String setPathForUri(String uri, String path) throws URISyntaxExce
}

public static String appendPathToUri(String uri, String path) throws URISyntaxException {
String existing = new URIBuilder(uri).getPath();
String existing = new URI(uri).getPath();
return setPathForUri(uri, StringUtils.appendIfMissing(existing == null ? "" : existing, "/") + path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.HashMap;
import java.util.Map;

public class ClientTest {
class ClientTest {

@Test
@DisplayName("test url parsing")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ static boolean isReservedHostname(String rawUri) {
if (!uri.isAbsolute()) {
return true;
}

String authority = uri.getAuthority().toLowerCase();
boolean isIPFlag = InetAddressUtils.isIPv4Address(authority) || InetAddressUtils.isIPv6Address(authority);
boolean isIPFlag;
if (authority.startsWith("[") && authority.endsWith("]")) {
authority = authority.substring(1, authority.length() - 1);
isIPFlag = true;
} else {
isIPFlag = InetAddressUtils.isIPv4Address(authority);
}

boolean isLocalFlag = authority.contains("localhost");

return isLocalFlag || isIPFlag || authority.equalsIgnoreCase("onebox.dev.kusto.windows.net");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import com.microsoft.azure.kusto.ingest.source.StreamSourceInfo;
import com.microsoft.azure.kusto.ingest.utils.IngestionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.lang.invoke.MethodHandles;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.zip.GZIPOutputStream;

public class StreamingIngestClient extends IngestClientBase implements IngestClient {
Expand Down Expand Up @@ -66,13 +67,20 @@ public class StreamingIngestClient extends IngestClientBase implements IngestCli
this.streamingClient = streamingClient;
}

public static String generateEngineUriSuggestion(URIBuilder existingEndpoint) {
if (!existingEndpoint.getHost().toLowerCase().startsWith(IngestClientBase.INGEST_PREFIX)) {
public static String generateEngineUriSuggestion(URI existingEndpoint) throws URISyntaxException {
if (!Objects.requireNonNull(existingEndpoint.getHost()).toLowerCase().startsWith(IngestClientBase.INGEST_PREFIX)) {
throw new IllegalArgumentException("The URL is already formatted as the suggested Engine endpoint, so no suggestion can be made");
}

existingEndpoint.setHost(existingEndpoint.getHost().substring(IngestClientBase.INGEST_PREFIX.length()));
return existingEndpoint.toString();
String host = existingEndpoint.getHost().substring(IngestClientBase.INGEST_PREFIX.length());
URI newUri = new URI(
existingEndpoint.getScheme(),
host,
existingEndpoint.getPath(),
existingEndpoint.getQuery(),
existingEndpoint.getFragment());

return newUri.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpResponse;
import com.azure.core.util.Context;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -59,7 +58,6 @@
import static com.microsoft.azure.kusto.ingest.IngestClientBase.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

public class IngestClientBaseTest {
class IngestClientBaseTest {
private static Stream<Arguments> provideStringsForAutoCorrectEndpointTruePass() {
return Stream.of(
Arguments.of("https://testendpoint.dev.kusto.windows.net", "https://ingest-testendpoint.dev.kusto.windows.net"),
Expand All @@ -17,7 +17,7 @@ private static Stream<Arguments> provideStringsForAutoCorrectEndpointTruePass()
Arguments.of("https://2345:shouldwork:0425", "https://ingest-2345:shouldwork:0425"),
Arguments.of("https://376.568.1564.1564", "https://ingest-376.568.1564.1564"),
Arguments.of("https://192.168.1.1", "https://192.168.1.1"),
Arguments.of("https://2345:0425:2CA1:0000:0000:0567:5673:23b5", "https://2345:0425:2CA1:0000:0000:0567:5673:23b5"),
Arguments.of("https://[2345:0425:2CA1:0000:0000:0567:5673:23b5]", "https://[2345:0425:2CA1:0000:0000:0567:5673:23b5]"),
Arguments.of("https://127.0.0.1", "https://127.0.0.1"),
Arguments.of("https://localhost", "https://localhost"),
Arguments.of("https://onebox.dev.kusto.windows.net", "https://onebox.dev.kusto.windows.net"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ private static Stream<Arguments> provideStringsForAutoCorrectEndpointTruePass()
Arguments.of("https://2345:shouldwork:0425", "https://2345:shouldwork:0425"),
Arguments.of("https://376.568.1564.1564", "https://376.568.1564.1564"),
Arguments.of("https://192.168.1.1", "https://192.168.1.1"),
Arguments.of("https://2345:0425:2CA1:0000:0000:0567:5673:23b5", "https://[2345:0425:2ca1:0000:0000:0567:5673:23b5]"),
Arguments.of("https://[2345:0425:2ca1:0000:0000:0567:5673:23b5]", "https://[2345:0425:2ca1:0000:0000:0567:5673:23b5]"),
Arguments.of("https://127.0.0.1", "https://127.0.0.1"),
Arguments.of("https://localhost", "https://localhost"),
Arguments.of("https://onebox.dev.kusto.windows.net", "https://onebox.dev.kusto.windows.net"));
Expand All @@ -619,7 +619,7 @@ private static Stream<Arguments> provideStringsForAutoCorrectEndpointFalsePass()
Arguments.of("https://2345:shouldwork:0425", "https://2345:shouldwork:0425"),
Arguments.of("https://376.568.1564.1564", "https://376.568.1564.1564"),
Arguments.of("https://192.168.1.1", "https://192.168.1.1"),
Arguments.of("https://2345:0425:2CA1:0000:0000:0567:5673:23b5", "https://[2345:0425:2ca1:0000:0000:0567:5673:23b5]"),
Arguments.of("https://[2345:0425:2ca1:0000:0000:0567:5673:23b5]", "https://[2345:0425:2ca1:0000:0000:0567:5673:23b5]"),
Arguments.of("https://127.0.0.1", "https://127.0.0.1"),
Arguments.of("https://localhost", "https://localhost"),
Arguments.of("https://onebox.dev.kusto.windows.net", "https://onebox.dev.kusto.windows.net"));
Expand Down

0 comments on commit d1db64a

Please sign in to comment.