- protected void validateTagsListAndRemoveDuplicates(User user, Component component, AuditingActionEnum actionEnum) {
- List<String> tagsList = component.getTags();
- try {
- validateComponentTags(tagsList, component.getName(), component.getComponentType(), user, component, actionEnum);
- } catch(ByActionStatusComponentException e){
- ResponseFormat responseFormat = componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
- componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, component.getComponentType());
- throw e;
- } catch(ByResponseFormatComponentException e){
- ResponseFormat responseFormat = e.getResponseFormat();
- componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, component.getComponentType());
- throw e;
- }
- ValidationUtils.removeDuplicateFromList(tagsList);
- }
-
- protected void validateComponentTags(List<String> tags, String name, ComponentTypeEnum componentType, User user, Component component, AuditingActionEnum action) {
- log.debug("validate component tags");
- boolean includesComponentName = false;
- int tagListSize = 0;
- ResponseFormat responseFormat;
- if (tags != null && !tags.isEmpty()) {
- for (String tag : tags) {
- if (!ValidationUtils.validateTagLength(tag)) {
- log.debug("tag length exceeds limit {}", ValidationUtils.TAG_MAX_LENGTH);
- responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT, "" + ValidationUtils.TAG_MAX_LENGTH);
- componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
- throw new ByActionStatusComponentException(ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT, "" + ValidationUtils.TAG_MAX_LENGTH);
- }
- if (validateTagPattern(tag)) {
- if (!includesComponentName) {
- includesComponentName = name.equals(tag);
- }
- } else {
- log.debug("invalid tag {}", tag);
- responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_FIELD_FORMAT, componentType.getValue(), TAG_FIELD_LABEL);
- componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
- throw new ByActionStatusComponentException(ActionStatus.INVALID_FIELD_FORMAT, componentType.getValue(), TAG_FIELD_LABEL);
- }
- tagListSize += tag.length() + 1;
- }
- if (tagListSize > 0) {
- tagListSize--;
- }
-
- if (!includesComponentName) {
- log.debug("tags must include component name");
- responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
- componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
- throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
- }
- if (!ValidationUtils.validateTagListLength(tagListSize)) {
- log.debug("overall tags length exceeds limit {}", ValidationUtils.TAG_LIST_MAX_LENGTH);
- responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT, "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
- componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
- throw new ByActionStatusComponentException(ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT, "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
- }
- } else {
- responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_TAGS);
- componentsUtils.auditComponentAdmin(responseFormat, user, component, action, componentType);
- throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_TAGS);
- }
- }
-
- protected boolean validateTagPattern(String tag) {
- return ValidationUtils.validateComponentNamePattern(tag);
- }
-
- protected Either<Boolean, ResponseFormat> validateProjectCode(User user, Component component, AuditingActionEnum actionEnum) {
- if (ComponentTypeEnum.RESOURCE.equals(component.getComponentType())) {
- return Either.left(true);
- }
- log.debug("validate ProjectCode name ");
- String projectCode = component.getProjectCode();
-
- if (!ValidationUtils.validateStringNotEmpty(projectCode)) {
- log.info("projectCode is missing.");
- ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_PROJECT_CODE);
- componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, component.getComponentType(),
- ResourceVersionInfo.newBuilder()
- .build());
- return Either.right(errorResponse);
- }
-
- Either<Boolean, ResponseFormat> validateProjectCodeResponse = validateProjectCode(projectCode);
- if (validateProjectCodeResponse.isRight()) {
- ResponseFormat responseFormat = validateProjectCodeResponse.right().value();
- componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, component.getComponentType(),
- ResourceVersionInfo.newBuilder()
- .build());
- }
- return validateProjectCodeResponse;
-
- }
-
- private Either<Boolean, ResponseFormat> validateProjectCode(String projectCode) {
- if (projectCode != null) {
- if (!ValidationUtils.validateProjectCode(projectCode)) {
- log.info("projectCode is not valid.");
- ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROJECT_CODE);
- return Either.right(errorResponse);
- }
- return Either.left(true);
- }
- return Either.left(false);
- }
-