Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4601 email contact issues #4636

Merged
merged 4 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/edu/harvard/iq/dataverse/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -61,18 +60,6 @@ public static Optional<String> 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we delete EMAIL_PATTERN in StringUtil.java?

}

public static final boolean isAlphaNumericChar(char c) {
// TODO: consider using Character.isLetterOrDigit(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should reformat this emails in the test to be a little less "real".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a couple that came directly from the RT tickets referenced in the issue (that are now passing validation). What else did you have in mind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mheppler kudos on catching that. Fixed in 0c52d26 it looks like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

*/
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));
}

}
14 changes: 1 addition & 13 deletions src/test/java/edu/harvard/iq/dataverse/util/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
}

}