You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by Andrei <gm...@gmail.com> on 2010/07/15 07:31:42 UTC
recaptcha works with click
I created this simple class and tried - it works fine with captcha
http://www.google.com/recaptcha
package com.andreig.hiredonphone;
import org.apache.click.control.AbstractControl;
public class ReCaptchaField extends AbstractControl {
private String value;
private String public_key;
// -------------------------------
public ReCaptchaField( String public_key ){
this.public_key = public_key;
setName( "captcha" );
}
// -------------------------------
public void setValue(String value) {
this.value = value;
}
// -------------------------------
public String getValue() {
return value;
}
// -------------------------------
public String getTag() {
// Return the HTML tag
return "br/><i>To verify that you are human</i><br/>" +
"<script type=\"text/javascript\"
src=\"http://api.recaptcha.net/challenge?k=" +
public_key +
"\"></script><noscript><iframe src=\"http://api.recaptcha.net/noscript?k=" +
public_key +
"\" height=\"300\" width=\"500\"
frameborder=\"0\"></iframe><br></noscript><br";
}
// -------------------------------
public boolean onProcess() {
// Bind the request parameter to the field value
String requestValue = getContext().getRequestParameter(getName());
setValue(requestValue);
// Invoke any listener of MyField
return true;
}
}
You use it as this, put your public key in constructor
ReCaptchaField captcha = new ReCaptchaField(
"6LfTf7sSAAAAAICJsZlojJaUAJX1Cws-aEBMm7oJ" );
form.add( captcha );
Then in "onSubmit" I put
HttpServletRequest request = getContext().getRequest();
String remoteAddr = request.getRemoteAddr();
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey( "my_private_key" );
String challenge = request.getParameter("recaptcha_challenge_field");
String uresponse = request.getParameter("recaptcha_response_field");
ReCaptchaResponse reCaptchaResponse =
reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
if( !reCaptchaResponse.isValid() ){
msg = "Captcha field not correct";
return false;
}
Re: recaptcha works with click
Posted by AndreyRybin <ap...@gmail.com>.
Andrei-4 wrote:
>
> return "br/>To verify that you are human<br/>" +
> "<script type=\"text/javascript\"
> src=\"http://api.recaptcha.net/challenge?k=" +
> public_key +
> "\"></script><noscript><iframe
> src=\"http://api.recaptcha.net/noscript?k=" +
> public_key +
> "\" height=\"300\" width=\"500\"
> frameborder=\"0\"></iframe><br></noscript><br";
>
Not related to captcha and this class, just inspired idea:
@Click developers:
May be add one method in Page class for this (some copy-paste from Spring):
/**
* Process the specified template with the given model and write
* the result to the given Writer.
* <p>When using this method to prepare a text for a mail to be sent with
Spring's
* mail support, consider wrapping IO/TemplateException in
MailPreparationException.
* @param model the model object, typically a Map that contains model names
* as keys and model objects as values
* @return the result as String
* @throws IOException if the template wasn't found or couldn't be read
* @throws TemplateException if rendering failed
*/
public String template (String templatePath, Map<String, ?> model) throws
IOException, TemplateException {
// Get the template object
Template template = configuration.getTemplate(templatePath);
StringWriter writer = new StringWriter();
try { // Merge the data-model and the template
template.process(model, writer);
} catch (freemarker.template.TemplateException fmte) {
throw new TemplateException(fmte);
}
return result.toString();
}//template
and also good idea to add handy functions from Google Guava to ClickUtils:
public static <K, V> Map<K, V> mapOf (final K k, final V v) {
final Map<K, V> map = new LinkedHashMap<K, V>();
map.put(k, v);
return map;
}//mapOf
public static <K, V> Map<K, V> mapOf (final K k1, final V v1, final K k2,
final V v2) {
final Map<K, V> map = new LinkedHashMap<K, V>();
map.put(k1, v1);
map.put(k2, v2);
return map;
}//mapOf
public static <K, V> Map<K, V> mapOf (final K k1, final V v1, final K k2,
final V v2, final K k3, final V v3) {
final Map<K, V> map = new LinkedHashMap<K, V>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
return map;
}//mapOf
then you can make above code easy
return template("/com/andreig/hiredonphone/cap.ftl_or_vm",
mapOf("public_key", public_key);
Template in external file (not in java String) is good think: IDE can
highlight and verify it.
Only one problem here: Freemarker/Velocity binding (if you change template
engine code stop working).
But there is my comment in JIRA with idea how to solve it:
https://issues.apache.org/jira/browse/CLK-364?focusedCommentId=12801392&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_12801392
PS: just my fast brain dump (so sorry for usual bad English)
--
View this message in context: http://click.1134972.n2.nabble.com/recaptcha-works-with-click-tp5296015p5324509.html
Sent from the click-user mailing list archive at Nabble.com.