> -----Original Message-----
> From: Dave [mailto:[email protected]]
> Sent: Monday, June 20, 2011 10:09 PM
> To: [email protected]
> Subject: Re: Comment authentication problem
>
> Unfortunately, we don't accept attachments.
> Please paste into an email message.
package org.rocsca.MathCommentAuthenticator;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Asks the commenter to answer a simple math question.
*/
public class MathCommentAuthenticator implements CommentAuthenticator {
private transient ResourceBundle bundle =
ResourceBundle.getBundle("ApplicationResources");
private static Log mLogger =
LogFactory.getLog(MathCommentAuthenticator.class);
public String getHtml(HttpServletRequest request) {
String answer = "";
HttpSession session = request.getSession(true);
if (session.getAttribute("mathAnswer") == null) {
// starting a new test
int value1 = (int)(Math.random()*10.0);
int value2 = (int)(Math.random()*100.0);
int sum = value1 + value2;
session.setAttribute("mathValue1", new Integer(value1));
session.setAttribute("mathValue2", new Integer(value2));
session.setAttribute("mathAnswer", new Integer(sum));
} else {
// preserve user's answer
answer = request.getParameter("answer");
answer = (answer == null) ? "" : answer;
}
// pull existing values out of session
Integer value1o =
(Integer)request.getSession().getAttribute("mathValue1");
Integer value2o =
(Integer)request.getSession().getAttribute("mathValue2");
StringBuffer sb = new StringBuffer();
sb.append("<p>");
sb.append(bundle.getString("comments.mathAuthenticatorQuestion"));
sb.append("</p><p>");
sb.append(value1o);
sb.append(" + ");
sb.append(value2o);
sb.append(" = ");
sb.append("<input name=\"answer\" value=\"");
sb.append(answer);
sb.append("\" /></p>");
sb.append("<input type=hidden name=\"mathAnswer\" value=\"");
sb.append(mathAnswer);
sb.append("\" />");
return sb.toString();
}
public boolean authenticate(HttpServletRequest request) {
boolean authentic = false;
HttpSession session = request.getSession(false);
String answerString = request.getParameter("answer");
if (answerString != null && session != null) {
try {
int answer = Integer.parseInt(answerString);
Integer sum = (Integer) session.getAttribute("mathAnswer");
if (mathAnswerString != null && answer ==
Integer.parseInt(mathAnswerString)) {
authentic = true;
session.removeAttribute("mathAnswer");
session.removeAttribute("mathValue1");
session.removeAttribute("mathValue2");
}
} catch (NumberFormatException ignored) {
// ignored ... someone is just really bad at math
} catch (Exception e) {
// unexpected
mLogger.error(e);
}
}
return authentic;
}
}
> - Dave
rocsca
>
>
>
> On Mon, Jun 20, 2011 at 3:57 PM, Scappatura Rocco
> <[email protected]> wrote:
> >> -----Original Message-----
> >> From: Dave [mailto:[email protected]]
> >> Sent: Monday, June 20, 2011 5:20 PM
> >> To: [email protected]
> >> Subject: Re: Comment authentication problem
> >>
> >> On Mon, Jun 20, 2011 at 9:28 AM, Scappatura Rocco
> >> <[email protected]> wrote:
> >> >> > import java.util.ResourceBundle;
> >> >> > import javax.servlet.http.HttpServletRequest;
> >> >> > import javax.servlet.http.HttpSession;
> >> >> > import org.apache.commons.logging.Log;
> >> >> > import org.apache.commons.logging.LogFactory;
> >> >>
> >> >> I don't see any org.apache.roller packages there.
> >> >>
> >> >> What is the package name of the class your are defining?
> >> > package org.rocsca.MathCommentAuthenticator;
> >>
> >> The problem is that you code cannot find the "CommentAuthenticator"
> >>
> >> You might be able to fix this by adding this to your imports:
> >> import
> >>
> org.apache.roller.weblogger.ui.rendering.plugins.comments.CommentAuthenticator
> >>
> >> But it's hard to tell because I don't have your full source.
> >
> > Obviously my code is open source! :-P
> >
> > In attachment there is the java code.
> >
> >> Thanks,
> >> Dave
> >
> > Thanks to you!
> > rocsca
> >