Skip to content

Commit

Permalink
[CALCITE-5218] Verify HTTP client class before instantiating it
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenada committed Jul 26, 2022
1 parent aad227f commit 0c097b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,23 @@ public static AvaticaHttpClientFactoryImpl getInstance() {
}

private AvaticaHttpClient instantiateClient(String className, URL url) {
AvaticaHttpClient client = null;
Exception clientCreationException = null;
try {
Class<?> clz = Class.forName(className);
Constructor<?> constructor = clz.getConstructor(URL.class);
Object instance = constructor.newInstance(Objects.requireNonNull(url));
return AvaticaHttpClient.class.cast(instance);
// Ensure that the given class is actually a subclass of AvaticaHttpClient
Class<? extends AvaticaHttpClient> clz =
Class.forName(className).asSubclass(AvaticaHttpClient.class);
Constructor<? extends AvaticaHttpClient> constructor = clz.getConstructor(URL.class);
client = constructor.newInstance(Objects.requireNonNull(url));
} catch (Exception e) {
clientCreationException = e;
}

if (client == null) {
throw new RuntimeException("Failed to construct AvaticaHttpClient implementation "
+ className, e);
+ className, clientCreationException);
} else {
return client;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class AvaticaHttpClientFactoryTest {
client instanceof AvaticaCommonsHttpClientImpl);
}

@Test public void testOverridenHttpClient() throws Exception {
@Test public void testOverriddenHttpClient() throws Exception {
Properties props = new Properties();
props.setProperty(BuiltInConnectionProperty.HTTP_CLIENT_IMPL.name(),
AvaticaHttpClientImpl.class.getName());
Expand All @@ -55,6 +55,18 @@ public class AvaticaHttpClientFactoryTest {
assertTrue("Client was an instance of " + client.getClass(),
client instanceof AvaticaHttpClientImpl);
}

@Test(expected = RuntimeException.class) public void testInvalidHttpClient() throws Exception {
Properties props = new Properties();
props.setProperty(BuiltInConnectionProperty.HTTP_CLIENT_IMPL.name(),
Properties.class.getName()); // Properties is intentionally *not* a valid class
URL url = new URL("http://localhost:8765");
ConnectionConfig config = new ConnectionConfigImpl(props);
AvaticaHttpClientFactory httpClientFactory = new AvaticaHttpClientFactoryImpl();

// This should throw since the Properties class is invalid
httpClientFactory.getClient(url, config, null);
}
}

// End AvaticaHttpClientFactoryTest.java

0 comments on commit 0c097b6

Please sign in to comment.