@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
Feedback obj = new Feedback();
TreeMap<String, String> check = new TreeMap<String, String>();
String name = req.getParameter("name");
String email = req.getParameter("email");
String title = req.getParameter("title");
String message = req.getParameter("message");
// check name.
if (name == null || name.length() == 0) {
check.put("name", "Please enter your name.");
} else {
obj.setName(name);
}
// check mail
if (email == null || email.length() == 0) {
check.put("email", "Please enter your email.");
} else {
Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
Matcher m = pattern.matcher(email);
boolean matchFound = m.matches();
if (!matchFound) {
check.put("email", "Please enter exactly email.");
} else {
obj.setEmail(email);
}
}
// check title.
if (title == null || title.length() == 0) {
check.put("title", "Please enter message title.");
} else {
obj.setTitle(title);
}
// check message.
if (message == null || message.length() == 0) {
check.put("message", "Please enter message content.");
} else {
obj.setMessage(message);
}
// check cap.cha
String remoteAddr = req.getRemoteAddr();
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey("6LfUNtESAAAAANSiC1e3dD974bMUFY21S5QANakS");
String challenge = req.getParameter("recaptcha_challenge_field");
String uresponse = req.getParameter("recaptcha_response_field");
ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
remoteAddr, challenge, uresponse);
if (!reCaptchaResponse.isValid()) {
check.put("capcha", "Please enter exactly what you see.");
}
if (check.size() > 0) {
req.setAttribute("feedback", obj);
req.setAttribute("check", check);
} else {
obj.setStatus(1);
obj.setId(IdUniqueHelper.getId());
FeedbackModel.insert(obj);
FeedbackModel.clearModelCache();
req.setAttribute("success",
"Your feedback has been sent, we'll reply as soon as possible.");
}