Saturday, October 18, 2008

Incorporating reCAPTCHA into your Struts 2 + Spring Application

Who thought that that CAPTCHA implementations are only good for preventing spam? It turns out that the reCAPTCHA project from Carnegie Mellon University also does some common good. Developed by the same people that coined the term CAPTCHA, the project is also being used to help digitizing old books and publications (Including my favorite US Newspaper). The process is further explained here.

In addition to that it is a slick CAPCHA implementation as well, that you can incorporate freely into your web applications. There are APIs available for various languages including Java, Ruby and Python.

For my Struts 2 + Spring project I am using reCAPTCHA for the user registration page. The user fills out the registration but before s(he) submits the form a reCAPCHA form field has to be filled out, which means recognizing 2 words that are part of an image and retyping those two words into a form field. Upon form submission, code in my Struts 2 action calls the reCAPCHA Java API and validates the two words that were previously entered by the user.

Setting everything up has been easy and involves the following steps:
  • Signup for a reCAPTCHA account and generate the necessary keys for your site (similar to setting up Google Maps)
  • Add the recaptcha4j library to your project
  • Add some Javascript code to your JSP
  • Define the main recaptcha4j bean in your Spring ApplicationContext.xml file
  • Configure your Struts 2 Actions to process and validate the reCAPCHA form data
In your applicationContext.xml file you need to add the following bean definition:

<bean id="reCaptcha" class="net.tanesha.recaptcha.ReCaptchaImpl">
<property name="privateKey" value="your private key"/>
<property name="publicKey" value="your public key"/>
<property name="recaptchaServer" value="http://api.recaptcha.net"/>
<!-- For SSL use:
<property name="recaptchaServer" value="https://api-secure.recaptcha.net"/> -->
<property name="includeNoscript" value="false"/>
</bean>

In your jsp:




Setup your Struts 2 action to process two form parameters:

  1. recaptcha_challenge_field

  2. recaptcha_response_field


Then in your actual action code you validate those 2 token by calling:

public class SignupAction {
private User user;

private String recaptcha_challenge_field;
private String recaptcha_response_field;
private @Autowired ReCaptcha reCaptcha;

public String execute() {

ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
ServletActionContext.getRequest().getRemoteHost(),
recaptcha_challenge_field,
recaptcha_response_field);

if (!reCaptchaResponse.isValid()) {
addActionError("Not a good CAPTCHA.");
return INPUT;
}

...process the user registration form etc.

return SUCCESS;

}

...your setters
}

That's it. These should be all the basic steps necessary to get you going.

Here are some further resources:

http://wheelersoftware.com/articles/recaptcha-java-2.html
http://tanesha.net/projects/recaptcha4j/
http://recaptcha.net/apidocs/captcha/

Thursday, October 16, 2008

First Visit to the Atlanta Flash and Flex User Group

I finally made it to a meeting of the Atlanta Flash and Flex User Group. Ryan Swanson gave a great presentation on the Flex Data Services Framework, which was quite interesting. He also also showed us his project Regular Expression Explorer. Written in Flex, it looks like a really slick application.

Below are a few mostly Flex related links that might be of interest:

Not Flex specific, but if you need to satisfy your Mashup needs: http://www.programmableweb.com/

Several interesting Flex blogs:

http://www.quietlyscheming.com/blog/
http://dougmccune.com/blog/

A ton of presentations:

http://www.carehart.org/ugtv/

Flex Libraries:

http://code.google.com/p/as3corelib/
http://code.google.com/p/flexlib/
SuperImage

Wednesday, October 8, 2008

Hibernate - Derived Properties

The other day I used for the first time derived properties in Hibernate. What this means is that instead of mapping a property of your POJO class to a database column, your property is computed dynamically by doing an additional sub-select.

It is briefly discussed in chapter 5.1.11. of the Hibernate documentation. It is probably not a feature that will need often, however it can be a life/time-safer if you need it. It worked great for me.

Monday, October 6, 2008

jQuery - Creating Modal Dialogs

I am still working my way through the jQuery in Action book and I still need to write a review on it...soon....
Well, this weekend I was checking out several jQuery plugins - In my home project I needed to replace a few old-school/legacy JavaScript confirmation boxes with something more stylish looking. While jQuery by itself is very nice, its plugin mechanism and consequently its plugin repository is simply impressive. There is almost too much choice ;-) Anyway, there were several plugins for creating modal dialogs. I finally settled on SimpleModal - So far it worked quite well and the documentation is helpful, too.

The next plugin I have to look at is Live Query. I use a few Ajax driven data grids that return DOM nodes to which I would like to attach events. Live Query watches out for those node changes and applies some magic to make it work.

Friday, September 26, 2008

Creating Excel Files with POI - Issue

HSSF (Horrible SpreadSheet Format) is a great library for creating Excel spread-sheets using Java. It is part of the larger Apache POI project. Yesterday I hit an interesting issue while creating an Excel file with a modestly large collection of elements.

The Excel file was generated fine by HSSF but when opening it, Excel greeted me with the following message:

"some text formatting may have changed in this file because the maximum number of fonts was exceeded"

As it turns out, Excel only allows for a limited amount of styles/fonts to be used. Thus, the good advice when dealing with Excel files is to reuse styles and fonts wherever possible. It also results in substantially smaller Excel files. I was able, in my little example, to reduce the file-size from 54kb to 17kb, just by refactoring my usage of styles.

On last interesting observation: Once you hit that popup error in Excel, it does not simply suffice to close the Excel file, but you must restart Excel in order for the message not to popup again.

Friday, September 12, 2008

Flex and Maven First Steps

I finally decided to take the plunge and started learning Flex last night. First I wanted to setup my environment. As I don't have a Flex Builder License (My Evaluation License Expired before I had a chance to actually use it...arrgh...) I started creating a simple build environment that works with a plain Eclipse setup.

As far as I see there are 2 options out there:

http://www.israfil.net/projects/mojo/maven-flex2-plugin/
http://code.google.com/p/flex-mojos/

After some Googling the there seemed to be a preference for flex-mojos. So I decided to go with it.

Getting my first hello-world compile using flex-mojos was a bit tricky. The issue is that flex-mojos is under heavy development and seems to change a bit. E.g. the location of their Subversion repository and Maven repository changed which however is not necessarily reflected in their examples. Nevertheless there is a very helpful blog and a useful wiki available.

In order to keep up with the project, I decided to also check-out the latest flex-mojo trunk from Subversion and build it locally. Well, it turns out that my local Maven version on my MacBook Pro was not up-to-date but finally I got everything running.

Thus, after some fiddling around I got my first Maven-based Flex-Hello-World running.

In my next post I blog about some further progress and also some of the configuration that was necessary.

As it got really late last night I came across good read about building Flex applications using Maven and Flex-mojos:

http://www.adobe.com/devnet/flex/articles/fullstack_pt1.html
http://www.adobe.com/devnet/flex/articles/fullstack_pt2.html
http://www.adobe.com/devnet/flex/articles/fullstack_pt3.html

Upgrading Maven on Apple's Leopard

Today on my MacBook Pro I needed to upgrade the pre-installed Maven version 2.0.6 to the latest Maven version 2.0.9.

This gave me a good reason to install MacPorts. The took a little bit of time, especially since as a pre-requisite I also needed to install Apple's Xcode which comes along as a hefty 1GB download.

Once it was downloaded and installed, I installed MacPorts. For some reason after the MacPorts installation successfully finished, executing sudo port -v selfupdate did not work. The port command was not found... Well, it turns out that I needed to add the path to my .profile which is located in the home directory:
  • export PATH=/opt/local/bin:/opt/local/sbin:$PATH
  • export MANPATH=/opt/local/share/man:$MANPATH
That did the trick.

Lastly, all I needed to do in order to install the latest Maven version was to execute:

sudo port install maven2

Maven 2.0.9 was immediately available thereafter:

mvn -v

Wednesday, September 10, 2008

Effective Java @ Parleys - Presentation

I loved reading Effective Java Second Edition by Joshua Bloch - At Parleys they have a presentation by Joshua Bloch on some of the issues discussed in the book. It is really a great presentation and I can only highly recommended it (e.g. for understanding PECS when dealing with generics (producer extends, consumer super)).

Tuesday, September 9, 2008

jpwgen - Password Generator for Java

If you ever have the requirement to auto-generate passwords that must meet certain security guidelines such as having at least one number, a special character etc. then take a look at jpwgen. It is written in Java and is based on pwgen (unix).

What is nice about it is that you can use it both as a command line program and/or as a library within your applications.

By the way, if you need a GUI tool for Windows to generate passwords, check out pwgen-win

Monday, September 8, 2008

A Life-Changing Event...

On August 27, 2008 our daughter Leah Annarose was born. As I am posting this, she is already almost 2 weeks old.

Time is racing by, to say the least - The last 2 weeks were really amazing and having a child is a truly new experience; a great one.

For the years to come, it's now time to be a dad.