diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java index b7dc229d2..0224f6ae3 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java @@ -56,151 +56,117 @@ public class ContactMailController extends FreemarkerHttpServlet { @Override - protected ResponseValues processRequest(VitroRequest vreq) { + protected ResponseValues processRequest(VitroRequest vreq) { + if (!FreemarkerEmailFactory.isConfigured(vreq)) { + return errorNoSmtpServer(); + } + + String[] recipients = figureRecipients(vreq); + if (recipients.length == 0) { + return errorNoRecipients(); + } - String templateName = null; - Map body = new HashMap(); - - ApplicationBean appBean = vreq.getAppBean(); - - String statusMsg = null; // holds the error status - - if (!FreemarkerEmailFactory.isConfigured(vreq)) { - body.put("errorMessage", - "This application has not yet been configured to send mail. " + - "Email properties must be specified in the configuration properties file."); - templateName = TEMPLATE_ERROR; - } - - else { + String webusername = nonNullAndTrim(vreq, WEB_USERNAME_PARAM); + String webuseremail = nonNullAndTrim(vreq, WEB_USEREMAIL_PARAM); + String comments = nonNullAndTrim(vreq, COMMENTS_PARAM); + String formType = nonNullAndTrim(vreq, "DeliveryType"); - String webusername = vreq.getParameter(WEB_USERNAME_PARAM); - String webuseremail = vreq.getParameter(WEB_USEREMAIL_PARAM); - String comments = vreq.getParameter(COMMENTS_PARAM); - - String validationMessage = validateInput(webusername, webuseremail, - comments); - - if (validationMessage != null) { - // rjy7 We should reload the form, not go to the error page! - body.put("errorMessage", - "Invalid submission"); - templateName = TEMPLATE_ERROR; - } - - else { - webusername = webusername.trim(); - webuseremail = webuseremail.trim(); - comments = comments.trim(); - - String spamReason = null; - - String originalReferer = (String) vreq.getSession().getAttribute("contactFormReferer"); - if (originalReferer != null) { - vreq.getSession().removeAttribute("contactFormReferer"); - /* does not support legitimate clients that don't send the Referer header - String referer = request.getHeader("Referer"); - if (referer == null || - (referer.indexOf("comments") <0 - && referer.indexOf("correction") <0) ) { - spamReason = "The form was not submitted from the " + - "Contact Us or Corrections page."; - statusMsg = SPAM_MESSAGE; - } - */ - } else { - originalReferer = "none"; - } - - if (spamReason == null) { - spamReason = checkForSpam(comments); - if (spamReason != null) { - statusMsg = SPAM_MESSAGE; - } - } - - String formType = vreq.getParameter("DeliveryType"); - String[] deliverToArray = null; - int recipientCount = 0; - String deliveryfrom = null; - - if ("contact".equals(formType)) { - if (appBean.getContactMail() == null || appBean.getContactMail().trim().length()==0) { - log.error("No contact mail address defined"); - throw new Error( - "To establish the Contact Us mail capability the system administrators must " - + "specify an email address."); - } else { - deliverToArray = appBean.getContactMail().split(","); - } - deliveryfrom = "Message from the " + appBean.getApplicationName() + " Contact Form"; - } else { - deliverToArray = appBean.getContactMail().split(","); - statusMsg = SPAM_MESSAGE ; - spamReason = "The form specifies no delivery type."; - } - recipientCount=(deliverToArray == null) ? 0 : deliverToArray.length; - if (recipientCount == 0) { - log.error("recipientCount is 0 when DeliveryType specified as \""+formType+"\""); - throw new Error( - "To establish the Contact Us mail capability the system administrators must " - + "specify at least one email address."); - } - - Configuration config = (Configuration) vreq.getAttribute("freemarkerConfig"); - String msgText = composeEmail(webusername, webuseremail, comments, - deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq); - - // Write the email to a backup file - try { - FileWriter fw = new FileWriter(getServletContext().getRealPath(EMAIL_BACKUP_FILE_PATH),true); - PrintWriter outFile = new PrintWriter(fw); - writeBackupCopy(outFile, msgText, spamReason, config, vreq); - - Session s = FreemarkerEmailFactory.getEmailSession(vreq); - try { - - if (spamReason == null) { - sendMessage(s, webuseremail, webusername, deliverToArray, deliveryfrom, - recipientCount, msgText); - } - - } catch (AddressException e) { - statusMsg = "Please supply a valid email address."; - outFile.println( statusMsg ); - outFile.println( e.getMessage() ); - } catch (SendFailedException e) { - statusMsg = "The system was unable to deliver your mail. Please try again later. [SEND FAILED]"; - outFile.println( statusMsg ); - outFile.println( e.getMessage() ); - } catch (MessagingException e) { - statusMsg = "The system was unable to deliver your mail. Please try again later. [MESSAGING]"; - outFile.println( statusMsg ); - outFile.println( e.getMessage() ); - e.printStackTrace(); - } - - outFile.flush(); - outFile.close(); - } - catch (IOException e){ - log.error("Can't open file to write email backup"); - } - - // Message was sent successfully - if (statusMsg == null && spamReason == null) { - templateName = TEMPLATE_CONFIRMATION; - } else { - body.put("errorMessage", statusMsg); - templateName = TEMPLATE_ERROR; - } - } - } - - return new TemplateResponseValues(templateName, body); + if (validateInput(webusername, webuseremail, comments) != null) { + return errorParametersNotValid(); + } + + String spamReason = checkForSpam(comments, formType); + if (spamReason != null) { + return errorSpam(); + } + + return processValidRequest(vreq, webusername, webuseremail, recipients, comments); + } + + private String[] figureRecipients(VitroRequest vreq) { + String contactMailAddresses = vreq.getAppBean().getContactMail().trim(); + if ((contactMailAddresses == null) || contactMailAddresses.isEmpty()) { + return new String[0]; + } + + return contactMailAddresses.split(","); + } + + private ResponseValues processValidRequest(VitroRequest vreq, + String webusername, String webuseremail, String[] recipients, + String comments) throws Error { + String statusMsg = null; // holds the error status + + ApplicationBean appBean = vreq.getAppBean(); + String deliveryfrom = "Message from the " + appBean.getApplicationName() + " Contact Form"; + + String originalReferer = getOriginalRefererFromSession(vreq); + + Configuration config = (Configuration) vreq.getAttribute("freemarkerConfig"); + String msgText = composeEmail(webusername, webuseremail, comments, + deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq); + + try { + // Write the email to a backup file + FileWriter fw = new FileWriter(getServletContext().getRealPath(EMAIL_BACKUP_FILE_PATH),true); + PrintWriter outFile = new PrintWriter(fw); + writeBackupCopy(outFile, msgText, config, vreq); + + Session s = FreemarkerEmailFactory.getEmailSession(vreq); + + try { + sendMessage(s, webuseremail, webusername, recipients, deliveryfrom, msgText); + } catch (AddressException e) { + statusMsg = "Please supply a valid email address."; + outFile.println( statusMsg ); + outFile.println( e.getMessage() ); + } catch (SendFailedException e) { + statusMsg = "The system was unable to deliver your mail. Please try again later. [SEND FAILED]"; + outFile.println( statusMsg ); + outFile.println( e.getMessage() ); + } catch (MessagingException e) { + statusMsg = "The system was unable to deliver your mail. Please try again later. [MESSAGING]"; + outFile.println( statusMsg ); + outFile.println( e.getMessage() ); + e.printStackTrace(); + } + + outFile.close(); + } + catch (IOException e){ + log.error("Can't open file to write email backup"); + } + + if (statusMsg == null) { + // Message was sent successfully + return new TemplateResponseValues(TEMPLATE_CONFIRMATION); + } else { + Map body = new HashMap(); + body.put("errorMessage", statusMsg); + return new TemplateResponseValues(TEMPLATE_ERROR, body); + } + } + + private String getOriginalRefererFromSession(VitroRequest vreq) { + String originalReferer = (String) vreq.getSession().getAttribute("contactFormReferer"); + if (originalReferer != null) { + vreq.getSession().removeAttribute("contactFormReferer"); + /* does not support legitimate clients that don't send the Referer header + String referer = request.getHeader("Referer"); + if (referer == null || + (referer.indexOf("comments") <0 + && referer.indexOf("correction") <0) ) { + spamReason = "The form was not submitted from the " + + "Contact Us or Corrections page."; + statusMsg = SPAM_MESSAGE; + } + */ + } else { + originalReferer = "none"; + } + return originalReferer; + } - } - /** Intended to mangle url so it can get through spam filtering * http://host/dir/servlet?param=value -> host: dir/servlet?param=value */ public String stripProtocol( String in ){ @@ -236,18 +202,13 @@ public class ContactMailController extends FreemarkerHttpServlet { } private void writeBackupCopy(PrintWriter outFile, String msgText, - String spamReason, Configuration config, HttpServletRequest request) { + Configuration config, HttpServletRequest request) { Map backup = new HashMap(); String template = TEMPLATE_BACKUP; Calendar cal = Calendar.getInstance(); backup.put("datetime", cal.getTime().toString()); - - if (spamReason != null) { - backup.put("spamReason", spamReason); - } - backup.put("msgText", msgText); try { @@ -261,8 +222,7 @@ public class ContactMailController extends FreemarkerHttpServlet { } private void sendMessage(Session s, String webuseremail, String webusername, - String[] deliverToArray, String deliveryfrom, int recipientCount, - String msgText) + String[] recipients, String deliveryfrom, String msgText) throws AddressException, SendFailedException, MessagingException { // Construct the message MimeMessage msg = new MimeMessage( s ); @@ -278,14 +238,11 @@ public class ContactMailController extends FreemarkerHttpServlet { } // Set the recipient address - - if (recipientCount>0){ - InternetAddress[] address=new InternetAddress[recipientCount]; - for (int i=0; i -1 @@ -342,4 +307,34 @@ public class ContactMailController extends FreemarkerHttpServlet { return null; } + + private ResponseValues errorNoSmtpServer() { + Map body = new HashMap(); + body.put("errorMessage", + "This application has not yet been configured to send mail. " + + "Email properties must be specified in the configuration properties file."); + return new TemplateResponseValues(TEMPLATE_ERROR, body); + } + + private ResponseValues errorNoRecipients() { + Map body = new HashMap(); + body.put("errorMessage", "To establish the Contact Us mail capability " + + "the system administrators must specify " + + "at least one email address."); + return new TemplateResponseValues(TEMPLATE_ERROR, body); + } + + private ResponseValues errorParametersNotValid() { + // rjy7 We should reload the form, not go to the error page! + Map body = new HashMap(); + body.put("errorMessage", "Invalid submission"); + return new TemplateResponseValues(TEMPLATE_ERROR, body); + } + + private ResponseValues errorSpam() { + Map body = new HashMap(); + body.put("errorMessage", SPAM_MESSAGE); + return new TemplateResponseValues(TEMPLATE_ERROR, body); + } + }