package org.jboss.resteasy.client.jaxrs.internal;
import org.apache.http.HttpHost;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.jboss.resteasy.client.jaxrs.ClientHttpEngine;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpAsyncClient4Engine;
import org.jboss.resteasy.client.jaxrs.engines.ClientHttpEngineBuilder43;
import org.jboss.resteasy.client.jaxrs.i18n.LogMessages;
import org.jboss.resteasy.client.jaxrs.i18n.Messages;
import org.jboss.resteasy.client.jaxrs.spi.ClientConfigProvider;
import org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryDelegate;
import org.jboss.resteasy.plugins.interceptors.AcceptEncodingGZIPFilter;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.ws.rs.Priorities;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.core.Configuration;
import java.security.AccessController;
import java.security.KeyStore;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ResteasyClientBuilderImpl extends ResteasyClientBuilder
{
protected KeyStore truststore;
protected KeyStore clientKeyStore;
protected String clientPrivateKeyPassword;
protected boolean disableTrustManager;
protected HostnameVerificationPolicy policy = HostnameVerificationPolicy.WILDCARD;
protected ResteasyProviderFactory providerFactory;
protected ExecutorService asyncExecutor;
protected ScheduledExecutorService scheduledExecutorService;
protected boolean cleanupExecutor;
protected SSLContext sslContext;
protected Map<String, Object> properties = new HashMap<String, Object>();
protected ClientHttpEngine httpEngine;
protected int connectionPoolSize = 50;
protected int maxPooledPerRoute = 0;
protected long connectionTTL = -1;
protected TimeUnit connectionTTLUnit = TimeUnit.MILLISECONDS;
protected long socketTimeout = -1;
protected TimeUnit socketTimeoutUnits = TimeUnit.MILLISECONDS;
protected long establishConnectionTimeout = -1;
protected TimeUnit establishConnectionTimeoutUnits = TimeUnit.MILLISECONDS;
protected int connectionCheckoutTimeoutMs = -1;
protected HostnameVerifier verifier = null;
protected HttpHost defaultProxy;
protected int responseBufferSize;
protected List<String> sniHostNames = new ArrayList<>();
protected boolean trustSelfSignedCertificates = true;
protected boolean cookieManagementEnabled;
protected boolean disableAutomaticRetries = false;
protected boolean followRedirects;
static ResteasyProviderFactory PROVIDER_FACTORY;
public static void setProviderFactory(ResteasyProviderFactory providerFactory) {
PROVIDER_FACTORY = providerFactory;
}
public ResteasyClientBuilderImpl() {
if (PROVIDER_FACTORY != null) {
ResteasyProviderFactory localProviderFactory = new LocalResteasyProviderFactory(PROVIDER_FACTORY);
if (ResteasyProviderFactory.peekInstance() != null) {
localProviderFactory.initializeClientProviders(ResteasyProviderFactory.getInstance());
}
providerFactory = localProviderFactory;
}
}
@Override
public ResteasyClientBuilderImpl providerFactory(ResteasyProviderFactory providerFactory)
{
if (providerFactory instanceof LocalResteasyProviderFactory)
{
this.providerFactory = providerFactory;
}
else
{
this.providerFactory = new ResteasyProviderFactoryDelegate(providerFactory)
{
@Override
public javax.ws.rs.RuntimeType getRuntimeType()
{
return RuntimeType.CLIENT;
}
};
}
return this;
}
@Deprecated
public ResteasyClientBuilderImpl asyncExecutor(ExecutorService asyncExecutor)
{
return asyncExecutor(asyncExecutor, false);
}
@Deprecated
public ResteasyClientBuilderImpl asyncExecutor(ExecutorService asyncExecutor, boolean cleanupExecutor)
{
this.asyncExecutor = asyncExecutor;
this.cleanupExecutor = cleanupExecutor;
return this;
}
public ResteasyClientBuilderImpl connectionTTL(long ttl, TimeUnit unit)
{
this.connectionTTL = ttl;
this.connectionTTLUnit = unit;
return this;
}
@Override
public ResteasyClientBuilderImpl readTimeout(long timeout, TimeUnit unit)
{
this.socketTimeout = timeout;
this.socketTimeoutUnits = unit;
return this;
}
@Override
public ResteasyClientBuilderImpl connectTimeout(long timeout, TimeUnit unit)
{
this.establishConnectionTimeout = timeout;
this.establishConnectionTimeoutUnits = unit;
return this;
}
public ResteasyClientBuilderImpl maxPooledPerRoute(int maxPooledPerRoute)
{
this.maxPooledPerRoute = maxPooledPerRoute;
return this;
}
public ResteasyClientBuilderImpl connectionCheckoutTimeout(long timeout, TimeUnit unit)
{
this.connectionCheckoutTimeoutMs = (int) TimeUnit.MILLISECONDS.convert(timeout, unit);
return this;
}
public ResteasyClientBuilderImpl connectionPoolSize(int connectionPoolSize)
{
this.connectionPoolSize = connectionPoolSize;
return this;
}
public ResteasyClientBuilderImpl responseBufferSize(int size)
{
this.responseBufferSize = size;
return this;
}
public ResteasyClientBuilderImpl disableTrustManager()
{
this.disableTrustManager = true;
return this;
}
public ResteasyClientBuilderImpl hostnameVerification(HostnameVerificationPolicy policy)
{
this.policy = policy;
return this;
}
public ResteasyClientBuilderImpl httpEngine(ClientHttpEngine httpEngine)
{
this.httpEngine = httpEngine;
return this;
}
public ResteasyClientBuilderImpl useAsyncHttpEngine()
{
this.httpEngine = new ApacheHttpAsyncClient4Engine(HttpAsyncClients.createSystem(), true);
return this;
}
@Override
public ResteasyClientBuilderImpl sslContext(SSLContext sslContext)
{
this.sslContext = sslContext;
return this;
}
@Override
public ResteasyClientBuilderImpl trustStore(KeyStore truststore)
{
this.truststore = truststore;
return this;
}
@Override
public ResteasyClientBuilderImpl keyStore(KeyStore keyStore, String password)
{
this.clientKeyStore = keyStore;
this.clientPrivateKeyPassword = password;
return this;
}
@Override
public ResteasyClientBuilderImpl keyStore(KeyStore keyStore, char[] password)
{
this.clientKeyStore = keyStore;
this.clientPrivateKeyPassword = new String(password);
return this;
}
@Override
public ResteasyClientBuilderImpl property(String name, Object value)
{
getProviderFactory().property(name, value);
return this;
}
public ResteasyClientBuilderImpl sniHostNames(String... sniHostNames) {
this.sniHostNames.addAll(Arrays.asList(sniHostNames));
return this;
}
public ResteasyClientBuilderImpl defaultProxy(String hostname)
{
return defaultProxy(hostname, -1, null);
}
public ResteasyClientBuilderImpl defaultProxy(String hostname, int port)
{
return defaultProxy(hostname, port, null);
}
public ResteasyClientBuilderImpl defaultProxy(String hostname, int port, final String scheme)
{
this.defaultProxy = hostname != null ? new HttpHost(hostname, port, scheme) : null;
return this;
}
public ResteasyProviderFactory getProviderFactory()
{
if (providerFactory == null)
{
ClassLoader loader = null;
if (System.getSecurityManager() == null) {
loader = Thread.currentThread().getContextClassLoader();
}else {
try {
loader = AccessController.doPrivileged(new PrivilegedExceptionAction<ClassLoader>() {
@Override
public ClassLoader run() throws Exception {
return Thread.currentThread().getContextClassLoader();
}
});
} catch (PrivilegedActionException pae) {
throw new RuntimeException(pae);
}
}
providerFactory = new LocalResteasyProviderFactory(RegisterBuiltin.getClientInitializedResteasyProviderFactory(loader));
if (ResteasyProviderFactory.peekInstance() != null)
{
providerFactory.initializeClientProviders(ResteasyProviderFactory.getInstance());
}
if (RegisterBuiltin.isGZipEnabled()) {
providerFactory.registerProvider(AcceptEncodingGZIPFilter.class, true);
}
}
return providerFactory;
}
@Override
public ResteasyClient build()
{
ClientConfiguration config = new ClientConfiguration(getProviderFactory());
for (Map.Entry<String, Object> entry : properties.entrySet())
{
config.property(entry.getKey(), entry.getValue());
}
ExecutorService executor = asyncExecutor;
if (executor == null)
{
cleanupExecutor = true;
executor = Executors.newCachedThreadPool();
}
boolean resetProxy = false;
if (this.defaultProxy == null) {
resetProxy = true;
setProxyIfNeeded(config);
}
ClientHttpEngine engine = httpEngine != null ? httpEngine : new ClientHttpEngineBuilder43().resteasyClientBuilder(this).build();
if (resetProxy) {
this.defaultProxy = null;
}
Iterator<ClientConfigProvider> serviceLoaderIterator = ServiceLoader.load(ClientConfigProvider.class).iterator();
if (serviceLoaderIterator.hasNext()) {
config.register(new ClientConfigProviderFilter(serviceLoaderIterator.next()), Priorities.AUTHENTICATION);
}
return createResteasyClient(engine, executor, cleanupExecutor, scheduledExecutorService, config);
}
private void setProxyIfNeeded(ClientConfiguration clientConfig) {
try {
Object proxyHostProp = clientConfig.getProperty(ResteasyClientBuilder.PROPERTY_PROXY_HOST);
if (proxyHostProp != null) {
Object proxyPortProp = clientConfig.getProperty(ResteasyClientBuilder.PROPERTY_PROXY_PORT);
Integer proxyPort = -1;
if (proxyPortProp != null && proxyPortProp instanceof Number) {
proxyPort = ((Number) proxyPortProp).intValue();
} else if (proxyPortProp != null && proxyPortProp instanceof String) {
proxyPort = Integer.parseInt((String) proxyPortProp);
}
Object proxySchemeProp = clientConfig.getProperty(ResteasyClientBuilder.PROPERTY_PROXY_SCHEME);
defaultProxy((String)proxyHostProp, proxyPort, (String)proxySchemeProp);
}
} catch(Exception e) {
LogMessages.LOGGER.warn(Messages.MESSAGES.unableToSetHttpProxy(), e);
}
}
protected ResteasyClient createResteasyClient(ClientHttpEngine engine,ExecutorService executor, boolean cleanupExecutor, ScheduledExecutorService scheduledExecutorService, ClientConfiguration config ) {
return new ResteasyClientImpl(engine, executor, cleanupExecutor, scheduledExecutorService, config);
}
@Override
public ResteasyClientBuilderImpl hostnameVerifier(HostnameVerifier verifier)
{
this.verifier = verifier;
return this;
}
@Override
public Configuration getConfiguration()
{
return getProviderFactory().getConfiguration();
}
@Override
public ResteasyClientBuilderImpl register(Class<?> componentClass)
{
getProviderFactory().register(componentClass);
return this;
}
@Override
public ResteasyClientBuilderImpl register(Class<?> componentClass, int priority)
{
getProviderFactory().register(componentClass, priority);
return this;
}
@Override
public ResteasyClientBuilderImpl register(Class<?> componentClass, Class<?>... contracts)
{
getProviderFactory().register(componentClass, contracts);
return this;
}
@Override
public ResteasyClientBuilderImpl register(Class<?> componentClass, Map<Class<?>, Integer> contracts)
{
getProviderFactory().register(componentClass, contracts);
return this;
}
@Override
public ResteasyClientBuilderImpl register(Object component)
{
getProviderFactory().register(component);
return this;
}
@Override
public ResteasyClientBuilderImpl register(Object component, int priority)
{
getProviderFactory().register(component, priority);
return this;
}
@Override
public ResteasyClientBuilderImpl register(Object component, Class<?>... contracts)
{
getProviderFactory().register(component, contracts);
return this;
}
@Override
public ResteasyClientBuilderImpl register(Object component, Map<Class<?>, Integer> contracts)
{
getProviderFactory().register(component, contracts);
return this;
}
@Override
public ResteasyClientBuilderImpl withConfig(Configuration config)
{
providerFactory = new LocalResteasyProviderFactory();
providerFactory.setProperties(config.getProperties());
for (Class clazz : config.getClasses())
{
Map<Class<?>, Integer> contracts = config.getContracts(clazz);
try {
register(clazz, contracts);
}
catch (RuntimeException e) {
throw new RuntimeException(Messages.MESSAGES.failedOnRegisteringClass(clazz.getName()), e);
}
}
for (Object obj : config.getInstances())
{
Map<Class<?>, Integer> contracts = config.getContracts(obj.getClass());
register(obj, contracts);
}
return this;
}
@Override
public ResteasyClientBuilder executorService(ExecutorService executorService)
{
return asyncExecutor(executorService, false);
}
@Override
public ResteasyClientBuilder executorService(ExecutorService executorService, boolean cleanupExecutor)
{
return asyncExecutor(executorService, cleanupExecutor);
}
@Override
public ResteasyClientBuilder scheduledExecutorService(ScheduledExecutorService scheduledExecutorService)
{
this.scheduledExecutorService = scheduledExecutorService;
return this;
}
@Override
public long getConnectionTTL(TimeUnit unit)
{
return connectionTTLUnit.equals(unit) ? connectionTTL : unit.convert(connectionTTL, connectionTTLUnit);
}
@Override
public int getMaxPooledPerRoute()
{
return maxPooledPerRoute;
}
@Override
public long getConnectionCheckoutTimeout(TimeUnit unit)
{
return TimeUnit.MILLISECONDS.equals(unit) ? connectionCheckoutTimeoutMs : unit.convert(connectionCheckoutTimeoutMs, TimeUnit.MILLISECONDS);
}
@Override
public int getConnectionPoolSize()
{
return connectionPoolSize;
}
@Override
public int getResponseBufferSize()
{
return responseBufferSize;
}
@Override
public boolean isTrustManagerDisabled()
{
return disableTrustManager;
}
@Override
public boolean isTrustSelfSignedCertificates(){
return trustSelfSignedCertificates;
}
@Override
public void setIsTrustSelfSignedCertificates(boolean b){
trustSelfSignedCertificates = b;
}
@Override
public HostnameVerificationPolicy getHostnameVerification()
{
return policy;
}
@Override
public ClientHttpEngine getHttpEngine()
{
return httpEngine;
}
@Override
public boolean isUseAsyncHttpEngine()
{
return httpEngine != null && (httpEngine instanceof ApacheHttpAsyncClient4Engine);
}
@Override
public List<String> getSniHostNames()
{
return sniHostNames;
}
@Override
public String getDefaultProxyHostname()
{
return defaultProxy != null ? defaultProxy.getHostName() : null;
}
@Override
public int getDefaultProxyPort()
{
return defaultProxy != null ? defaultProxy.getPort() : -1;
}
@Override
public String getDefaultProxyScheme()
{
return defaultProxy != null ? defaultProxy.getSchemeName() : null;
}
@Override
public long getReadTimeout(TimeUnit unit)
{
return socketTimeoutUnits.equals(unit) ? socketTimeout : unit.convert(socketTimeout, socketTimeoutUnits);
}
@Override
public long getConnectionTimeout(TimeUnit unit)
{
return establishConnectionTimeoutUnits.equals(unit) ? establishConnectionTimeout : unit.convert(establishConnectionTimeout, establishConnectionTimeoutUnits);
}
@Override
public SSLContext getSSLContext()
{
return sslContext;
}
@Override
public KeyStore getKeyStore()
{
return clientKeyStore;
}
@Override
public String getKeyStorePassword()
{
return clientPrivateKeyPassword;
}
@Override
public KeyStore getTrustStore()
{
return truststore;
}
@Override
public HostnameVerifier getHostnameVerifier()
{
return verifier;
}
@Override
public ResteasyClientBuilder enableCookieManagement()
{
this.cookieManagementEnabled = true;
return this;
}
@Override
public boolean isCookieManagementEnabled()
{
return cookieManagementEnabled;
}
@Override
public ResteasyClientBuilder disableAutomaticRetries() {
this.disableAutomaticRetries = true;
return this;
}
@Override
public boolean isDisableAutomaticRetries() {
return disableAutomaticRetries;
}
@Override
public ResteasyClientBuilder setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
@Override
public boolean isFollowRedirects() {
return followRedirects;
}
}