Thursday, May 27, 2010

Proxy Authentication from a Java Code

People who are behind company/campus proxies especially those requiring authentication must have faced this problem many times. Say, you are using an API call which does some resource fetching from outside the proxy for you. Now, you need to tell the API that, look, this is the proxy, this is the port and this is the username and password for you to pass through it. Typically, you would try doing this by setting system properties like http.proxyHost, http.proxyPort, http.proxyUser and http.proxyPass respectively. However, turns out that this may not help in some unfortunate cases like mine.. :-)

Some background: I was working on a code which was using an API call to a function (it was actually an application based on the OWLS to UDDI matchmaker client API) which did some ontology accessing using Jena. I needed my code to be able to refer to the ontologies outside seemlessly.

I set all those system properties, changed them to see if it works. But it didn't. It continuously gave me the http 407 response code (proxy authentication reqd).

Now, there are 2 ways in which you can solve such a problem:
1. Creating a proxy authenticator class
2. Setting up a local proxy for the remote proxy.


1. Creating a proxy authenticator class:

I must say this is a rather non-trivial solution. One cannot possibly think of this unless told. Thanks to my friend Nishant who showed me how he had used this in one of his programs and the chap here who has posted this solution.

(a) Create a separate class in the project, say, ProxyAuthenticator:

import java.net.*;

class ProxyAuthenticator extends Authenticator {

private String user, password;

public ProxyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}

}

(b) Then, create an object of this class in the code just before the call that accesses outside data:

Authenticator.setDefault(new ProxyAuthenticator("MyUsername", "MyPassword"));

Also set the proxy username and password through system properties:

System.setProperty("http.proxyHost""proxy host");  
System.setProperty("http.proxyPort""port");


2. Setting up a local proxy for the remote proxy:

A local proxy is a program that will basically do all remote proxy authentication for requests going out of your machine.
So, you make calls from your machine as if there was no proxy. These calls pass through the local proxy which does the remote proxy authentication for you.

Squid is one such proxy that you can set up. A very good guide to setting up squid can be found on my friend Prashant Borole's blog here. Don't forget to read the notes of caution given by him at the end.

Happy Coding!
Dhaval

Sunday, May 9, 2010

Connection Pooling in Tomcat for MySQL

Ok, a slight digression from my promise to keep this blog 'simple'... ;-)
But I am sure there must be many first timers in databases and application deployment who will find this useful.

What is Connection Pooling?

Well, connection pooling is a way of creating a pool of database connections on the server.

When you create a single dedicated connection as in:

Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB", "username", "password");

the driver instance stays dedicated to this thread that has created it. However, the application may require to use this connection for a very short time (few milliseconds) while for the rest of the time, it would be sitting idle but blocking resources.

In such a case, it is beneficial to create a connection pool which just picks a connection from the pool and 'loans' it to the application thread using it.

This gives a very nice overview of connection pooling.


How to perform Connection Pooling?

1. Open the ${CATALINA_HOME}/conf/server.xml file and enter the following inside the <host> </host> tags:

<Context path="/myApp" docbase="myApp" debug="5" reloadable="true" crosscontext="true">

<resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource" maxactive="100" maxidle="30" maxwait="10000" username="DBusername" password="DBpassword" driverclassname="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/myAppDB">

</resource>
</context>



2. If not already there, add this to WEB-INF/web.xml of your application (which you must have placed under ${CATALINA_HOME}/webapps - so the location will be something like ${CATALINA_HOME}/webapps//WEB-INF/web.xml):


<resource-ref>
<description> My DataSource </description>
<res-ref-name> jdbc/myDB </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> CONTAINER </res-auth>
</resource-ref>


3. Now, your code to access this connection pool should look something like this:

dsname = "java:comp/env/jdbc/myDB";
ctx = new InitialContext();
ds = (DataSource)ctx.lookup(dsname);
conn = ds.getConnection();


..followed by the usual conn.createStatement(), etc. code.


How I got here:

I was trying to set up a jUDDI server on my machine. However, I never found a correspondence between what is written in their user guide and what is available for download. The only consistency I found was when I used the instructions given on the apache wiki for the deployment of this version of jUDDI given on sourceforge.net. So, I will discuss these instructions here.

All the instructions given therein are correct except the stuff suggested to be put under the <host> tag in ${CATALINA_HOME}/conf/server.xml. The following needs to be added instead:


<Context path="/juddi" docbase="juddi" debug="5" reloadable="true" crosscontext="true">

<resource name="jdbc/juddiDB" auth="Container" type="javax.sql.DataSource" maxactive="100" maxidle="30" maxwait="10000" username="juddi" password="123456" driverclassname="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/juddi?autoReconnect=true">

</resource>
</context>

Also, in the WEB-INF/classes/juddi.properties file inside juddi, set the juddi.useConnectionPool property to 'true'.

Now, the http://localhost:8080/juddi/happyjuddi.jsp page should be visible without any red lines - a problem which many people have tried to solve and write blogs about, but seems there is always something that gets missed out. It is possible that I miss out something too. Hence, here are some very good references; if something doesn't work, do try these out too:

Have a good day!
- Dhaval

Fixing your Logitech WebCam on Ubuntu 9.04

Hello guys,

If you are using skype and a Logitech webcam (ID 046d:08af) on Ubuntu 9.04 (Jaunty Jackalope), you must have noticed this.
When you open skype, and go to options > video devices and 'test' your camera, you see some ugly looking green coloured lines all over. When you do a video chat, the other person hears a lot of 'noise' instead of your voice.

Before the solution, how do u check the ID of your webcam?
Just type in:
lsusb | grep QuickCam

And now, here's a quick solution to the problem:
1. Shut Down Skype
2. Make a new file in /usr/local/bin:
sudo vim /usr/local/bin/skype

3. Enter the following lines (INSERT):
#!/bin/sh
LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so /usr/bin/skype

and close the file (ESC :wq).
4. Change file permissions:
sudo chmod a+x /usr/local/bin/skype

5. Start skype and video should work now.. :)

Thanks to the gentleman here who posted this solution!

-Dhaval