2008年1月6日日曜日

[Java>Original]Useful String Utility(Utility to use String Object)

String Utility, converter, check routine, and compare method to avoid NullPointerException or other Exception.

References
  • http://math.hws.edu/eck/cs124/javanotes4/c4/ex-4-1-answer.html
  • http://forum.java.sun.com/thread.jspa?threadID=442797&messageID=2013116
Original Source Code
  • http://taapps-sourcecode-libraries.googlegroups.com/web/Str.java
  • http://taapps-sourcecode-libraries.googlegroups.com/web/taapps-bnr-0.2.jar
Methods
  • Convert first letter of words to capitalized letter.
    String testStr3=Str.getCapitalizedFirstLetter("capitalIZED fiRSt leTTER");
    Output of testStr3 object is "Capitalized First Letter"
  • Compare 2 String objects
    Compare 2 String objects and check whether the value of String object are the same or not. You don't need to check null of String object before calling this method.
    Third argument compare 2 strings ignore case
    String test1="Test1";
    String test2="test1";
    //case sensitive
    boolean result=Str.isSame(test1,test2,false));
    //result is false

    //ignore case Pattern
    result=Str.isSame(test1,test2,true));
    //result is true because comparing 2 string by using equalsIgnoreCase function
  • nvl(return the second object if the first argument object is null)
    Function return the second argument object if the first argument is null. This function works like ORACLE PL/SQL's nvl function.
    String test1=null;
    String test2="test1";
    String test3=Str.nvl(test1,test2);
    //test3 is "test1" because test1 object is null
    String test4=Str.nvl(test3,test2);
    //test4 is same as test3 object
  • padding and format number
    //right padding with @ char
    String paddingRight=Str.padStr("12345",20,'@',Str.NUMBER_PADDING_RIGHT);
    //Output is 12345@@@@@@@@@@@@@@@
    //left padding with X char
    String paddingLeft =Str.padStr("12345",20,'X',Str.NUMBER_PADDING_LEFT);
    //Output is XXXXXXXXXXXXXXX12345
  • padding with 0
    String padTest1=Str.padWithZero(12345,20);
    //Output of padTest1 String is 00000000000000012345
  • indexOf
    String test1="Test1";
    String test2="test1";
    boolean result=Str.indexOf(test1,test2,false));
    //result is false
    boolean result=Str.indexOf(test1,test2,true));
    //result is true
  • split string
    String test1="test1,test2,test3,test4,test5";
    String[] splittedStrs=Str.splitStr(test1,",")
  • Check whether the value of second argument is being included into the first argument array or not. Third argument is ignorecase flag.
    String[] array=new String[]{"test1","test2","test3","test4"};
    boolean result=Str.checkContain(array,"TEST3",false);
    //result is false

    //ignore case pattern
    boolean result=Str.checkContain(array,"TEST3",true);
    //result is true
  • Concatenate string array with second argument separator string
    String[] array=new String[]{"test1","test2","test3","test4"};
    String concat=Str.concat(array,"--");
    Output of concat Object is test1--test2--test3--test4
  • Check Utility(String length and format)
    • checkLength
    • checkCharsExpression
    • checkLengthAndExpression
  • URL encoder and decoder
    • decodeUrl
      encodeUrl

2008年1月5日土曜日

[Java>Jcaptcha]CaptchaServiceException occures and validation is always failed when the browser's cookie is disabled.

CaptchaServiceException occures and validation is always failed when the browser's cookie is disabled.

References
  • JCaptcha Home
    http://jcaptcha.sourceforge.net/
  • jcaptcha 5 minutes application integration tutorial http://forge.octo.com/jcaptcha/confluence/display/general/5+minutes+application+integration+tutorial
Description
When the browser's Cookies is disabled, the CaptchaServiceException always occures and validation is always failed. When the browser's cookie function is changed to be enabled, Capthca validation works fine without erroring out.
(When the browser's Cookies is disabled, I access url of the custom signup form with the session id manually like this "http://localhost/signup/;jsessionid=XXXXXXXXXX?app_name=test".)
My source code of Image Captcha Servlet and Captcha Service is based on the application integration tutorial(refer to the JCaptcha homepage http://forge.octo.com/jcaptcha/confluence/display/general/5+minutes+application+integration+tutorial), but it doesn't work when the browser's cookie function is disabled.

Error Detail
Exception occures as follows when validation is failed.
com.octo.captcha.service.CaptchaServiceException: Invalid ID, could not validate unexisting or already validated captcha
This exception is written in com.octo.captcha.service.AbstractCaptchaService. validateResponseForID method and this error is raised when checking and validating the captcha response.

Cause
JCaptcha use the session id as a key to store the value of captcha into the Map Object(when using DefaultManageableImageCaptchaService()). When accessing the Image Captcha Servlet to get captcha image, System can't get session id properly in case of disabling the browser cookie.

Solution
When accessing the Image Captcha Servlet to get captcha, image tag is always as follows.
<img id="captchaImage" src="/jcaptcha" border="1" />
But it should be changed as follows.
<img id="captchaImage" src="/jcaptcha;jsessionid=XXXXXXXXXX" border="1" />
The image tag is should be with the value of session id manually if the browser's cookie is disabled. the value of "XXXXXXXXXX" replaces with the client's session id.