diff --git a/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java index 9513d061385..0e498445a7a 100644 --- a/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java @@ -71,9 +71,7 @@ public class MailServiceBean implements java.io.Serializable { private static final Logger logger = Logger.getLogger(MailServiceBean.class.getCanonicalName()); private static final String charset = "UTF-8"; - private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" - + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; - + /** * Creates a new instance of MailServiceBean */ @@ -111,7 +109,7 @@ public void sendMail(String host, String reply, String to, String subject, Strin me.printStackTrace(System.out); } } - + @Resource(name = "mail/notifyMailSession") private Session session; @@ -181,8 +179,7 @@ public void sendMail(String reply, String to, String subject, String messageText logger.severe(ex.getMessage()); } msg.setFrom(fromAddress); - - if (reply.matches(EMAIL_PATTERN)) { + if (EMailValidator.isEmailValid(reply, null)) { //But set the reply-to address to direct replies to the requested 'from' party if it is a valid email address msg.setReplyTo(new Address[] {new InternetAddress(reply)}); } else { diff --git a/src/main/java/edu/harvard/iq/dataverse/util/StringUtil.java b/src/main/java/edu/harvard/iq/dataverse/util/StringUtil.java index fd4656fa463..b4970fc3899 100644 --- a/src/main/java/edu/harvard/iq/dataverse/util/StringUtil.java +++ b/src/main/java/edu/harvard/iq/dataverse/util/StringUtil.java @@ -28,7 +28,6 @@ public class StringUtil { private static final Logger logger = Logger.getLogger(StringUtil.class.getCanonicalName()); - private static final Pattern EMAIL_PATTERN = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); public static final boolean nonEmpty( String str ) { return ! isEmpty(str); @@ -61,18 +60,6 @@ public static Optional toOption(String s) { return s.isEmpty() ? Optional.empty() : Optional.of(s); } - /** - * @todo Unless there is a compelling reason not to, we should switch to the - * validation routines in EMailValidator. - */ - @Deprecated - public static boolean isValidEmail( String s ) { - logger.fine("Validating <<<" + s + ">>>."); - if (s == null) { - return false; - } - return EMAIL_PATTERN.matcher(s).matches(); - } public static final boolean isAlphaNumericChar(char c) { // TODO: consider using Character.isLetterOrDigit(c) diff --git a/src/test/java/edu/harvard/iq/dataverse/EMailValidatorTest.java b/src/test/java/edu/harvard/iq/dataverse/EMailValidatorTest.java index 858e1b91b08..9b6c4bf8005 100644 --- a/src/test/java/edu/harvard/iq/dataverse/EMailValidatorTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/EMailValidatorTest.java @@ -37,6 +37,12 @@ public void testIsEmailValid() { assertEquals(false, EMailValidator.isEmailValid("dora@.com", null)); assertEquals(false, EMailValidator.isEmailValid("", null)); assertEquals(false, EMailValidator.isEmailValid(null, null)); + /* + Add tests for 4601 + */ + assertEquals(true, EMailValidator.isEmailValid("blah@wiso.uni-unc.de", null)); + assertEquals(true, EMailValidator.isEmailValid("foo@essex.co.uk", null)); + assertEquals(true, EMailValidator.isEmailValid("jack@bu.cloud", null)); } } diff --git a/src/test/java/edu/harvard/iq/dataverse/util/StringUtilTest.java b/src/test/java/edu/harvard/iq/dataverse/util/StringUtilTest.java index 7fd154e9031..306e059af57 100644 --- a/src/test/java/edu/harvard/iq/dataverse/util/StringUtilTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/util/StringUtilTest.java @@ -130,17 +130,5 @@ public void testSymmetricEncryption() { assertEquals(source, decrypted); } - @Test - public void testIsValidEmail() { - assertTrue( StringUtil.isValidEmail("hello@world.com") ); - assertTrue( StringUtil.isValidEmail("hello@world.co.il") ); - assertTrue( StringUtil.isValidEmail("hello@under.world.co.il") ); - - assertFalse( StringUtil.isValidEmail("hello at under.world.co.il") ); - assertFalse( StringUtil.isValidEmail("hellounder.world.co.il") ); - assertFalse( StringUtil.isValidEmail("hellounder@.world.co.il") ); - assertFalse( StringUtil.isValidEmail("hellounder") ); - assertFalse( StringUtil.isValidEmail("") ); - assertFalse( StringUtil.isValidEmail(null) ); - } + }