1313import java .util .HashMap ;
1414import java .util .Map ;
1515import java .util .regex .Pattern ;
16- import org .apache .commons .validator .routines .DomainValidator ;
1716import org .apache .commons .validator .routines .EmailValidator ;
1817
1918/**
@@ -28,6 +27,9 @@ public final class Email extends AbstractModel {
2827 private static final Map <String , String > equivalentDomains ;
2928 private static final Map <String , Boolean > fastmailDomains ;
3029 private static final Map <String , Boolean > yahooDomains ;
30+ private static final Pattern DOMAIN_LABEL_PATTERN = Pattern .compile (
31+ "^[a-zA-Z0-9]$|^[a-zA-Z0-9][a-zA-Z0-9\\ -]*[a-zA-Z0-9]$"
32+ );
3133 private static final Pattern DOT_PATTERN = Pattern .compile ("\\ ." );
3234 private static final Pattern TRAILING_DOT_PATTERN = Pattern .compile ("\\ .+$" );
3335 private static final Pattern REPEAT_COM_PATTERN = Pattern .compile ("(?:\\ .com){2,}$" );
@@ -373,7 +375,7 @@ public Email.Builder hashAddress() {
373375 * @throws IllegalArgumentException when domain is not a valid domain.
374376 */
375377 public Email .Builder domain (String domain ) {
376- if (enableValidation && !DomainValidator . getInstance (). isValid (domain )) {
378+ if (enableValidation && !isValidDomain (domain )) {
377379 throw new IllegalArgumentException ("The email domain " + domain + " is not valid." );
378380 }
379381 this .domain = domain ;
@@ -491,6 +493,49 @@ private String cleanDomain(String domain) {
491493 return domain ;
492494 }
493495
496+ private static boolean isValidDomain (String domain ) {
497+ if (domain == null || domain .isEmpty ()) {
498+ return false ;
499+ }
500+
501+ String asciiDomain ;
502+ try {
503+ asciiDomain = IDN .toASCII (domain );
504+ } catch (IllegalArgumentException e ) {
505+ return false ;
506+ }
507+
508+ if (domain .endsWith ("." )) {
509+ domain = domain .substring (0 , domain .length () - 1 );
510+ }
511+
512+ if (asciiDomain .length () > 255 ) {
513+ return false ;
514+ }
515+
516+ String [] labels = asciiDomain .split ("\\ ." );
517+
518+ if (labels .length < 2 ) {
519+ return false ;
520+ }
521+
522+ for (String label : labels ) {
523+ if (!isValidDomainLabel (label )) {
524+ return false ;
525+ }
526+ }
527+
528+ return true ;
529+ }
530+
531+ private static boolean isValidDomainLabel (String label ) {
532+ if (label == null || label .isEmpty () || label .length () > 63 ) {
533+ return false ;
534+ }
535+
536+ return DOMAIN_LABEL_PATTERN .matcher (label ).matches ();
537+ }
538+
494539 /**
495540 * @return The domain of the email address used in the transaction.
496541 */
0 commit comments