Example usage for org.apache.commons.validator EmailValidator getInstance

List of usage examples for org.apache.commons.validator EmailValidator getInstance

Introduction

In this page you can find the example usage for org.apache.commons.validator EmailValidator getInstance.

Prototype

public static EmailValidator getInstance() 

Source Link

Document

Returns the Singleton instance of this validator.

Usage

From source file:com.nagarro.core.v2.controller.UsersController.java

/**
 * Changes customer's login.//w w  w.  j  a v  a  2 s.  c om
 *
 * @formparam newLogin Customer's new login. Customer login is case insensitive.
 * @formparam password Customer's current password.
 * @throws DuplicateUidException
 * @throws PasswordMismatchException
 * @throws RequestParameterException
 */
@Secured({ "ROLE_CUSTOMERGROUP", "ROLE_TRUSTED_CLIENT", "ROLE_CUSTOMERMANAGERGROUP" })
@RequestMapping(value = "/{userId}/login", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public void changeLogin(@RequestParam final String newLogin, @RequestParam final String password)
        throws DuplicateUidException, PasswordMismatchException, RequestParameterException {
    if (!EmailValidator.getInstance().isValid(newLogin)) {
        throw new RequestParameterException("Login [" + newLogin + "] is not a valid e-mail address!",
                RequestParameterException.INVALID, "newLogin");
    }
    customerFacade.changeUid(newLogin, password);
}

From source file:de.hybris.platform.ycommercewebservices.v1.controller.CustomersController.java

/**
 * Web service for changing customer login.<br>
 * Sample call: https://localhost:9002/rest/v1/mysite/customers/current/login?newLogin=:newLogin&password=:password<br>
 * Method requires customer authentication and is restricted to <code>HTTPS<code> channel.<br>
 * Method type : <code>POST</code>.
 * /*from w  w  w  .  j a va2  s .  c o m*/
 * @param newLogin
 * @param password
 * @return
 * @throws DuplicateUidException
 * @throws PasswordMismatchException
 * @throws RequestParameterException
 */
@Secured("ROLE_CUSTOMERGROUP")
@RequestMapping(value = "/current/login", method = { RequestMethod.PUT, RequestMethod.POST })
@ResponseBody
public LoginChangeResponse changeLogin(@RequestParam final String newLogin, @RequestParam final String password)
        throws DuplicateUidException, PasswordMismatchException, RequestParameterException {
    if (!EmailValidator.getInstance().isValid(newLogin)) {
        throw new RequestParameterException("Login [" + newLogin + "] is not a valid e-mail address!",
                RequestParameterException.INVALID, "newLogin");
    }
    customerFacade.changeUid(newLogin, password);
    final LoginChangeResponse loginChangeResponse = new LoginChangeResponse();
    loginChangeResponse.setSuccess(true);
    return loginChangeResponse;
}

From source file:com.nagarro.core.v1.controller.CustomersController.java

/**
 * Web service for changing customer login.<br>
 * Sample call: https://localhost:9002/rest/v1/mysite/customers/current/login?newLogin=:newLogin&password=:password<br>
 * Method requires customer authentication and is restricted to <code>HTTPS<code> channel.<br>
 * Method type : <code>POST</code>.
 * /*from   ww w . ja  v  a 2  s .  c  om*/
 * @param newLogin
 * @param password
 * @return {@link LoginChangeResponse}
 * @throws DuplicateUidException
 * @throws PasswordMismatchException
 * @throws RequestParameterException
 */
@Secured("ROLE_CUSTOMERGROUP")
@RequestMapping(value = "/current/login", method = { RequestMethod.PUT, RequestMethod.POST })
@ResponseBody
public LoginChangeResponse changeLogin(@RequestParam final String newLogin, @RequestParam final String password)
        throws DuplicateUidException, PasswordMismatchException, RequestParameterException {
    if (!EmailValidator.getInstance().isValid(newLogin)) {
        throw new RequestParameterException("Login [" + sanitize(newLogin) + "] is not a valid e-mail address!",
                RequestParameterException.INVALID, "newLogin");
    }
    customerFacade.changeUid(newLogin, password);
    final LoginChangeResponse loginChangeResponse = new LoginChangeResponse();
    loginChangeResponse.setSuccess(true);
    return loginChangeResponse;
}

From source file:gov.nih.nci.cananolab.restful.sample.SampleBO.java

protected List<String> validatePointOfContactInput(SimplePointOfContactBean simplePOC) {

    List<String> errors = new ArrayList<String>();

    if (simplePOC == null) {
        errors.add("Input point of contact object invalid"); //shouldn't happen
        return errors;
    }//from  ww  w  . j  a  v  a2s. c o  m

    //errors = RestValidator.validate(simplePOC);

    SimpleOrganizationBean simpleOrg = simplePOC.getOrganization();
    if (simpleOrg != null) {
        String orgName = simpleOrg.getName();
        if (orgName == null || !InputValidationUtil.isTextFieldWhiteList(orgName))
            errors.add(PropertyUtil.getProperty("sample", "organization.name.invalid"));
    } else
        errors.add("Organization Name is a required field");

    SimpleAddressBean addrBean = simplePOC.getAddress();
    if (addrBean != null) {
        String val = addrBean.getLine1();
        if (val != null && val.length() > 0 && !InputValidationUtil.isTextFieldWhiteList(val))
            errors.add(PropertyUtil.getProperty("sample", "organization.address1.invalid"));

        val = addrBean.getLine2();
        if (val != null && val.length() > 0 && !InputValidationUtil.isTextFieldWhiteList(val))
            errors.add(PropertyUtil.getProperty("sample", "organization.address2.invalid"));
        val = addrBean.getCity();
        if (val != null && val.length() > 0 && !InputValidationUtil.isRelaxedAlphabetic(val))
            errors.add(PropertyUtil.getProperty("sample", "organization.city.invalid"));

        val = addrBean.getStateProvince();
        if (val != null && val.length() > 0 && !InputValidationUtil.isRelaxedAlphabetic(val))
            errors.add(PropertyUtil.getProperty("sample", "organization.state.invalid"));

        val = addrBean.getCountry();
        if (val != null && val.length() > 0 && !InputValidationUtil.isRelaxedAlphabetic(val))
            errors.add(PropertyUtil.getProperty("sample", "organization.country.invalid"));

        val = addrBean.getZip();
        if (val != null && val.length() > 0 && !InputValidationUtil.isZipValid(addrBean.getZip()))
            errors.add(PropertyUtil.getProperty("sample", "postalCode.invalid"));
    }

    String name = simplePOC.getFirstName();
    if (name != null && name.length() > 0 && !InputValidationUtil.isRelaxedAlphabetic(name))
        errors.add(PropertyUtil.getProperty("sample", "firstName.invalid"));

    name = simplePOC.getLastName();
    if (name != null && name.length() > 0 && !InputValidationUtil.isRelaxedAlphabetic(name))
        errors.add(PropertyUtil.getProperty("sample", "lastName.invalid"));

    name = simplePOC.getMiddleInitial();
    if (name != null && name.length() > 0 && !InputValidationUtil.isRelaxedAlphabetic(name))
        errors.add(PropertyUtil.getProperty("sample", "middleInitial.invalid"));

    String phone = simplePOC.getPhoneNumber();
    if (phone.length() > 0 && !InputValidationUtil.isPhoneValid(phone))
        errors.add(PropertyUtil.getProperty("sample", "phone.invalid"));
    //         
    String email = simplePOC.getEmail();
    EmailValidator emailValidator = EmailValidator.getInstance();
    if (email != null && email.length() > 0 && !emailValidator.isValid(email))
        errors.add("Email is invalid");

    return errors;
}

From source file:net.sziebert.tutorials.web.JoinFormValidator.java

public void validate(Object obj, Errors errors) {
    logger.debug("Validating join form.");
    JoinForm form = (JoinForm) obj;//  ww w  .j  ava 2s. co  m
    // Insure that a value with specified.
    rejectIfEmptyOrWhitespace(errors, "username", "error.username.empty");
    rejectIfEmptyOrWhitespace(errors, "email", "error.email.empty");
    rejectIfEmptyOrWhitespace(errors, "password", "error.password.empty");
    rejectIfEmptyOrWhitespace(errors, "confirm", "error.confirm.empty");
    // Insure the inputs don't contain any illegal characters.
    if (!isAlphanumeric(form.getUsername()))
        errors.rejectValue("username", "error.username.illegal.chars");
    if (!isAlphanumeric(form.getPassword()))
        errors.rejectValue("password", "error.password.illegal.chars");
    // Insure that the entries are within the valid length range.
    if (isNotBlank(form.getUsername()) && form.getUsername().length() < 4)
        errors.rejectValue("username", "error.username.too.short");
    if (isNotBlank(form.getPassword()) && form.getPassword().length() < 6)
        errors.rejectValue("password", "error.password.too.short");
    // Insure the password and confirmation match.
    if (isNotBlank(form.getPassword()) && isNotBlank(form.getConfirm())) {
        if (!form.getPassword().equals(form.getConfirm())) {
            errors.reject("error.password.mismatch");
        }
    }
    // Insure the email address is valid.
    if (isNotBlank(form.getEmail())) {
        EmailValidator ev = EmailValidator.getInstance();
        if (!ev.isValid(form.getEmail())) {
            errors.rejectValue("email", "error.email.invalid");
        }
    }
    // Insure that the terms of use have been accepted.
    if (!form.getAgreeToTerms()) {
        errors.rejectValue("agreeToTerms", "error.agree.to.terms");
    }
}

From source file:org.agnitas.util.AgnUtils.java

public static String checkAndNormalizeEmail(String email) throws Exception {
    if (StringUtils.isBlank(email)) {
        throw new Exception("Empty email address");
    } else {/*w w  w .j  a  v a2 s.com*/
        if (!EmailValidator.getInstance().isValid(StringOps.punycoded(email))) {
            throw new Exception("Invalid email address");
        } else {
            return normalizeEmail(email);
        }
    }
}

From source file:org.alfresco.web.bean.wizard.NewUserWizard.java

/**
 * Validate Email field data is acceptable
 * //  www.  j  a  v  a2  s.  c  om
 * @param context FacesContext
 * @param component UIComponent
 * @param value Object
 * @throws ValidatorException
 */
public void validateEmail(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    EmailValidator emailValidator = EmailValidator.getInstance();
    if (!emailValidator.isValid((String) value)) {
        String err = Application.getMessage(context, MSG_ERROR_MAIL_NOT_VALID);
        throw new ValidatorException(new FacesMessage(err));
    }
}

From source file:org.ambraproject.article.action.EmailArticleAction.java

private boolean validates() {
    boolean isValid = true;

    isValid = validatesArticleURI();//  w ww.ja  va2 s.com

    if (StringUtils.isBlank(emailFrom)) {
        addFieldError("emailFrom", "Your e-mail address cannot be empty");
        isValid = false;
    }

    if (!EmailValidator.getInstance().isValid(emailFrom)) {
        addFieldError("emailFrom", "Invalid e-mail address");
        isValid = false;
    }

    isValid = checkEmails(emailTo) && isValid;

    if (StringUtils.isBlank(senderName)) {
        addFieldError("senderName", "Your name cannot be empty");
        isValid = false;
    }

    return isValid;
}

From source file:org.ambraproject.article.action.EmailArticleAction.java

private boolean checkEmails(String emailList) {
    if (StringUtils.isBlank(emailList)) {
        addFieldError("emailTo", "To e-mail address cannot be empty");
        return false;
    } else {/*w  w w .  j a v  a  2s.  c o  m*/
        final StringTokenizer emailTokens = new StringTokenizer(emailList, " \t\n\r\f,");
        if (emailTokens.countTokens() > MAX_TO_EMAIL) {
            addFieldError("emailTo", "Maximum of " + MAX_TO_EMAIL + " email addresses");
            return false;
        }
        EmailValidator validator = EmailValidator.getInstance();
        ArrayList<String> invalidEmails = new ArrayList<String>();

        while (emailTokens.hasMoreTokens()) {
            String email = emailTokens.nextToken();
            if (!validator.isValid(email)) {
                invalidEmails.add(email);
            }
        }
        final int numInvalid = invalidEmails.size();
        if (numInvalid != 0) {
            StringBuilder errorMsg = new StringBuilder("Invalid e-mail address");
            if (numInvalid > 1) {
                errorMsg.append("es: ");
            } else {
                errorMsg.append(": ");
            }
            Iterator<String> iter = invalidEmails.iterator();
            while (iter.hasNext()) {
                errorMsg.append(iter.next());
                if (iter.hasNext()) {
                    errorMsg.append(", ");
                }
            }
            addFieldError("emailTo", errorMsg.toString());
        }
        return (numInvalid == 0);
    }
}

From source file:org.apache.myfaces.html5.renderkit.input.util.Html5EmailConverter.java

public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
    if (value == null)
        return null;

    if (value.isEmpty())
        return null;

    String[] emails = value.split(",");
    if (emails != null && emails.length > 0) {
        for (String email : emails) {
            email = email.trim();// w  ww  . jav a 2 s  .c  o m
            if (!EmailValidator.getInstance().isValid(email)) {
                throw new ConverterException(new FacesMessage("Provided value for component "
                        + RendererUtils.getPathToComponent(component) + " is not a valid email: " + email));
            }
        }

        return emails;
    } else {
        return null;
    }
}