Tomcat 6 - Java proxy tunneling failed on Windows - ERROR - 407 -


i've created class deal http proxy connect remote server web services. deployed on tomcat 6, on windows server 2008 , called in servlet.

  1. it working $catalina_home\bin\tomcat6.exe, i.e. on cmd.
  2. it couldn't go through proxy windows service utility, i.e. $catalina_home\bin\tomcat6w.exe.

both reading same configurations, behaving differently while establishing connection remote server through proxy.

i've found few way proxy settings, follows:

  1. proxy vole utility jar (proxy-vole_20131209.jar).
  2. java.net.usesystemproxies set true , fetch proxy info.
  3. reading pac java code (deploy.jar).
  4. passing constant hostname/ip , port.

all of above work $catalina_home\bin\tomcat6.exe, other pac reading fetches private ip instead or public ip (well can ignore long know exact hostname , port). note: there no proxy credentials i've found , working without cmd. when try run tomcat windows service utility, i.e. $catalina_home\bin\tomcat6w.exe fails connect remote server , throws exception: java.io.ioexception: unable tunnel through proxy. proxy returns "http/1.1 407 proxy authentication required"

please find class, retry each case 1 one above mentioned (pac 1 skipped).

import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.net.inetaddress; import java.net.inetsocketaddress; import java.net.malformedurlexception; import java.net.proxy; import java.net.proxyselector; import java.net.socketaddress; import java.net.uri; import java.net.urisyntaxexception; import java.net.url; import java.net.urlconnection; import java.security.keymanagementexception; import java.security.keystore; import java.security.keystoreexception; import java.security.nosuchalgorithmexception; import java.security.cert.certificateexception; import java.util.arraylist; import java.util.list; import javax.net.ssl.sslcontext; import javax.net.ssl.trustmanager; import javax.net.ssl.trustmanagerfactory; import com.btr.proxy.search.proxysearch; import com.btr.proxy.search.proxysearch.strategy; import com.btr.proxy.util.platformutil; import com.btr.proxy.util.platformutil.platform; import com.sun.deploy.net.proxy.browserproxyinfo; import com.sun.deploy.net.proxy.dummyautoproxyhandler; import com.sun.deploy.net.proxy.proxyconfigexception; import com.sun.deploy.net.proxy.proxyinfo; import com.sun.deploy.net.proxy.proxytype;  public class proxypacmanager { public static void main(string args[]) throws exception{     getproxy(); }  public static proxy getproxy(){     string almprotocol = constants.getpropereties().getproperty("dashboard.alm.protocol");     string almhost = constants.getpropereties().getproperty("dashboard.alm.host");     string almport = constants.getpropereties().getproperty("dashboard.alm.port");     string urlstr = almprotocol+almhost+":"+almport;     proxy proxy = null;     list<proxy> proxylist = null;     string successmsg = "proxy not found.";     try{         system.out.println("trying connect through proxy vole plugin.");         proxylist = getsslcertificateautoproxy(urlstr);         proxy = getproxytested(proxylist, urlstr);         successmsg="successfully connected through proxy vole plugin.";     } catch(exception ex){         system.out.println("proxy vole plugin didn't work."+ex.getmessage());         try{             system.out.println("trying connect through java.net.usesystemproxies proxy.");             proxylist = getsslcertificatesysproxy(urlstr);             proxy = getproxytested(proxylist, urlstr);             successmsg="successfully connected through java.net.usesystemproxies proxy.";         } catch(exception ex1){             system.out.println("java.net.usesystemproxies didn't work."+ex1.getmessage());             try{                 /*system.out.println("trying connect through pac proxy.");                 proxylist = getsslcertificatepacproxy(urlstr);                 proxy = getproxytested(proxylist, urlstr);                 successmsg="successfully connected through pac proxy.";*/                 throw new exception("bypass pac proxy testing.");             }catch(exception ex2){                 system.out.println("pac proxy read didn't work."+ex2.getmessage());                 try{                     system.out.println("trying connect through constant proxy.");                     proxylist = getsslcertificateconstantproxy();                     proxy = getproxytested(proxylist, urlstr);                     successmsg="successfully connected through constant proxy.";                 }catch(exception ex3){                     system.out.println("constant proxy read didn't work."+ex3.getmessage());                     proxylist = new arraylist<proxy>();                     proxylist.add(proxy.no_proxy);                     proxy = getproxytested(proxylist, urlstr);                     successmsg = "connected no_proxy";                 }             }         }     }     system.out.println(successmsg);     return proxy; }  private static proxy getproxytested(list<proxy> proxylist, string urlstr){     if (proxylist != null && !proxylist.isempty()) {           (proxy proxy : proxylist) {               socketaddress address = proxy.address();               if (address instanceof inetsocketaddress) {                  system.out.println("trying connect through proxy: "+((inetsocketaddress) address).gethostname()+":"+((inetsocketaddress) address).getport());                 try {                     urlconnection connection = new url(urlstr).openconnection(proxy);                     connection.connect();                                    system.out.println("connected through proxy: "+((inetsocketaddress) address).gethostname()+":"+((inetsocketaddress) address).getport());                     return proxy;                  } catch (malformedurlexception e) {                     e.printstacktrace();                 } catch (ioexception e) {                     e.printstacktrace();                 }                                                }           }      }     return null; }  private static list<proxy> getsslcertificateconstantproxy() throws exception{     setcertificate();     list<proxy> proxylist = new arraylist<proxy>();     string proxyhost = constants.getpropereties().getproperty("dashboard.alm.proxy.host");     inetaddress hostip = inetaddress.getbyname(proxyhost);     int proxyport = integer.parseint(constants.getpropereties().getproperty("dashboard.alm.proxy.port"));      //create proxy , setup authentication it.     proxy proxy = new proxy(proxy.type.http, new inetsocketaddress(hostip.gethostaddress(), proxyport));                             //setup authentication proxy.     /*authenticator.setdefault(new authenticator() {            @override           protected passwordauthentication getpasswordauthentication() {             return new passwordauthentication("<user>", "<password>".tochararray());           }     });*/      proxylist.add(proxy);     return proxylist;  }  private static void setcertificate() throws keystoreexception, nosuchalgorithmexception, certificateexception, ioexception, keymanagementexception{     //first, load key store file     string jksfile = constants.getpropereties().getproperty("dashboard.alm.certificate");     inputstream truststream = new fileinputstream(jksfile);      string jkspass = constants.getpropereties().getproperty("dashboard.alm.certificate.pass");     char[] trustpassword = jkspass.tochararray();      //initialize keystore     keystore truststore = keystore.getinstance(keystore.getdefaulttype());     truststore.load(truststream, trustpassword);      //initialize trustmanager objects.     trustmanagerfactory trustfactory = trustmanagerfactory.getinstance(trustmanagerfactory.getdefaultalgorithm());     trustfactory.init(truststore);     trustmanager[] trustmanagers = trustfactory.gettrustmanagers();      //create new sslcontext, load trustmanager objects , set default.     sslcontext sslcontext = sslcontext.getinstance("ssl");     sslcontext.init(null, trustmanagers, null);     sslcontext.setdefault(sslcontext);   } private static proxyinfo[] getproxyinfo(string urlstr) throws proxyconfigexception, malformedurlexception{           string proxypac = constants.getpropereties().getproperty("dashboard.alm.proxy.pac");             browserproxyinfo b = new browserproxyinfo();     /*wdefaultbrowserproxyconfig wd = new wdefaultbrowserproxyconfig();     browserproxyinfo b = wd.getbrowserproxyinfo();        */     b.settype(proxytype.auto);     b.setautoconfigurl(proxypac);            dummyautoproxyhandler handler = new dummyautoproxyhandler();     handler.init(b);      url url = new url(urlstr);     proxyinfo[] ps = handler.getproxyinfo(url);           return ps; }  public static list<proxy> getsslcertificateautoproxy(string urlstr) throws exception{                setcertificate();     /*proxysearch proxysearch = proxysearch.getdefaultproxysearch();*/     proxysearch proxysearch = new proxysearch();     proxysearch.setpaccachesettings(32, 1000*60*5);     if (platformutil.getcurrentplattform() == platform.win) {          proxysearch.addstrategy(strategy.ie);          proxysearch.addstrategy(strategy.firefox);          proxysearch.addstrategy(strategy.java);      } else if (platformutil.getcurrentplattform() == platform.linux) {          proxysearch.addstrategy(strategy.gnome);          proxysearch.addstrategy(strategy.kde);          proxysearch.addstrategy(strategy.firefox);      } else {          proxysearch.addstrategy(strategy.os_default);      }              proxyselector proxyselector = proxysearch.getproxyselector();     /*bufferedproxyselector cachedselector = new bufferedproxyselector(32, 1000*60*5, proxyselector);*/       proxyselector.setdefault(proxyselector);      //proxyselector.setdefault(cachedselector);     uri home = uri.create(urlstr);       //list<proxy> proxylist = cachedselector.select(home);      list<proxy> proxylist = proxyselector.select(home);     return proxylist; }  public static list<proxy> getsslcertificatepacproxy(string urlstr) throws keystoreexception, nosuchalgorithmexception, certificateexception, ioexception, keymanagementexception, proxyconfigexception{     list<proxy> proxylist = new arraylist<proxy>();     setcertificate();            proxyinfo[] ps = getproxyinfo(urlstr);           for(proxyinfo p: ps){          string proxyhost = p.getproxy();         int proxyport = p.getport();           //create proxy , setup authentication it.         proxy proxy = new proxy(proxy.type.http, new inetsocketaddress(proxyhost, proxyport));                               //setup authentication proxy.         /*authenticator.setdefault(new authenticator() {                @override               protected passwordauthentication getpasswordauthentication() {                 return new passwordauthentication("<user>", "<password>".tochararray());               }         });*/         proxylist.add(proxy);      }     return proxylist;  }   public static list<proxy> getsslcertificatesysproxy(string urlstr) throws keystoreexception, nosuchalgorithmexception, certificateexception, ioexception, keymanagementexception, proxyconfigexception, urisyntaxexception{     setcertificate();            system.setproperty("java.net.usesystemproxies","true");     list<proxy> proxylist = proxyselector.getdefault().select(new uri(urlstr));      return proxylist;  }  } 

failure output of class,

trying connect through java.net.usesystemproxies proxy. java.net.usesystemproxies didn't work.null pac proxy read didn't work.bypass pac proxy testing. trying connect through constant proxy. trying connect through proxy: xx.xx.xxx.xx:8080 [masked security reasons] java.io.ioexception: unable tunnel through proxy. proxy returns "http/1.1 407 proxy authentication required" 

please me through proxy tomcat 6 windows service utility. note: windows security policy: network security: lan manager authentication level = send ntlm response only

please if have solution issue let me know, asap. i'll break code more understanding:

  1. get proxy proxy vole plugin/jar

     public static list<proxy> getsslcertificateautoproxy(string urlstr) throws exception{           setcertificate(); /*proxysearch proxysearch = proxysearch.getdefaultproxysearch();*/ proxysearch proxysearch = new proxysearch(); proxysearch.setpaccachesettings(32, 1000*60*5); if (platformutil.getcurrentplattform() == platform.win) {      proxysearch.addstrategy(strategy.ie);      proxysearch.addstrategy(strategy.firefox);      proxysearch.addstrategy(strategy.java);  } else if (platformutil.getcurrentplattform() == platform.linux) {      proxysearch.addstrategy(strategy.gnome);      proxysearch.addstrategy(strategy.kde);      proxysearch.addstrategy(strategy.firefox);  } else {      proxysearch.addstrategy(strategy.os_default);  }          proxyselector proxyselector = proxysearch.getproxyselector(); /*bufferedproxyselector cachedselector = new bufferedproxyselector(32, 1000*60*5, proxyselector);*/   proxyselector.setdefault(proxyselector);  //proxyselector.setdefault(cachedselector); uri home = uri.create(urlstr);   //list<proxy> proxylist = cachedselector.select(home);  list<proxy> proxylist = proxyselector.select(home); return proxylist; } 
  2. get proxy using java.net.usesystemproxies

    public static list<proxy> getsslcertificatesysproxy(string urlstr) throws exception{  setcertificate();        system.setproperty("java.net.usesystemproxies","true");  list<proxy> proxylist = proxyselector.getdefault().select(new uri(urlstr));   return proxylist;  } 
  3. get proxy pac:

    public static list<proxy> getsslcertificatepacproxy(string urlstr) throws keystoreexception, nosuchalgorithmexception, certificateexception, ioexception, keymanagementexception, proxyconfigexception{ setcertificate();        list<proxy> proxylist = new arraylist<proxy>(); string proxypac = "<http://mydomain/proxy.pac>";         browserproxyinfo b = new browserproxyinfo(); /*wdefaultbrowserproxyconfig wd = new wdefaultbrowserproxyconfig(); browserproxyinfo b = wd.getbrowserproxyinfo();        */ b.settype(proxytype.auto); b.setautoconfigurl(proxypac);        dummyautoproxyhandler handler = new dummyautoproxyhandler(); handler.init(b);  url url = new url(urlstr); proxyinfo[] ps = handler.getproxyinfo(url);  for(proxyinfo p: ps){      string proxyhost = p.getproxy();     int proxyport = p.getport();       //create proxy , setup authentication it.     proxy proxy = new proxy(proxy.type.http, new inetsocketaddress(proxyhost, proxyport));                           //setup authentication proxy.     /*authenticator.setdefault(new authenticator() {            @override           protected passwordauthentication getpasswordauthentication() {             return new passwordauthentication("<user>", "<password>".tochararray());           }     });*/     proxylist.add(proxy);  } return proxylist;  } 
  4. get proxy directly passing known proxy hostname , port

    private static list<proxy> getsslcertificateconstantproxy() throws exception{   setcertificate();  list<proxy> proxylist = new arraylist<proxy>();  string proxyhost = "<myproxy.hostname>";  inetaddress hostip = inetaddress.getbyname(proxyhost);  int proxyport = integer.parseint("<myproxyport>"));   //create proxy , setup authentication it.  proxy proxy = new proxy(proxy.type.http, new inetsocketaddress(hostip.gethostaddress(), proxyport));                         //setup authentication proxy.  /*authenticator.setdefault(new authenticator() {        @override       protected passwordauthentication getpasswordauthentication() {         return new passwordauthentication("<user>", "<password>".tochararray());       }  });*/   proxylist.add(proxy);  return proxylist;  } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -