Saturday, May 20, 2017

RestClient 2.x Customization of Proxy and Connection Timeout

If there is a need to configure proxy setting, or connection timeout setting for RestEasy client, here is the code sample.


public class RestClientUtil {
private static final Logger logger = LoggerFactory.getLogger(RestClientUtil.class);

public static String buildQueryPath(@NotNull final URI uri, final String id) {
logger.debug("uri: " + uri + ", id:" + id);
final StringBuilder finalUri = new StringBuilder(uri.toString());
if (id != null && !id.isEmpty()) {
finalUri.append("/").append(id).toString();
}

logger.debug("finalUri: " + finalUri.toString());
return finalUri.toString();
}

public static ClientRequest createClientRequest(@NotNull final String finalUri, final String proxyHost, final int proxyPort,
final boolean trustAllCerts) {
final ClientRequest request;

if (proxyHost != null) {
final DefaultHttpClient httpClient;
if (trustAllCerts == true) {
try {
httpClient = createAllTrustingClient();
} catch (GeneralSecurityException e) {
throw new IllegalStateException("failed in createAllTrustingClient", e);
}
} else {
httpClient = new DefaultHttpClient();
}

HttpParams params = setConnectionParams(httpClient);

final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
final ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor(httpClient);
request = new ClientRequest(finalUri, executor);
} else {
final DefaultHttpClient httpClient;
if (trustAllCerts == true) {
try {
httpClient = createAllTrustingClient();
} catch (GeneralSecurityException e) {
throw new IllegalStateException("failed in createAllTrustingClient", e);
}
final ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor(httpClient);
request = new ClientRequest(finalUri, executor);
} else {
request = new ClientRequest(finalUri);
}
}
return request;
}

protected static HttpParams setConnectionParams(final DefaultHttpClient httpClient) {
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
return params;
}

public static DefaultHttpClient createAllTrustingClient() throws GeneralSecurityException {
final SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

final TrustStrategy trustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};

final SSLSocketFactory factory = new SSLSocketFactory(trustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", 443, factory));

final ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);
mgr.setMaxTotal(1000);
mgr.setDefaultMaxPerRoute(1000);

final DefaultHttpClient client = new DefaultHttpClient(mgr, new DefaultHttpClient().getParams());
return client;
}
}




No comments:

Post a Comment