Showing posts with label Proxy. Show all posts
Showing posts with label Proxy. Show all posts

Wednesday, October 25, 2017

Enterprise Application Architecture Design Patterns - Facade vs Gateway vs Proxy

Facade

an object that provides a simplified interface to a larger body of code

Gateway

An object that encapsulates access to an external system or resource.

Proxy

a wrapper or agent object that is being called by the client to access the real serving object behind the scenes

The three patterns are similar in a way that it used to simplify the access to more complicated services, systems or resources etc.

The differences are:

Proxy is usually expose same interface as the real object it represents, it usually locates at the client side and communicates with the real object remotely.

Gateway handles more complicated situations where external systems, resources etc. could be represented as a simplified form. The goal is to encapsulate the implementation details and complexities of external services, libraries, systems, resources etc.

Facade is the opposite of the Gateway pattern, it provides a simplified interface to the objects you own.

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