Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Proxy with HttpComponentsClientHttpRequestFactory and RestTemplate

Can some one guide me how can I configure HttpComponentsClientHttpRequestFactory to use proxy server.

All examples I have seen are using SimpleClientHttpRequestFactory.

like image 861
NullPointerException Avatar asked Dec 16 '15 18:12

NullPointerException


People also ask

How to set proxy details in RestTemplate?

Sending a request to a proxy using RestTemplate is pretty simple. All we need to do is to call the setProxy(java. net. Proxy) from SimpleClientHttpRequestFactory before building the RestTemplate object.

What is HttpComponentsClientHttpRequestFactory?

HttpComponentsClientHttpRequestFactory is ClientHttpRequestFactory implementation that uses Apache HttpComponents HttpClient to create requests. We have used @Scheduled annotation in httpClient configuration. To support this, we have to add support of scheduled execution of thread.


2 Answers

If you do not mind using Apache Http Client it is not very complicated and there are 2 possibilities:

  1. If single proxy for all targets is enough for you:

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory 
        = new HttpComponentsClientHttpRequestFactory(
            HttpClientBuilder.create()
                    .setProxy(new HttpHost("myproxy.com", 80, "http"))
                    .build());
    restTemplate = new RestTemplate(clientHttpRequestFactory);
    
  2. Or if you want to use different proxies for different target URIs, schemas, etc. you can use HttpRoutePlanner with custom ProxySelector:

    HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new MyProxySelector());
    
    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory 
        = new HttpComponentsClientHttpRequestFactory(
            HttpClientBuilder.create()
                .setRoutePlanner(routePlanner)
                .build());
    restTemplate = new RestTemplate(clientHttpRequestFactory);
    

    Example proxy selector: MyProxySelector.java:

    package hello;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.Proxy.Type;
    import java.net.ProxySelector;
    import java.net.SocketAddress;
    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyProxySelector extends ProxySelector {
    
        ProxySelector defaultproxySelector = ProxySelector.getDefault();
    
        ArrayList<Proxy> noProxy = new ArrayList<Proxy>();
        ArrayList<Proxy> secureProxy = new ArrayList<Proxy>();
        ArrayList<Proxy> sociaMediaProxy = new ArrayList<Proxy>();
    
        public MyProxySelector(){
    
            noProxy.add(Proxy.NO_PROXY);
    
            secureProxy.add(new Proxy(Type.HTTP, new InetSocketAddress(
                "secure.proxy.mycompany.com", 8080)));
    
            sociaMediaProxy.add(new Proxy(Type.HTTP, new InetSocketAddress(
                    "social-media.proxy.mycompany.com", 8080)));
        }
    
        @Override
        public List<Proxy> select(URI uri) {
    
            // No proxy for local company addresses.
            if ( uri.getHost().toLowerCase().endsWith("mycompany.com") ) {
                return noProxy ;
            }
    
            // Special proxy for social networks.
            String host = uri.getHost().toLowerCase();
            if (    host.endsWith("facebook.com") ||
                    host.endsWith("twitter.com") ||
                    host.endsWith("cfapps.io") ||               
                    host.endsWith("flickr.com") ) 
            {
                return sociaMediaProxy ;
            }
    
            // for https URIs use secureProxy
            if ( uri.getScheme().toLowerCase().equals("https") ){
                return secureProxy ;
            }
    
            if (defaultproxySelector != null) {
                return defaultproxySelector.select(uri);
            }
    
            return noProxy;
        }
    
        @Override
        public void connectFailed(URI arg0, SocketAddress arg1, IOException arg2) {
            // TODO Auto-generated method stub
        }
    }
    
like image 103
Michal Foksa Avatar answered Sep 21 '22 22:09

Michal Foksa


Very simple way to let the HttpComponentsClientHttpRequestFactory use the standard java SystemProperties for proxy-stuff (see https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html)

is this:

        HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());

    HttpClient httpClient = HttpClientBuilder
            .create()
            .setRoutePlanner(routePlanner)
            .build();
    restTemplate.setRequestFactory(
            new HttpComponentsClientHttpRequestFactory(httpClient));

That way, it even regards the nonProxyHosts setting.

like image 26
Oliver Jurkschat Avatar answered Sep 20 '22 22:09

Oliver Jurkschat