Saturday, May 20, 2017

HttpRoutePlanner Usage Sample

HttpRoutePlanner:

From its Java doc:
Encapsulates logic to compute a HttpRoute to a target host. Implementations may for example be based on parameters, or on the standard Java system properties.
Implementations of this interface must be thread-safe. Access to shared data must be synchronized as methods of this interface may be executed from multiple threads.

Not tested yet.


import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.routing.RouteInfo.LayerType;
import org.apache.http.conn.routing.RouteInfo.TunnelType;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

public class CustomHttpRoutePlanner implements HttpRoutePlanner {

       @Override
       public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
             final String hostname = target.getHostName();
             final HttpRoute httpRoute;
             if (this.isMovedAddress(hostname)) {
                    httpRoute = new HttpRoute(target, null, new HttpHost("x.x.x.x", 9191, "https"), true, TunnelType.PLAIN,
                                 LayerType.PLAIN);
             } else {
                    httpRoute = new HttpRoute(target, null, target, true, TunnelType.PLAIN, LayerType.PLAIN);
             }
             return httpRoute;
       }

       private boolean isMovedAddress(String hostname) {
             Pattern pattern = Pattern.compile(".+odd.addr$");
             Matcher matcher = pattern.matcher(hostname);
             boolean result = matcher.matches();
             return result;
       }


       public static void main(String[] args) {
             final DefaultHttpClient defaultHttpClient = new DefaultHttpClient();

             final HttpHost proxy = new HttpHost("x.x.x.x", 9090, "https");
              defaultHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

             SchemeSocketFactory factory = null;
              try {
                     factory = new SSLSocketFactory(new SimpleTrustStrategy()); //Trus All
              } catch (GeneralSecurityException ex) {
                     Logger.error("failed to create SSL Socket Factory.", ex);
              }

             final Scheme https = new Scheme("https", 443, factory);
              defaultHttpClient.getConnectionManager().getSchemeRegistry().register(https);
             defaultHttpClient.setRoutePlanner(new CustomHttpRoutePlanner());

             final HttpGet request = new HttpGet("http://www.google.com");

             try {
                    defaultHttpClient.execute(request);
             } catch (ClientProtocolException e) {
                    logger.error("protocol exception.", e);
             } catch (IOException e) {
                    logger.error("io exception.", e);
             }

       }

}

No comments:

Post a Comment