javax.faces.validator.ValidatorException Java Examples

The following examples show how to use javax.faces.validator.ValidatorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IterableValidator.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if (!disabled && value instanceof Iterable<?>) {
        if(pattern == null) {
            throw new ValidatorException(new FacesMessage("Validator problem", "Set regex pattern for validator"));
        }
        Iterable<?> iterable = (Iterable<?>) value;
        List<FacesMessage> facesMessages = StreamSupport.stream(iterable.spliterator(), false)
            .map(Object::toString)
                .filter(item -> !pattern.matcher(item).matches())
            .map(item -> new FacesMessage("Incorrect value", "Value '"+ item +"' format mismatch"))
            .collect(Collectors.toList());
        
        if (!facesMessages.isEmpty()) {
            throw new ValidatorException(facesMessages);
        }
    }
}
 
Example #2
Source File: CssValueValidator.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Validates that the given value doesn't contain a '{', '}' or "/*".
 * 
 * @param context
 *            FacesContext for the request we are processing
 * @param component
 *            UIComponent we are checking for correctness
 * @param value
 *            the value to validate
 * @throws ValidatorException
 *             if validation fails
 */
public void validate(FacesContext facesContext, UIComponent component,
        Object value) throws ValidatorException {
    if (value == null) {
        return;
    }
    String str = value.toString();
    if (str.indexOf('{') < 0 && str.indexOf('}') < 0
            && str.indexOf("/*") < 0) {
        return;
    }
    Object[] args = null;
    String label = JSFUtils.getLabel(component);
    // if (label != null) {
    // args = new Object[] { label, str };
    // } else {
    // args = new Object[] { "", str };
    // }
    ValidationException e = new ValidationException(
            ValidationException.ReasonEnum.CSS_VALUE, label, null);
    String text = JSFUtils.getText(e.getMessageKey(), args, facesContext);
    throw new ValidatorException(new FacesMessage(
            FacesMessage.SEVERITY_ERROR, text, null));
}
 
Example #3
Source File: LongValidator.java    From development with Apache License 2.0 6 votes vote down vote up
private static void validate(FacesContext context, UIComponent uiComponent,
        String value) throws ValidatorException {
    Long minValue = getMinValue(uiComponent);
    Long maxValue = getMaxValue(uiComponent);
    long parsedLong = parse(context, value, minValue, maxValue);
    if (!isInRange(parsedLong, minValue, maxValue)) {
        minValue = (minValue != null ? minValue : Long
                .valueOf(Long.MIN_VALUE));
        maxValue = (maxValue != null ? maxValue : Long
                .valueOf(Long.MAX_VALUE));
        String message = JSFUtils.getText(
                BaseBean.ERROR_LONG_VALUE_OUT_OF_RANGE,
                new String[] { String.valueOf(minValue),
                        String.valueOf(maxValue) }, context);
        throw getException(message);
    }
}
 
Example #4
Source File: ParameterValueValidator.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Validate integer value.
 * 
 * @param context
 *            JSF context.
 * @param uiComponent
 *            UI component.
 * @param value
 *            Value for validation.
 */
private void validateInteger(FacesContext context, UIComponent uiComponent,
        String value) {
    if (!GenericValidator.isInt(value.toString())) {
        Object[] args = null;
        String label = JSFUtils.getLabel(uiComponent);
        if (label != null) {
            args = new Object[] { label };
        }
        ValidationException e = new ValidationException(
                ValidationException.ReasonEnum.INTEGER, label, null);
        String text = JSFUtils.getText(e.getMessageKey(), args, context);
        throw new ValidatorException(new FacesMessage(
                FacesMessage.SEVERITY_ERROR, text, null));
    }
}
 
Example #5
Source File: UrlValidator.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Validates that the given value is an URL
 * 
 * @param context
 *            FacesContext for the request we are processing
 * @param component
 *            UIComponent we are checking for correctness
 * @param value
 *            the value to validate
 * @throws ValidatorException
 *             if validation fails
 */
public void validate(FacesContext facesContext, UIComponent component,
        Object value) throws ValidatorException {
    if (value == null) {
        return;
    }
    String str = value.toString();
    if (str.length() == 0) {
        return;
    }

    if (ADMValidator.isUrl(str)) {
        return;
    }
    Object[] args = null;
    String label = JSFUtils.getLabel(component);
    if (label != null) {
        args = new Object[] { label };
    }
    ValidationException e = new ValidationException(
            ValidationException.ReasonEnum.URL, label, null);
    String text = JSFUtils.getText(e.getMessageKey(), args, facesContext);
    throw new ValidatorException(new FacesMessage(
            FacesMessage.SEVERITY_ERROR, text, null));
}
 
Example #6
Source File: TenantValidator.java    From development with Apache License 2.0 6 votes vote down vote up
@Override
public void validate(FacesContext context, UIComponent uiComponent,
        Object input) throws ValidatorException {
    
    TenantService tenantService = serviceLocator.findService(TenantService.class);
    
    String tenantKey = input.toString();

    if (StringUtils.isBlank(tenantKey) || "0".equals(tenantKey)) {
        return;
    }

    try {
        tenantService.getTenantByKey(Long.parseLong(tenantKey));
    } catch (ObjectNotFoundException e) {

        String msg = JSFUtils
                .getText(BaseBean.ERROR_TENANT_NO_LONGER_EXISTS, null);
        FacesMessage facesMessage = new FacesMessage(
                FacesMessage.SEVERITY_ERROR, msg, null);
        throw new ValidatorException(facesMessage);
    }
}
 
Example #7
Source File: EmailValidator.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Validates that the given value contains an email address.
 * 
 * @param context
 *            FacesContext for the request we are processing
 * @param component
 *            UIComponent we are checking for correctness
 * @param value
 *            the value to validate
 * @throws ValidatorException
 *             if validation fails
 */
public void validate(FacesContext facesContext, UIComponent component,
        Object value) throws ValidatorException {
    if (value == null) {
        return;
    }
    String email = value.toString();
    if (email.length() == 0) {
        return;
    }
    if (!ADMValidator.isEmail(email)) {
        Object[] args = null;
        String label = JSFUtils.getLabel(component);
        if (label != null) {
            args = new Object[] { label };
        }
        ValidationException e = new ValidationException(
                ValidationException.ReasonEnum.EMAIL, label, null);
        String text = JSFUtils.getText(e.getMessageKey(), args,
                facesContext);
        throw new ValidatorException(new FacesMessage(
                FacesMessage.SEVERITY_ERROR, text, null));
    }
}
 
Example #8
Source File: DurationConverter.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Conversion to server representation, so converting days to milliseconds.
 * Prior to the conversion the input value is validated.
 */
@Override
public Object getAsObject(FacesContext context, UIComponent component,
        String value) {
    try {
        // Checks if mandatory and not empty
        new ParameterValueValidator().validate(context, component, value);
    } catch (ValidatorException e) {
        throw new ConverterException(e.getFacesMessage());
    }

    // Validation passed; so if the value is empty it's not mandatory
    if (value == null || value.trim().length() == 0) {
        return null;
    } else {
        Long durationInMs = DurationValidation.convertDurationToMs(context,
                value);
        if (durationInMs != null) {
            return durationInMs.toString();
        } else {
            throw new ConverterException(
                    ParameterValueValidator.getFacesMessage(component,
                            context));
        }
    }
}
 
Example #9
Source File: TranslationBean.java    From development with Apache License 2.0 6 votes vote down vote up
private ValidatorException constructValidatorException(
        final FacesContext context, final UIComponent component) {
    // create ValidationException
    String label = JSFUtils.getLabel(component);
    ValidationException ex = new ValidationException(
            ValidationException.ReasonEnum.URL, label, null);

    // map to ValidatorException
    Object[] args = null;
    if (label != null) {
        args = new Object[] { label };
    }
    String text = JSFUtils.getText(ex.getMessageKey(), args, context);
    return new ValidatorException(new FacesMessage(
            FacesMessage.SEVERITY_ERROR, text, null));
}
 
Example #10
Source File: SchedulerTool.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void validateTriggerExpression(FacesContext context,
    UIComponent component, Object value)
{
  if (value != null)
  {
    try
    {
      String expression = (String) value;

      // additional check
      // quartz does not check for more than 7 tokens in expression
      String[] arr = expression.split("\\s");
      if (arr.length > 7)
      {
        throw new RuntimeException(new ParseException("Expression has more than 7 tokens", 7));
      }

      TriggerBuilder.newTrigger()
              .withSchedule(CronScheduleBuilder.cronSchedule(expression))
              .build();

    }
    catch (RuntimeException e)
    {
      Throwable cause = e.getCause();
      String error = e.getMessage();
      if (cause != null)
      {
          error = cause.getMessage();
      }
      FacesMessage message = new FacesMessage(rb.getFormattedMessage("parse_exception", error));
      message.setSeverity(FacesMessage.SEVERITY_ERROR);
      throw new ValidatorException(message);
    }
  }
}
 
Example #11
Source File: ExportAuditLogDataCtrl.java    From development with Apache License 2.0 5 votes vote down vote up
public void validateFromAndToDate(final FacesContext context,
        final UIComponent toValidate, final Object value) {
    String clientId = toValidate.getClientId(context);
    validator.setToDate(model.getToDate());
    validator.setFromDate(model.getFromDate());
    try {
        validator.validate(context, toValidate, value);
    } catch (ValidatorException ex) {
        context.addMessage(
                clientId,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, ex
                        .getLocalizedMessage(), null));
    }
}
 
Example #12
Source File: ConfigurationSettingsValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidate_String_Mandatory_TypeMail_Null() {
    // given
    UIComponentStub stub = getComponent(ConfigurationKey.TYPE_MAIL, true,
            null, null);

    // when
    validator.validate(context, stub, null);
}
 
Example #13
Source File: ConfigurationSettingsValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidate_String_Mandatory_TypeMail_Empty() {
    // given
    UIComponentStub stub = getComponent(ConfigurationKey.TYPE_MAIL, true,
            null, null);

    // when
    validator.validate(context, stub, "");
}
 
Example #14
Source File: PasswordValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidateSecondParameterEmptyString() throws Exception {
    ucStub.setValue("");
    try {
        validator.validate(fcStub, ucStub2, "secret");
    } catch (ValidatorException e) {
        assertEquals("Wrong exception type - ",
                BaseBean.ERROR_USER_PWD_MATCH, requestedResourceKey);
        throw e;
    }
}
 
Example #15
Source File: BillingAdapterValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidateTooLong() throws ValidatorException {
    String a41 = "A01234567890123456789012345678901234567890123456789012345678901234567890123456789" +
            "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567" +
            "89012345678901234567890123456789";
    validator.validate(context, component, a41);
}
 
Example #16
Source File: EmptyStringValidator.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Throw exception if there is only space(s) character as input.
 */
public void validate(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
	String str = (String) value;
	if (str.trim().length() < 1) {
		((UIInput) toValidate).setValid(false);

		FacesMessage message = new FacesMessage();
		message.setDetail(Utilities.rb.getString("signup.validator.stringWithSpaceOnly"));
		message.setSummary(Utilities.rb.getString("signup.validator.stringWithSpaceOnly"));
		message.setSeverity(FacesMessage.SEVERITY_ERROR);
		throw new ValidatorException(message);
	}
}
 
Example #17
Source File: LanguageISOCodeValidator.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Validates that the given value contains an language ISO code.
 * 
 * @param facesContext
 *            FacesContext for the request we are processing
 * @param component
 *            UIComponent we are checking for correctness
 * @param value
 *            the value to validate
 * @throws ValidatorException
 *             if validation fails
 */
@Override
public void validate(FacesContext facesContext, UIComponent component,
        Object value) throws ValidatorException {
    if (value == null) {
        return;
    }
    String languageISOCode = value.toString();
    if (languageISOCode.length() == 0) {
        return;
    }
    checkLocale(languageISOCode, facesContext);
}
 
Example #18
Source File: CaptchaValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void validateCaptcha_emptyInput() {
    // given
    setCaptcha("0815");
    inputField.setValue("");
    // when
    try {
        validator.validate(context, inputField, inputField.getValue());
    } finally {
        // then
        assertEquals(false, getCaptchaInputStatus());
        assertEquals(true, isInputCleared());
    }
}
 
Example #19
Source File: ParameterValueValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidateLongTooLong() {
    UIComponentStub stub = getComponent(DATATYPE_STRING, false, null, null);
    StringBuilder value = new StringBuilder();
    value.append("1");
    for (int i = 0; i < 500; i++) {
        value.append("0");
    }
    validator.validate(context, stub, value.toString());
}
 
Example #20
Source File: PasswordValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidateNullInputFieldReference() throws Exception {
    vrStub.resetComponents();
    try {
        validator.validate(fcStub, ucStub2, "bla");
    } catch (ValidatorException e) {
        assertEquals("Wrong exception type - ",
                BaseBean.ERROR_USER_PWD_MATCH, requestedResourceKey);
        throw e;
    }
}
 
Example #21
Source File: ConfigurationSettingsValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidate_String_Mandatory_TypeString_Empty() {
    // given
    UIComponentStub stub = getComponent(ConfigurationKey.TYPE_STRING, true,
            null, null);

    // when
    validator.validate(context, stub, "");
}
 
Example #22
Source File: PasswordValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidateFirstParameterEmptyString() throws Exception {
    ucStub.setValue("");
    try {
        validator.validate(fcStub, ucStub, "");
    } catch (ValidatorException e) {
        assertEquals("Wrong exception type - ",
                BaseBean.ERROR_USER_PWD_LENGTH, requestedResourceKey);
        throw e;
    }
}
 
Example #23
Source File: DateFromToValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidateToBeforeFrom() throws Exception {
    Calendar cal = Calendar.getInstance();
    toDateStub.setValue(cal.getTime());
    validator.setToDate(cal.getTime());
    cal.add(Calendar.DAY_OF_MONTH, 5);
    fromDateStub.setValue(cal.getTime());
    validator.setFromDate(cal.getTime());

    validator.validate(fcStub, toDateStub, toDateStub.getValue());
}
 
Example #24
Source File: ConfigurationSettingsValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidate_String_Mandatory_TypeUrl_Null() {
    // given
    UIComponentStub stub = getComponent(ConfigurationKey.TYPE_URL, true,
            null, null);

    // when
    validator.validate(context, stub, null);
}
 
Example #25
Source File: ConfigurationSettingsValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidate_String_Mandatory_TypeUrl_Empty() {
    // given
    UIComponentStub stub = getComponent(ConfigurationKey.TYPE_URL, true,
            null, null);

    // when
    validator.validate(context, stub, "");
}
 
Example #26
Source File: ParameterValueValidator.java    From development with Apache License 2.0 5 votes vote down vote up
private long getDurationInMs(FacesContext context, UIComponent component,
        String value) {
    Long duration = DurationValidation.getDurationInMs(context, value);
    if (duration == null) {
        throw new ValidatorException(getFacesMessage(component, context));
    } else {
        return duration.longValue();
    }
}
 
Example #27
Source File: LdapUserValidator.java    From development with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(FacesContext context, UIComponent component,
        Object valueToValidate) throws ValidatorException {

    if (valueToValidate == null) {
        return;
    }

    UserManagementService service = srvLocator
            .findService(UserManagementService.class);

    String organizationId;
    try {
        organizationId = retrieveOrganizationId(valueToValidate);
        if (service.isOrganizationLDAPManaged(organizationId)) {
            ValidationException e = new ValidationException(
                    ValidationException.ReasonEnum.LDAP_USER_ID, null, null);
            String text = JSFUtils
                    .getText(e.getMessageKey(), null, context);
            throw new ValidatorException(new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, text, null));
        }
    } catch (ObjectNotFoundException | OperationNotPermittedException
            | OrganizationRemovedException e1) {
        return;
    }
}
 
Example #28
Source File: PasswordValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidateEmpty() throws ValidatorException {
    try {
        validator.validate(fcStub, ucStub2, "");
    } catch (ValidatorException e) {
        assertEquals("Wrong exception type - ",
                BaseBean.ERROR_USER_PWD_MATCH, requestedResourceKey);
        throw e;
    }
}
 
Example #29
Source File: ConfigurationSettingsValidatorTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidatorException.class)
public void testValidate_String_Mandatory_TypeLong_Null() {
    // given
    UIComponentStub stub = getComponent(ConfigurationKey.TYPE_LONG, true,
            null, null);

    // when
    validator.validate(context, stub, null);
}
 
Example #30
Source File: BillingBeanTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void validateFromAndToDate_ValidatorException() {
    // given
    context = mock(FacesContext.class);
    toValidate = mock(UIComponent.class);
    value = mock(Object.class);
    ValidatorException ex = mock(ValidatorException.class);
    doThrow(ex).when(validator).validate(any(FacesContext.class),
            any(UIComponent.class), any(Object.class));
    // when
    bean.validateFromAndToDate(context, toValidate, value);
    // then
    verify(context, times(1)).addMessage(anyString(),
            any(FacesMessage.class));
}