Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes apache http libraries and replace with azure core httpclient #335

Merged
merged 55 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
73b9311
initial commit removing apache http libraries
Dec 10, 2023
9d35246
updated MSAL wrapper class to include headers in response
Dec 10, 2023
cd5a5a5
Added TestHttpResponse class to replace BasicHttpResponse in Tests
Dec 10, 2023
73754c7
Added KustoParseException to replace ParseException from old Apache C…
Dec 10, 2023
989019f
removed todo regarding ParseException
Dec 10, 2023
981a45c
removed EofSensorInputStream
Dec 10, 2023
00001b8
added fixme to replace EofSensorInputStream
Dec 10, 2023
f7b7ce5
Return of the Apache
Dec 13, 2023
2f66eb2
Fixed UtilitiesTest
Dec 13, 2023
3a61d30
return UriBuilder to UriUtils
Dec 13, 2023
18555a9
add proxy options back to HttpClientFactory
Dec 13, 2023
d7e9b2b
ran formatter
Dec 13, 2023
7b7efea
closeable and fix build
ohbitton Dec 13, 2023
3429dee
fix test
ohbitton Dec 13, 2023
5fc9830
format
ohbitton Dec 13, 2023
0cc1c22
removed extra throws
The-Funk Dec 14, 2023
d0cada0
Per Ohad review comments:
Dec 22, 2023
2ab94f3
removed caught exceptions that were rethrown with no changes/side eff…
Dec 22, 2023
245185b
removes block on body by using BinaryData
Dec 23, 2023
13d2cd6
ran formatter
Dec 23, 2023
e9a0aa6
added Tracing object and builder to shorten method signatures
Dec 23, 2023
56cb44c
puts old URIBuilder back in place
The-Funk Jan 17, 2024
83f7ff9
Merge branch 'master' into remove_apache_http_libraries
The-Funk Jan 17, 2024
c1705a7
Merge branch 'master' into remove_apache_http_libraries
The-Funk Jan 18, 2024
3591aae
update readme with ProxyOptions
The-Funk Jan 18, 2024
66bc361
aligns with PR 342
The-Funk Jan 22, 2024
60e4b1d
Merge branch 'master' into remove_apache_http_libraries
The-Funk Jan 22, 2024
e22f38c
Merge branch 'master' into remove_apache_http_libraries
The-Funk Feb 1, 2024
0698dbd
Merge branch 'master' into remove_apache_http_libraries
The-Funk Feb 2, 2024
04bb268
fix issue with Closeable
The-Funk Feb 2, 2024
0755bd2
ran formatter
The-Funk Feb 2, 2024
4ef99c9
Fix sync HttpWrapper implementation
ohbitton Feb 7, 2024
e9a9c62
Merge branch 'master' into remove_apache_http_libraries
ohadbitt Feb 7, 2024
2a37cfa
fix build
ohbitton Feb 7, 2024
16c4fe9
format
ohbitton Feb 7, 2024
e64e0ee
build
ohbitton Feb 7, 2024
495f4a6
Few bug fixes
ohbitton Feb 28, 2024
61ee771
Merge branch 'master' into remove_apache_http_libraries
The-Funk Feb 29, 2024
c25960f
readd the exclusion of commons-codec for security posture
Mar 1, 2024
abae5fb
adds response timeout and client provider to the client properties wr…
Mar 2, 2024
286bbe1
removes references to Netty
Mar 2, 2024
d9dc27c
removes extra comment
Mar 2, 2024
3349315
adds Keep-Alive back to prevent breaking change for users of Keep-Ali…
Mar 2, 2024
9370530
removes deflate algorithm from encoding header
Mar 2, 2024
350dc1b
revert removing Apache core (URIBuilder depends on utilities in core)
Mar 2, 2024
b17d8a0
remove netty from client factory
The-Funk Mar 4, 2024
df53501
Merge branch 'master' into remove_apache_http_libraries
ohadbitt Mar 7, 2024
ad12749
fix
ohbitton Mar 10, 2024
b2edfe7
Merge branch 'remove_apache_http_libraries' of https://github.com/The…
ohbitton Mar 10, 2024
767c7b5
Merge branch 'master' into remove_apache_http_libraries
ohadbitt Mar 10, 2024
54d1ec5
removes CloseParentResourcesStream.java
The-Funk Mar 11, 2024
5b6e2f7
reusable Gzip logic
Mar 14, 2024
5d3a6ed
factory now uses azure-core default http client options
Mar 14, 2024
8676c50
I always forget the formatter
The-Funk Mar 17, 2024
1f7785b
fix test
ohbitton Mar 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,9 @@
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>${httpcore.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.io.Closeable;

public interface Client extends Closeable {
public interface Client {
KustoOperationResult execute(String command) throws DataServiceException, DataClientException;

KustoOperationResult execute(String database, String command) throws DataServiceException, DataClientException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

package com.microsoft.azure.kusto.data;

import com.azure.core.http.HttpClient;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import com.microsoft.azure.kusto.data.http.HttpClientProperties;

import java.net.URISyntaxException;

Expand Down Expand Up @@ -47,7 +48,7 @@ public static Client createClient(ConnectionStringBuilder csb, HttpClientPropert
* @return a fully constructed {@linkplain Client} instance
* @throws URISyntaxException if the cluster URL is invalid
*/
public static Client createClient(ConnectionStringBuilder csb, CloseableHttpClient client) throws URISyntaxException {
public static Client createClient(ConnectionStringBuilder csb, HttpClient client) throws URISyntaxException {
return client == null ? createClient(csb, (HttpClientProperties) null) : new ClientImpl(csb, client, true);
}

Expand Down Expand Up @@ -85,7 +86,7 @@ public static StreamingClient createStreamingClient(ConnectionStringBuilder csb,
* @return a fully constructed {@linkplain StreamingClient} instance
* @throws URISyntaxException if the cluster URL is invalid
*/
public static StreamingClient createStreamingClient(ConnectionStringBuilder csb, CloseableHttpClient httpClient) throws URISyntaxException {
public static StreamingClient createStreamingClient(ConnectionStringBuilder csb, HttpClient httpClient) throws URISyntaxException {
return new ClientImpl(csb, httpClient, true);
}
}
50 changes: 22 additions & 28 deletions data/src/main/java/com/microsoft/azure/kusto/data/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@

package com.microsoft.azure.kusto.data;

import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaderName;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.azure.kusto.data.auth.CloudInfo;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
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.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import com.microsoft.azure.kusto.data.exceptions.KustoClientInvalidConnectionStringException;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import com.microsoft.azure.kusto.data.exceptions.*;
import com.microsoft.azure.kusto.data.http.HttpClientFactory;
import com.microsoft.azure.kusto.data.http.HttpClientProperties;
import com.microsoft.azure.kusto.data.http.UncloseableStream;
import com.microsoft.azure.kusto.data.http.HttpPostUtils;
import com.microsoft.azure.kusto.data.instrumentation.MonitoredActivity;
import com.microsoft.azure.kusto.data.instrumentation.SupplierTwoExceptions;
import com.microsoft.azure.kusto.data.instrumentation.TraceableAttributes;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.ParseException;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.net.URIBuilder;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand Down Expand Up @@ -56,7 +54,7 @@ class ClientImpl implements Client, StreamingClient {
private final TokenProviderBase aadAuthenticationHelper;
private final String clusterUrl;
private final ClientDetails clientDetails;
private final CloseableHttpClient httpClient;
private final HttpClient httpClient;
private final boolean leaveHttpClientOpen;
private boolean endpointValidated = false;

Expand All @@ -70,16 +68,16 @@ public ClientImpl(ConnectionStringBuilder csb, HttpClientProperties properties)
this(csb, HttpClientFactory.create(properties), false);
}

// Accepting a CloseableHttpClient so that we can create InputStream from response
public ClientImpl(ConnectionStringBuilder csb, CloseableHttpClient httpClient, boolean leaveHttpClientOpen) throws URISyntaxException {
public ClientImpl(ConnectionStringBuilder csb, HttpClient httpClient, boolean leaveHttpClientOpen) throws URISyntaxException {
URI clusterUrlForParsing = new URI(csb.getClusterUrl());
String host = clusterUrlForParsing.getHost();
Objects.requireNonNull(clusterUrlForParsing.getAuthority(), "clusterUri.authority");
String auth = clusterUrlForParsing.getAuthority().toLowerCase();
if (host == null) {
host = StringUtils.removeEndIgnoreCase(auth, FEDERATED_SECURITY_SUFFIX);
}
URIBuilder uriBuilder = new URIBuilder().setScheme(clusterUrlForParsing.getScheme())
URIBuilder uriBuilder = new URIBuilder()
.setScheme(clusterUrlForParsing.getScheme())
.setHost(host);
String path = clusterUrlForParsing.getPath();
if (path != null && !path.isEmpty()) {
Expand Down Expand Up @@ -267,15 +265,15 @@ private KustoOperationResult executeStreamingIngestImpl(String clusterEndpoint,
"KJC.executeStreamingIngest" + (isStreamSource ? "" : "FromBlob"),
CommandType.STREAMING_INGEST.getActivityTypeSuffix());
if (isStreamSource) {
headers.put(HttpHeaders.CONTENT_ENCODING, "gzip");
headers.put(HttpHeaderName.CONTENT_ENCODING.getCaseSensitiveName(), "gzip");
}

Long timeoutMs = populateHeadersAndGetTimeout(properties, headers);
try (InputStream ignored = (isStreamSource && !leaveOpen) ? stream : null) {
validateEndpoint();

// We use UncloseableStream to prevent HttpClient From closing it
AbstractHttpEntity entity = isStreamSource ? new InputStreamEntity(new UncloseableStream(stream))
AbstractHttpEntity entity = isStreamSource ? new InputStreamEntity(new UncloseableStream(stream), ContentType.DEFAULT_BINARY)
: new StringEntity(new IngestionSourceStorage(blobUrl).toString(), ContentType.APPLICATION_JSON);
String response;
// trace executeStreamingIngest
Expand Down Expand Up @@ -356,6 +354,7 @@ public InputStream executeStreamingQuery(String database, String command, Client

addCommandHeaders(headers);
String jsonPayload = generateCommandPayload(database, command, properties);
StringEntity requestEntity = new StringEntity(jsonPayload, ContentType.APPLICATION_JSON);

try {
validateEndpoint();
Expand All @@ -366,15 +365,15 @@ public InputStream executeStreamingQuery(String database, String command, Client
return MonitoredActivity.invoke(
(SupplierTwoExceptions<InputStream, DataServiceException, DataClientException>) () -> HttpPostUtils.postToStreamingOutput(httpClient,
clusterEndpoint,
jsonPayload, timeoutMs + CLIENT_SERVER_DELTA_IN_MILLISECS, headers),
requestEntity, timeoutMs + CLIENT_SERVER_DELTA_IN_MILLISECS, headers),
"ClientImpl.executeStreamingQuery", updateAndGetExecuteTracingAttributes(database, properties));
}

private long determineTimeout(ClientRequestProperties properties, CommandType commandType, String clusterUrl) throws DataClientException {
Long timeoutMs;
try {
timeoutMs = properties == null ? null : properties.getTimeoutInMilliSec();
} catch (ParseException e) {
} catch (KustoParseException e) {
throw new DataClientException(clusterUrl, "Failed to parse timeout from ClientRequestProperties");
}
if (timeoutMs == null) {
Expand Down Expand Up @@ -402,7 +401,8 @@ private Map<String, String> generateIngestAndCommandHeaders(ClientRequestPropert
Map<String, String> headers = extractTracingHeaders(properties);

if (aadAuthenticationHelper != null) {
headers.put(HttpHeaders.AUTHORIZATION, String.format("Bearer %s", aadAuthenticationHelper.acquireAccessToken()));
headers.put(HttpHeaderName.AUTHORIZATION.getCaseSensitiveName(),
String.format("Bearer %s", aadAuthenticationHelper.acquireAccessToken()));
}

String clientRequestId;
Expand Down Expand Up @@ -461,18 +461,12 @@ private String generateCommandPayload(String database, String command, ClientReq
}

private void addCommandHeaders(Map<String, String> headers) {
headers.put(HttpHeaders.CONTENT_TYPE, "application/json");
headers.put(HttpHeaderName.CONTENT_TYPE.getCaseSensitiveName(), "application/json");
headers.put("Fed", "True");
}

public String getClusterUrl() {
return clusterUrl;
}

@Override
public void close() throws IOException {
if (!leaveHttpClientOpen) {
httpClient.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.azure.kusto.data.exceptions.KustoParseException;
import com.microsoft.azure.kusto.data.format.CslBoolFormat;
import com.microsoft.azure.kusto.data.format.CslDateTimeFormat;
import com.microsoft.azure.kusto.data.format.CslIntFormat;
import com.microsoft.azure.kusto.data.format.CslLongFormat;
import com.microsoft.azure.kusto.data.format.CslRealFormat;
import com.microsoft.azure.kusto.data.format.CslTimespanFormat;
import com.microsoft.azure.kusto.data.format.CslUuidFormat;
import com.microsoft.azure.kusto.data.http.HttpPostUtils;
import com.microsoft.azure.kusto.data.instrumentation.TraceableAttributes;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.ParseException;

import java.io.Serializable;
import java.time.Duration;
Expand Down Expand Up @@ -152,7 +151,7 @@ public void clearParameters() {
parameters.clear();
}

public Long getTimeoutInMilliSec() throws ParseException {
public Long getTimeoutInMilliSec() {
Object timeoutObj = getOption(OPTION_SERVER_TIMEOUT);
Long timeout = null;
if (timeoutObj instanceof Long) {
Expand All @@ -166,10 +165,10 @@ public Long getTimeoutInMilliSec() throws ParseException {
return timeout;
}

private long parseTimeoutFromTimespanString(String str) throws ParseException {
private long parseTimeoutFromTimespanString(String str) {
Matcher matcher = KUSTO_TIMESPAN_REGEX.matcher(str);
if (!matcher.matches()) {
throw new ParseException(String.format("Failed to parse timeout string as a timespan. Value: '%s'", str));
throw new KustoParseException(String.format("Failed to parse timeout string as a timespan. Value: '%s'", str));
}

if ("-".equals(matcher.group(1))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.microsoft.azure.kusto.data;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.kusto.data.http.HttpPostUtils;

class IngestionSourceStorage {
public String sourceUri;
Expand All @@ -12,9 +10,8 @@ public IngestionSourceStorage(String uri) {
}

public String toString() {
ObjectMapper objectMapper = Utils.getObjectMapper();
try {
return objectMapper.writeValueAsString(this);
return Utils.getObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.microsoft.azure.kusto.data.exceptions.JsonPropertyMissingException;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import com.microsoft.azure.kusto.data.http.HttpPostUtils;
import com.microsoft.azure.kusto.data.instrumentation.MonitoredActivity;
import com.microsoft.azure.kusto.data.instrumentation.SupplierOneException;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;

import java.io.Closeable;
import java.io.InputStream;

public interface StreamingClient extends Closeable {
public interface StreamingClient {
/**
* <p>Ingest data from a given stream directly into Kusto database.</p>
* This method ingests the data from a given stream directly into Kusto database, using streaming ingestion endpoint,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.microsoft.azure.kusto.data;

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

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

Expand All @@ -15,7 +16,11 @@ private UriUtils() {
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();
String pathString = new URIBuilder(uri)
.setPath(path)
.build()
.toString();

if (ensureTrailingSlash) {
pathString = StringUtils.appendIfMissing(pathString, "/");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

package com.microsoft.azure.kusto.data.auth;

import com.azure.core.http.HttpClient;
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
import com.microsoft.aad.msal4j.IClientCertificate;
import com.microsoft.aad.msal4j.IConfidentialClientApplication;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

import org.apache.http.client.HttpClient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

package com.microsoft.azure.kusto.data.auth;

import com.azure.core.http.HttpClient;
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
import com.microsoft.aad.msal4j.IClientSecret;
import com.microsoft.aad.msal4j.IConfidentialClientApplication;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

import org.apache.http.client.HttpClient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package com.microsoft.azure.kusto.data.auth;

import com.azure.core.http.HttpClient;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;

import org.apache.http.client.HttpClient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.microsoft.azure.kusto.data.auth;

import org.apache.http.client.HttpClient;
import com.azure.core.http.HttpClient;

@FunctionalInterface
public interface CallbackTokenProviderFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

package com.microsoft.azure.kusto.data.auth;

import com.azure.core.http.HttpClient;
import com.microsoft.azure.kusto.data.instrumentation.SupplierTwoExceptions;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.microsoft.azure.kusto.data.instrumentation.MonitoredActivity;
import org.apache.http.client.HttpClient;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
Loading