package votorola.g.mail; // Copyright 2008-2009, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.io.*; import java.util.regex.Pattern; import javax.mail.internet.*; import votorola.g.lang.*; /** Internet (email) address utilities. * * @see ietf.org/html/rfc822 */ public final @ThreadSafe class InternetAddressX { private InternetAddressX() {} /** Converts an email address to canonical form. The canonical form is the bare * addr-spec having no personal part and no angle braces, and with the domain name in * lower case. This method does strict parsing. * * @see #canonicalize(InternetAddress) * @see #VALID_PATTERN_BARE_DOMNAME */ public static String canonicalAddress( final String address ) throws AddressException { final InternetAddress iAddress = new InternetAddress( address, /*strict*/true ); // iAddress.validate(); // probably redundant with strict parsing in constructor if( !VALID_PATTERN_BARE_DOMNAME.matcher( iAddress.getAddress() ).matches() ) // stricter checks { throw new AddressException( "malformed email address: \"" + iAddress.getAddress() + "\"" ); } canonicalize( iAddress ); return iAddress.getAddress(); } /** Converts an email address to canonical form. * * @see #canonicalAddress(String) */ public static void canonicalize( final InternetAddress iAddress ) { final String addrSpec = iAddress.getAddress(); final int a = addrSpec.indexOf( '@' ); iAddress.setAddress( addrSpec.substring(0,a) + "@" + addrSpec.substring(a+1).toLowerCase() ); } /** Returns the local-part of the address. This is the part prior to the '@' * character in the addr-spec. It is typically a user name. * * @param iAddress a single (non-group) address * * @throws AddressException if the address is a group address, * or contains no local-part */ public static String localPart( final InternetAddress iAddress ) throws AddressException { if( iAddress.isGroup() ) throw new AddressException( "local-part requested of group address: " + iAddress ); return localPart( iAddress.getAddress() ); } /** Returns the local-part of the address. This is the part prior to the '@' * character. It is typically a user name. * * @param addrSpec the bare addr-spec of an email address, having no * personal part and no angle braces * * @throws AddressException if addrSpec contains no local-part */ public static String localPart( final String addrSpec ) throws AddressException { final int a = addrSpec.indexOf( '@' ); if( a <= 0 ) throw new AddressException( "no local-part in addr-spec: " + addrSpec ); return addrSpec.substring( 0, a ); } /** Constructs an email address from a local-part and a domain name, and validates it. * * @param loc the local-part * @param dom the domain name * * @throws AddressException if loc or dom is null, or if the resulting address * cannot be validated * * @see #VALID_PATTERN_BARE_DOMNAME */ public static String newValidAddress( final String loc, final String dom ) throws AddressException { if( loc == null || loc.length() == 0 ) { throw new AddressException( "malformed email address, no local-part" ); } if( dom == null || dom.length() == 0 ) { throw new AddressException( "malformed email address, no domain name" ); } final String address = loc + "@" + dom; if( !VALID_PATTERN_BARE_DOMNAME.matcher( address ).matches() ) { throw new AddressException( "malformed email address: \"" + address + "\"" ); } return address; } /** Same as {@linkplain InternetAddress#setPersonal(String) setPersonal}(name), * but returns any UnsupportedEncodingException that occurs, rather than throwing it. * * @return any UnsupportedEncodingException that occured */ public static UnsupportedEncodingException trySetPersonal( InternetAddress iAddress, String name ) { try { iAddress.setPersonal( name ); return null; } catch( UnsupportedEncodingException x ) { return x; } } /** Same as {@linkplain InternetAddress#setPersonal(String,String) setPersonal}(name,charset), * but returns any UnsupportedEncodingException that occurs, rather than throwing it. * * @return any UnsupportedEncodingException that occured */ public static UnsupportedEncodingException trySetPersonal( InternetAddress iAddress, String name, String charset ) { try { iAddress.setPersonal( name, charset ); return null; } catch( UnsupportedEncodingException x ) { return x; } } /** The pattern of valid email address consisting of the bare addr-spec having no * personal part and no angle braces, and with no domain literals (like * joe@[192.168.1.100]). * * @see AddressValidationP */ public static final Pattern VALID_PATTERN_BARE_DOMNAME = AddressValidationP.newPattern(); }