Coding Clarity

Writing simple, clear and readable code.

Browsing Posts published in October, 2007

There has been a lot of buzz about Ruby in recent days, particularly around Ruby on Rails. I love these Ruby on Rails vs. X commercials that www.railsenvy.com has created. Rails provides a much better framework for the “web backed by a database” problem than anything I have seen in the pure Java world. Java has become a very bloated language and even the so-called “lightweight” frameworks are large and clunky. Rails is a great alternative for most people.

But not for everyone. I work mostly with Java based server applications that do not fit the “web backed by a database” type of project. These applications could benefit from the Ruby language itself, but not much from Rails. I have looked into using JRuby as a way to leverage the strengths of the Ruby language on the Java platform.

JRuby is a Ruby interpreter written 100% in Java and therefore runs on the JVM. It allows you to have the benefits of Ruby but still be able to use existing Java code. My main issue with this implementation is that it does not inter operate well with Java. Code written for JRuby typically use the Ruby libraries which work distinctly different from their Java counterparts. This makes it difficult to create a hybrid application where both Ruby and Java is used.

This is about the time I found the Groovy project. Groovy is “an agile dynamic language for the Java Platform” (http://groovy.codehaus.org). It is a powerful dynamic language that has many of the sought after features of other languages like Ruby and Python. The key difference here is that Java is a first class citizen in Groovy. The Groovy libraries are built on top of Java, extending and modifying the core API. Groovy makes creating a hybrid Groovy/Java application possible and easy.

For those coming from a Java background, Groovy is very easy to learn. The syntax is very similar to Java with a twist. Semicolons are not required, getters and setters are not required for properties, code closures are available and metaprogramming is possible. If you are looking for Rails, there is Grails which is a web framework for Groovy.

So if you long to wet your feet in an agile dynamic language like Ruby but are stuck with supporting legacy Java applications, I would recommend giving Groovy a try.

I ran into a situation the other day where I needed to connect to a remote Web server from a server side application. This web server requires HTTP authentication. The user sets the Web Server location from a configuration file.

What I wanted to be able to do was have the user enter the URL in the following format:

http://user:password@server.com/somewhere

The plan was to use Java’s URL connection library to make my connection to the website. I figured that the default implementation of the URL authentication would allow the user name and password to be supplied as part of the URL. To my surprise, I was greeted with a “401 Unauthorized” message.

The way to provide HTTP based authentication in Java is using the java.net.Authenticator mechanism. Java 5 provides a new method on the Authenticator called getRequestingURL(). This allows access to the URL that requested the authentication. By using this method, an Authenticator can get access to the user name and password embedded in the URL.

So, I created a simple Authenticator that uses the Apache Commons HTTPClient to parse the URL and pull out the authentication information.

Here is the code:

public class HTTPAuthenticator extends Authenticator {
    protected PasswordAuthentication getPasswordAuthentication() {
       if (!getRequestingProtocol().equals("http") &&
               !getRequestingProtocol().equals("https")) {
           return null;
       }       URL url = getRequestingURL();
       try {
           // Get the Username and password from the URL
           HttpURL httpUrl = new HttpURL(url.toString());
           String username = httpUrl.getUser();
           char[] password = httpUrl.getRawPassword();
           if (username == null || password == null) {
               return null;
           }
           return new PasswordAuthentication(username, password);
       }
       catch (Exception e) {
           return null;
       }
    }
    public void init() {

        Authenticator.setDefault(this);

    }
}
Powered by WordPress Web Design by SRS Solutions © 2010 Coding Clarity Design by SRS Solutions