Skip to content

Commit af1158e

Browse files
committed
try to add Apache HttpClient version to UserAgent
1 parent e4a95c5 commit af1158e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

commercetools/commercetools-apachehttp-client/src/main/java/com/commercetools/http/apachehttp/CtApacheHttpClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
import org.apache.commons.io.IOUtils;
1818
import org.apache.commons.io.input.AutoCloseInputStream;
19+
import org.apache.hc.client5.http.async.HttpAsyncClient;
1920
import org.apache.hc.client5.http.async.methods.SimpleBody;
2021
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
2122
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
23+
import org.apache.hc.client5.http.classic.HttpClient;
2224
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
2325
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
2426
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
@@ -33,6 +35,7 @@
3335
import org.apache.hc.core5.http2.HttpVersionPolicy;
3436
import org.apache.hc.core5.reactor.IOReactorStatus;
3537
import org.apache.hc.core5.reactor.ssl.TlsDetails;
38+
import org.apache.hc.core5.util.VersionInfo;
3639

3740
public class CtApacheHttpClient extends HttpClientBase {
3841
public static final int MAX_REQUESTS = 64;
@@ -68,6 +71,10 @@ private void init() {
6871
}
6972
}
7073

74+
public static String clientVersion() {
75+
return "ApacheHttpAsyncClient/" + VersionInfo.loadVersionInfo("org.apache.hc.client5", HttpAsyncClient.class.getClassLoader()).getRelease();
76+
}
77+
7178
public CtApacheHttpClient() {
7279
super();
7380
apacheHttpClient = clientBuilder.get().build();

commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/MiddlewareTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import java.util.concurrent.ExecutionException;
99
import java.util.concurrent.atomic.AtomicInteger;
1010

11+
import com.commercetools.api.client.ApiRoot;
1112
import com.commercetools.api.client.ProjectApiRoot;
1213
import com.commercetools.api.defaultconfig.ApiRootBuilder;
1314
import com.commercetools.api.defaultconfig.ServiceRegion;
1415
import com.commercetools.api.models.category.Category;
1516
import com.commercetools.api.models.category.CategoryDraftBuilder;
1617
import com.commercetools.api.models.project.Project;
18+
import com.commercetools.http.apachehttp.CtApacheHttpClient;
1719
import commercetools.utils.CommercetoolsTestUtils;
1820

1921
import io.vrap.rmf.base.client.*;
@@ -111,4 +113,16 @@ public CompletableFuture<ApiHttpResponse<byte[]>> execute(ApiHttpRequest request
111113
ApiHttpResponse<Project> response = client.execute(request, Project.class).get();
112114
Assertions.assertThat(response).isNotNull();
113115
}
116+
117+
@Test
118+
public void testApacheUserAgent() {
119+
ClientCredentials credentials = ClientCredentials.of()
120+
.withClientId(CommercetoolsTestUtils.getClientId())
121+
.withClientSecret(CommercetoolsTestUtils.getClientSecret())
122+
.build();
123+
ProjectApiRoot root = ApiRootBuilder.of(new CtApacheHttpClient())
124+
.defaultClient(credentials, ServiceRegion.GCP_EUROPE_WEST1)
125+
.build(CommercetoolsTestUtils.getProjectKey());
126+
root.get().executeBlocking();
127+
}
114128
}

rmf/rmf-java-base/src/main/java/io/vrap/rmf/base/client/ClientBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import static java.util.Objects.requireNonNull;
55

6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
68
import java.net.URI;
79
import java.util.*;
810
import java.util.concurrent.ExecutorService;
@@ -737,6 +739,14 @@ public static String buildDefaultUserAgent() {
737739
String osName = SystemUtils.OS_NAME;
738740
String osArch = SystemUtils.OS_ARCH;
739741
String sdkVersion = BuildInfo.VERSION;
740-
return userAgent + sdkVersion + " " + " Java/" + runtimeVersion + " (" + osName + "; " + osArch + ")";
742+
String httpClient = "";
743+
try {
744+
Class<?> clazz = Class.forName("com.commercetools.http.apachehttp.CtApacheHttpClient");
745+
Method method = clazz.getMethod("clientVersion");
746+
httpClient = " " + method.invoke(null).toString();
747+
}
748+
catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) {
749+
}
750+
return userAgent + sdkVersion + " " + " Java/" + runtimeVersion + " (" + osName + "; " + osArch + ")" + httpClient;
741751
}
742752
}

0 commit comments

Comments
 (0)