package org.gcube.portlets.user; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import javax.portlet.ProcessAction; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import javax.servlet.http.HttpServletRequest; import org.gcube.common.portal.mailing.EmailNotification; import com.liferay.portal.kernel.captcha.CaptchaException; import com.liferay.portal.kernel.captcha.CaptchaUtil; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.util.PortalUtil; import com.liferay.util.bridges.mvc.MVCPortlet; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; /** * Portlet implementation class CaptchaDemo */ public class MailForm extends MVCPortlet { @ProcessAction(name = "validateCaptcha") public void validateCaptcha(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String name = ParamUtil.getString(actionRequest, "name"); String email = ParamUtil.getString(actionRequest, "email"); String message = ParamUtil.getString(actionRequest, "message"); System.out.println("name :" + name); System.out.println("Email :" + email); System.out.println("message :" + message); HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); String hostname = PortalUtil.getHost(actionRequest); String emailAddress = "info@"+ hostname.split("www.")[1]; try { /** * This is the actual code which validate captcha data entered by user. */ CaptchaUtil.check(actionRequest); System.out.println("CAPTCHA validated successfully"); } catch (CaptchaException e) { e.printStackTrace(); SessionErrors.add(actionRequest, "errorMessage"); } String bodyTextHTML = "

From: " + name + "

"; bodyTextHTML += "

Email: " + email + "

"; bodyTextHTML += "

Message:

" + message + "

"; String validEmail = "info@d4science.org"; //fallback if (isValidEmailAddress(emailAddress)) { validEmail = emailAddress; } EmailNotification emailNotification = new EmailNotification(validEmail, "Feedback from Contact us Web Form", bodyTextHTML, request); emailNotification.sendEmail(); } public static boolean isValidEmailAddress(String email) { boolean result = true; try { InternetAddress emailAddr = new InternetAddress(email); emailAddr.validate(); } catch (AddressException ex) { result = false; } return result; } /** * The below code is responsible for rendering the CAPTCHA image */ @Override public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { try { CaptchaUtil.serveImage(resourceRequest, resourceResponse); } catch (Exception e) { System.out.println(e.getMessage()); } } }