@Test public void testRejectWithoutDefaultMessage() { TestBean tb = new TestBean(); tb.setName("myName"); tb.setAge(99); BeanPropertyBindingResult ex = new BeanPropertyBindingResult(tb, "tb"); ex.reject("invalid"); ex.rejectValue("age", "invalidField"); StaticMessageSource ms = new StaticMessageSource(); ms.addMessage("invalid", Locale.US, "general error"); ms.addMessage("invalidField", Locale.US, "invalid field"); assertEquals("general error", ms.getMessage(ex.getGlobalError(), Locale.US)); assertEquals("invalid field", ms.getMessage(ex.getFieldError("age"), Locale.US)); }
private void validateLangEntry(Entry<String, String> entry, List<String> systemLangs, List<String> configuredLangs, String defaultLangCode, BeanPropertyBindingResult bindingResult) { String currentLangCode = entry.getKey(); if (!systemLangs.contains(currentLangCode)) { bindingResult.reject(LabelValidator.ERRCODE_LABELGROUP_LANGS_INVALID_LANG, new String[]{currentLangCode}, "labelGroup.langs.lang.invalid"); return; } if (!configuredLangs.contains(currentLangCode)) { bindingResult.reject(LabelValidator.ERRCODE_LABELGROUP_LANGS_NOT_ACTIVE_LANG, new String[]{currentLangCode}, "labelGroup.langs.lang.notActive"); } if (currentLangCode.equals(defaultLangCode) && StringUtils.isBlank(entry.getValue())) { bindingResult.reject(LabelValidator.ERRCODE_LABELGROUP_LANGS_TEXT_REQUIRED, new String[]{currentLangCode}, "labelGroup.langs.defaultLang.textRequired"); } }
protected boolean validateDefaultLang(LabelDto labelDto, BeanPropertyBindingResult bindingResult, String defaultLang) { String label = (null != labelDto && null != labelDto.getTitles()) ? labelDto.getTitles().get(defaultLang) : null; if (StringUtils.isEmpty(label)) { bindingResult.reject(LabelValidator.ERRCODE_LABELGROUP_LANGS_DEFAULT_LANG_REQUIRED, new String[]{defaultLang}, "labelGroup.langs.defaultLang.required"); return false; } return true; }
protected void validatePermissions(Role role, BeanPropertyBindingResult errors) { List<String> systemPermissions = this.getRoleManager().getPermissionsCodes(); for (String permission : role.getPermissions()) { if (!systemPermissions.contains(permission)) { errors.reject(RoleValidator.ERRCODE_PERMISSON_NOT_FOUND, new String[]{permission}, "role.permission.notFound"); } } }
protected BeanPropertyBindingResult checkFragmentForDelete(GuiFragment fragment, GuiFragmentDto dto) throws ApsSystemException { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(fragment, "fragment"); if (null == fragment) { return bindingResult; } if (!dto.getFragments().isEmpty() || !dto.getPageModels().isEmpty()) { List<String> fragments = dto.getFragments().stream().map(GuiFragmentDto.FragmentRef::getCode).collect(Collectors.toList()); List<String> pagemodels = dto.getPageModels().stream().map(GuiFragmentDto.PageModelRef::getCode).collect(Collectors.toList()); bindingResult.reject(GuiFragmentValidator.ERRCODE_FRAGMENT_REFERENCES, new Object[]{fragment.getCode(), fragments, pagemodels}, "guifragment.cannot.delete.references"); } if (fragment.isLocked()) { bindingResult.reject(GuiFragmentValidator.ERRCODE_FRAGMENT_LOCKED, new Object[]{fragment.getCode()}, "guifragment.cannot.delete.locked"); } return bindingResult; }
protected BeanPropertyBindingResult validateDelete(PageModel pageModel) throws ApsSystemException { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(pageModel, "pageModel"); Map<String, List<Object>> references = this.getReferencingObjects(pageModel); if (references.size() > 0) { bindingResult.reject(PageModelValidator.ERRCODE_PAGEMODEL_REFERENCES, new Object[]{pageModel.getCode(), references}, "pageModel.cannot.delete.references"); } return bindingResult; }
protected BeanPropertyBindingResult checkGroupForDelete(Group group) throws ApsSystemException { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(group, "group"); if (null == group) { return bindingResult; } if (Group.FREE_GROUP_NAME.equals(group.getName()) || Group.ADMINS_GROUP_NAME.equals(group.getName())) { bindingResult.reject(GroupValidator.ERRCODE_CANNOT_DELETE_RESERVED_GROUP, new String[]{group.getName()}, "group.cannot.delete.reserved"); } if (!bindingResult.hasErrors()) { Map<String, Boolean> references = this.getReferencesInfo(group); if (references.size() > 0) { for (Map.Entry<String, Boolean> entry : references.entrySet()) { if (true == entry.getValue().booleanValue()) { bindingResult.reject(GroupValidator.ERRCODE_GROUP_REFERENCES, new Object[]{group.getName(), entry.getKey()}, "group.cannot.delete.references"); } } } } return bindingResult; }
protected void validateCodeExists(Role role, BeanPropertyBindingResult errors) { if (null != roleManager.getRole(role.getName())) { errors.reject(RoleValidator.ERRCODE_ROLE_ALREADY_EXISTS, new String[]{role.getName()}, "role.exists"); } }
protected BeanPropertyBindingResult checkDataObjectModelForDelete(DataObjectModel model, DataModelDto dto) throws ApsSystemException { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(model, "dataObjectModel"); if (null == model) { return bindingResult; } Map<String, List<IPage>> pages = this.getDataObjectModelManager().getReferencingPages(model.getId()); if (!bindingResult.hasErrors() && !pages.isEmpty()) { bindingResult.reject(GuiFragmentValidator.ERRCODE_FRAGMENT_REFERENCES, new Object[]{String.valueOf(model.getId())}, "guifragment.cannot.delete.references"); } return bindingResult; }
public void validateRestListResult(RestListRequest listRequest, PagedMetadata<?> result) { if (listRequest.getPage() > 1 && (null == result.getBody() || result.getBody().isEmpty())) { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(listRequest, "listRequest"); bindingResult.reject(ERRCODE_NO_ITEM_ON_PAGE, new String[]{String.valueOf(listRequest.getPage())}, "pagination.item.empty"); throw new RestRourceNotFoundException(bindingResult); } }
protected BeanPropertyBindingResult validateRoleForDelete(Role role) { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(role, "role"); List<UserDto> users = this.getAuthorizationService().getRoleUtilizer(role.getName()); if (!users.isEmpty()) { errors.reject(RoleValidator.ERRCODE_ROLE_REFERENCES, new String[]{role.getName()}, "role.cannot.delete.references"); } return errors; }
protected BeanPropertyBindingResult validateDisable(Lang lang) { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(lang, "lang"); if (lang.isDefault()) { bindingResult.reject(LanguageValidator.ERRCODE_LANGUAGE_CANNOT_DISABLE_DEFAULT, new Object[]{lang.getCode(), lang.getDescr()}, "language.cannot.disable.default"); } return bindingResult; }
protected BeanPropertyBindingResult validateAdd(PageModelRequest pageModelRequest) { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(pageModelRequest, "pageModel"); PageModel pageModel = pageModelManager.getPageModel(pageModelRequest.getCode()); if (null == pageModel) { return bindingResult; } bindingResult.reject(PageModelValidator.ERRCODE_CODE_EXISTS, new String[]{pageModelRequest.getCode()}, "pageModel.code.exists"); return bindingResult; }
private BeanPropertyBindingResult checkWidgetForDelete(WidgetType widgetType) throws ApsSystemException { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(widgetType, "widget"); if (null == widgetType) { return bindingResult; } if (widgetType.isLocked()) { bindingResult.reject(WidgetValidator.ERRCODE_OPERATION_FORBIDDEN_LOCKED, new String[]{widgetType.getCode()}, "widgettype.delete.locked"); } List<IPage> onLinePages = this.getPageManager().getOnlineWidgetUtilizers(widgetType.getCode()); List<IPage> draftPages = this.getPageManager().getDraftWidgetUtilizers(widgetType.getCode()); if ((null != onLinePages && onLinePages.size() > 0) || (null != draftPages && draftPages.size() > 0)) { bindingResult.reject(WidgetValidator.ERRCODE_CANNOT_DELETE_USED_PAGES, new String[]{widgetType.getCode()}, "widgettype.delete.references.page"); } return bindingResult; }
protected void checkResource(String currentPath, String objectName, Boolean protectedFolder) { if (null == protectedFolder && !StringUtils.isEmpty(currentPath)) { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(objectName, objectName); bindingResult.reject(FileBrowserValidator.ERRCODE_REQUIRED_FOLDER_TYPE, "fileBrowser.browser.protectedFolder.required"); throw new ValidationGenericException(bindingResult); } if (null == protectedFolder) { return; } try { boolean exists = this.getStorageManager().exists(currentPath, protectedFolder); if (!exists) { logger.warn("no resource found for path {} - type {}", currentPath, protectedFolder); throw new RestRourceNotFoundException(FileBrowserValidator.ERRCODE_RESOURCE_DOES_NOT_EXIST, objectName, currentPath); } } catch (RestRourceNotFoundException e) { throw e; } catch (Exception e) { logger.error("Error checking resource {} , protected {} ", currentPath, protectedFolder, e); throw new RestServerError("error checking resource", e); } }
protected void deleteEntityType(String entityManagerCode, String entityTypeCode) { IEntityManager entityManager = this.extractEntityManager(entityManagerCode); try { IApsEntity entityType = entityManager.getEntityPrototype(entityTypeCode); if (null == entityType) { return; } List<String> ids = entityManager.searchId(entityTypeCode, null); if (!CollectionUtils.isEmpty(ids)) { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(entityType, "entityType"); bindingResult.reject(AbstractEntityTypeValidator.ERRCODE_ENTITY_TYPE_REFERENCES, new Object[]{entityTypeCode}, "entityType.cannot.delete.references"); throw new ValidationConflictException(bindingResult); } ((IEntityTypesConfigurer) entityManager).removeEntityPrototype(entityTypeCode); } catch (ApsSystemException e) { logger.error("Error in delete entityType {}", entityTypeCode, e); throw new RestServerError("error in delete entityType", e); } }
@Override public void deleteCategory(String categoryCode) { Category category = this.getCategoryManager().getCategory(categoryCode); if (null == category) { return; } try { for (CategoryUtilizer categoryUtilizer : this.getCategoryUtilizers()) { List references = categoryUtilizer.getCategoryUtilizers(categoryCode); if (null != references && !references.isEmpty()) { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(category, "category"); bindingResult.reject(CategoryValidator.ERRCODE_CATEGORY_REFERENCES, new String[]{categoryCode}, "category.cannot.delete.references"); throw new ValidationGenericException(bindingResult); } } this.getCategoryManager().deleteCategory(categoryCode); } catch (ValidationGenericException e) { throw e; } catch (Exception e) { logger.error("error deleting category " + categoryCode, e); throw new RestServerError("error deleting category " + categoryCode, e); } }
private IPage loadPage(String pageCode, String status) { IPage page = null; switch (status) { case STATUS_DRAFT: page = this.getPageManager().getDraftPage(pageCode); break; case STATUS_ONLINE: page = this.getPageManager().getOnlinePage(pageCode); break; default: break; } if (status.equals(STATUS_ONLINE) && null == page && null != this.getPageManager().getDraftPage(pageCode)) { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(page, "page"); bindingResult.reject(ERRCODE_PAGE_ONLY_DRAFT, new Object[]{pageCode}, "page.status.draftOnly"); throw new ValidationGenericException(bindingResult); } return page; }
protected BeanPropertyBindingResult validateAddLabelGroup(LabelDto labelDto) { try { BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(labelDto, "labelGroup"); String code = labelDto.getKey(); ApsProperties labelGroup = this.getI18nManager().getLabelGroup(code); if (null != labelGroup) { bindingResult.reject(LabelValidator.ERRCODE_LABELGROUP_EXISTS, new String[]{code}, "labelGroup.code.already.present"); return bindingResult; } String defaultLangCode = this.getLangManager().getDefaultLang().getCode(); boolean isDefaultLangValid = this.validateDefaultLang(labelDto, bindingResult, defaultLangCode); if (!isDefaultLangValid) { return bindingResult; } this.validateLabelEntry(labelDto, defaultLangCode, bindingResult); return bindingResult; } catch (ApsSystemException t) { logger.error("error in validate add label group with code {}", labelDto.getKey(), t); throw new RestServerError("error in validate add label group", t); } }
@Test public void testValidateOnDeleteReservedGroups() throws ApsSystemException, Exception { UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build(); String accessToken = mockOAuthInterceptor(user); String groupName = Group.FREE_GROUP_NAME; BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult("group", "group"); bindingResult.reject(GroupValidator.ERRCODE_CANNOT_DELETE_RESERVED_GROUP, new String[]{groupName}, "group.cannot.delete.reserved"); doThrow(new ValidationConflictException(bindingResult)).when(groupService).removeGroup(groupName); this.controller.setGroupValidator(new GroupValidator()); ResultActions result = mockMvc.perform( delete("/groups/{groupName}", groupName) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + accessToken)); result.andExpect(status().isConflict()); result.andExpect(jsonPath("$.errors[0].code", is(GroupValidator.ERRCODE_CANNOT_DELETE_RESERVED_GROUP))); }