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

@ -18,19 +18,21 @@ import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet;
* * @author bjl23 * * @author bjl23
*/ */
public class CommentsController extends VitroHttpServlet{ public class CommentsController extends VitroHttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
doGet(request, response); doGet(request, response);
} }
@Override
public void doGet( HttpServletRequest request, HttpServletResponse response ) public void doGet( HttpServletRequest request, HttpServletResponse response )
throws IOException, ServletException { throws IOException, ServletException {
super.doGet(request,response); super.doGet(request,response);
VitroRequest vreq = new VitroRequest(request); VitroRequest vreq = new VitroRequest(request);
try { try {
//this try block passes any errors to error.jsp //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("title", "Comments and Feedback Form");
request.setAttribute("bodyJsp", "/contact_err.jsp");// <<< this is where the body gets set 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 -- " + 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.AddressException;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -29,6 +28,8 @@ import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.beans.Portal;
public class ContactMailServlet extends VitroHttpServlet { public class ContactMailServlet extends VitroHttpServlet {
public static final String SMTPHOST_PROPERTY = "Vitro.smtpHost";
private static final Log log = LogFactory.getLog(ContactMailServlet.class); private static final Log log = LogFactory.getLog(ContactMailServlet.class);
private final static String CONFIRM_PAGE = "/templates/parts/thankyou.jsp"; private final static String CONFIRM_PAGE = "/templates/parts/thankyou.jsp";
@ -42,28 +43,21 @@ public class ContactMailServlet extends VitroHttpServlet {
private static String smtpHost = null; private static String smtpHost = null;
public void init(ServletConfig servletConfig) throws javax.servlet.ServletException { public static boolean isSmtpHostConfigured(HttpServletRequest req) {
super.init(servletConfig); return ConfigurationProperties.getProperty(SMTPHOST_PROPERTY, "").length() > 0;
smtpHost = getSmtpHostFromProperties();
} }
public static boolean isSmtpHostConfigured() { @Override
if( smtpHost==null || smtpHost.equals("")) { public void init() {
return false; smtpHost = ConfigurationProperties.getProperty(SMTPHOST_PROPERTY, "");
} if (smtpHost.isEmpty()) {
return true; log.debug("No Vitro.smtpHost specified");
} else {
log.debug("Found Vitro.smtpHost value of " + smtpHost);
}
} }
public static String getSmtpHostFromProperties() { @Override
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;
}
public void doGet( HttpServletRequest request, HttpServletResponse response ) public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException { throws ServletException, IOException {

View file

@ -16,7 +16,6 @@ import javax.mail.Transport;
import javax.mail.internet.AddressException; import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -33,32 +32,21 @@ public class MailUsersServlet extends VitroHttpServlet {
public static HttpServletRequest request; public static HttpServletRequest request;
public static HttpServletRequest response; public static HttpServletRequest response;
private static String smtpHost = null; private static String smtpHost = "";
// private static final Log log = LogFactory.getLog(ContactMailServlet.class.getName()); // private static final Log log = LogFactory.getLog(ContactMailServlet.class.getName());
public void init(ServletConfig servletConfig) throws javax.servlet.ServletException { @Override
super.init(servletConfig); public void init() {
smtpHost = getSmtpHostFromProperties(); smtpHost = ConfigurationProperties.getProperty(ContactMailServlet.SMTPHOST_PROPERTY, "");
} if (smtpHost.isEmpty()) {
log.debug("No Vitro.smtpHost specified");
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);
} else { } 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 { throws ServletException, IOException {
VitroRequest vreq = new VitroRequest(request); VitroRequest vreq = new VitroRequest(request);
Portal portal = vreq.getPortal(); Portal portal = vreq.getPortal();

View file

@ -19,18 +19,20 @@ import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet;
*/ */
public class UserMailController extends VitroHttpServlet{ public class UserMailController extends VitroHttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) @Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
doGet(request, response); doGet(request, response);
} }
public void doGet( HttpServletRequest request, HttpServletResponse response ) @Override
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws IOException, ServletException { throws IOException, ServletException {
super.doGet(request,response); super.doGet(request,response);
VitroRequest vreq = new VitroRequest(request); VitroRequest vreq = new VitroRequest(request);
try { try {
//this try block passes any errors to error.jsp //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("title", "Mail All Users Form");
request.setAttribute("bodyJsp", "/contact_err.jsp");// <<< this is where the body gets set 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 -- " + 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 email = uDao.getUserEmailAddress(userURI);
String deliveryFrom = "hjk54@cornell.edu";//TO DO: replace with email address to be used String deliveryFrom = "hjk54@cornell.edu";//TO DO: replace with email address to be used
//Now send message //Now send message
MailUtil mu = new MailUtil(); MailUtil mu = new MailUtil(request);
List<String> deliverToArray = new ArrayList<String>(); List<String> deliverToArray = new ArrayList<String>();
deliverToArray.add(email); deliverToArray.add(email);

View file

@ -2,27 +2,19 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker; package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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.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.beans.Portal; import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet; 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.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; 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.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils; import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
import freemarker.template.Configuration;
/** /**
* Controller for comments ("contact us") page * Controller for comments ("contact us") page
@ -48,7 +40,7 @@ public class ContactFormController extends FreemarkerHttpServlet {
Portal portal = vreq.getPortal(); Portal portal = vreq.getPortal();
Map<String, Object> body = new HashMap<String, Object>(); Map<String, Object> body = new HashMap<String, Object>();
if (!ContactMailServlet.isSmtpHostConfigured()) { if (!ContactMailServlet.isSmtpHostConfigured(vreq)) {
body.put("errorMessage", body.put("errorMessage",
"This application has not yet been configured to send mail. " + "This application has not yet been configured to send mail. " +
"An smtp host has not been specified in the configuration properties file."); "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.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.Portal; 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.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;
@ -54,7 +55,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
@Override @Override
public void init() { public void init() {
smtpHost = ConfigurationProperties.getProperty("Vitro.smtpHost", ""); smtpHost = ConfigurationProperties.getProperty(ContactMailServlet.SMTPHOST_PROPERTY, "");
if (smtpHost.isEmpty()) { if (smtpHost.isEmpty()) {
log.debug("No Vitro.smtpHost specified"); log.debug("No Vitro.smtpHost specified");
} else { } else {

View file

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

View file

@ -11,18 +11,25 @@ import javax.mail.Session;
import javax.mail.Transport; import javax.mail.Transport;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet;
public class MailUtil { public class MailUtil {
private static final Log log = LogFactory.getLog(MailUtil.class); private static final Log log = LogFactory.getLog(MailUtil.class);
private String smtpHost = null; private String smtpHost = "";
public MailUtil(){ public MailUtil(HttpServletRequest req){
smtpHost = getSmtpHostFromProperties(); 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{ 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;
}
} }