下面列出了javax.validation.ValidationException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 验证服务提供者接口
*
* @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());
}
}
}
}
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);
}
}
}
@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);
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
@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());
}
}
@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();
}
@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();
}
@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;
}