@Override public boolean isValid(final ValidationConstraintContext vcc, final Object value) { if (value == null) { return true; } RFC2822AddressParser.ParsedAddress address = RFC2822AddressParser.LOOSE.parse(value.toString()); if (!address.isValid()) { return false; } return address.isValid(); } }
final Matcher mailboxMatcher = MAILBOX_PATTERN().matcher(email); final boolean mailboxMatcherMatches = mailboxMatcher.matches(); final String[] mailboxMatcherParts = mailboxMatcherMatches ? _calcMatcherParts(mailboxMatcher) : null; final Matcher returnPathMatcher = RETURN_PATH_PATTERN().matcher(email); final boolean returnPathMatches = returnPathMatcher.matches(); localPart = mailboxMatcherParts[1]; domain = mailboxMatcherParts[2]; internetAddress = pullFromGroups(mailboxMatcher);
private Pattern MAILBOX_PATTERN() { if (_MAILBOX_PATTERN == null) { buildPatterns(); } return _MAILBOX_PATTERN; }
if (personalString == null && EXTRACT_CFWS_PERSONAL_NAMES) { personalString = m.group(9); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); if (personalString == null && EXTRACT_CFWS_PERSONAL_NAMES) { personalString = m.group(8); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = cleanupPersonalString(personalString); final String testAddr = removeAnyBounding('"', '"', currentLocalpart) + "@" + currentDomainpart; currentLocalpart = removeAnyBounding('"', '"', currentLocalpart);
@Test void testReturnPath() { assertTrue(new RFC2822AddressParser().parse("\"[Kayaks]\" <kayaks@kayaks.org>").isValid()); assertFalse(new RFC2822AddressParser().parse("\"[Kayaks]\" <kayaks@kayaks.org>").isValidReturnPath()); assertTrue(new RFC2822AddressParser().parse("<kayaks@kayaks.org>").isValid()); assertTrue(new RFC2822AddressParser().parse("<kayaks@kayaks.org>").isValidReturnPath()); }
/** * Allows ")" or "(" to appear in quoted versions of * the localpart (they are never allowed in unquoted versions). * The default (2822) behavior is to allow this, i.e. boolean true. * You can disallow it, but better to leave it true. */ public RFC2822AddressParser allowParentheseInLocalpart(final boolean allow) { ALLOW_PARENS_IN_LOCALPART = allow; resetPatterns(); return this; }
private String cleanupPersonalString(String text) { if (text == null) { return null; } text = text.trim(); final Matcher m = _QUOTED_STRING_WO_CFWS_PATTERN.matcher(text); if (!m.matches()) { return text; } text = removeAnyBounding('"', '"', m.group()); text = ESCAPED_BSLASH_PATTERN.matcher(text).replaceAll("\\\\"); text = ESCAPED_QUOTE_PATTERN.matcher(text).replaceAll("\""); return text.trim(); }
private InternetAddress pullFromGroups(final Matcher m) { InternetAddress currentInternetAddress; final String[] parts = _calcMatcherParts(m); if (parts[1] == null || parts[2] == null) { return null; } // if for some reason you want to require that the result be re-parsable by // InternetAddress, you // could uncomment the appropriate stuff below, but note that not all the utility // functions use pullFromGroups; some call getMatcherParts directly. try { //currentInternetAddress = new InternetAddress(parts[0] + " <" + parts[1] + "@" + // parts[2]+ ">", true); // so it parses it OK, but since javamail doesn't extract too well // we make sure that the consistent parts // are correct currentInternetAddress = new InternetAddress(); currentInternetAddress.setPersonal(parts[0]); currentInternetAddress.setAddress(parts[1] + "@" + parts[2]); } catch (final UnsupportedEncodingException uee) { currentInternetAddress = null; } return currentInternetAddress; }
@Test void testCommentAsName() { RFC2822AddressParser.ParsedAddress address = new RFC2822AddressParser().parse("<bob@example.com> (Bob Smith)"); assertEquals("Bob Smith", address.getPersonalName()); address = new RFC2822AddressParser().parse("\"bob smith\" <bob@example.com> (Bobby)"); assertEquals("bob smith", address.getPersonalName()); address = new RFC2822AddressParser().parse("<bob@example.com> (Bobby)"); assertEquals("Bobby", address.getPersonalName()); address = new RFC2822AddressParser().parse("bob@example.com (Bobby)"); assertEquals("Bobby", address.getPersonalName()); address = new RFC2822AddressParser().parse("bob@example.com (Bob) (Smith)"); assertEquals("Bob", address.getPersonalName()); }
if (personalString == null && EXTRACT_CFWS_PERSONAL_NAMES) { personalString = m.group(9); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); if (personalString == null && EXTRACT_CFWS_PERSONAL_NAMES) { personalString = m.group(8); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = removeAnyBounding('(', ')', getFirstComment(personalString)); personalString = cleanupPersonalString(personalString); final String testAddr = removeAnyBounding('"', '"', currentLocalpart) + "@" + currentDomainpart; currentLocalpart = removeAnyBounding('"', '"', currentLocalpart);
/** * Defines if quoted identifiers are allowed. * Using quotes and angle brackets around the raw address may be allowed, e.g.: * <p> * <ul> * <li><code>"John Smith" <john.smith@somewhere.com></code></li> * </ul> * <p> * The RFC says this is a valid mailbox. If you don't want to * allow this, because for example, you only want users to enter in * a raw address (<code>john.smith@somewhere.com</code> - no quotes or angle * brackets), then set the flag {@code false}. */ public RFC2822AddressParser allowQuotedIdentifiers(final boolean allow) { ALLOW_QUOTED_IDENTIFIERS = allow; resetPatterns(); return this; }
private String cleanupPersonalString(String text) { if (text == null) { return null; } text = text.trim(); final Matcher m = _QUOTED_STRING_WO_CFWS_PATTERN.matcher(text); if (!m.matches()) { return text; } text = removeAnyBounding('"', '"', m.group()); text = ESCAPED_BSLASH_PATTERN.matcher(text).replaceAll("\\\\"); text = ESCAPED_QUOTE_PATTERN.matcher(text).replaceAll("\""); return text.trim(); }
private InternetAddress pullFromGroups(final Matcher m) { InternetAddress currentInternetAddress; final String[] parts = _calcMatcherParts(m); if (parts[1] == null || parts[2] == null) { return null; } // if for some reason you want to require that the result be re-parsable by // InternetAddress, you // could uncomment the appropriate stuff below, but note that not all the utility // functions use pullFromGroups; some call getMatcherParts directly. try { //currentInternetAddress = new InternetAddress(parts[0] + " <" + parts[1] + "@" + // parts[2]+ ">", true); // so it parses it OK, but since javamail doesn't extract too well // we make sure that the consistent parts // are correct currentInternetAddress = new InternetAddress(); currentInternetAddress.setPersonal(parts[0]); currentInternetAddress.setAddress(parts[1] + "@" + parts[2]); } catch (final UnsupportedEncodingException uee) { currentInternetAddress = null; } return currentInternetAddress; }
@Test void testValidEmails() { assertTrue(new RFC2822AddressParser().parse("bob @example.com").isValid()); assertTrue(new RFC2822AddressParser().parse("\"bob\" @ example.com").isValid()); assertTrue(new RFC2822AddressParser().parse("\"bob\" (hi) @ example.com").isValid()); assertTrue(new RFC2822AddressParser().parse("name.surname@example.com").isValid()); assertTrue(new RFC2822AddressParser().parse("devnull@onyxbits.de").isValid()); assertTrue(new RFC2822AddressParser().parse("< devnull @ onyxbits.de >").isValid()); assertTrue(new RFC2822AddressParser().parse("<devnull@onyxbits.de>").isValid()); assertFalse(new RFC2822AddressParser().parse("Patrick devnull@onyxbits.de").isValid()); assertTrue(new RFC2822AddressParser().parse("Patrick <devnull@onyxbits.de>").isValid()); assertTrue(new RFC2822AddressParser().parse("Patrickdevnull@onyxbits.de").isValid()); assertFalse(new RFC2822AddressParser().parse("\"Patrick Ahlbrecht\" devnull@onyxbits.de").isValid()); assertTrue(new RFC2822AddressParser().parse("\"Patrick Ahlbrecht\" <devnull@onyxbits.de>").isValid()); assertTrue(new RFC2822AddressParser().parse("Patrick Ahlbrecht <devnull@onyxbits.de>").isValid()); assertFalse(new RFC2822AddressParser().parse("Kayaks.org <kayaks@kayaks.org>").isValid()); assertTrue(new RFC2822AddressParser().parse("\"Kayaks.org\" <kayaks@kayaks.org>").isValid()); assertFalse(new RFC2822AddressParser().parse("[Kayaks] <kayaks@kayaks.org>").isValid()); assertTrue(new RFC2822AddressParser().parse("\"[Kayaks]\" <kayaks@kayaks.org>").isValid()); }
/** * Convenient shortcut of {@link #parse(String)} that returns {@code InternetAddress} or {@code null}. */ public InternetAddress parseToInternetAddress(final String email) { final ParsedAddress parsedAddress = parse(email); if (!parsedAddress.isValid()) { return null; } return parsedAddress.getInternetAddress(); }
final Matcher mailboxMatcher = MAILBOX_PATTERN().matcher(email); final boolean mailboxMatcherMatches = mailboxMatcher.matches(); final String[] mailboxMatcherParts = mailboxMatcherMatches ? _calcMatcherParts(mailboxMatcher) : null; final Matcher returnPathMatcher = RETURN_PATH_PATTERN().matcher(email); final boolean returnPathMatches = returnPathMatcher.matches(); localPart = mailboxMatcherParts[1]; domain = mailboxMatcherParts[2]; internetAddress = pullFromGroups(mailboxMatcher);
/** * Allows "[" or "]" to appear in atext. * The address: * <ul><li><code>[Kayaks] <kayaks@kayaks.org></code></li></ul> * <p> * ...is not valid. It should be: * <p> * <ul><li><code>"[Kayaks]" <kayaks@kayaks.org></code></li></ul> * <p> * If this boolean is set to false, the parser will act per 2822 and will require * the quotes; if set to true, it will allow them to be missing. * <p> * Use at your own risk. There may be some issue with enabling this feature in conjunction * with {@link #allowDomainLiterals(boolean)}. */ public RFC2822AddressParser allowSquareBracketsInAtext(final boolean allow) { ALLOW_SQUARE_BRACKETS_IN_ATEXT = allow; resetPatterns(); return this; }
private Pattern RETURN_PATH_PATTERN() { if (_RETURN_PATH_PATTERN == null) { buildPatterns(); } return _RETURN_PATH_PATTERN; }
@Test void testValidEmails2() { assertTrue(new RFC2822AddressParser().parse("me@example.com").isValid()); assertTrue(new RFC2822AddressParser().parse("a.nonymous@example.com").isValid()); assertTrue(new RFC2822AddressParser().parse("name+tag@example.com").isValid()); assertTrue(RFC2822AddressParser.STRICT.parse("!#$%&'+-/=.?^`{|}~@[1.0.0.127]").isValid()); assertFalse(RFC2822AddressParser.LOOSE.parse("!#$%&'+-/=.?^`{|}~@[IPv6:0123:4567:89AB:CDEF:0123:4567:89AB:CDEF]").isValid()); assertTrue(RFC2822AddressParser.STRICT.parse("!#$%&'+-/=.?^`{|}~@[IPv6:0123:4567:89AB:CDEF:0123:4567:89AB:CDEF]").isValid()); assertTrue(new RFC2822AddressParser().parse("me(this is a comment)@example.com").isValid()); assertFalse(RFC2822AddressParser.LOOSE.parse("me.example@com").isValid()); assertTrue(RFC2822AddressParser.STRICT.parse("me.example@com").isValid()); assertTrue(new RFC2822AddressParser().parse("309d4696df38ff12c023600e3bc2bd4b@fakedomain.com").isValid()); assertTrue(new RFC2822AddressParser().parse("ewiuhdghiufduhdvjhbajbkerwukhgjhvxbhvbsejskuadukfhgskjebf@gmail.net").isValid()); assertFalse(new RFC2822AddressParser().parse("NotAnEmail").isValid()); assertFalse(new RFC2822AddressParser().parse("me@").isValid()); assertFalse(new RFC2822AddressParser().parse("@example.com").isValid()); assertFalse(new RFC2822AddressParser().parse(".me@example.com").isValid()); assertFalse(new RFC2822AddressParser().parse("me@example..com").isValid()); assertFalse(new RFC2822AddressParser().parse("me\\@example.com").isValid()); }
/** * Convenient shortcut of {@link #parse(String)} that returns {@link EmailAddress} or {@code null}. */ public EmailAddress parseToEmailAddress(final String email) { final ParsedAddress parsedAddress = parse(email); if (!parsedAddress.isValid()) { return null; } return new EmailAddress(parsedAddress.getPersonalName(), parsedAddress.getLocalPart() + '@' + parsedAddress.getDomain()); }