Unravel some of the spaghetti logic.
This commit is contained in:
parent
ff35554bf8
commit
563593981f
1 changed files with 158 additions and 163 deletions
|
@ -57,44 +57,97 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
|
||||
@Override
|
||||
protected ResponseValues processRequest(VitroRequest vreq) {
|
||||
if (!FreemarkerEmailFactory.isConfigured(vreq)) {
|
||||
return errorNoSmtpServer();
|
||||
}
|
||||
|
||||
String templateName = null;
|
||||
Map<String, Object> body = new HashMap<String, Object>();
|
||||
String[] recipients = figureRecipients(vreq);
|
||||
if (recipients.length == 0) {
|
||||
return errorNoRecipients();
|
||||
}
|
||||
|
||||
ApplicationBean appBean = vreq.getAppBean();
|
||||
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");
|
||||
|
||||
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
|
||||
|
||||
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;
|
||||
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();
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
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;
|
||||
outFile.close();
|
||||
}
|
||||
catch (IOException e){
|
||||
log.error("Can't open file to write email backup");
|
||||
}
|
||||
|
||||
else {
|
||||
webusername = webusername.trim();
|
||||
webuseremail = webuseremail.trim();
|
||||
comments = comments.trim();
|
||||
|
||||
String spamReason = null;
|
||||
if (statusMsg == null) {
|
||||
// Message was sent successfully
|
||||
return new TemplateResponseValues(TEMPLATE_CONFIRMATION);
|
||||
} else {
|
||||
Map<String, Object> body = new HashMap<String, Object>();
|
||||
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");
|
||||
|
@ -111,94 +164,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
} 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);
|
||||
|
||||
return originalReferer;
|
||||
}
|
||||
|
||||
/** Intended to mangle url so it can get through spam filtering
|
||||
|
@ -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<String, Object> backup = new HashMap<String, Object>();
|
||||
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<recipientCount; i++){
|
||||
address[i] = new InternetAddress(deliverToArray[i]);
|
||||
InternetAddress[] address=new InternetAddress[recipients.length];
|
||||
for (int i=0; i<recipients.length; i++){
|
||||
address[i] = new InternetAddress(recipients[i]);
|
||||
}
|
||||
msg.setRecipients( Message.RecipientType.TO, address );
|
||||
}
|
||||
|
||||
// Set the subject and text
|
||||
msg.setSubject( deliveryfrom );
|
||||
|
@ -300,18 +257,23 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
|
||||
}
|
||||
|
||||
private String nonNullAndTrim(HttpServletRequest req, String key) {
|
||||
String value = req.getParameter(key);
|
||||
return (value == null) ? "" : value.trim();
|
||||
}
|
||||
|
||||
private String validateInput(String webusername, String webuseremail,
|
||||
String comments) {
|
||||
|
||||
if( webusername == null || "".equals(webusername.trim()) ){
|
||||
if( webusername.isEmpty() ){
|
||||
return "A proper webusername field was not found in the form submitted.";
|
||||
}
|
||||
|
||||
if( webuseremail == null || "".equals(webuseremail.trim()) ){
|
||||
if( webuseremail.isEmpty() ){
|
||||
return "A proper webuser email field was not found in the form submitted.";
|
||||
}
|
||||
|
||||
if (comments==null || "".equals(comments.trim())) {
|
||||
if (comments.isEmpty()) {
|
||||
return "The proper comments field was not found in the form submitted.";
|
||||
}
|
||||
|
||||
|
@ -319,11 +281,14 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param request
|
||||
* @return null if message not judged to be spam, otherwise a String
|
||||
* containing the reason the message was flagged as spam.
|
||||
*/
|
||||
private String checkForSpam(String comments) {
|
||||
private String checkForSpam(String comments, String formType) {
|
||||
/* If the form doesn't specify a delivery type, treat as spam. */
|
||||
if (!"contact".equals(formType)) {
|
||||
return "The form specifies no delivery type.";
|
||||
}
|
||||
|
||||
/* if this blog markup is found, treat comment as blog spam */
|
||||
if (
|
||||
|
@ -342,4 +307,34 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
return null;
|
||||
|
||||
}
|
||||
|
||||
private ResponseValues errorNoSmtpServer() {
|
||||
Map<String, Object> body = new HashMap<String, Object>();
|
||||
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<String, Object> body = new HashMap<String, Object>();
|
||||
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<String, Object> body = new HashMap<String, Object>();
|
||||
body.put("errorMessage", "Invalid submission");
|
||||
return new TemplateResponseValues(TEMPLATE_ERROR, body);
|
||||
}
|
||||
|
||||
private ResponseValues errorSpam() {
|
||||
Map<String, Object> body = new HashMap<String, Object>();
|
||||
body.put("errorMessage", SPAM_MESSAGE);
|
||||
return new TemplateResponseValues(TEMPLATE_ERROR, body);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue