Unravel some of the spaghetti logic.
This commit is contained in:
parent
ff35554bf8
commit
563593981f
1 changed files with 158 additions and 163 deletions
|
@ -56,151 +56,117 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@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;
|
String webusername = nonNullAndTrim(vreq, WEB_USERNAME_PARAM);
|
||||||
Map<String, Object> body = new HashMap<String, Object>();
|
String webuseremail = nonNullAndTrim(vreq, WEB_USEREMAIL_PARAM);
|
||||||
|
String comments = nonNullAndTrim(vreq, COMMENTS_PARAM);
|
||||||
ApplicationBean appBean = vreq.getAppBean();
|
String formType = nonNullAndTrim(vreq, "DeliveryType");
|
||||||
|
|
||||||
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 = vreq.getParameter(WEB_USERNAME_PARAM);
|
if (validateInput(webusername, webuseremail, comments) != null) {
|
||||||
String webuseremail = vreq.getParameter(WEB_USEREMAIL_PARAM);
|
return errorParametersNotValid();
|
||||||
String comments = vreq.getParameter(COMMENTS_PARAM);
|
}
|
||||||
|
|
||||||
String validationMessage = validateInput(webusername, webuseremail,
|
String spamReason = checkForSpam(comments, formType);
|
||||||
comments);
|
if (spamReason != null) {
|
||||||
|
return errorSpam();
|
||||||
if (validationMessage != null) {
|
}
|
||||||
// rjy7 We should reload the form, not go to the error page!
|
|
||||||
body.put("errorMessage",
|
return processValidRequest(vreq, webusername, webuseremail, recipients, comments);
|
||||||
"Invalid submission");
|
}
|
||||||
templateName = TEMPLATE_ERROR;
|
|
||||||
}
|
private String[] figureRecipients(VitroRequest vreq) {
|
||||||
|
String contactMailAddresses = vreq.getAppBean().getContactMail().trim();
|
||||||
else {
|
if ((contactMailAddresses == null) || contactMailAddresses.isEmpty()) {
|
||||||
webusername = webusername.trim();
|
return new String[0];
|
||||||
webuseremail = webuseremail.trim();
|
}
|
||||||
comments = comments.trim();
|
|
||||||
|
return contactMailAddresses.split(",");
|
||||||
String spamReason = null;
|
}
|
||||||
|
|
||||||
String originalReferer = (String) vreq.getSession().getAttribute("contactFormReferer");
|
private ResponseValues processValidRequest(VitroRequest vreq,
|
||||||
if (originalReferer != null) {
|
String webusername, String webuseremail, String[] recipients,
|
||||||
vreq.getSession().removeAttribute("contactFormReferer");
|
String comments) throws Error {
|
||||||
/* does not support legitimate clients that don't send the Referer header
|
String statusMsg = null; // holds the error status
|
||||||
String referer = request.getHeader("Referer");
|
|
||||||
if (referer == null ||
|
ApplicationBean appBean = vreq.getAppBean();
|
||||||
(referer.indexOf("comments") <0
|
String deliveryfrom = "Message from the " + appBean.getApplicationName() + " Contact Form";
|
||||||
&& referer.indexOf("correction") <0) ) {
|
|
||||||
spamReason = "The form was not submitted from the " +
|
String originalReferer = getOriginalRefererFromSession(vreq);
|
||||||
"Contact Us or Corrections page.";
|
|
||||||
statusMsg = SPAM_MESSAGE;
|
Configuration config = (Configuration) vreq.getAttribute("freemarkerConfig");
|
||||||
}
|
String msgText = composeEmail(webusername, webuseremail, comments,
|
||||||
*/
|
deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq);
|
||||||
} else {
|
|
||||||
originalReferer = "none";
|
try {
|
||||||
}
|
// Write the email to a backup file
|
||||||
|
FileWriter fw = new FileWriter(getServletContext().getRealPath(EMAIL_BACKUP_FILE_PATH),true);
|
||||||
if (spamReason == null) {
|
PrintWriter outFile = new PrintWriter(fw);
|
||||||
spamReason = checkForSpam(comments);
|
writeBackupCopy(outFile, msgText, config, vreq);
|
||||||
if (spamReason != null) {
|
|
||||||
statusMsg = SPAM_MESSAGE;
|
Session s = FreemarkerEmailFactory.getEmailSession(vreq);
|
||||||
}
|
|
||||||
}
|
try {
|
||||||
|
sendMessage(s, webuseremail, webusername, recipients, deliveryfrom, msgText);
|
||||||
String formType = vreq.getParameter("DeliveryType");
|
} catch (AddressException e) {
|
||||||
String[] deliverToArray = null;
|
statusMsg = "Please supply a valid email address.";
|
||||||
int recipientCount = 0;
|
outFile.println( statusMsg );
|
||||||
String deliveryfrom = null;
|
outFile.println( e.getMessage() );
|
||||||
|
} catch (SendFailedException e) {
|
||||||
if ("contact".equals(formType)) {
|
statusMsg = "The system was unable to deliver your mail. Please try again later. [SEND FAILED]";
|
||||||
if (appBean.getContactMail() == null || appBean.getContactMail().trim().length()==0) {
|
outFile.println( statusMsg );
|
||||||
log.error("No contact mail address defined");
|
outFile.println( e.getMessage() );
|
||||||
throw new Error(
|
} catch (MessagingException e) {
|
||||||
"To establish the Contact Us mail capability the system administrators must "
|
statusMsg = "The system was unable to deliver your mail. Please try again later. [MESSAGING]";
|
||||||
+ "specify an email address.");
|
outFile.println( statusMsg );
|
||||||
} else {
|
outFile.println( e.getMessage() );
|
||||||
deliverToArray = appBean.getContactMail().split(",");
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
deliveryfrom = "Message from the " + appBean.getApplicationName() + " Contact Form";
|
|
||||||
} else {
|
outFile.close();
|
||||||
deliverToArray = appBean.getContactMail().split(",");
|
}
|
||||||
statusMsg = SPAM_MESSAGE ;
|
catch (IOException e){
|
||||||
spamReason = "The form specifies no delivery type.";
|
log.error("Can't open file to write email backup");
|
||||||
}
|
}
|
||||||
recipientCount=(deliverToArray == null) ? 0 : deliverToArray.length;
|
|
||||||
if (recipientCount == 0) {
|
if (statusMsg == null) {
|
||||||
log.error("recipientCount is 0 when DeliveryType specified as \""+formType+"\"");
|
// Message was sent successfully
|
||||||
throw new Error(
|
return new TemplateResponseValues(TEMPLATE_CONFIRMATION);
|
||||||
"To establish the Contact Us mail capability the system administrators must "
|
} else {
|
||||||
+ "specify at least one email address.");
|
Map<String, Object> body = new HashMap<String, Object>();
|
||||||
}
|
body.put("errorMessage", statusMsg);
|
||||||
|
return new TemplateResponseValues(TEMPLATE_ERROR, body);
|
||||||
Configuration config = (Configuration) vreq.getAttribute("freemarkerConfig");
|
}
|
||||||
String msgText = composeEmail(webusername, webuseremail, comments,
|
}
|
||||||
deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq);
|
|
||||||
|
private String getOriginalRefererFromSession(VitroRequest vreq) {
|
||||||
// Write the email to a backup file
|
String originalReferer = (String) vreq.getSession().getAttribute("contactFormReferer");
|
||||||
try {
|
if (originalReferer != null) {
|
||||||
FileWriter fw = new FileWriter(getServletContext().getRealPath(EMAIL_BACKUP_FILE_PATH),true);
|
vreq.getSession().removeAttribute("contactFormReferer");
|
||||||
PrintWriter outFile = new PrintWriter(fw);
|
/* does not support legitimate clients that don't send the Referer header
|
||||||
writeBackupCopy(outFile, msgText, spamReason, config, vreq);
|
String referer = request.getHeader("Referer");
|
||||||
|
if (referer == null ||
|
||||||
Session s = FreemarkerEmailFactory.getEmailSession(vreq);
|
(referer.indexOf("comments") <0
|
||||||
try {
|
&& referer.indexOf("correction") <0) ) {
|
||||||
|
spamReason = "The form was not submitted from the " +
|
||||||
if (spamReason == null) {
|
"Contact Us or Corrections page.";
|
||||||
sendMessage(s, webuseremail, webusername, deliverToArray, deliveryfrom,
|
statusMsg = SPAM_MESSAGE;
|
||||||
recipientCount, msgText);
|
}
|
||||||
}
|
*/
|
||||||
|
} else {
|
||||||
} catch (AddressException e) {
|
originalReferer = "none";
|
||||||
statusMsg = "Please supply a valid email address.";
|
}
|
||||||
outFile.println( statusMsg );
|
return originalReferer;
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Intended to mangle url so it can get through spam filtering
|
/** Intended to mangle url so it can get through spam filtering
|
||||||
* http://host/dir/servlet?param=value -> host: dir/servlet?param=value */
|
* http://host/dir/servlet?param=value -> host: dir/servlet?param=value */
|
||||||
public String stripProtocol( String in ){
|
public String stripProtocol( String in ){
|
||||||
|
@ -236,18 +202,13 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeBackupCopy(PrintWriter outFile, String msgText,
|
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>();
|
Map<String, Object> backup = new HashMap<String, Object>();
|
||||||
String template = TEMPLATE_BACKUP;
|
String template = TEMPLATE_BACKUP;
|
||||||
|
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
backup.put("datetime", cal.getTime().toString());
|
backup.put("datetime", cal.getTime().toString());
|
||||||
|
|
||||||
if (spamReason != null) {
|
|
||||||
backup.put("spamReason", spamReason);
|
|
||||||
}
|
|
||||||
|
|
||||||
backup.put("msgText", msgText);
|
backup.put("msgText", msgText);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -261,8 +222,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendMessage(Session s, String webuseremail, String webusername,
|
private void sendMessage(Session s, String webuseremail, String webusername,
|
||||||
String[] deliverToArray, String deliveryfrom, int recipientCount,
|
String[] recipients, String deliveryfrom, String msgText)
|
||||||
String msgText)
|
|
||||||
throws AddressException, SendFailedException, MessagingException {
|
throws AddressException, SendFailedException, MessagingException {
|
||||||
// Construct the message
|
// Construct the message
|
||||||
MimeMessage msg = new MimeMessage( s );
|
MimeMessage msg = new MimeMessage( s );
|
||||||
|
@ -278,14 +238,11 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the recipient address
|
// Set the recipient address
|
||||||
|
InternetAddress[] address=new InternetAddress[recipients.length];
|
||||||
if (recipientCount>0){
|
for (int i=0; i<recipients.length; i++){
|
||||||
InternetAddress[] address=new InternetAddress[recipientCount];
|
address[i] = new InternetAddress(recipients[i]);
|
||||||
for (int i=0; i<recipientCount; i++){
|
|
||||||
address[i] = new InternetAddress(deliverToArray[i]);
|
|
||||||
}
|
|
||||||
msg.setRecipients( Message.RecipientType.TO, address );
|
|
||||||
}
|
}
|
||||||
|
msg.setRecipients( Message.RecipientType.TO, address );
|
||||||
|
|
||||||
// Set the subject and text
|
// Set the subject and text
|
||||||
msg.setSubject( deliveryfrom );
|
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,
|
private String validateInput(String webusername, String webuseremail,
|
||||||
String comments) {
|
String comments) {
|
||||||
|
|
||||||
if( webusername == null || "".equals(webusername.trim()) ){
|
if( webusername.isEmpty() ){
|
||||||
return "A proper webusername field was not found in the form submitted.";
|
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.";
|
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.";
|
return "The proper comments field was not found in the form submitted.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,12 +281,15 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param request
|
|
||||||
* @return null if message not judged to be spam, otherwise a String
|
* @return null if message not judged to be spam, otherwise a String
|
||||||
* containing the reason the message was flagged as spam.
|
* 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 this blog markup is found, treat comment as blog spam */
|
||||||
if (
|
if (
|
||||||
(comments.indexOf("[/url]") > -1
|
(comments.indexOf("[/url]") > -1
|
||||||
|
@ -342,4 +307,34 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
||||||
return null;
|
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