On Wed, 2013-01-30 at 22:23 -0800, Sachin Survase wrote:
> Hi Oleg,
> 
> Got it working with Authentication as well.
> I used java.net.Authenticator which provides credentials.
> I have placed this Custom Authenticator in my extended
> ClientConnectionOperator.
> 
> Please have a look at my ClientConnectionOperator code below.
> 
> Please let me know if you find any issues with the code.
> 

I see nothing wrong with your code. I would just move this logic to a
custom SchemeSocketFactory#connect implementation. Socket factories are
much easier to plug in than a client connection operator.

Oleg

> /public class SocksProxyClientConnOperator extends
>               DefaultClientConnectionOperator {
>       
>       private static final Object AUTHENTICATOR_LOCK = new Object();
>       
>       private SocksProxyAuthenticator socksAuthenticator;
>       
>       public SocksProxyClientConnOperator(SchemeRegistry schemes) {
>               super(schemes);
>       }
>       
>       public void openConnection(final OperatedClientConnection conn, final
> HttpHost target,
>             final InetAddress local, final HttpContext context, final
> HttpParams params) throws IOException {
>         if (conn == null) {
>             throw new IllegalArgumentException("Connection may not be
> null");
>         }
>         if (target == null) {
>             throw new IllegalArgumentException("Target host may not be
> null");
>         }
>         if (params == null) {
>             throw new IllegalArgumentException("Parameters may not be
> null");
>         }
>         if (conn.isOpen()) {
>             throw new IllegalStateException("Connection must not be open");
>         }
> 
>         SchemeSocketFactory socksSocketFactory = new SocksSocketFactory();
> 
>         Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
>         SchemeSocketFactory sf = schm.getSchemeSocketFactory();
>         
>         InetAddress[] addresses = resolveHostname(target.getHostName());
>         int port = schm.resolvePort(target.getPort());
>         
>         Socket sock = socksSocketFactory.createSocket(params);
>         conn.opening(sock, target);
>         
>         for (int i = 0; i < addresses.length; i++) {
>             InetAddress address = addresses[i];
>             boolean last = i == addresses.length - 1;
>             InetSocketAddress remoteAddress = new InetSocketAddress(address,
> port);
>             InetSocketAddress localAddress = null;
>             if (local != null) {
>                 localAddress = new InetSocketAddress(local, 0);
>             }
>             try {
>               Socket connsock = null;
>               
>               if(this.socksAuthenticator != null){
>                       synchronized (AUTHENTICATOR_LOCK) {
>                               
> Authenticator.setDefault(this.socksAuthenticator);
>                               connsock = sf.connectSocket(sock, remoteAddress,
> localAddress, params);
>                               }
>               }else{
>                       connsock = sf.connectSocket(sock, remoteAddress, 
> localAddress,
> params);
>               }
>                 
>                 if (sock != connsock) {
>                     sock = connsock;
>                     conn.opening(sock, target);
>                 }
>                 prepareSocket(sock, context, params);
>                 conn.openCompleted(sf.isSecure(sock), params);
>                 break;
>             } catch (ConnectException ex) {
>                 if (last) {
>                     throw new HttpHostConnectException(target, ex);
>                 }
>             } catch (ConnectTimeoutException ex) {
>                 if (last) {
>                     throw ex;
>                 }
>             }
>         }
>     }
>       
>       public SocksProxyAuthenticator getSocksAuthenticator() {
>               return this.socksAuthenticator;
>       }
> 
>       public void setSocksAuthenticator(SocksProxyAuthenticator
> socksAuthenticator) {
>               this.socksAuthenticator = socksAuthenticator;
>       }
> 
> }/
> 
> Thanks,
> Sachin Survase
> 
> 
> 
> --
> View this message in context: 
> http://httpcomponents.10934.n7.nabble.com/How-to-configure-basic-authentication-for-SOCKs-Proxy-tp19030p19118.html
> Sent from the HttpClient-User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to