From ba733cf34270510bdc6a6acf6a4d981a09f09cba Mon Sep 17 00:00:00 2001 From: Gabriel Roldan Date: Fri, 3 May 2024 17:59:28 -0300 Subject: [PATCH] Fix SpringEnvironmentAwareGeoToolsHttpClientFactory registration Fix SpringEnvironmentAwareGeoToolsHttpClientFactory registration as the default `HTTPClient` factory using the `Class` object as value instead of the qualified class name when doing `Hints.putSystemDefault( Hints.HTTP_CLIENT_FACTORY, )` in `GeoToolsStaticContextInitializer`. It doesn't work with the class name after changes upstream. --- .../GeoToolsStaticContextInitializer.java | 10 ++++------ .../GeoToolsHttpClientAutoConfigurationTest.java | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/catalog/backends/common/src/main/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsStaticContextInitializer.java b/src/catalog/backends/common/src/main/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsStaticContextInitializer.java index fc2c53161..2cb8cf98b 100644 --- a/src/catalog/backends/common/src/main/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsStaticContextInitializer.java +++ b/src/catalog/backends/common/src/main/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsStaticContextInitializer.java @@ -30,17 +30,15 @@ public void initialize(ConfigurableApplicationContext applicationContext) { } System.setProperty("org.geotools.referencing.forceXY", "true"); - Boolean useEnvAwareHttpClient = + boolean useEnvAwareHttpClient = applicationContext .getEnvironment() .getProperty( "geotools.httpclient.proxy.enabled", Boolean.class, Boolean.TRUE); if (useEnvAwareHttpClient) { - // factoryName matches the one in - // src/main/resources/META-INF/services/org.geotools.http.HTTPClientFactory - String factoryName = - SpringEnvironmentAwareGeoToolsHttpClientFactory.class.getCanonicalName(); - Hints.putSystemDefault(Hints.HTTP_CLIENT_FACTORY, factoryName); + Hints.putSystemDefault( + Hints.HTTP_CLIENT_FACTORY, + SpringEnvironmentAwareGeoToolsHttpClientFactory.class); } } } diff --git a/src/catalog/backends/common/src/test/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsHttpClientAutoConfigurationTest.java b/src/catalog/backends/common/src/test/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsHttpClientAutoConfigurationTest.java index 85304f437..6d209911c 100644 --- a/src/catalog/backends/common/src/test/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsHttpClientAutoConfigurationTest.java +++ b/src/catalog/backends/common/src/test/java/org/geoserver/cloud/autoconfigure/geotools/GeoToolsHttpClientAutoConfigurationTest.java @@ -7,6 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNull; +import org.geotools.http.HTTPClient; +import org.geotools.http.HTTPClientFinder; import org.geotools.util.factory.Hints; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -44,6 +46,16 @@ void enabledByDefault() { context.getBean( GeoToolsHttpClientProxyConfigurationProperties.class)) .hasFieldOrPropertyWithValue("enabled", true); + + HTTPClient client = HTTPClientFinder.createClient(); + assertThat(client) + .as( + """ + Expected SpringEnvironmentAwareGeoToolsHttpClient \ + after GeoToolsStaticContextInitializer sets \ + SpringEnvironmentAwareGeoToolsHttpClientFactory as the default factory + """) + .isInstanceOf(SpringEnvironmentAwareGeoToolsHttpClient.class); }); } @@ -56,8 +68,7 @@ void testInitializerSetsForceXYSystemProperty() { @Test void testInitializerSetsHttpClientFactorySystemProperty() { - final String expected = - SpringEnvironmentAwareGeoToolsHttpClientFactory.class.getCanonicalName(); + final var expected = SpringEnvironmentAwareGeoToolsHttpClientFactory.class; assertNull(Hints.getSystemDefault(Hints.HTTP_CLIENT_FACTORY)); runner.run(