/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.nir.nmail.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import hu.nir.nmail.spring.beans.MailsData;
import javax.mail.Address;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import org.springframework.validation.ValidationUtils;

/**
 *
 * @author attila
 */
public class MailsDataValidator implements Validator {

    public boolean supports(Class clazz) {
        return clazz.equals(MailsData.class);
    }

    public void validate(Object target, Errors errors) {
        MailsData mailsData = (MailsData) target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "senderName", "required.senderName");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "senderAddress", "required.senderAddress");
        String senderAddress = mailsData.getSenderAddress();
        
        try {
            Address address = new InternetAddress(senderAddress);
        } catch (AddressException ex) {
            errors.reject("error.senderAddress");
        }
        
    }

}
