private HttpClient createClient()

in maven-resolver-transport-jetty/src/main/java/org/eclipse/aether/transport/jetty/JettyTransporter.java [418:545]


    private HttpClient createClient() throws RuntimeException {
        BasicAuthentication.BasicResult serverAuth = null;
        BasicAuthentication.BasicResult proxyAuth = null;
        SSLContext sslContext = null;
        BasicAuthentication basicAuthentication = null;
        try (AuthenticationContext repoAuthContext = AuthenticationContext.forRepository(session, repository)) {
            if (repoAuthContext != null) {
                sslContext = repoAuthContext.get(AuthenticationContext.SSL_CONTEXT, SSLContext.class);

                String username = repoAuthContext.get(AuthenticationContext.USERNAME);
                String password = repoAuthContext.get(AuthenticationContext.PASSWORD);

                URI uri = URI.create(repository.getUrl());
                basicAuthentication = new BasicAuthentication(uri, Authentication.ANY_REALM, username, password);
                if (preemptiveAuth || preemptivePutAuth) {
                    serverAuth = new BasicAuthentication.BasicResult(uri, HttpHeader.AUTHORIZATION, username, password);
                }
            }
        }

        if (sslContext == null) {
            try {
                if (insecure) {
                    sslContext = SSLContext.getInstance("TLS");
                    X509TrustManager tm = new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}

                        @Override
                        public void checkServerTrusted(X509Certificate[] chain, String authType) {}

                        @Override
                        public X509Certificate[] getAcceptedIssuers() {
                            return new X509Certificate[0];
                        }
                    };
                    sslContext.init(null, new X509TrustManager[] {tm}, null);
                } else {
                    sslContext = SSLContext.getDefault();
                }
            } catch (Exception e) {
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else {
                    throw new IllegalStateException("SSL Context setup failure", e);
                }
            }
        }

        SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
        sslContextFactory.setSslContext(sslContext);
        if (insecure) {
            sslContextFactory.setEndpointIdentificationAlgorithm(null);
            sslContextFactory.setHostnameVerifier((name, context) -> true);
        }

        ClientConnector clientConnector = new ClientConnector();
        clientConnector.setSslContextFactory(sslContextFactory);

        HTTP2Client http2Client = new HTTP2Client(clientConnector);
        ClientConnectionFactoryOverHTTP2.HTTP2 http2 = new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client);

        HttpClientTransportDynamic transport;
        if ("https".equalsIgnoreCase(repository.getProtocol())) {
            transport = new HttpClientTransportDynamic(
                    clientConnector, http2, HttpClientConnectionFactory.HTTP11); // HTTPS, prefer H2
        } else {
            transport = new HttpClientTransportDynamic(
                    clientConnector, HttpClientConnectionFactory.HTTP11, http2); // plaintext HTTP, H2 cannot be used
        }

        HttpClient httpClient = new HttpClient(transport);
        httpClient.setConnectTimeout(connectTimeout);
        httpClient.setIdleTimeout(requestTimeout);
        httpClient.setFollowRedirects(ConfigUtils.getBoolean(
                session,
                JettyTransporterConfigurationKeys.DEFAULT_FOLLOW_REDIRECTS,
                JettyTransporterConfigurationKeys.CONFIG_PROP_FOLLOW_REDIRECTS));
        httpClient.setMaxRedirects(ConfigUtils.getInteger(
                session,
                JettyTransporterConfigurationKeys.DEFAULT_MAX_REDIRECTS,
                JettyTransporterConfigurationKeys.CONFIG_PROP_MAX_REDIRECTS));

        httpClient.setUserAgentField(null); // we manage it

        if (basicAuthentication != null) {
            httpClient.getAuthenticationStore().addAuthentication(basicAuthentication);
        }

        if (repository.getProxy() != null) {
            HttpProxy proxy = new HttpProxy(
                    repository.getProxy().getHost(), repository.getProxy().getPort());

            httpClient.getProxyConfiguration().addProxy(proxy);
            try (AuthenticationContext proxyAuthContext = AuthenticationContext.forProxy(session, repository)) {
                if (proxyAuthContext != null) {
                    String username = proxyAuthContext.get(AuthenticationContext.USERNAME);
                    String password = proxyAuthContext.get(AuthenticationContext.PASSWORD);

                    BasicAuthentication proxyAuthentication =
                            new BasicAuthentication(proxy.getURI(), Authentication.ANY_REALM, username, password);

                    httpClient.getAuthenticationStore().addAuthentication(proxyAuthentication);
                    if (preemptiveAuth || preemptivePutAuth) {
                        proxyAuth = new BasicAuthentication.BasicResult(
                                proxy.getURI(), HttpHeader.PROXY_AUTHORIZATION, username, password);
                    }
                }
            }
        }
        if (serverAuth != null) {
            this.basicServerAuthenticationResult.set(serverAuth);
        }
        if (proxyAuth != null) {
            this.basicProxyAuthenticationResult.set(proxyAuth);
        }

        try {
            httpClient.start();
            return httpClient;
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new IllegalStateException("Jetty client start failure", e);
            }
        }
    }