|
| 1 | +/* |
| 2 | + * Copyright © 2015 The Gravitee team (http://gravitee.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.gravitee.rest.api.management.v2.rest.validation; |
| 17 | + |
| 18 | +import static io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationItem.TypeEnum; |
| 19 | + |
| 20 | +import io.gravitee.apim.core.portal_page.query_service.PortalNavigationItemsQueryService; |
| 21 | +import io.gravitee.common.utils.UUID; |
| 22 | +import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationItem; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.Stream; |
| 27 | +import lombok.Builder; |
| 28 | +import lombok.Getter; |
| 29 | + |
| 30 | +public class PortalNavigationItemsValidator { |
| 31 | + |
| 32 | + private static final String MESSAGE_INVALID_UUID = "Invalid UUID string: %s"; |
| 33 | + private static final String MESSAGE_INAPPLICABLE_PARAMETER = "Parameter '%s' is only applicable to items of type %s"; |
| 34 | + private static final String MESSAGE_REQUIRED_PARAMETER = "Parameter '%s' is required for items of type %s"; |
| 35 | + private static final String CONTENT_ID = "contentId"; |
| 36 | + private static final String PARENT_ID = "parentId"; |
| 37 | + private static final String URI = "uri"; |
| 38 | + |
| 39 | + private PortalNavigationItemsValidator() {} |
| 40 | + |
| 41 | + public static Set<ValidationError> validate(CreatePortalNavigationItem createPortalNavigationItem) { |
| 42 | + final var commonErrors = new HashSet<ValidationError>(); |
| 43 | + final var parentId = createPortalNavigationItem.getParentId(); |
| 44 | + if (parentId != null) { |
| 45 | + final var error = validateUUID(PARENT_ID, parentId); |
| 46 | + if (error != null) { |
| 47 | + commonErrors.add(error); |
| 48 | + } |
| 49 | + } |
| 50 | + final var type = createPortalNavigationItem.getType(); |
| 51 | + final var specificErrors = switch (type) { |
| 52 | + case FOLDER -> validateFolderCreationParams(createPortalNavigationItem); |
| 53 | + case PAGE -> validatePageCreationParams(createPortalNavigationItem); |
| 54 | + case LINK -> validateLinkCreationParams(createPortalNavigationItem); |
| 55 | + }; |
| 56 | + return Stream.concat(commonErrors.stream(), specificErrors.stream()).collect(Collectors.toSet()); |
| 57 | + } |
| 58 | + |
| 59 | + private static Set<ValidationError> validateFolderCreationParams(CreatePortalNavigationItem createPortalNavigationItem) { |
| 60 | + final var errors = new HashSet<ValidationError>(); |
| 61 | + if (createPortalNavigationItem.getContentId() != null) { |
| 62 | + errors.add( |
| 63 | + ValidationError.builder() |
| 64 | + .errorMessage(String.format(MESSAGE_INAPPLICABLE_PARAMETER, CONTENT_ID, TypeEnum.PAGE.name())) |
| 65 | + .location(CONTENT_ID) |
| 66 | + .present(true) |
| 67 | + .build() |
| 68 | + ); |
| 69 | + } |
| 70 | + if (createPortalNavigationItem.getUri() != null) { |
| 71 | + errors.add( |
| 72 | + ValidationError.builder() |
| 73 | + .errorMessage(String.format(MESSAGE_INAPPLICABLE_PARAMETER, URI, TypeEnum.LINK.name())) |
| 74 | + .location(URI) |
| 75 | + .present(true) |
| 76 | + .build() |
| 77 | + ); |
| 78 | + } |
| 79 | + return errors; |
| 80 | + } |
| 81 | + |
| 82 | + private static Set<ValidationError> validatePageCreationParams(CreatePortalNavigationItem createPortalNavigationItem) { |
| 83 | + final var errors = new HashSet<ValidationError>(); |
| 84 | + if (createPortalNavigationItem.getContentId() == null) { |
| 85 | + errors.add( |
| 86 | + ValidationError.builder() |
| 87 | + .errorMessage(String.format(MESSAGE_REQUIRED_PARAMETER, CONTENT_ID, TypeEnum.PAGE.name())) |
| 88 | + .location(CONTENT_ID) |
| 89 | + .present(false) |
| 90 | + .build() |
| 91 | + ); |
| 92 | + } else { |
| 93 | + final var uuidError = validateUUID(CONTENT_ID, createPortalNavigationItem.getContentId()); |
| 94 | + if (uuidError != null) { |
| 95 | + errors.add(uuidError); |
| 96 | + } |
| 97 | + } |
| 98 | + if (createPortalNavigationItem.getUri() != null) { |
| 99 | + errors.add( |
| 100 | + ValidationError.builder() |
| 101 | + .errorMessage(String.format(MESSAGE_INAPPLICABLE_PARAMETER, URI, TypeEnum.LINK.name())) |
| 102 | + .location(URI) |
| 103 | + .present(true) |
| 104 | + .build() |
| 105 | + ); |
| 106 | + } |
| 107 | + return errors; |
| 108 | + } |
| 109 | + |
| 110 | + private static Set<ValidationError> validateLinkCreationParams(CreatePortalNavigationItem createPortalNavigationItem) { |
| 111 | + final var errors = new HashSet<ValidationError>(); |
| 112 | + if (createPortalNavigationItem.getUri() == null) { |
| 113 | + errors.add( |
| 114 | + ValidationError.builder() |
| 115 | + .errorMessage(String.format(MESSAGE_REQUIRED_PARAMETER, URI, TypeEnum.LINK.name())) |
| 116 | + .location(URI) |
| 117 | + .present(false) |
| 118 | + .build() |
| 119 | + ); |
| 120 | + } |
| 121 | + if (createPortalNavigationItem.getContentId() != null) { |
| 122 | + errors.add( |
| 123 | + ValidationError.builder() |
| 124 | + .errorMessage(String.format(MESSAGE_INAPPLICABLE_PARAMETER, CONTENT_ID, TypeEnum.PAGE.name())) |
| 125 | + .location(CONTENT_ID) |
| 126 | + .present(true) |
| 127 | + .build() |
| 128 | + ); |
| 129 | + } |
| 130 | + return errors; |
| 131 | + } |
| 132 | + |
| 133 | + private static ValidationError validateUUID(String field, String uuid) { |
| 134 | + try { |
| 135 | + UUID.fromString(uuid); |
| 136 | + return null; |
| 137 | + } catch (IllegalArgumentException e) { |
| 138 | + return ValidationError.builder().errorMessage(String.format(MESSAGE_INVALID_UUID, uuid)).location(field).present(true).build(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Getter |
| 143 | + @Builder |
| 144 | + public static class ValidationError { |
| 145 | + |
| 146 | + private String errorMessage; |
| 147 | + private String location; |
| 148 | + private boolean present; |
| 149 | + } |
| 150 | +} |
0 commit comments