Wednesday, April 29, 2009

Problem with JDK and network proxy validation

I was just playing around with java.net package to use amazing Google Search API. For this I need to pass through the proxy setup by my company and hence tried the usual java.net.URL and providing java.net.Authenticator / java.net.Proxy classes. Call to URL#getContent() or HttpURLConnection#getInputStream() results in following exception...

java.net.ProtocolException: Server redirected too many  times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1180)

After googling a lot, trying many different solutions suggested by various posts and nothing to my help, I discussed this with Sandy and came to know that its a typical known issue with JDK provided proxy authentication that code goes into recursive loop trying to resolve the proxy and fetch the URL content where server sends a 3xx code asking for the redirection. When enough of redirection has happened above mentioned ProtocolException is thrown. Better way to handle the proxy authentication is to use Apahce's HTTP Component's HttpClient.

So at the end I removed JDK provided proxy authentication to Apache HttpClient and its very clean, easy to use and overcomes JDK's proxy authentication problem very neatly. Apache HTTP Client is still under beta stage and not much of user guide sort of documentation is available except some examples showcasing the capabilities of Apache HTTP Components. For the easy reference below is the code excerpt for the basic proxy authentication for Apache HttpClient 4.0-beta2 I used in my code...

...
...
DefaultHttpClient httpclient = new DefaultHttpClient() ;
HttpHost proxy = new HttpHost( proxy.ip, proxy.port ) ;
httpclient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy ) ;
String localhost = InetAddress.getLocalHost().getHostName() ;
NTCredentials credentials = new NTCredentials( proxy.username, proxy.password, localhost, proxy.domain ) ;
httpclient.getCredentialsProvider().setCredentials( AuthScope.ANY, credentials) ;
...
...
With above you have a HttpClient object configured to pass through your proxy with authentication enabled... Happy "proxying"... :)

Cheers !!!
- Jay

Sunday, April 26, 2009

Installing J2EE 5.0 SDK on Ubuntu 8.10

Just 2 days back I installed Ubuntu 8.10 on my laptop along with Windows XP and made my system multiboot. Next step was to get my tools onboard. i.e. J2SE SDK, J2EE SDK, Eclipse and list goes on...

So to start with I downloaded J2EE 5.0 self extracting bin file java_ee_sdk-5_01-linux.bin and started the installation after changing the file permissions (chmod a+x java_ee_sdk-5_01-linux.bin)

As soon as the installer started, it ended with an pinching error message...

./java_ee_sdk-5_01-linux.bin: error while loading shared libraries: libstdc++.so.5: cannot open shared object file: No such file or directory


My immediate thought was required shared library libstdc++.so.5 is missing with Ubuntu 8.10 release. So tried to get it using...

sudo apt-get install libstdc++5


This failed, saying no such package. Probably default packages does not list this. Then I started looking in /usr/lib and found libstdc++.so.6.0.10. Having found this I just made a soft link to libstdc++.so.5 to point to existing version 6.0.10.

ln -s /usr/lib/libstdc++.so.6.0.10 /usr/lib/libstdc++.so.5


Started again installation and everything went on fine and I was a happy man when I saw the output of...

java -version

:)

Cheers !!!
- Jay
 
Disclaimer : This is a personal blog and all content represent what I think and it does not advocate/support/advertise any other person/company. I do not earn money or intended to do so with this blog or any of the contents the blog hosts (except the google ads which you see). If I post something here that you find helpful, that's wonderful. Just in case, if I say something stupid, the stupidity is mine, and mine alone and I can not be held for anything if you fall for such stupidity :-). I cannot be held responsible for any kind of damage that may be caused by downloading or viewing the files or information provided herewith. Anybody and everybody can use/refer the contents of this blog at their own will and of course at own risk. There is no need for any kind of approval of the author. Although it would be great if feedback is left for any such usage to the author.