Java Code Examples for javax.validation.ValidationException#getMessage()

The following examples show how to use javax.validation.ValidationException#getMessage() . 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: IntfValidator.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
/**
 * 验证服务提供者接口
 *
 * @param config
 * @param clazz
 * @return
 */
protected Pair<String, String> valid(final ProviderConfig config, final Class clazz) {
    Object ref = config.getRef();
    if (ref != null && !clazz.isInstance(ref)) {
        return new Pair<>("interfaceClass", String.format("%s is not an instance of %s", ref.getClass().getName(), clazz.getName()));
    } else if (config.getEnableValidator() == null || !config.getEnableValidator()) {
        return null;
    } else {
        String validatorName = isEmpty(config.getInterfaceValidator()) ?
                Constants.INTERFACE_VALIDATOR_OPTION.get() :
                config.getInterfaceValidator();
        InterfaceValidator validator = INTERFACE_VALIDATOR.get(validatorName);
        if (validator == null) {
            return new Pair<>("interfaceValidator",
                    String.format("No such extension \'%s\' of %s", validatorName, InterfaceValidator.class.getName()));
        } else {
            try {
                validator.validate(clazz);
                return null;
            } catch (ValidationException e) {
                return new Pair<>("interfaceClass", e.getMessage());
            }
        }
    }
}
 
Example 2
Source File: ParamSupply.java    From onedev with MIT License 6 votes vote down vote up
public static void validateParamMatrix(Map<String, ParamSpec> paramSpecMap, Map<String, List<List<String>>> paramMatrix) {
	validateParamNames(paramSpecMap.keySet(), paramMatrix.keySet());
	for (Map.Entry<String, List<List<String>>> entry: paramMatrix.entrySet()) {
		if (entry.getValue() != null) {
			try {
				validateParamValues(entry.getValue());
			} catch (ValidationException e) {
				throw new ValidationException("Error validating values of parameter '" 
						+ entry.getKey() + "': " + e.getMessage());
			}
			
			ParamSpec paramSpec = Preconditions.checkNotNull(paramSpecMap.get(entry.getKey()));
			for (List<String> value: entry.getValue()) 
				validateParamValue(paramSpec, entry.getKey(), value);
		}
	}
}
 
Example 3
Source File: RunJobAction.java    From onedev with MIT License 6 votes vote down vote up
@Override
public void validateWithContext(BuildSpec buildSpec, Job job) {
	super.validateWithContext(buildSpec, job);
	
	Job jobToRun = buildSpec.getJobMap().get(jobName);
	if (jobToRun != null) {
		try {
			ParamSupply.validateParams(jobToRun.getParamSpecs(), jobParams);
		} catch (ValidationException e) {
			throw new ValidationException("Error validating parameters of run job '" 
					+ jobToRun.getName() + "': " + e.getMessage());
		}
	} else {
		throw new ValidationException("Run job not found: " + jobName);
	}
}
 
Example 4
Source File: GlobalExceptionHandler.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
/**
 * 400 - Bad Request
 */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public String handleValidationException(ValidationException e, Model model) {
    logger.error("参数验证失败", e);
    String message = "【参数验证失败】" + e.getMessage();
    model.addAttribute("message", message);
    model.addAttribute("code", 400);
    return viewName;
}
 
Example 5
Source File: GlobalExceptionHandler.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
/**
 * 400 - Bad Request
 */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public String handleValidationException(ValidationException e, Model model) {
    logger.error("参数验证失败", e);
    String message = "【参数验证失败】" + e.getMessage();
    model.addAttribute("message", message);
    model.addAttribute("code", 400);
    return viewName;
}
 
Example 6
Source File: GlobalExceptionHand.java    From spring-boot-shiro with Apache License 2.0 5 votes vote down vote up
/**
 * 400 - Bad Request
 */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public Response handleValidationException(ValidationException e) {
    String msg = e.getMessage();
    log.error("参数验证失败:", e);
    return new Response().failure(msg);
}
 
Example 7
Source File: GlobalExceptionHandler.java    From SENS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 400 - Bad Request
 */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public String handleValidationException(ValidationException e, Model model) {
    log.error("参数验证失败", e);
    String message = "【参数验证失败】" + e.getMessage();
    model.addAttribute("message", message);
    model.addAttribute("code", 400);
    return viewName;
}
 
Example 8
Source File: CreateIssueAction.java    From onedev with MIT License 5 votes vote down vote up
@Override
public void validateWithContext(BuildSpec buildSpec, Job job) {
	super.validateWithContext(buildSpec, job);
	
	GlobalIssueSetting issueSetting = OneDev.getInstance(SettingManager.class).getIssueSetting();
	try {
		FieldSupply.validateFields(issueSetting.getFieldSpecMap(getFieldNames()), issueFields);
	} catch (ValidationException e) {
		throw new ValidationException("Error validating issue fields: " + e.getMessage());
	}
	
}
 
Example 9
Source File: ValidationWebExceptionMapper.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Response toResponse(ValidationException exception) {
    ValidationWebExceptionResponse response = new ValidationWebExceptionResponse();
    if (exception instanceof ConstraintViolationException) {
        ConstraintViolation<?> violation = ((ConstraintViolationException) exception).getConstraintViolations().iterator().next();
        response.field = violation.getPropertyPath().toString();
        response.errorMessage = violation.getMessage();
    } else {
        response.errorMessage = exception.getMessage();
    }
    return Response.status(400).entity(response)
        .type(MediaType.APPLICATION_JSON).build();
}
 
Example 10
Source File: ValidationExceptionMapper.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Response toResponse(ValidationException exception) {
    ValidationExceptionResponse response = new ValidationExceptionResponse();
    if (exception instanceof ConstraintViolationException) {
        ConstraintViolation<?> violation = ((ConstraintViolationException) exception).getConstraintViolations().iterator().next();
        response.path = violation.getPropertyPath().toString();
        response.errorMessage = violation.getMessage();
    } else {
        response.errorMessage = exception.getMessage();
    }
    return Response.status(400).entity(response)
        .type(MediaType.APPLICATION_JSON).build();
}
 
Example 11
Source File: CasAuthorizingRealm.java    From frpMgr with MIT License 4 votes vote down vote up
@Override
protected User getUserInfo(FormToken token) {
	
	User user = super.getUserInfo(token);
	if (user == null){
		Map<String, Object> attrs = token.getParams();
		
		// 如果允许客户端创建账号,则创建账号
		if (ObjectUtils.toBoolean(attrs.get("isAllowClientCreateUser"))){

			// 获取CAS传递过来的用户属性信息
			user = new User(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("userCode"))));
			user.setLoginCode(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("loginCode"))));
			user.setPassword(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("password"))));
			user.setUserName(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("userName"))));
			user.setEmail(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("email"))));
			user.setMobile(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("mobile"))));
			user.setPhone(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("phone"))));
			user.setUserType(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("userType"))));
			user.setRefCode(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("refCode"))));
			user.setRefName(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("refName"))));
			user.setMgrType(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("mgrType"))));
			user.setStatus(EncodeUtils.decodeUrl(ObjectUtils.toString(attrs.get("status"))));
			
			// 如果是员工类型,则平台自动创建
			if (User.USER_TYPE_EMPLOYEE.equals(user.getUserType())){
				
				// 保存员工和用户
				try{
					EmpUser empUser = new EmpUser();
					empUser.setIsNewRecord(true);
					empUser.setMobile(user.getMobile());
					empUser.setEmail(user.getEmail());
					empUser.setPhone(user.getPhone());
					empUser.getEmployee().getCompany().setCompanyCode(EncodeUtils
							.decodeUrl(ObjectUtils.toString(attrs.get("companyCode"))));
					empUser.getEmployee().getOffice().setOfficeCode(EncodeUtils
							.decodeUrl(ObjectUtils.toString(attrs.get("officeCode"))));
					getEmpUserService().save(empUser);
				}catch(ValidationException ve){
					throw new AuthenticationException("msg:" + ve.getMessage());
				}
				
				// 重新获取用户登录
				user = UserUtils.getByLoginCode(token.getUsername()/*, corpCode*/);
				if (user != null) {
					return user;
				}
				
			}
			
			// 其它类型,根据项目需要自行创建
			else{
				try{
					CasCreateUser casCreateUser = SpringUtils.getBean(CasCreateUser.class);
					if(casCreateUser != null){
						casCreateUser.createUser(user, attrs);
					}
				}catch(NoSuchBeanDefinitionException e){
					throw new AuthenticationException("msg:用户 “" + token.getUsername()
							+ "”, 类型 “" + user.getUserType() + "” 在本系统中不存在, 请联系管理员.");
				}
			}
		}else{
			throw new AuthenticationException("msg:用户 “" + token.getUsername() + "” 在本系统中不存在, 请联系管理员.");
		}
	}
	return user;
}