NIHVIVO-1261 Simplify the logic around Vitro.smtpHost, to prepare for the change in ConfigurationProperties.

This commit is contained in:
jeb228 2011-02-24 16:53:18 +00:00
parent 854536c531
commit ec12d6c743
9 changed files with 47 additions and 77 deletions

View file

@ -19,18 +19,20 @@ import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet;
*/
public class CommentsController extends VitroHttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
@Override
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws IOException, ServletException {
super.doGet(request,response);
VitroRequest vreq = new VitroRequest(request);
try {
//this try block passes any errors to error.jsp
if (!ContactMailServlet.isSmtpHostConfigured()) {
if (!ContactMailServlet.isSmtpHostConfigured(request)) {
request.setAttribute("title", "Comments and Feedback Form");
request.setAttribute("bodyJsp", "/contact_err.jsp");// <<< this is where the body gets set
request.setAttribute("ERR","This application has not yet been configured to send mail -- " +

View file

@ -17,7 +17,6 @@ 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.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -29,6 +28,8 @@ import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
public class ContactMailServlet extends VitroHttpServlet {
public static final String SMTPHOST_PROPERTY = "Vitro.smtpHost";
private static final Log log = LogFactory.getLog(ContactMailServlet.class);
private final static String CONFIRM_PAGE = "/templates/parts/thankyou.jsp";
@ -42,28 +43,21 @@ public class ContactMailServlet extends VitroHttpServlet {
private static String smtpHost = null;
public void init(ServletConfig servletConfig) throws javax.servlet.ServletException {
super.init(servletConfig);
smtpHost = getSmtpHostFromProperties();
public static boolean isSmtpHostConfigured(HttpServletRequest req) {
return ConfigurationProperties.getProperty(SMTPHOST_PROPERTY, "").length() > 0;
}
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 {
@Override
public void init() {
smtpHost = ConfigurationProperties.getProperty(SMTPHOST_PROPERTY, "");
if (smtpHost.isEmpty()) {
log.debug("No Vitro.smtpHost specified");
} else {
log.debug("Found Vitro.smtpHost value of " + smtpHost);
}
return (host != null && host.length() > 0) ? host : null;
}
@Override
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {

View file

@ -16,7 +16,6 @@ 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.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -33,31 +32,20 @@ public class MailUsersServlet extends VitroHttpServlet {
public static HttpServletRequest request;
public static HttpServletRequest response;
private static String smtpHost = null;
private static String smtpHost = "";
// private static final Log log = LogFactory.getLog(ContactMailServlet.class.getName());
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;
}
private String getSmtpHostFromProperties() {
String host = ConfigurationProperties.getProperty("Vitro.smtpHost");
if (host != null && !host.equals("")) {
log.debug("Found Vitro.smtpHost value of " + host);
@Override
public void init() {
smtpHost = ConfigurationProperties.getProperty(ContactMailServlet.SMTPHOST_PROPERTY, "");
if (smtpHost.isEmpty()) {
log.debug("No Vitro.smtpHost specified");
} else {
log.warn("No Vitro.smtpHost specified");
log.debug("Found Vitro.smtpHost value of " + smtpHost);
}
return (host != null && host.length() > 0) ? host : null;
}
@Override
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
VitroRequest vreq = new VitroRequest(request);

View file

@ -19,18 +19,20 @@ import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet;
*/
public class UserMailController extends VitroHttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
@Override
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws IOException, ServletException {
super.doGet(request,response);
VitroRequest vreq = new VitroRequest(request);
try {
//this try block passes any errors to error.jsp
if (!ContactMailServlet.isSmtpHostConfigured()) {
if (!ContactMailServlet.isSmtpHostConfigured(request)) {
request.setAttribute("title", "Mail All Users Form");
request.setAttribute("bodyJsp", "/contact_err.jsp");// <<< this is where the body gets set
request.setAttribute("ERR","This application has not yet been configured to send mail -- " +

View file

@ -517,7 +517,7 @@ public class N3MultiPartUpload extends VitroHttpServlet {
String email = uDao.getUserEmailAddress(userURI);
String deliveryFrom = "hjk54@cornell.edu";//TO DO: replace with email address to be used
//Now send message
MailUtil mu = new MailUtil();
MailUtil mu = new MailUtil(request);
List<String> deliverToArray = new ArrayList<String>();
deliverToArray.add(email);

View file

@ -2,27 +2,19 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
import freemarker.template.Configuration;
/**
* Controller for comments ("contact us") page
@ -48,7 +40,7 @@ public class ContactFormController extends FreemarkerHttpServlet {
Portal portal = vreq.getPortal();
Map<String, Object> body = new HashMap<String, Object>();
if (!ContactMailServlet.isSmtpHostConfigured()) {
if (!ContactMailServlet.isSmtpHostConfigured(vreq)) {
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.");

View file

@ -27,6 +27,7 @@ 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.ContactMailServlet;
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;
@ -54,7 +55,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
@Override
public void init() {
smtpHost = ConfigurationProperties.getProperty("Vitro.smtpHost", "");
smtpHost = ConfigurationProperties.getProperty(ContactMailServlet.SMTPHOST_PROPERTY, "");
if (smtpHost.isEmpty()) {
log.debug("No Vitro.smtpHost specified");
} else {

View file

@ -256,7 +256,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
urls.put("base", UrlBuilder.contextPath);
urls.put("about", urlBuilder.getPortalUrl(Route.ABOUT));
if (ContactMailServlet.getSmtpHostFromProperties() != null) {
if (ContactMailServlet.isSmtpHostConfigured(vreq)) {
urls.put("contact", urlBuilder.getPortalUrl(Route.CONTACT));
}
urls.put("search", urlBuilder.getPortalUrl(Route.SEARCH));

View file

@ -11,18 +11,25 @@ import javax.mail.Session;
import javax.mail.Transport;
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.controller.ContactMailServlet;
public class MailUtil {
private static final Log log = LogFactory.getLog(MailUtil.class);
private String smtpHost = null;
public MailUtil(){
smtpHost = getSmtpHostFromProperties();
private String smtpHost = "";
public MailUtil(HttpServletRequest req){
smtpHost = ConfigurationProperties.getProperty(ContactMailServlet.SMTPHOST_PROPERTY, "");
if (smtpHost.isEmpty()) {
log.debug("No Vitro.smtpHost specified");
} else {
log.debug("Found Vitro.smtpHost value of " + smtpHost);
}
}
public void sendMessage(String messageText, String subject, String from, String to, List<String> deliverToArray) throws IOException{
@ -67,20 +74,4 @@ public class MailUtil {
}
}
public boolean isSmtpHostConfigured() {
if( smtpHost==null || smtpHost.equals("")) {
return false;
}
return true;
}
private String getSmtpHostFromProperties() {
String host = ConfigurationProperties.getProperty("Vitro.smtpHost");
if (host != null && !host.equals("")) {
log.debug("Found Vitro.smtpHost value of " + host);
} else {
log.error("No Vitro.smtpHost specified");
}
return (host != null && host.length() > 0) ? host : null;
}
}