001package votorola.g.mail; // Copyright 2008-2009, 2012, 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. 002 003import javax.mail.internet.*; 004import org.apache.wicket.request.cycle.RequestCycle; 005import org.apache.wicket.validation.*; 006import votorola.g.lang.*; 007import votorola.g.locale.BundleFormatter; 008 009 010/** A validator of email addresses, that validates using JavaMail. 011 * 012 * @see org.apache.wicket.validation.validator.EmailAddressValidator 013 * @see <a href='http://static.ddpoker.com/javadoc/wicket-extensions/1.3.4/org/apache/wicket/extensions/validation/validator/RfcCompliantEmailAddressValidator.html' 014 * >RfcCompliantEmailAddressValidator</a> 015 */ 016public @ThreadRestricted("wicket") class WicEmailAddressValidator implements IValidator<String> 017{ 018 019 020 /** Hook method, called when validation succeeds. The base implementation 021 * of this method (in WicEmailAddressValidator) does nothing. 022 * 023 * @param iAddress the valid, strictly parsed, email address 024 */ 025 protected void onSuccess( InternetAddress iAddress ) {} 026 027 028 029 // - I - V a l i d a t o r ------------------------------------------------------------ 030 031 032 public void validate( final IValidatable<String> v ) 033 { 034 final String value = v.getValue(); 035 assert value != null : "no null if not INullAcceptingValidator"; 036 try 037 { 038 final InternetAddress iAddress = new InternetAddress( value, /*strict*/true ); 039 // iAddress.validate(); // probably redundant with strict parsing in constructor 040 if( !InternetAddressX.VALID_PATTERN_BARE_DOMNAME.matcher( // stricter checks 041 iAddress.getAddress() ).matches() ) 042 { 043 throw new AddressException( "malformed email address: \"" + iAddress.getAddress() + "\"" ); 044 } 045 onSuccess( iAddress ); 046 } 047 catch( AddressException x ) 048 { 049 v.error( new ValidationError().setMessage( 050 ((BundleFormatter.GProvider)RequestCycle.get()).bunG().l( 051 "g.malformedEmail", value, x.getMessage() ))); 052 } 053 } 054 055 056 057}