Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a mechanism for easily configuring global client HTTP request factory settings #36266

Open
wilkinsona opened this issue Jul 6, 2023 · 6 comments
Labels
status: pending-design-work Needs design work before any code can be developed theme: http-client-config Issues related to configuration of HTTP clients type: enhancement A general enhancement
Milestone

Comments

@wilkinsona
Copy link
Member

We use ClientHttpRequestFactorySettings when creating RestTemplateBuilder, RestClient.Builder, and indirectly TestRestTemplate. It would be useful to have a mechanism for changing the settings for each of these three usages in a single place, perhaps by using configuration properties. In addition to those settings, it should also be possible specify the type of ClientHttpRequestFactory to use. This would be useful when you want to override the class path-based auto-detection.

@wilkinsona wilkinsona added the type: enhancement A general enhancement label Jul 6, 2023
@wilkinsona wilkinsona added this to the 3.x milestone Jul 6, 2023
@wilkinsona
Copy link
Member Author

wilkinsona commented Jul 6, 2023

We think that the ability to easily specify the ClientHttpRequestFactory is a prerequisite for adding the new JdkClientHttpRequestFactory to the auto-detection due to it always being available yet behaving differently to SimpleClientHttpRequestFactory.

@be0x74a
Copy link

be0x74a commented Jul 9, 2023

Hi @wilkinsona, possible first-timer contribution here! Do you mind I pick this issue up?
Also, would it be better for the auto-configuration to provide a bean for the ClientHttpRequestFactorySettings with the added field to define the ClientHttpRequestFactory to use or to provide the ClientHttpRequestFactory itself and maintain the ClientHttpRequestFactorySettings structure as is?

@wilkinsona wilkinsona added the status: pending-design-work Needs design work before any code can be developed label Jul 9, 2023
@wilkinsona
Copy link
Member Author

Thanks for the offer, @be0x74a, but we're not exactly sure what we want to do here just yet. I probably should have labelled the issue as pending design work when I opened it. I've done that now.

@be0x74a
Copy link

be0x74a commented Jul 9, 2023

Ah alright, thanks for letting me know @wilkinsona! Will keep an eye on it, for when it gets decided

@spencergibb
Copy link
Member

I did something similar to this in the new Spring Cloud Gateway MVC Server

        GatewayMvcProperties.HttpClient properties = gatewayMvcProperties.getHttpClient();


        SslBundle sslBundle = null;
        if (StringUtils.hasText(properties.getSslBundle())) {
            sslBundle = sslBundles.getBundle(properties.getSslBundle());
        }
        ClientHttpRequestFactorySettings settings = new ClientHttpRequestFactorySettings(properties.getConnectTimeout(),
                properties.getReadTimeout(), sslBundle);


        if (properties.getType() == GatewayMvcProperties.HttpClientType.JDK) {
            // TODO: customize restricted headers
            String restrictedHeaders = System.getProperty("jdk.httpclient.allowRestrictedHeaders");
            if (!StringUtils.hasText(restrictedHeaders)) {
                System.setProperty("jdk.httpclient.allowRestrictedHeaders", "host");
            }
            else if (StringUtils.hasText(restrictedHeaders) && !restrictedHeaders.contains("host")) {
                System.setProperty("jdk.httpclient.allowRestrictedHeaders", restrictedHeaders + ",host");
            }


            return ClientHttpRequestFactories.get(JdkClientHttpRequestFactory.class, settings);
        }


        // Autodetect
        return ClientHttpRequestFactories.get(settings);

https://github.com/spring-cloud/spring-cloud-gateway/blob/5202c773fd796e7f5efbeea461854bfb91e561f6/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java#L73-L96

@sephiroth-j
Copy link

sephiroth-j commented Sep 23, 2023

I stumbled upon this issue because I was wondering why ClientHttpRequestFactories.get(ClientHttpRequestFactorySettings) still returns SimpleClientHttpRequestFactory instead of JdkClientHttpRequestFactory by default. And I just wanted to mention that ClientHttpRequestFactories is also used in HttpWebServiceMessageSenderBuilder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed theme: http-client-config Issues related to configuration of HTTP clients type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants