public final class EmailAddressParser extends Object
extractCfwsPersonalNames
:
This criteria controls the behavior of getInternetAddress and extractHeaderAddresses. If included, it allows the
not-totally-kosher-but-happens-in-the-real-world practice of:
<[email protected]> (Bob Smith)
In this case, "Bob Smith" is not techinically the personal name, just a comment. If this is included, the methods will convert this into: Bob Smith
<[email protected]>
This also happens somewhat more often and appropriately with
[email protected] (Mail Delivery System)
If a personal name appears to the left and CFWS appears to the right of an address, the methods will favor the personal name to the left. If the methods need
to use the CFWS following the address, they will take the first comment token they find. e.g.:
"bob smith" <[email protected]> (Bobby)
will yield personal name "bob smith"
<[email protected]> (Bobby)
will yield personal name "Bobby"
[email protected] (Bobby)
will yield personal name "Bobby"
[email protected] (Bob) (Smith)
will yield personal name
"Bob"
Modifier and Type | Method and Description |
---|---|
static @Nullable String |
cleanupPersonalString(@Nullable String string,
@NotNull EnumSet<EmailAddressCriteria> criteria)
Given a string, if the string is a quoted string (without CFWS around it, although it will be trimmed) then remove the bounding quotations and then
unescape it.
|
static @NotNull javax.mail.internet.InternetAddress[] |
extractHeaderAddresses(@Nullable String header_txt,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
Given the value of a header, like the From:, extract valid 2822 addresses from it and place them in an array.
|
static @Nullable String[] |
getAddressParts(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
See getInternetAddress; does the same thing but returns the constituent parts of the address in a three-element array (or null if the address is
invalid).
|
static @Nullable String |
getDomain(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
See getInternetAddress; does the same thing but returns the domain part in string form (essentially, the part to the right of the @).
|
static @Nullable String |
getFirstComment(@Nullable String text,
@NotNull EnumSet<EmailAddressCriteria> criteria)
Given a string, extract the first matched comment token as defined in 2822, trimmed; return null on all errors or non-findings
This is probably not super-useful.
|
static @Nullable javax.mail.internet.InternetAddress |
getInternetAddress(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
Given a 2822-valid single address string, give us an InternetAddress object holding that address, otherwise returns null.
|
static @Nullable String |
getLocalPart(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
See getInternetAddress; does the same thing but returns the local part that would have been returned from getInternetAddress() in String form
(essentially, the part to the left of the @).
|
static @NotNull String[] |
getMatcherParts(@NotNull Matcher m,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
|
static @Nullable String |
getPersonalName(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
See getInternetAddress; does the same thing but returns the personal name that would have been returned from getInternetAddress() in String form.
|
static @Nullable String |
getReturnPathAddress(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
Pull out the cleaned-up return path address.
|
static @Nullable String |
getReturnPathBracketContents(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria)
WARNING: You may want to use getReturnPathAddress() instead if you're looking for a clean version of the return path without CFWS, etc.
|
static boolean |
isValidAddressList(@NotNull String header_txt,
@NotNull EnumSet<EmailAddressCriteria> criteria)
Tells us if a header line is valid, i.e.
|
static boolean |
isValidMailboxList(@NotNull String header_txt,
@NotNull EnumSet<EmailAddressCriteria> criteria)
Tells us if a header line is valid, i.e.
|
static boolean |
isValidReturnPath(@Nullable String email,
@NotNull EnumSet<EmailAddressCriteria> criteria)
Tells us if the email represents a valid return path header string.
|
static @Nullable javax.mail.internet.InternetAddress |
pullFromGroups(@NotNull Matcher m,
@NotNull EnumSet<EmailAddressCriteria> criteria,
boolean extractCfwsPersonalNames)
Using knowledge of the group-ID numbers (see comments at top) pull the data relevant to us from an already-successfully-matched matcher.
|
static @Nullable String |
removeAnyBounding(char s,
char e,
@Nullable String str)
If the string starts and ends with s and e, remove them, otherwise return the string as it was passed in.
|
public static boolean isValidReturnPath(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria)
Note that <""> is not a valid return-path.
@Nullable public static @Nullable String getReturnPathBracketContents(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria)
<(my > path) >
...are legal return-paths and may throw a simpler parser off. However this method will return all CFWS (comments, whitespace) that may be between the brackets as well. So the example above will return:
(my > path)_
(where the _ is the
trailing space from the original string)
@Nullable public static @Nullable String getReturnPathAddress(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
extractCfwsPersonalNames
- See EmailAddressParser
public static boolean isValidMailboxList(@NotNull @NotNull String header_txt, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria)
isValidAddressList(String, EnumSet)
public static boolean isValidAddressList(@NotNull @NotNull String header_txt, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria)
isValidMailboxList(String, EnumSet)
@Nullable public static @Nullable javax.mail.internet.InternetAddress getInternetAddress(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
"hi" <[email protected]>
will return an address, so the moral is that you may want to check isValidReturnPath() first, if you care. This method is useful if you trust the return path and want to extract a clean address from it without CFWS (getReturnPathBracketContents() will return any CFWS), or if you want to determine if a validated return path actually contains an address in it and isn't just empty or full of CFWS. Except for empty return paths (those lacking an address) the Return-Path specification is a subset of valid 2822 addresses, so this method will work on all non-empty return-paths, failing only on the empty ones.
In general for this method, note: although this method does not use InternetAddress to parse/extract the information, it does ensure that InternetAddress can use the results (i.e. that there are no encoding issues), but note that an InternetAddress object can hold (and use) values for the address which it could not have parsed itself. Thus, it's possible that for InternetAddress addr, which came as the result of this method, the following may throw an exception or may silently fail:extractCfwsPersonalNames
- See EmailAddressParser
@Nullable public static @Nullable String[] getAddressParts(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
extractCfwsPersonalNames
- See EmailAddressParser
@Nullable public static @Nullable String getPersonalName(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
extractCfwsPersonalNames
- See EmailAddressParser
@Nullable public static @Nullable String getLocalPart(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
extractCfwsPersonalNames
- See EmailAddressParser
@Nullable public static @Nullable String getDomain(@Nullable @Nullable String email, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
extractCfwsPersonalNames
- See EmailAddressParser
@NotNull public static @NotNull javax.mail.internet.InternetAddress[] extractHeaderAddresses(@Nullable @Nullable String header_txt, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
header_txt
- is text from whatever header (not including the header name and ": ". I don't think the String needs to be
unfolded, but i haven't tested that.
see getInternetAddress() for more info: this extracts the same wayextractCfwsPersonalNames
- See EmailAddressParser
@Nullable public static @Nullable javax.mail.internet.InternetAddress pullFromGroups(@NotNull @NotNull Matcher m, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
extractCfwsPersonalNames
- See EmailAddressParser
@NotNull public static @NotNull String[] getMatcherParts(@NotNull @NotNull Matcher m, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria, boolean extractCfwsPersonalNames)
extractCfwsPersonalNames
- See EmailAddressParser
@Nullable public static @Nullable String getFirstComment(@Nullable @Nullable String text, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria)
@Nullable public static @Nullable String cleanupPersonalString(@Nullable @Nullable String string, @NotNull @NotNull EnumSet<EmailAddressCriteria> criteria)
Copyright © 2016–2019. All rights reserved.