KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > util > MathCommentAuthenticator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.util;
20
21 import java.util.ResourceBundle JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28 /**
29  * Asks the commenter to answer a simple math question.
30  */

31 public class MathCommentAuthenticator implements CommentAuthenticator {
32     
33     private transient ResourceBundle JavaDoc bundle =
34             ResourceBundle.getBundle("ApplicationResources");
35     
36     private static Log mLogger = LogFactory.getLog(MathCommentAuthenticator.class);
37     
38     
39     public String JavaDoc getHtml(HttpServletRequest JavaDoc request) {
40         
41         String JavaDoc answer = "";
42         
43         HttpSession JavaDoc session = request.getSession(true);
44         if (session.getAttribute("mathAnswer") == null) {
45             // starting a new test
46
int value1 = (int)(Math.random()*10.0);
47             int value2 = (int)(Math.random()*100.0);
48             int sum = value1 + value2;
49             session.setAttribute("mathValue1", new Integer JavaDoc(value1));
50             session.setAttribute("mathValue2", new Integer JavaDoc(value2));
51             session.setAttribute("mathAnswer", new Integer JavaDoc(sum));
52         } else {
53             // preserve user's answer
54
answer = request.getParameter("answer");
55             answer = (answer == null) ? "" : answer;
56         }
57         
58         // pull existing values out of session
59
Integer JavaDoc value1o = (Integer JavaDoc)request.getSession().getAttribute("mathValue1");
60         Integer JavaDoc value2o = (Integer JavaDoc)request.getSession().getAttribute("mathValue2");
61         
62         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
63         
64         sb.append("<p>");
65         sb.append(bundle.getString("comments.mathAuthenticatorQuestion"));
66         sb.append("</p><p>");
67         sb.append(value1o);
68         sb.append(" + ");
69         sb.append(value2o);
70         sb.append(" = ");
71         sb.append("<input name=\"answer\" value=\"");
72         sb.append(answer);
73         sb.append("\" /></p>");
74         
75         return sb.toString();
76     }
77     
78     
79     public boolean authenticate(HttpServletRequest JavaDoc request) {
80         
81         boolean authentic = false;
82         
83         HttpSession JavaDoc session = request.getSession(false);
84         String JavaDoc answerString = request.getParameter("answer");
85         
86         if (answerString != null && session != null) {
87             try {
88                 int answer = Integer.parseInt(answerString);
89                 Integer JavaDoc sum = (Integer JavaDoc) session.getAttribute("mathAnswer");
90                 
91                 if (sum != null && answer == sum.intValue()) {
92                     authentic = true;
93                     session.removeAttribute("mathAnswer");
94                     session.removeAttribute("mathValue1");
95                     session.removeAttribute("mathValue2");
96                 }
97             } catch (NumberFormatException JavaDoc ignored) {
98                 // ignored ... someone is just really bad at math
99
} catch (Exception JavaDoc e) {
100                 // unexpected
101
mLogger.error(e);
102             }
103         }
104         
105         return authentic;
106     }
107     
108 }
109
110
Popular Tags