<commit_before>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.freemarker;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
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.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import freemarker.template.Configuration;

public class ContactMailController extends FreemarkerHttpServlet {
	private static final Log log = LogFactory
			.getLog(ContactMailController.class);
    private static final long serialVersionUID = 1L;
	
    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_USEREMAIL_PARAM = "webuseremail";
    private final static String COMMENTS_PARAM      = "s34gfd88p9x1";
	
    private final static String TEMPLATE_CONFIRMATION = "contactForm-confirmation.ftl";
    private final static String TEMPLATE_EMAIL = "contactForm-email.ftl";
    private final static String TEMPLATE_BACKUP = "contactForm-backup.ftl";
    private final static String TEMPLATE_ERROR = "contactForm-error.ftl";
    
    private static String smtpHost = null;

    public void init(ServletConfig servletConfig) throws javax.servlet.ServletException {
        super.init(servletConfig);
        smtpHost = getSmtpHostFromProperties();
    }
    
    public static boolean isSmtpHostConfigured() {
        if( smtpHost==null || smtpHost.equals("")) {
            return false;
        }
        return true;
    }

	public static String getSmtpHostFromProperties() {
		String host = ConfigurationProperties.getProperty("Vitro.smtpHost");
		if (host != null && !host.equals("")) {
			log.debug("Found Vitro.smtpHost value of " + host);
		} else {
			log.debug("No Vitro.smtpHost specified");
		}
		return (host != null && host.length() > 0) ? host : null;
	}
	
	@Override
    protected String getTitle(String siteName, VitroRequest vreq) {
        return siteName + " Feedback Form";
    }
    

    @Override
    protected ResponseValues processRequest(VitroRequest vreq) {
    	
        Portal portal = vreq.getPortal();
        String templateName = null;
        Map<String, Object> body = new HashMap<String, Object>();
        
        String statusMsg = null; // holds the error status
        
        if (!isSmtpHostConfigured()) {
            body.put("errorMessage", 
                    "This application has not yet been configured to send mail. " +
                    "An smtp host has not been specified in the configuration properties file.");
            templateName = TEMPLATE_ERROR; 
        }
        
        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;
            }
            
            else {<commit_msg>NIHVIVO-1261 Simplify the logic around smtpHost, to prepare for the change in ConfigurationProperties.<commit_after>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.freemarker;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
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.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import freemarker.template.Configuration;

public class ContactMailController extends FreemarkerHttpServlet {
	private static final Log log = LogFactory
			.getLog(ContactMailController.class);
    private static final long serialVersionUID = 1L;
	
    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_USEREMAIL_PARAM = "webuseremail";
    private final static String COMMENTS_PARAM      = "s34gfd88p9x1";
	
    private final static String TEMPLATE_CONFIRMATION = "contactForm-confirmation.ftl";
    private final static String TEMPLATE_EMAIL = "contactForm-email.ftl";
    private final static String TEMPLATE_BACKUP = "contactForm-backup.ftl";
    private final static String TEMPLATE_ERROR = "contactForm-error.ftl";
    
    private static String smtpHost = "";

    @Override
    public void init() {
        smtpHost = ConfigurationProperties.getProperty("Vitro.smtpHost", "");
		if (smtpHost.isEmpty()) {
			log.debug("No Vitro.smtpHost specified");
		} else {
			log.debug("Found Vitro.smtpHost value of " + smtpHost);
		}
    }
    
	@Override
    protected String getTitle(String siteName, VitroRequest vreq) {
        return siteName + " Feedback Form";
    }
    

    @Override
    protected ResponseValues processRequest(VitroRequest vreq) {
    	
        Portal portal = vreq.getPortal();
        String templateName = null;
        Map<String, Object> body = new HashMap<String, Object>();
        
        String statusMsg = null; // holds the error status
        
        if (smtpHost.isEmpty()) {
            body.put("errorMessage", 
                    "This application has not yet been configured to send mail. " +
                    "An smtp host has not been specified in the configuration properties file.");
            templateName = TEMPLATE_ERROR; 
        }
        
        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;
            }
            
            else {
{'fraction_non_alphanumeric': 0.084716459197787, 'fraction_numerical': 0.0024204702627939143, 'mean_word_length': 3.1516746411483254, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7396505', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2397, 'n_words': 575}