1 16 package org.apache.cocoon.forms.formmodel; 17 18 import java.security.NoSuchAlgorithmException ; 19 import java.security.SecureRandom ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.CascadingRuntimeException; 24 import org.apache.avalon.framework.context.Context; 25 import org.apache.cocoon.components.ContextHelper; 26 import org.apache.cocoon.environment.ObjectModelHelper; 27 import org.apache.cocoon.environment.Session; 28 import org.apache.cocoon.forms.FormsConstants; 29 import org.apache.cocoon.xml.AttributesImpl; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.SAXException ; 32 33 34 53 public class CaptchaField extends Field { 54 55 public static final String SESSION_ATTR_PREFIX = "captcha-"; 56 57 private static final String IMAGE_EL = "captcha-image"; 58 private static final String SECRET_CHARS = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; 59 private static final int SESSION_ATTR_NAME_LENGTH = 6; 60 61 private Context avalonContext; 62 private int length; 63 64 67 protected static SecureRandom random; 68 69 static { 70 try { 71 random = SecureRandom.getInstance("SHA1PRNG"); 72 } catch(java.security.NoSuchAlgorithmException nsae) { 73 try { 75 random = SecureRandom.getInstance("IBMSecureRandom"); 76 } catch (NoSuchAlgorithmException e) { 77 throw new CascadingRuntimeException("No random number generator available", e); 78 } 79 } 80 random.setSeed(System.currentTimeMillis()); 81 } 82 83 public CaptchaField(CaptchaFieldDefinition fieldDefinition, Context avalonContext) { 84 super(fieldDefinition); 85 this.avalonContext = avalonContext; 86 this.length = fieldDefinition.getLength(); 87 } 88 89 private String generateSecret() { 90 StringBuffer secret = new StringBuffer (length); 91 for (int n = 0 ; n < length ; n++) { 92 int randomnumber = random.nextInt(SECRET_CHARS.length()); 93 secret.append(SECRET_CHARS.charAt(randomnumber)); 94 } 95 return secret.toString(); 96 } 97 98 public void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 99 super.generateItemSaxFragment(contentHandler, locale); 100 byte[] bytes = new byte[SESSION_ATTR_NAME_LENGTH]; 101 char[] result = new char[bytes.length * 2]; 102 random.nextBytes(bytes); 103 for (int i = 0; i < SESSION_ATTR_NAME_LENGTH; i++) { 104 byte ch = bytes[i]; 105 result[2 * i] = Character.forDigit(Math.abs(ch >> 4), 16); 106 result[2 * i + 1] = Character.forDigit(Math.abs(ch & 0x0f), 16); 107 } 108 String id = new String (result); 109 Map objectModel = ContextHelper.getObjectModel(this.avalonContext); 110 Session session = ObjectModelHelper.getRequest(objectModel).getSession(true); 111 String secret = generateSecret(); 112 session.setAttribute(SESSION_ATTR_PREFIX + id, secret); 113 this.setAttribute("secret", secret); 114 AttributesImpl attrs = new AttributesImpl(); 115 attrs.addAttribute("", "id", "id", "PCDATA", id); 116 contentHandler.startElement(FormsConstants.INSTANCE_NS, IMAGE_EL, FormsConstants.INSTANCE_PREFIX_COLON + IMAGE_EL, attrs); 117 contentHandler.endElement(FormsConstants.INSTANCE_NS, IMAGE_EL, FormsConstants.INSTANCE_PREFIX_COLON + IMAGE_EL); 118 } 119 } 120 | Popular Tags |