-
Notifications
You must be signed in to change notification settings - Fork 8
Replace use of commons-validator #490
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cbfd4e8
Do not use commons-validator for domain validation
oschwald ab5105c
Replace commons-validator email validation
oschwald a93e832
Remove commons-validator as a dependency
oschwald c847c59
Use correct value when handling FQDNs
oschwald dc526d7
Add missing word
oschwald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,6 @@ | |
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
| import org.apache.commons.validator.routines.DomainValidator; | ||
| import org.apache.commons.validator.routines.EmailValidator; | ||
|
|
||
| /** | ||
| * The email information for the transaction. | ||
|
|
@@ -28,6 +26,9 @@ public final class Email extends AbstractModel { | |
| private static final Map<String, String> equivalentDomains; | ||
| private static final Map<String, Boolean> fastmailDomains; | ||
| private static final Map<String, Boolean> yahooDomains; | ||
| private static final Pattern DOMAIN_LABEL_PATTERN = Pattern.compile( | ||
| "^[a-zA-Z0-9]$|^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]$" | ||
| ); | ||
| private static final Pattern DOT_PATTERN = Pattern.compile("\\."); | ||
| private static final Pattern TRAILING_DOT_PATTERN = Pattern.compile("\\.+$"); | ||
| private static final Pattern REPEAT_COM_PATTERN = Pattern.compile("(?:\\.com){2,}$"); | ||
|
|
@@ -338,7 +339,7 @@ public Builder(boolean enableValidation) { | |
| * @throws IllegalArgumentException when address is not a valid email address. | ||
| */ | ||
| public Email.Builder address(String address) { | ||
| if (enableValidation && !EmailValidator.getInstance().isValid(address)) { | ||
| if (enableValidation && !isValidEmail(address)) { | ||
| throw new IllegalArgumentException( | ||
| "The email address " + address + " is not valid."); | ||
| } | ||
|
|
@@ -373,7 +374,7 @@ public Email.Builder hashAddress() { | |
| * @throws IllegalArgumentException when domain is not a valid domain. | ||
| */ | ||
| public Email.Builder domain(String domain) { | ||
| if (enableValidation && !DomainValidator.getInstance().isValid(domain)) { | ||
| if (enableValidation && !isValidDomain(domain)) { | ||
| throw new IllegalArgumentException("The email domain " + domain + " is not valid."); | ||
| } | ||
| this.domain = domain; | ||
|
|
@@ -458,6 +459,34 @@ private String cleanAddress(String address) { | |
| return localPart + "@" + domain; | ||
| } | ||
|
|
||
| private static boolean isValidEmail(String email) { | ||
| if (email == null || email.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| // RFC 5321 the forward path limits the mailbox to 254 characters | ||
| // even though a domain can be 255 and the local part 64 | ||
| if (email.length() > 254) { | ||
| return false; | ||
| } | ||
|
|
||
| int atIndex = email.lastIndexOf('@'); | ||
| if (atIndex <= 0) { | ||
| return false; | ||
| } | ||
|
|
||
| String localPart = email.substring(0, atIndex); | ||
| String domainPart = email.substring(atIndex + 1); | ||
|
|
||
| // The local-part has a maximum length of 64 characters. | ||
| if (localPart.length() > 64) { | ||
| return false; | ||
| } | ||
|
|
||
| return isValidDomain(domainPart); | ||
| } | ||
|
|
||
|
|
||
| private String cleanDomain(String domain) { | ||
| if (domain == null) { | ||
| return null; | ||
|
|
@@ -491,6 +520,49 @@ private String cleanDomain(String domain) { | |
| return domain; | ||
| } | ||
|
|
||
| private static boolean isValidDomain(String domain) { | ||
| if (domain == null || domain.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| String asciiDomain; | ||
| try { | ||
| asciiDomain = IDN.toASCII(domain); | ||
| } catch (IllegalArgumentException e) { | ||
| return false; | ||
| } | ||
|
|
||
| if (domain.endsWith(".")) { | ||
| domain = domain.substring(0, domain.length() - 1); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we want to use asciiDomain on these two lines?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I fixed this and the other issue. |
||
| } | ||
|
|
||
| if (asciiDomain.length() > 255) { | ||
| return false; | ||
| } | ||
|
|
||
| String[] labels = asciiDomain.split("\\."); | ||
|
|
||
| if (labels.length < 2) { | ||
| return false; | ||
| } | ||
|
|
||
| for (String label : labels) { | ||
| if (!isValidDomainLabel(label)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private static boolean isValidDomainLabel(String label) { | ||
| if (label == null || label.isEmpty() || label.length() > 63) { | ||
| return false; | ||
| } | ||
|
|
||
| return DOMAIN_LABEL_PATTERN.matcher(label).matches(); | ||
| } | ||
|
|
||
| /** | ||
| * @return The domain of the email address used in the transaction. | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we missing a word here in referring to the RFC?