Eclipse proxies part 2

July 27, 2012 | 1 min Read

In June I discussed how to programatically configure Eclipse proxy settings.  This is useful if you are using Eclipse APIs (such as ECF) for your communication.  However, in some cases you may also use your own transport implementation, or something like Apache HTTP Components.  In this case you’ll want to query the proxy configuration settings and use those when setting up your http connection.

The EclipseProxyService has a very useful method – select – that returns all the applicable proxy data for a given URI.  Using both the ProxyService and HTTPClient’s route planner, you can construct a route that uses the appropriate proxy setttings.

private void setupProxy(DefaultHttpClient httpClient) {
  final IProxyService proxyService = getProxyService();
  httpClient.setRoutePlanner(new HttpRoutePlanner() {

    public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context)
     throws HttpException {
      IProxyData[] select = null;
      try {
        select = proxyService.select(new URI(target.toURI()));
      } catch (URISyntaxException e) {
        e.printStackTrace();
      }   
    
      HttpHost host = target;
      for (IProxyData proxyData : select) {
        if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
   host = new HttpHost(proxyData.getHost(), proxyData.getPort());
 }
      }
      return new HttpRoute(target, null, host, isSecureConnection(target));
    }
  });
}

Since there can be several proxy entries for a given URI, you must ensure you choose the right proxy data. In my case, I only care about HTTP proxies, so I only extract the proxyData with type “HTTP”.

Stay Updated with Our Latest Articles

Want to ensure you get notifications for all our new blog posts? Follow us on LinkedIn and turn on notifications:

  1. Go to the EclipseSource LinkedIn page and click "Follow"
  2. Click the bell icon in the top right corner of our page
  3. Select "All posts" instead of the default setting
Follow EclipseSource on LinkedIn
Ian Bull

Ian Bull

Ian is an Eclipse committer and EclipseSource Distinguished Engineer with a passion for developer productivity.

He leads the J2V8 project and has served on several …