[HTTPCLIENT-2044] Don't create stateless/immutable factory objects

Decide later if some of these classes should have a private default
constructor.

This closes #195
This commit is contained in:
Gary Gregory 2019-12-31 11:08:35 -05:00 committed by Michael Osipov
parent 711672a62f
commit 3fd383f870
14 changed files with 70 additions and 45 deletions

View File

@ -67,10 +67,10 @@ public class WinHttpClients {
private static HttpClientBuilder createBuilder() {
if (isWinAuthAvailable()) {
final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>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);

View File

@ -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) {

View File

@ -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) {

View File

@ -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<AuthSchemeFactory> authSchemeRegistryCopy = this.authSchemeRegistry;
if (authSchemeRegistryCopy == null) {
authSchemeRegistryCopy = RegistryBuilder.<AuthSchemeFactory>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<CookieSpecFactory> cookieSpecRegistryCopy = this.cookieSpecRegistry;

View File

@ -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<AuthSchemeFactory> authSchemeRegistryCopy = this.authSchemeRegistry;
if (authSchemeRegistryCopy == null) {
authSchemeRegistryCopy = RegistryBuilder.<AuthSchemeFactory>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<CookieSpecFactory> cookieSpecRegistryCopy = this.cookieSpecRegistry;

View File

@ -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;
/**

View File

@ -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();

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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<AuthSchemeFactory> authSchemeRegistryCopy = this.authSchemeRegistry;
if (authSchemeRegistryCopy == null) {
authSchemeRegistryCopy = RegistryBuilder.<AuthSchemeFactory>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<CookieSpecFactory> cookieSpecRegistryCopy = this.cookieSpecRegistry;

View File

@ -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.<AuthSchemeFactory>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();
}

View File

@ -109,8 +109,8 @@ public class TestAuthenticationStrategy {
new BasicNameValuePair("realm", "realm3")));
final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>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<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>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);

View File

@ -94,9 +94,9 @@ public class TestHttpAuthenticator {
this.credentialsProvider = Mockito.mock(CredentialsProvider.class);
this.context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
this.authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>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);