diff --git a/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WinHttpClients.java b/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WinHttpClients.java index 89a7af978..a8f32f639 100644 --- a/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WinHttpClients.java +++ b/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WinHttpClients.java @@ -67,10 +67,10 @@ public class WinHttpClients { private static HttpClientBuilder createBuilder() { if (isWinAuthAvailable()) { final Registry authSchemeRegistry = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()) - .register(AuthSchemes.NTLM.id, new WindowsNTLMSchemeFactory(null)) - .register(AuthSchemes.SPNEGO.id, new WindowsNegotiateSchemeFactory(null)) + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE) + .register(AuthSchemes.NTLM.id, WindowsNTLMSchemeFactory.DEFAULT) + .register(AuthSchemes.SPNEGO.id, WindowsNegotiateSchemeFactory.DEFAULT) .build(); return HttpClientBuilder.create() .setDefaultAuthSchemeRegistry(authSchemeRegistry); diff --git a/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNTLMSchemeFactory.java b/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNTLMSchemeFactory.java index cc0863237..90063bdeb 100644 --- a/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNTLMSchemeFactory.java +++ b/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNTLMSchemeFactory.java @@ -45,6 +45,11 @@ import org.apache.hc.core5.http.protocol.HttpContext; @Experimental public class WindowsNTLMSchemeFactory implements AuthSchemeFactory { + /** + * Singleton instance with a null name. + */ + public static final WindowsNTLMSchemeFactory DEFAULT = new WindowsNTLMSchemeFactory(null); + private final String servicePrincipalName; public WindowsNTLMSchemeFactory(final String servicePrincipalName) { diff --git a/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNegotiateSchemeFactory.java b/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNegotiateSchemeFactory.java index 6348fb5f0..394150b67 100644 --- a/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNegotiateSchemeFactory.java +++ b/httpclient5-win/src/main/java/org/apache/hc/client5/http/impl/win/WindowsNegotiateSchemeFactory.java @@ -45,6 +45,11 @@ import org.apache.hc.core5.http.protocol.HttpContext; @Experimental public class WindowsNegotiateSchemeFactory implements AuthSchemeFactory { + /** + * Singleton instance with a null name. + */ + public static final WindowsNegotiateSchemeFactory DEFAULT = new WindowsNegotiateSchemeFactory(null); + private final String servicePrincipalName; public WindowsNegotiateSchemeFactory(final String servicePrincipalName) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java index adeb0bca9..fa6ef7301 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java @@ -42,12 +42,10 @@ import org.apache.hc.client5.http.AuthenticationStrategy; import org.apache.hc.client5.http.DnsResolver; import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.SchemePortResolver; -import org.apache.hc.client5.http.SystemDefaultDnsResolver; import org.apache.hc.client5.http.async.AsyncExecChainHandler; import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.AuthSchemes; import org.apache.hc.client5.http.auth.CredentialsProvider; -import org.apache.hc.client5.http.auth.KerberosConfig; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.cookie.BasicCookieStore; import org.apache.hc.client5.http.cookie.CookieSpecFactory; @@ -763,13 +761,11 @@ public class H2AsyncClientBuilder { Lookup authSchemeRegistryCopy = this.authSchemeRegistry; if (authSchemeRegistryCopy == null) { authSchemeRegistryCopy = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()) - .register(AuthSchemes.NTLM.id, new NTLMSchemeFactory()) - .register(AuthSchemes.SPNEGO.id, - new SPNegoSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) - .register(AuthSchemes.KERBEROS.id, - new KerberosSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE) + .register(AuthSchemes.NTLM.id, NTLMSchemeFactory.INSTANCE) + .register(AuthSchemes.SPNEGO.id, SPNegoSchemeFactory.DEFAULT) + .register(AuthSchemes.KERBEROS.id, KerberosSchemeFactory.DEFAULT) .build(); } Lookup cookieSpecRegistryCopy = this.cookieSpecRegistry; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java index 21823e634..154737ca7 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java @@ -42,13 +42,11 @@ import org.apache.hc.client5.http.AuthenticationStrategy; import org.apache.hc.client5.http.ConnectionKeepAliveStrategy; import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.SchemePortResolver; -import org.apache.hc.client5.http.SystemDefaultDnsResolver; import org.apache.hc.client5.http.UserTokenHandler; import org.apache.hc.client5.http.async.AsyncExecChainHandler; import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.AuthSchemes; import org.apache.hc.client5.http.auth.CredentialsProvider; -import org.apache.hc.client5.http.auth.KerberosConfig; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.cookie.BasicCookieStore; import org.apache.hc.client5.http.cookie.CookieSpecFactory; @@ -970,13 +968,11 @@ public class HttpAsyncClientBuilder { Lookup authSchemeRegistryCopy = this.authSchemeRegistry; if (authSchemeRegistryCopy == null) { authSchemeRegistryCopy = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()) - .register(AuthSchemes.NTLM.id, new NTLMSchemeFactory()) - .register(AuthSchemes.SPNEGO.id, - new SPNegoSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) - .register(AuthSchemes.KERBEROS.id, - new KerberosSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE) + .register(AuthSchemes.NTLM.id, NTLMSchemeFactory.INSTANCE) + .register(AuthSchemes.SPNEGO.id, SPNegoSchemeFactory.DEFAULT) + .register(AuthSchemes.KERBEROS.id, KerberosSchemeFactory.DEFAULT) .build(); } Lookup cookieSpecRegistryCopy = this.cookieSpecRegistry; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicSchemeFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicSchemeFactory.java index 9b471c36c..b81875b50 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicSchemeFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicSchemeFactory.java @@ -44,6 +44,11 @@ import org.apache.hc.core5.http.protocol.HttpContext; @Contract(threading = ThreadingBehavior.STATELESS) public class BasicSchemeFactory implements AuthSchemeFactory { + /** + * Singleton instance with a null Charset. + */ + public static final BasicSchemeFactory INSTANCE = new BasicSchemeFactory(); + private final Charset charset; /** diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java index 7a5107414..26e789852 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java @@ -42,6 +42,11 @@ import org.apache.hc.core5.http.protocol.HttpContext; @Contract(threading = ThreadingBehavior.STATELESS) public class DigestSchemeFactory implements AuthSchemeFactory { + /** + * Singleton instance. + */ + public static final DigestSchemeFactory INSTANCE = new DigestSchemeFactory(); + @Override public AuthScheme create(final HttpContext context) { return new DigestScheme(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/KerberosSchemeFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/KerberosSchemeFactory.java index 0bb265812..9119a75de 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/KerberosSchemeFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/KerberosSchemeFactory.java @@ -27,6 +27,7 @@ package org.apache.hc.client5.http.impl.auth; import org.apache.hc.client5.http.DnsResolver; +import org.apache.hc.client5.http.SystemDefaultDnsResolver; import org.apache.hc.client5.http.auth.AuthScheme; import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.KerberosConfig; @@ -49,6 +50,12 @@ import org.apache.hc.core5.http.protocol.HttpContext; @Experimental public class KerberosSchemeFactory implements AuthSchemeFactory { + /** + * Singleton instance for the default configuration. + */ + public static final KerberosSchemeFactory DEFAULT = new KerberosSchemeFactory(KerberosConfig.DEFAULT, + SystemDefaultDnsResolver.INSTANCE); + private final KerberosConfig config; private final DnsResolver dnsResolver; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMSchemeFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMSchemeFactory.java index fd304d832..66642a59c 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMSchemeFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMSchemeFactory.java @@ -43,6 +43,11 @@ import org.apache.hc.core5.http.protocol.HttpContext; @Contract(threading = ThreadingBehavior.STATELESS) public class NTLMSchemeFactory implements AuthSchemeFactory { + /** + * Singleton instance. + */ + public static final NTLMSchemeFactory INSTANCE = new NTLMSchemeFactory(); + @Override public AuthScheme create(final HttpContext context) { return new NTLMScheme(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SPNegoSchemeFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SPNegoSchemeFactory.java index 3bbb116bb..7953c8f3b 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SPNegoSchemeFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SPNegoSchemeFactory.java @@ -27,6 +27,7 @@ package org.apache.hc.client5.http.impl.auth; import org.apache.hc.client5.http.DnsResolver; +import org.apache.hc.client5.http.SystemDefaultDnsResolver; import org.apache.hc.client5.http.auth.AuthScheme; import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.KerberosConfig; @@ -49,6 +50,12 @@ import org.apache.hc.core5.http.protocol.HttpContext; @Experimental public class SPNegoSchemeFactory implements AuthSchemeFactory { + /** + * Singleton instance for the default configuration. + */ + public static final SPNegoSchemeFactory DEFAULT = new SPNegoSchemeFactory(KerberosConfig.DEFAULT, + SystemDefaultDnsResolver.INSTANCE); + private final KerberosConfig config; private final DnsResolver dnsResolver; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java index 89197cdc6..5a3251b82 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java @@ -41,12 +41,10 @@ import org.apache.hc.client5.http.AuthenticationStrategy; import org.apache.hc.client5.http.ConnectionKeepAliveStrategy; import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.SchemePortResolver; -import org.apache.hc.client5.http.SystemDefaultDnsResolver; import org.apache.hc.client5.http.UserTokenHandler; import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.AuthSchemes; import org.apache.hc.client5.http.auth.CredentialsProvider; -import org.apache.hc.client5.http.auth.KerberosConfig; import org.apache.hc.client5.http.classic.BackoffManager; import org.apache.hc.client5.http.classic.ConnectionBackoffStrategy; import org.apache.hc.client5.http.classic.ExecChainHandler; @@ -942,11 +940,11 @@ public class HttpClientBuilder { Lookup authSchemeRegistryCopy = this.authSchemeRegistry; if (authSchemeRegistryCopy == null) { authSchemeRegistryCopy = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()) - .register(AuthSchemes.NTLM.id, new NTLMSchemeFactory()) - .register(AuthSchemes.SPNEGO.id, new SPNegoSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) - .register(AuthSchemes.KERBEROS.id, new KerberosSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE) + .register(AuthSchemes.NTLM.id, NTLMSchemeFactory.INSTANCE) + .register(AuthSchemes.SPNEGO.id, SPNegoSchemeFactory.DEFAULT) + .register(AuthSchemes.KERBEROS.id, KerberosSchemeFactory.DEFAULT) .build(); } Lookup cookieSpecRegistryCopy = this.cookieSpecRegistry; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java index 3156cd513..f70928b32 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java @@ -34,14 +34,12 @@ import org.apache.hc.client5.http.AuthenticationStrategy; import org.apache.hc.client5.http.HttpRoute; import org.apache.hc.client5.http.RouteInfo.LayerType; import org.apache.hc.client5.http.RouteInfo.TunnelType; -import org.apache.hc.client5.http.SystemDefaultDnsResolver; import org.apache.hc.client5.http.auth.AuthExchange; import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.AuthSchemes; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.ChallengeType; import org.apache.hc.client5.http.auth.Credentials; -import org.apache.hc.client5.http.auth.KerberosConfig; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy; import org.apache.hc.client5.http.impl.TunnelRefusedException; @@ -115,13 +113,11 @@ public class ProxyClient { this.authenticator = new HttpAuthenticator(); this.proxyAuthExchange = new AuthExchange(); this.authSchemeRegistry = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()) - .register(AuthSchemes.NTLM.id, new NTLMSchemeFactory()) - .register(AuthSchemes.SPNEGO.id, - new SPNegoSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) - .register(AuthSchemes.KERBEROS.id, - new KerberosSchemeFactory(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE)) + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE) + .register(AuthSchemes.NTLM.id, NTLMSchemeFactory.INSTANCE) + .register(AuthSchemes.SPNEGO.id, SPNegoSchemeFactory.DEFAULT) + .register(AuthSchemes.KERBEROS.id, KerberosSchemeFactory.DEFAULT) .build(); this.reuseStrategy = new DefaultConnectionReuseStrategy(); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java index 975c4e27e..50eac5321 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java @@ -109,8 +109,8 @@ public class TestAuthenticationStrategy { new BasicNameValuePair("realm", "realm3"))); final Registry authSchemeRegistry = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()).build(); + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE).build(); context.setAuthSchemeRegistry(authSchemeRegistry); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); @@ -143,8 +143,8 @@ public class TestAuthenticationStrategy { new BasicNameValuePair("realm", "realm2"), new BasicNameValuePair("nonce", "1234"))); final Registry authSchemeRegistry = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()).build(); + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE).build(); context.setAuthSchemeRegistry(authSchemeRegistry); context.setRequestConfig(config); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java index 2c0247bf4..edad5a0e0 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java @@ -94,9 +94,9 @@ public class TestHttpAuthenticator { this.credentialsProvider = Mockito.mock(CredentialsProvider.class); this.context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider); this.authSchemeRegistry = RegistryBuilder.create() - .register(AuthSchemes.BASIC.id, new BasicSchemeFactory()) - .register(AuthSchemes.DIGEST.id, new DigestSchemeFactory()) - .register(AuthSchemes.NTLM.id, new NTLMSchemeFactory()).build(); + .register(AuthSchemes.BASIC.id, BasicSchemeFactory.INSTANCE) + .register(AuthSchemes.DIGEST.id, DigestSchemeFactory.INSTANCE) + .register(AuthSchemes.NTLM.id, NTLMSchemeFactory.INSTANCE).build(); this.context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry); this.authCache = Mockito.mock(AuthCache.class); this.context.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);