NIHVIVO-2534 Store the contact form emails in the vitro home directory, not inside Tomcat.

This commit is contained in:
j2blake 2012-03-07 16:54:00 +00:00
parent 7da535e70a
commit eb0b921b9f
2 changed files with 44 additions and 7 deletions

View file

@ -128,8 +128,6 @@ public class MailUsersServlet extends VitroHttpServlet {
msgBuf.append("</html>" + lineSeparator ); msgBuf.append("</html>" + lineSeparator );
String msgText = msgBuf.toString(); String msgText = msgBuf.toString();
// debugging
//PrintWriter outFile = new PrintWriter (new FileWriter(request.getSession().getServletContext().getRealPath("/WEB-INF/LatestMessage.html"),true)); //autoflush
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();

View file

@ -2,6 +2,7 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker; package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -25,6 +26,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean; import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper.TemplateProcessingException; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper.TemplateProcessingException;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
@ -38,7 +40,6 @@ public class ContactMailController extends FreemarkerHttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final static String SPAM_MESSAGE = "Your message was flagged as spam."; private final static String SPAM_MESSAGE = "Your message was flagged as spam.";
private final static String EMAIL_BACKUP_FILE_PATH = "/WEB-INF/LatestMessage.html";
private final static String WEB_USERNAME_PARAM = "webusername"; private final static String WEB_USERNAME_PARAM = "webusername";
private final static String WEB_USEREMAIL_PARAM = "webuseremail"; private final static String WEB_USEREMAIL_PARAM = "webuseremail";
@ -49,6 +50,10 @@ public class ContactMailController extends FreemarkerHttpServlet {
private final static String TEMPLATE_BACKUP = "contactForm-backup.ftl"; private final static String TEMPLATE_BACKUP = "contactForm-backup.ftl";
private final static String TEMPLATE_ERROR = "contactForm-error.ftl"; private final static String TEMPLATE_ERROR = "contactForm-error.ftl";
private static final String PROPERTY_VITRO_HOME_DIR = "vitro.home.directory";
private static final String EMAIL_JOURNAL_FILE_DIR = "emailJournal";
private static final String EMAIL_JOURNAL_FILE_NAME = "contactFormEmails.html";
@Override @Override
protected String getTitle(String siteName, VitroRequest vreq) { protected String getTitle(String siteName, VitroRequest vreq) {
return siteName + " Feedback Form"; return siteName + " Feedback Form";
@ -107,14 +112,14 @@ public class ContactMailController extends FreemarkerHttpServlet {
deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq); deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq);
try { try {
// Write the email to a backup file // Write the message to the journal file
FileWriter fw = new FileWriter(getServletContext().getRealPath(EMAIL_BACKUP_FILE_PATH),true); FileWriter fw = new FileWriter(locateTheJournalFile(vreq),true);
PrintWriter outFile = new PrintWriter(fw); PrintWriter outFile = new PrintWriter(fw);
writeBackupCopy(outFile, msgText, config, vreq); writeBackupCopy(outFile, msgText, config, vreq);
Session s = FreemarkerEmailFactory.getEmailSession(vreq);
try { try {
// Send the message
Session s = FreemarkerEmailFactory.getEmailSession(vreq);
sendMessage(s, webuseremail, webusername, recipients, deliveryfrom, msgText); sendMessage(s, webuseremail, webusername, recipients, deliveryfrom, msgText);
} catch (AddressException e) { } catch (AddressException e) {
statusMsg = "Please supply a valid email address."; statusMsg = "Please supply a valid email address.";
@ -147,6 +152,40 @@ public class ContactMailController extends FreemarkerHttpServlet {
} }
} }
/**
* The journal file belongs in a sub-directory of the Vitro home directory.
* If the sub-directory doesn't exist, create it.
*/
private File locateTheJournalFile(VitroRequest vreq) {
String homeDirPath = ConfigurationProperties.getBean(vreq).getProperty(
PROPERTY_VITRO_HOME_DIR);
if (homeDirPath == null) {
throw new IllegalArgumentException(
"Configuration properties must contain a value for '"
+ PROPERTY_VITRO_HOME_DIR + "'");
}
File homeDir = new File(homeDirPath);
if (!homeDir.exists()) {
throw new IllegalStateException("Vitro home directory '"
+ homeDir.getAbsolutePath() + "' does not exist.");
}
File journalDir = new File(homeDir, EMAIL_JOURNAL_FILE_DIR);
if (!journalDir.exists()) {
boolean created = journalDir.mkdir();
if (!created) {
throw new IllegalStateException(
"Unable to create email journal directory at '"
+ journalDir + "'");
}
}
File journalFile = new File(journalDir, EMAIL_JOURNAL_FILE_NAME);
return journalFile;
}
private String getOriginalRefererFromSession(VitroRequest vreq) { private String getOriginalRefererFromSession(VitroRequest vreq) {
String originalReferer = (String) vreq.getSession().getAttribute("contactFormReferer"); String originalReferer = (String) vreq.getSession().getAttribute("contactFormReferer");
if (originalReferer != null) { if (originalReferer != null) {