package votorola.g.mail; // Copyright 2008, 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 javax.mail.internet.*; import votorola.g.lang.*; /** Internet address utilities. * * @see ietf.org/rfc/rfc822.txt */ public final @ThreadSafe class InternetAddressX { private InternetAddressX() {} /** Converts an email address to canonical form. The canonical form * is the bare addr-spec (no personal part, and no angle braces). * This method does strict parsing. */ public static String canonicalAddress( String address ) throws AddressException { InternetAddress iAddress = new InternetAddress( address, /*strict*/true ); return iAddress.getAddress(); } /** 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( InternetAddress iAddress ) throws AddressException { if( iAddress.isGroup() ) throw new AddressException( "local part requested of group address: " + iAddress ); final String addrSpec = iAddress.getAddress(); final int a = addrSpec.indexOf( '@' ); if( a <= 0 ) throw new AddressException( "no local part in address: " + iAddress ); return addrSpec.substring( 0, a ); } /** 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; } } }