MathCommentAuthenticationPlugin.java [plain text]
package org.blojsom.plugin.comment;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.Blog;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.util.BlojsomUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.HashMap;
public class MathCommentAuthenticationPlugin extends CommentModerationPlugin {
private Log _logger = LogFactory.getLog(MathCommentAuthenticationPlugin.class);
public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER";
public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE1 = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE1";
public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE2 = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE2";
public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_OPERATION = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_OPERATION";
public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER_CHECK_PARAM = "mathAnswerCheck";
public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_STATUS_MESSAGE = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_STATUS_MESSAGE";
private static final int AVAILABLE_OPERATIONS = 3;
public MathCommentAuthenticationPlugin() {
}
protected void moderateComment(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlogUser user, Map context, BlogEntry[] entries) throws BlojsomPluginException {
Blog blog = user.getBlog();
if ("true".equalsIgnoreCase(blog.getBlogProperty(COMMENT_MODERATION_ENABLED))) {
HttpSession httpSession = httpServletRequest.getSession();
if ("y".equalsIgnoreCase(httpServletRequest.getParameter(CommentPlugin.COMMENT_PARAM)) && user.getBlog().getBlogCommentsEnabled().booleanValue()) {
String mathAnswerCheck = BlojsomUtils.getRequestValue(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER_CHECK_PARAM, httpServletRequest);
boolean passedCheck = false;
if (httpSession.getAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER) != null) {
Integer mathAnswer = (Integer) httpSession.getAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER);
try {
int mathAnswerCheckValue = Integer.parseInt(mathAnswerCheck);
int originalMathAnswerValue = mathAnswer.intValue();
if (mathAnswerCheckValue == originalMathAnswerValue) {
passedCheck = true;
}
} catch (NumberFormatException e) {
}
}
if (!passedCheck) {
Map commentMetaData;
if (context.containsKey(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA)) {
commentMetaData = (Map) context.get(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA);
} else {
commentMetaData = new HashMap();
}
commentMetaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE);
context.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA, commentMetaData);
_logger.info("Failed math comment authentication check.");
context.put(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_STATUS_MESSAGE, "Failed math comment authentication check.");
}
}
int operation = (int) (Math.random() * AVAILABLE_OPERATIONS);
int value1 = (int) (Math.random() * 50.0);
int value2 = (int) (Math.random() * 50.0);
int answer;
answer = getAnswerForOperation(value1, value2, operation);
httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE1, new Integer(value1));
httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE2, new Integer(value2));
httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER, new Integer(answer));
httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_OPERATION, getOperatorForOperation(operation));
}
}
protected int getAnswerForOperation(int value1, int value2, int operation) {
int answer;
switch (operation) {
case 0:
{
answer = value1 + value2;
break;
}
case 1:
{
answer = value1 - value2;
break;
}
case 2:
{
answer = value1 * value2;
break;
}
default:
{
answer = value1 + value2;
}
}
return answer;
}
protected String getOperatorForOperation(int operation) {
switch (operation) {
case 0:
{
return "+";
}
case 1:
{
return "-";
}
case 2:
{
return "*";
}
default:
{
return "+";
}
}
}
}