Skip to content

Commit

Permalink
try to add Apache HttpClient version to UserAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Jan 22, 2025
1 parent e4a95c5 commit af1158e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.apache.hc.client5.http.async.HttpAsyncClient;
import org.apache.hc.client5.http.async.methods.SimpleBody;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
Expand All @@ -33,6 +35,7 @@
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.reactor.IOReactorStatus;
import org.apache.hc.core5.reactor.ssl.TlsDetails;
import org.apache.hc.core5.util.VersionInfo;

public class CtApacheHttpClient extends HttpClientBase {
public static final int MAX_REQUESTS = 64;
Expand Down Expand Up @@ -68,6 +71,10 @@ private void init() {
}
}

public static String clientVersion() {
return "ApacheHttpAsyncClient/" + VersionInfo.loadVersionInfo("org.apache.hc.client5", HttpAsyncClient.class.getClassLoader()).getRelease();
}

public CtApacheHttpClient() {
super();
apacheHttpClient = clientBuilder.get().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

import com.commercetools.api.client.ApiRoot;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.defaultconfig.ApiRootBuilder;
import com.commercetools.api.defaultconfig.ServiceRegion;
import com.commercetools.api.models.category.Category;
import com.commercetools.api.models.category.CategoryDraftBuilder;
import com.commercetools.api.models.project.Project;
import com.commercetools.http.apachehttp.CtApacheHttpClient;
import commercetools.utils.CommercetoolsTestUtils;

import io.vrap.rmf.base.client.*;
Expand Down Expand Up @@ -111,4 +113,16 @@ public CompletableFuture<ApiHttpResponse<byte[]>> execute(ApiHttpRequest request
ApiHttpResponse<Project> response = client.execute(request, Project.class).get();
Assertions.assertThat(response).isNotNull();
}

@Test
public void testApacheUserAgent() {
ClientCredentials credentials = ClientCredentials.of()
.withClientId(CommercetoolsTestUtils.getClientId())
.withClientSecret(CommercetoolsTestUtils.getClientSecret())
.build();
ProjectApiRoot root = ApiRootBuilder.of(new CtApacheHttpClient())
.defaultClient(credentials, ServiceRegion.GCP_EUROPE_WEST1)
.build(CommercetoolsTestUtils.getProjectKey());
root.get().executeBlocking();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import static java.util.Objects.requireNonNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.*;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -737,6 +739,14 @@ public static String buildDefaultUserAgent() {
String osName = SystemUtils.OS_NAME;
String osArch = SystemUtils.OS_ARCH;
String sdkVersion = BuildInfo.VERSION;
return userAgent + sdkVersion + " " + " Java/" + runtimeVersion + " (" + osName + "; " + osArch + ")";
String httpClient = "";
try {
Class<?> clazz = Class.forName("com.commercetools.http.apachehttp.CtApacheHttpClient");
Method method = clazz.getMethod("clientVersion");
httpClient = " " + method.invoke(null).toString();
}
catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) {
}
return userAgent + sdkVersion + " " + " Java/" + runtimeVersion + " (" + osName + "; " + osArch + ")" + httpClient;
}
}

0 comments on commit af1158e

Please sign in to comment.