From ec12d6c74371b3992445bf11e29d7344f87c4ca6 Mon Sep 17 00:00:00 2001 From: jeb228 Date: Thu, 24 Feb 2011 16:53:18 +0000 Subject: [PATCH] NIHVIVO-1261 Simplify the logic around Vitro.smtpHost, to prepare for the change in ConfigurationProperties. --- .../webapp/controller/CommentsController.java | 6 ++-- .../webapp/controller/ContactMailServlet.java | 32 ++++++++----------- .../webapp/controller/MailUsersServlet.java | 32 ++++++------------- .../webapp/controller/UserMailController.java | 8 +++-- .../controller/edit/N3MultiPartUpload.java | 2 +- .../freemarker/ContactFormController.java | 10 +----- .../freemarker/ContactMailController.java | 3 +- .../freemarker/FreemarkerHttpServlet.java | 2 +- .../mannlib/vitro/webapp/utils/MailUtil.java | 29 ++++++----------- 9 files changed, 47 insertions(+), 77 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/CommentsController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/CommentsController.java index a1757ab01..dc21bfd12 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/CommentsController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/CommentsController.java @@ -18,19 +18,21 @@ import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet; * * @author bjl23 */ 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 -- " + diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/ContactMailServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/ContactMailServlet.java index 5301b74ed..013ea2d1d 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/ContactMailServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/ContactMailServlet.java @@ -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; + @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); + } } - 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 public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java index d08a85e67..69df59d85 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java @@ -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,32 +32,21 @@ 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; - } + } - public void doGet( HttpServletRequest request, HttpServletResponse response ) + @Override + public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { VitroRequest vreq = new VitroRequest(request); Portal portal = vreq.getPortal(); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/UserMailController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/UserMailController.java index 45449cf45..4a2572ba3 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/UserMailController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/UserMailController.java @@ -19,18 +19,20 @@ import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet; */ public class UserMailController extends VitroHttpServlet{ - public void doPost(HttpServletRequest request, HttpServletResponse response) + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } - public void doGet( HttpServletRequest request, HttpServletResponse 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 -- " + diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java index 6dee80e07..d0804c992 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java @@ -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 deliverToArray = new ArrayList(); deliverToArray.add(email); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactFormController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactFormController.java index b365fc8a1..9a45e82a5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactFormController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactFormController.java @@ -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 body = new HashMap(); - 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."); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java index 1d6418565..b6f1c1de7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java @@ -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 { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java index d4d5ea8c4..a78cfa063 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java @@ -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)); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java index f77d4b40f..225c21aef 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java @@ -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 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; - } }