Wednesday 30 July 2014

Simple Captcha Text Producer Implementation

This blog is in reference to the Liferay hook in Marketplace. Following is the link for the Captcha Internationalization.
Captcha Internationalization Hook

Same Concept can be used to implement multilingual Captcha for any web site.

Simple Captcha is the opensource implementation for captcha service. Please check the following URL for more details about SimpleCaptcha.
http://simplecaptcha.sourceforge.net/

Simple Captcha is providing textProducer for Captcha implementation. The textProducer class produces a string with some random Character or Numbers or Alphanumeric. Just simply add that captcha as attribute into the Session. Give an input Field in the HTML form for user to insert captcha text. Once user submits the form, compare the input field with the value stored in the session. This is how captcha will work.

Simple captcha is providing textProducer for Arabic, Chinese and Digits. Most of the time digit in captcha will work fine. In case customer asks for regional language for the captcha. You need to simply make your own implementation for the textProducer. Please refer the implementation of the ArabicTextProducer from SimpleCaptcha.
http://sourceforge.net/p/simplecaptcha/code/ci/master/tree/Java/src/nl/captcha/text/producer/ArabicTextProducer.java

You just need to implement TextProducer interface from SimpleCaptcha and provide your own implementation for your language. You can refer following code that I have developed for Gujarati text producer. 

package com.wellofjava.captcha.text.producer;

import nl.captcha.text.producer.DefaultTextProducer;
import nl.captcha.text.producer.TextProducer;

public class GujaratiTextProducer implements TextProducer {

    static final int DEFAULT_LENGTH = 5;

 private static char[] GUJARATI_CHARS = { '\u0627', '\u0A85', '\u0A86',
   '\u0A87', '\u0A88', '\u0A89', '\u0A8A', '\u0AE6', '\u0AE7',
   '\u0AE8', '\u0AE9', '\u0AEA', '\u0AEB', '\u0AEC' };
  
    private final TextProducer _txtProd;
    
    public GujaratiTextProducer() {
        this(DEFAULT_LENGTH);
    }
    
    public GujaratiTextProducer(int length) {
        _txtProd = new DefaultTextProducer(length, GUJARATI_CHARS);
    }
    
    @Override
    public String getText() {
        return _txtProd.getText();
    }
}

0 comments:

Post a Comment