Example usage for org.apache.commons.validator GenericValidator isEmail

List of usage examples for org.apache.commons.validator GenericValidator isEmail

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericValidator isEmail.

Prototype

public static boolean isEmail(String value) 

Source Link

Document

Checks if a field has a valid e-mail address.

Usage

From source file:it.eng.spagobi.tools.scheduler.dispatcher.UniqueMailDocumentDispatchChannel.java

public static String[] findRecipients(DispatchContext info, BIObject biobj, IDataStore dataStore) {
    logger.debug("IN");
    String[] toReturn = null;//  w ww  .  j a v  a  2 s . co m
    List<String> recipients = new ArrayList();
    try {
        recipients.addAll(findRecipientsFromFixedList(info));
    } catch (Exception e) {
        logger.error(e);
    }
    try {
        recipients.addAll(findRecipientsFromDataSet(info, biobj, dataStore));
    } catch (Exception e) {
        logger.error(e);
    }
    try {
        recipients.addAll(findRecipientsFromExpression(info, biobj));
    } catch (Exception e) {
        logger.error(e);
    }
    // validates addresses
    List<String> validRecipients = new ArrayList();
    Iterator it = recipients.iterator();
    while (it.hasNext()) {
        String recipient = (String) it.next();
        if (GenericValidator.isBlankOrNull(recipient) || !GenericValidator.isEmail(recipient)) {
            logger.error("[" + recipient + "] is not a valid email address.");
            continue;
        }
        if (validRecipients.contains(recipient))
            continue;
        validRecipients.add(recipient);
    }
    toReturn = validRecipients.toArray(new String[0]);
    logger.debug("OUT: returning " + toReturn);
    return toReturn;
}

From source file:it.eng.spagobi.tools.scheduler.jobs.CopyOfExecuteBIDocumentJob.java

private String[] findRecipients(DispatchContext info, BIObject biobj, IDataStore dataStore) {
    logger.debug("IN");
    String[] toReturn = null;//from  w  ww  .  j av  a  2  s  . c o m
    List<String> recipients = new ArrayList();
    try {
        recipients.addAll(findRecipientsFromFixedList(info));
    } catch (Exception e) {
        logger.error(e);
    }
    try {
        recipients.addAll(findRecipientsFromDataSet(info, biobj, dataStore));
    } catch (Exception e) {
        logger.error(e);
    }
    try {
        recipients.addAll(findRecipientsFromExpression(info, biobj));
    } catch (Exception e) {
        logger.error(e);
    }
    // validates addresses
    List<String> validRecipients = new ArrayList();
    Iterator it = recipients.iterator();
    while (it.hasNext()) {
        String recipient = (String) it.next();
        if (GenericValidator.isBlankOrNull(recipient) || !GenericValidator.isEmail(recipient)) {
            logger.error("[" + recipient + "] is not a valid email address.");
            continue;
        }
        if (validRecipients.contains(recipient))
            continue;
        validRecipients.add(recipient);
    }
    toReturn = validRecipients.toArray(new String[0]);
    logger.debug("OUT: returning " + toReturn);
    return toReturn;
}

From source file:org.agnitas.cms.web.forms.ContentModuleForm.java

private boolean isEmailLink(String link) {
    int separatorIndex = link.indexOf(":");
    if (separatorIndex == -1) {
        return false;
    }//w ww. j  a  v  a  2s  . co  m
    String mailtoString = link.substring(0, separatorIndex);
    if (!mailtoString.toLowerCase().equals("mailto")) {
        return false;
    }
    int emailsEnd = link.indexOf("?");
    if (emailsEnd == -1) {
        emailsEnd = link.length();
    }
    if (emailsEnd - 1 <= separatorIndex) {
        return false;
    }
    String emails = link.substring(separatorIndex + 1, emailsEnd);
    String[] emailsArray = emails.split(",");
    for (String email : emailsArray) {
        if (!GenericValidator.isEmail(email.trim())) {
            return false;
        }
    }
    return true;
}

From source file:org.agnitas.service.csv.validator.FieldsValidator.java

/**
 * Checks if the field is an e-mail address.
 *
 * @param value The value validation is being performed on.
 * @return boolean        If the field is an e-mail address
 *         <code>true</code> is returned.
 *         Otherwise <code>false</code>.
 *///  w ww.  java  2 s .c o  m
public static boolean validateEmail(Object bean, Field field, Validator validator) {
    String value = getValueAsString(bean, field);
    final ImportProfile profile = (ImportProfile) validator
            .getParameterValue("org.agnitas.beans.ImportProfile");
    final ColumnMapping currentColumn = getColumnMappingForCurentField(field, profile);
    if (currentColumn != null && currentColumn.getMandatory()) {
        return !GenericValidator.isBlankOrNull(value) && GenericValidator.isEmail(value);
    } else {
        return currentColumn == null || GenericValidator.isEmail(value);
    }

}

From source file:org.agnitas.web.forms.ImportProfileForm.java

public ActionErrors formSpecificValidate(ActionMapping actionMapping, HttpServletRequest request) {
    ActionErrors actionErrors = new ActionErrors();

    if (action == ImportProfileAction.ACTION_SAVE) {
        if (AgnUtils.parameterNotEmpty(request, "save")) {
            if (profile.getName().length() < 3) {
                actionErrors.add("shortname", new ActionMessage("error.nameToShort"));
            }/*w  w  w.  j a v a2s.  c  o m*/
            if (!GenericValidator.isBlankOrNull(profile.getMailForReport())
                    && !GenericValidator.isEmail(profile.getMailForReport())) {
                actionErrors.add("mailForReport", new ActionMessage("error.invalid.email"));
            }
        }
    }
    return actionErrors;
}

From source file:org.agnitas.web.NewImportWizardAction.java

/**
 * Sends import report if profile has emailForReport
 *
 * @param request request//w w  w. j  a  va  2s  .  com
 * @param aForm   a form
 */
private void sendReportEmail(HttpServletRequest request, NewImportWizardForm aForm) {
    ImportProfileDao profileDao = (ImportProfileDao) getWebApplicationContext().getBean("ImportProfileDao");
    ImportProfile profile = profileDao.getImportProfileById(aForm.getDefaultProfileId());
    String address = profile.getMailForReport();
    if (!GenericValidator.isBlankOrNull(address) && GenericValidator.isEmail(address)) {
        Locale locale = (Locale) request.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY);
        ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
        Collection<EmailAttachment> attachments = new ArrayList<EmailAttachment>();
        File invalidRecipientsFile = aForm.getInvalidRecipientsFile();
        File fixedRecipientsFile = aForm.getFixedRecipientsFile();
        EmailAttachment invalidRecipients = createZipAttachment(invalidRecipientsFile, "invalid_recipients.zip",
                bundle.getString("import.recipients.invalid"));
        if (invalidRecipients != null) {
            attachments.add(invalidRecipients);
        }
        EmailAttachment fixedRecipients = createZipAttachment(fixedRecipientsFile, "fixed_recipients.zip",
                bundle.getString("import.recipients.fixed"));
        if (fixedRecipients != null) {
            attachments.add(fixedRecipients);
        }
        EmailAttachment[] attArray = attachments.toArray(new EmailAttachment[] {});
        String subject = bundle.getString("import.recipients.report");
        String message = generateReportEmailBody(bundle, aForm);
        message = subject + ":\n" + message;
        ImportUtils.sendEmailWithAttachments(AgnUtils.getDefaultValue("import.report.from.address"),
                AgnUtils.getDefaultValue("import.report.from.name"), address, subject, message, attArray);
    }
}

From source file:org.apache.commons.validator.TestValidator.java

/**
 * Checks if the field is an e-mail address.
 *
 * @param    value       The value validation is being performed on.
 * @return   boolean      If the field is an e-mail address
 *                           <code>true</code> is returned.  
 *                           Otherwise <code>false</code>.
 *//* w  w  w  .ja va2  s  . c o  m*/
public static boolean validateEmail(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    return GenericValidator.isEmail(value);
}

From source file:org.apache.commons.validator.TestValidator.java

public static void main(String[] args) throws IOException, ParseException {
    System.out.println(GenericValidator.isEmail("abc"));
    FileReader reader = new FileReader(
            Thread.currentThread().getContextClassLoader().getResource("student.json").getPath());
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(reader);

    JSONObject jsonObject = (JSONObject) obj;

    String name = (String) jsonObject.get("studentForm");
    System.out.println(name);/*  ww  w  . j a  va  2 s. c o  m*/

    long age = (Long) jsonObject.get("age");
    System.out.println(age);

    // loop array
    JSONArray msg = (JSONArray) jsonObject.get("messages");
    Iterator<String> iterator = msg.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
    /* JsonParser parser = new JsonParser();
     JsonElement element = parser.parse(reader);
     JsonObject json = element.getAsJsonObject();
     System.out.println(json.get("name"));*/

}

From source file:org.apache.myfaces.custom.emailvalidator.EmailValidator.java

/**
 * methode that validates an email-address.
 * it uses the commons-validator/*  w  w  w  .  ja v a 2 s.c o  m*/
 */
public void validate(FacesContext facesContext, UIComponent uiComponent, Object value)
        throws ValidatorException {

    if (facesContext == null)
        throw new NullPointerException("facesContext");
    if (uiComponent == null)
        throw new NullPointerException("uiComponent");

    if (value == null) {
        return;
    }
    if (!GenericValidator.isEmail(value.toString().trim())) {
        Object[] args = { value.toString() };
        throw new ValidatorException(getFacesMessage(EMAIL_MESSAGE_ID, args));
    }

}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a field has a valid e-mail address.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//  www .  jav  a 2s  . c o m
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if valid, false otherwise.
 */
public static boolean validateEmail(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

        return false;
    } else {
        return true;
    }
}