hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jose Escobar <eb.j...@gmail.com>
Subject Set outgoing connection port on httpclient
Date Mon, 24 Sep 2012 14:04:46 GMT
Hello to all,

A paranoid firewall rule force me to use a range of outgoing ports on
a httpclient based aplication. Is there an easy way to configure this
on httpclient?

Meanwhile, I don't find that easy way so I have developed a custom
socket (also for ssl) I want to share:


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Random;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomPlainSocketFactory extends PlainSocketFactory {
	
	private int portMin=-1;
	private int portMax=-1;
	
	private static final int MAX_RETRIES=20;
	
	private Random randomGenerator;
	
	private final Logger logger =
LoggerFactory.getLogger(CustomPlainSocketFactory.class);
	
	public CustomPlainSocketFactory(int portMin, int portMax, Random
randomGenerator) {
        super();

        this.portMin=portMin;
        this.portMax=portMax;
        this.randomGenerator=randomGenerator;
    }
	
	@Override
	public Socket connectSocket(
            final Socket socket,
            final InetSocketAddress remoteAddress,
            final InetSocketAddress localAddress,
            final HttpParams params) throws IOException,
ConnectTimeoutException {
        if (remoteAddress == null) {
            throw new IllegalArgumentException("Remote address may not
be null");
        }
        if (params == null) {
            throw new IllegalArgumentException("HTTP parameters may
not be null");
        }
        Socket sock = socket;
        if (sock == null) {
            sock = createSocket();
        }
        if(portMin>0 && portMax>0 && portMax>portMin){
        	sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        	boolean binded=false;
        	int bindRetries=0;
        	InetSocketAddress localAddressPort;
        	while(!binded && bindRetries<MAX_RETRIES){
        		bindRetries++;
        		localAddressPort=new
InetSocketAddress(portMin+randomGenerator.nextInt(portMax-portMin));
        		logger.info("Tratando de conseguir un socket de salida con
puerto {}", localAddressPort.getPort());
        		try{
        			sock.bind(localAddressPort);
        			binded=true;
            	}catch(Exception ex){
            		binded=false;
            	}	
        	}
        }
        else{
	        if (localAddress != null) {
	            sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
	            sock.bind(localAddress);
	        }
        }
        int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
        int soTimeout = HttpConnectionParams.getSoTimeout(params);

        try {
            sock.setSoTimeout(soTimeout);
            sock.connect(remoteAddress, connTimeout);
        } catch (SocketTimeoutException ex) {
            throw new ConnectTimeoutException("Connect to " +
remoteAddress + " timed out");
        }
        return sock;
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message