Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import javax.net.ssl.SSLContext;
import java.lang.reflect.Field;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class ApacheSetSslSocketFactoryAdvice {
Expand All @@ -17,7 +16,7 @@ public static void beforeCreateSocket(@Advice.This Object thisFactory) throws Ex
for (String factoryFieldName : Arrays.asList("socketfactory", "socketFactory")) {
try {
// Detect which field(s) are present on this class
Field field = thisFactory.getClass().getDeclaredField(factoryFieldName);
Field field = getDeclaredFieldInClassTree(thisFactory.getClass(), factoryFieldName);

// Allow ourselves to change the socket factory value
field.setAccessible(true);
Expand All @@ -32,4 +31,14 @@ public static void beforeCreateSocket(@Advice.This Object thisFactory) throws Ex
throw new IllegalStateException("Apache HttpClient interception setup failed");
}
}

public static Field getDeclaredFieldInClassTree(Class<?> type, String fieldName) throws NoSuchFieldException {
for (Class<?> clazz = type; clazz != null; clazz = clazz.getSuperclass()) {
try {
return clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException ignored) { }
}
throw new NoSuchFieldException();
}

}
2 changes: 2 additions & 0 deletions test-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies {

implementation group: 'io.vertx', name: 'vertx-core', version: '4.2.2'
implementation group: 'io.vertx', name: 'vertx-web-client', version: '4.2.2'

implementation group: 'org.jboss.resteasy', name: 'resteasy-client', version: '4.7.4.Final'
}

test {
Expand Down
1 change: 1 addition & 0 deletions test-app/src/main/java/tech/httptoolkit/testapp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Main {
private static final Map<String, ClientCase<?>> cases = Map.ofEntries(
entry("apache-v3", new ApacheHttpClientV3Case()),
entry("apache-v4", new ApacheHttpClientV4Case()),
entry("rest-easy-with-apache-v4", new RestEasyWithApacheHttpClientV4Case()),
entry("apache-v5", new ApacheHttpClientV5Case()),
entry("apache-async-v4", new ApacheHttpAsyncClientV4Case()),
entry("apache-async-v5", new ApacheHttpAsyncClientV5Case()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tech.httptoolkit.testapp.cases;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.engines.ClientHttpEngineBuilder43;
import org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.security.NoSuchAlgorithmException;

public class RestEasyWithApacheHttpClientV4Case extends ClientCase<ResteasyClient> {

@Override
public ResteasyClient newClient(String url) throws MalformedURLException {
ResteasyClientBuilderImpl resteasyClientBuilder = new ResteasyClientBuilderImpl();
resteasyClientBuilder.sslContext(getSslContext());
resteasyClientBuilder.httpEngine(new ClientHttpEngineBuilder43()
.resteasyClientBuilder(resteasyClientBuilder).build());
return resteasyClientBuilder.build();
}

@Override
public int test(String url, ResteasyClient resteasyClient) throws IOException {
return resteasyClient.target(URI.create(url)).request().get().getStatus();
}

private SSLContext getSslContext() {
try {
return SSLContext.getDefault();
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
}