Friday, October 24, 2008

Using Rinetd in Parallels on a Mac for testing your local web applications in IE

Firefox is great for web application development but due to Internet Explorers market-share, testing your application against it is mandatory. I use a Mac at home and within it I run Windows using Parallels. This works really fantastic and while my parallels instance is running, I can access my web application from within IE using my host system's IP address (I use 'Shared Networking' within Parallels).

Of course you can make accessing you local webserver a little bit more comfortable by using the Windows hosts file under %SystemRoot%\system32\drivers\etc\ to bind the IP address of my Mac instance to a more descriptive name.

However, there is still a problem with all those third party webservices such as Google Maps API and reCaptcha. When you sign up with them you create a key that works against a defined domain name. Nevertheless, for testing you can typically also use http://localhost/ etc. which is nice as I only need one key for both deployment and local testing.

Well, for accessing your web application from within parallels via localhost, rinetd works like a charm. Rinetd does port redirection and it is a tiny little command line utility that runs as a daemon. Originally from the Linux world there is also a Windows port available.

After downloading, all you have to do is to create/edit the config file rinetd.conf. In it I have only one line:

127.0.0.1 8080 10.211.55.2 8080

This line redirects all local calls on port 8080 to port 8080 of the ip address 10.211.55.2.

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.