├── src ├── main │ ├── resources │ │ ├── application.properties │ │ ├── META-INF │ │ │ └── services │ │ │ │ └── javax.validation.ConstraintValidator │ │ ├── ValidationMessages_fr.properties │ │ └── ValidationMessages.properties │ └── java │ │ ├── org │ │ └── blackdread │ │ │ └── lib │ │ │ └── restfilter │ │ │ ├── spring │ │ │ ├── jpa │ │ │ │ └── .gitkeep │ │ │ ├── util │ │ │ │ └── .gitkeep │ │ │ ├── filter │ │ │ │ ├── QueryServiceImpl.java │ │ │ │ ├── JooqQueryServiceImpl.java │ │ │ │ └── JooqQueryService.java │ │ │ ├── query │ │ │ │ ├── SortQueryParamSpringUtil.java │ │ │ │ └── PageableQueryParamSpringUtil.java │ │ │ └── criteria │ │ │ │ ├── CriteriaQueryParamSpringExtend.java │ │ │ │ └── CriteriaQueryParamSpring.java │ │ │ ├── validation │ │ │ ├── file │ │ │ │ ├── internal │ │ │ │ │ ├── package-info.java │ │ │ │ │ ├── FilenamePatternValidatorForPath.java │ │ │ │ │ ├── FilenamePatternValidatorForFile.java │ │ │ │ │ ├── FilenamePatternValidatorForPart.java │ │ │ │ │ ├── FilenamePatternValidatorForMultipartFile.java │ │ │ │ │ ├── FilenamePatternValidator.java │ │ │ │ │ ├── FileSizeValidatorForFile.java │ │ │ │ │ ├── PathExistValidatorForFile.java │ │ │ │ │ ├── FileSizeValidatorForPart.java │ │ │ │ │ ├── IsAbsolutePathValidatorForFile.java │ │ │ │ │ ├── IsAbsolutePathValidatorForPath.java │ │ │ │ │ ├── PathExistValidatorForPath.java │ │ │ │ │ ├── FileSizeValidatorForMultipartFile.java │ │ │ │ │ ├── FileExtensionValidatorForFile.java │ │ │ │ │ ├── FileExtensionValidatorForPart.java │ │ │ │ │ ├── FileExtensionValidatorForPath.java │ │ │ │ │ ├── FileSizeValidator.java │ │ │ │ │ ├── FileExtensionValidatorForMultipartFile.java │ │ │ │ │ ├── FileSizeValidatorForPath.java │ │ │ │ │ └── FileMimeTypeValidator.java │ │ │ │ ├── PathExist.java │ │ │ │ ├── IsAbsolutePath.java │ │ │ │ ├── FileNotEmpty.java │ │ │ │ └── FileMimeType.java │ │ │ ├── criteria │ │ │ │ ├── internal │ │ │ │ │ ├── package-info.java │ │ │ │ │ ├── InForbiddenValidator.java │ │ │ │ │ ├── NotInForbiddenValidator.java │ │ │ │ │ ├── EqualsForbiddenValidator.java │ │ │ │ │ ├── NotEqualsForbiddenValidator.java │ │ │ │ │ ├── SpecifiedForbiddenValidator.java │ │ │ │ │ ├── ContainsForbiddenValidator.java │ │ │ │ │ ├── LessThanForbiddenValidator.java │ │ │ │ │ ├── GreaterThanForbiddenValidator.java │ │ │ │ │ ├── NotContainsForbiddenValidator.java │ │ │ │ │ ├── LessThanOrEqualForbiddenValidator.java │ │ │ │ │ └── GreaterThanOrEqualForbiddenValidator.java │ │ │ │ ├── InForbidden.java │ │ │ │ ├── NotInForbidden.java │ │ │ │ ├── EqualsForbidden.java │ │ │ │ ├── ContainsForbidden.java │ │ │ │ ├── LessThanForbidden.java │ │ │ │ ├── NotEqualsForbidden.java │ │ │ │ ├── SpecifiedForbidden.java │ │ │ │ ├── GreaterThanForbidden.java │ │ │ │ ├── NotContainsForbidden.java │ │ │ │ ├── LessThanOrEqualForbidden.java │ │ │ │ └── GreaterThanOrEqualForbidden.java │ │ │ └── nullability │ │ │ │ ├── internal │ │ │ │ ├── package-info.java │ │ │ │ ├── OneNotNullValidator.kt │ │ │ │ ├── AllOrNoneNullValidator.kt │ │ │ │ └── NullOrMaxNotNullValidator.kt │ │ │ │ ├── OneNotNull.kt │ │ │ │ ├── AllOrNoneNull.kt │ │ │ │ └── NullOrMaxNotNull.kt │ │ │ ├── filter │ │ │ ├── package-info.java │ │ │ └── UUIDFilter.java │ │ │ ├── criteria │ │ │ ├── Criteria.java │ │ │ ├── FilterQueryParamFormatter.java │ │ │ ├── UnsupportedFilterForQueryParamException.java │ │ │ └── annotation │ │ │ │ ├── CriteriaIgnore.java │ │ │ │ └── CriteriaAlias.java │ │ │ └── util │ │ │ └── IterableUtil.java │ │ └── module-info.text └── test │ ├── java │ └── org │ │ └── blackdread │ │ └── lib │ │ └── restfilter │ │ ├── List2.java │ │ ├── Map2.java │ │ ├── demo │ │ ├── jooq │ │ │ ├── package-info.java │ │ │ ├── DefaultCatalog.java │ │ │ ├── Tables.java │ │ │ ├── Indexes.java │ │ │ └── Public.java │ │ ├── ChildRepository.java │ │ ├── ParentRepository.java │ │ ├── SpringTestApp.java │ │ ├── ChildEntity.java │ │ ├── ParentEntity.java │ │ ├── converter │ │ │ └── InstantConverter.java │ │ └── BaseEntity.java │ │ └── criteria │ │ ├── parser │ │ ├── CriteriaFieldParserUtilAllIgnoreAnnotationTest.java │ │ └── CriteriaFieldParserUtilNoAnnotationTest.java │ │ └── DebugController.java │ └── resources │ ├── application.yml │ ├── 1KB.txt │ └── database.sql ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── .github ├── workflows │ └── maven.yml └── dependabot.yml ├── .editorconfig ├── .travis.yml ├── LICENSE └── .gitignore /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/jpa/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/util/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Blackdread/rest-filter/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Validators implementations, internal only, might be moved to another module later 3 | *

Created on 2019/12/25.

4 | * 5 | * @author Yoann CAPLAIN 6 | */ 7 | package org.blackdread.lib.restfilter.validation.file.internal; 8 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Validators implementations, internal only, might be moved to another module later 3 | *

Created on 2019/12/25.

4 | * 5 | * @author Yoann CAPLAIN 6 | */ 7 | package org.blackdread.lib.restfilter.validation.criteria.internal; 8 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/nullability/internal/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Validators implementations, internal only, might be moved to another module later 3 | *

Created on 2019/12/25.

4 | * 5 | * @author Yoann CAPLAIN 6 | */ 7 | package org.blackdread.lib.restfilter.validation.nullability.internal; 8 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Set up JDK 12 13 | uses: actions/setup-java@v1 14 | with: 15 | java-version: 12 16 | - name: Build with Maven 17 | run: mvn package --file pom.xml 18 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | 9 | # Change these settings to your own preference 10 | indent_style = space 11 | indent_size = 4 12 | 13 | # We recommend you to keep these unchanged 14 | end_of_line = lf 15 | charset = utf-8 16 | trim_trailing_whitespace = true 17 | insert_final_newline = true 18 | 19 | [*.yml] 20 | indent_size = 2 21 | 22 | [*.md] 23 | trim_trailing_whitespace = false 24 | 25 | [{package,bower}.json] 26 | indent_style = space 27 | indent_size = 2 28 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "maven" 9 | directory: "/" 10 | schedule: 11 | interval: "monthly" 12 | groups: 13 | maven-dependencies: 14 | update-types: 15 | - "patch" 16 | - "minor" 17 | - "major" 18 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/List2.java: -------------------------------------------------------------------------------- 1 | package org.blackdread.lib.restfilter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Just to keep code compatible with java 8, while trying same syntax as java 9+, and allow easy refactoring once drop java 8 8 | *

Created on 2019/10/28.

9 | * 10 | * @author Yoann CAPLAIN 11 | */ 12 | public class List2 { 13 | 14 | public static List of(T... values) { 15 | final ArrayList list = new ArrayList<>(values.length); 16 | for (final T value : values) { 17 | list.add(value); 18 | } 19 | return list; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | #addons: 4 | # sonarcloud: 5 | # organization: "blackdread-github" 6 | # Token provided by travis 7 | # token: 8 | # secure: "" 9 | 10 | jdk: 11 | # - oraclejdk8 might want to customize build sequence to manually compile with 1.8 then 11 12 | - openjdk12 13 | 14 | before_install: 15 | - chmod +x mvnw 16 | 17 | after_success: 18 | #- mvn clean test 19 | # - mvn clean test jacoco:report 20 | - mvn clean test jacoco:report coveralls:report 21 | # - mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar 22 | 23 | 24 | #notifications: 25 | # email: 26 | # - yoann.caplain@blackdread.org 27 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/Map2.java: -------------------------------------------------------------------------------- 1 | package org.blackdread.lib.restfilter; 2 | 3 | import java.util.Collections; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | /** 8 | * Just to keep code compatible with java 8, while trying same syntax as java 9+, and allow easy refactoring once drop java 8 9 | *

Created on 2019/10/28.

10 | * 11 | * @author Yoann CAPLAIN 12 | */ 13 | public class Map2 { 14 | 15 | public static Map of(K k1, V v1) { 16 | final HashMap map = new HashMap<>(1); 17 | map.put(k1, v1); 18 | return Collections.unmodifiableMap(map); 19 | } 20 | 21 | public static Map of(K k1, V v1, K k2, V v2) { 22 | final HashMap map = new HashMap<>(2); 23 | map.put(k1, v1); 24 | map.put(k2, v2); 25 | return Collections.unmodifiableMap(map); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:h2:mem:testdb 4 | driverClassName: org.h2.Driver 5 | username: sa 6 | password: 7 | jpa: 8 | database-platform: org.hibernate.dialect.H2Dialect 9 | show-sql: true 10 | hibernate: 11 | ddl-auto: update 12 | dialect: org.hibernate.dialect.H2Dialect 13 | properties: 14 | hibernate.jdbc.time_zone: UTC # very important part otherwise dates queries from db get translated to current pc timezone 15 | h2: 16 | console: 17 | enabled: false 18 | jackson: 19 | serialization: 20 | write-durations-as-timestamps: false # since jackson 2.5 21 | # write-dates-as-timestamps: false 22 | # write-date-timestamps-as-nanoseconds: false 23 | # deserialization: 24 | # read-date-timestamps-as-nanoseconds: false 25 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FilenamePatternValidatorForPath.java: -------------------------------------------------------------------------------- 1 | package org.blackdread.lib.restfilter.validation.file.internal; 2 | 3 | import org.blackdread.lib.restfilter.validation.file.FilenamePattern; 4 | 5 | import javax.validation.ConstraintValidator; 6 | import javax.validation.ConstraintValidatorContext; 7 | import java.nio.file.Path; 8 | 9 | public class FilenamePatternValidatorForPath extends FilenamePatternValidator implements ConstraintValidator { 10 | 11 | @Override 12 | public boolean isValid(Path value, ConstraintValidatorContext context) { 13 | if (value == null) { 14 | return true; 15 | } 16 | Path fileName = value.getFileName(); 17 | if (fileName == null) { 18 | // todo check logic to apply 19 | return false; 20 | } 21 | return super.isValid(fileName.toString(), context); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/resources/1KB.txt: -------------------------------------------------------------------------------- 1 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FilenamePatternValidatorForFile.java: -------------------------------------------------------------------------------- 1 | package org.blackdread.lib.restfilter.validation.file.internal; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.blackdread.lib.restfilter.validation.file.FilenamePattern; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | import java.io.File; 9 | 10 | public class FilenamePatternValidatorForFile extends FilenamePatternValidator implements ConstraintValidator { 11 | 12 | @Override 13 | public boolean isValid(File value, ConstraintValidatorContext context) { 14 | if (value == null) { 15 | return true; 16 | } 17 | String fileName = value.getName(); 18 | if (StringUtils.isBlank(fileName)) { 19 | // todo check logic to apply 20 | return false; 21 | } 22 | return super.isValid(fileName, context); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/module-info.text: -------------------------------------------------------------------------------- 1 | /** 2 | *

Created on 2019/09/21.

3 | * 4 | * @author Yoann CAPLAIN 5 | */ 6 | //module rest.filter { 7 | // requires static java.persistence; 8 | // requires static spring.data.commons; 9 | // requires static spring.context; 10 | // requires static spring.tx; 11 | // requires static spring.data.jpa; 12 | // requires static org.jooq; 13 | // requires static org.apache.commons.text; 14 | // 15 | // requires slf4j.api; 16 | // requires jsr305; 17 | // 18 | // exports org.blackdread.lib.restfilter.criteria; 19 | // exports org.blackdread.lib.restfilter.filter; 20 | // exports org.blackdread.lib.restfilter.jooq; 21 | // exports org.blackdread.lib.restfilter.spring.filter; 22 | // exports org.blackdread.lib.restfilter.spring.sort; 23 | //} 24 | 25 | // TODO See later 26 | // https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-resource-bundle-locator 27 | // https://in.relation.to/2016/06/17/bean-validation-and-the-jigsaw-liaison/ 28 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FilenamePatternValidatorForPart.java: -------------------------------------------------------------------------------- 1 | package org.blackdread.lib.restfilter.validation.file.internal; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.blackdread.lib.restfilter.validation.file.FilenamePattern; 5 | 6 | import javax.servlet.http.Part; 7 | import javax.validation.ConstraintValidator; 8 | import javax.validation.ConstraintValidatorContext; 9 | import java.io.File; 10 | 11 | public class FilenamePatternValidatorForPart extends FilenamePatternValidator implements ConstraintValidator { 12 | 13 | @Override 14 | public boolean isValid(Part value, ConstraintValidatorContext context) { 15 | if (value == null) { 16 | return true; 17 | } 18 | String fileName = value.getSubmittedFileName(); 19 | if (StringUtils.isBlank(fileName)) { 20 | // todo check logic to apply 21 | return false; 22 | } 23 | return super.isValid(fileName, context); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FilenamePatternValidatorForMultipartFile.java: -------------------------------------------------------------------------------- 1 | package org.blackdread.lib.restfilter.validation.file.internal; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.blackdread.lib.restfilter.validation.file.FilenamePattern; 5 | import org.springframework.web.multipart.MultipartFile; 6 | 7 | import javax.servlet.http.Part; 8 | import javax.validation.ConstraintValidator; 9 | import javax.validation.ConstraintValidatorContext; 10 | 11 | public class FilenamePatternValidatorForMultipartFile extends FilenamePatternValidator implements ConstraintValidator { 12 | 13 | @Override 14 | public boolean isValid(MultipartFile value, ConstraintValidatorContext context) { 15 | if (value == null) { 16 | return true; 17 | } 18 | String fileName = value.getOriginalFilename(); 19 | if (StringUtils.isBlank(fileName)) { 20 | // todo check logic to apply 21 | return false; 22 | } 23 | return super.isValid(fileName, context); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019-2020 Yoann CAPLAIN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/filter/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | /** 25 | * Utilities for criteria classes, used for filtering data on the back-end. 26 | */ 27 | package org.blackdread.lib.restfilter.filter; 28 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/jooq/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | /** 25 | * Put generated classes here as had issues with test generation, compilation did not find those even though it was generated in phase "generate-test-sources" 26 | */ 27 | package org.blackdread.lib.restfilter.demo.jooq; 28 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/criteria/Criteria.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria; 25 | 26 | /** 27 | * Implementation should usually contain fields of Filter instances. 28 | */ 29 | public interface Criteria { 30 | 31 | /** 32 | * @return a new criteria with copied filters 33 | */ 34 | Criteria copy(); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/jooq/DefaultCatalog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is generated by jOOQ. 3 | */ 4 | package org.blackdread.lib.restfilter.demo.jooq; 5 | 6 | 7 | import org.jooq.Schema; 8 | import org.jooq.impl.CatalogImpl; 9 | 10 | import javax.annotation.Generated; 11 | import java.util.ArrayList; 12 | import java.util.Arrays; 13 | import java.util.List; 14 | 15 | 16 | /** 17 | * This class is generated by jOOQ. 18 | */ 19 | @Generated( 20 | value = { 21 | "http://www.jooq.org", 22 | "jOOQ version:3.11.11" 23 | }, 24 | comments = "This class is generated by jOOQ" 25 | ) 26 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 27 | public class DefaultCatalog extends CatalogImpl { 28 | 29 | private static final long serialVersionUID = -1098090960; 30 | 31 | /** 32 | * The reference instance of 33 | */ 34 | public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog(); 35 | 36 | /** 37 | * The schema PUBLIC. 38 | */ 39 | public final Public PUBLIC = Public.PUBLIC; 40 | 41 | /** 42 | * No further instances allowed 43 | */ 44 | private DefaultCatalog() { 45 | super(""); 46 | } 47 | 48 | @Override 49 | public final List getSchemas() { 50 | List result = new ArrayList(); 51 | result.addAll(getSchemas0()); 52 | return result; 53 | } 54 | 55 | private final List getSchemas0() { 56 | return Arrays.asList( 57 | Public.PUBLIC); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/javax.validation.ConstraintValidator: -------------------------------------------------------------------------------- 1 | org.blackdread.lib.restfilter.validation.file.internal.FileExtensionValidatorForFile 2 | org.blackdread.lib.restfilter.validation.file.internal.FileExtensionValidatorForPath 3 | org.blackdread.lib.restfilter.validation.file.internal.FileExtensionValidatorForPart 4 | org.blackdread.lib.restfilter.validation.file.internal.FileExtensionValidatorForMultipartFile 5 | org.blackdread.lib.restfilter.validation.file.internal.FileSizeValidatorForFile 6 | org.blackdread.lib.restfilter.validation.file.internal.FileSizeValidatorForPath 7 | org.blackdread.lib.restfilter.validation.file.internal.FileSizeValidatorForPart 8 | org.blackdread.lib.restfilter.validation.file.internal.FileSizeValidatorForMultipartFile 9 | org.blackdread.lib.restfilter.validation.file.internal.IsAbsolutePathValidatorForFile 10 | org.blackdread.lib.restfilter.validation.file.internal.IsAbsolutePathValidatorForPath 11 | org.blackdread.lib.restfilter.validation.file.internal.PathExistValidatorForFile 12 | org.blackdread.lib.restfilter.validation.file.internal.PathExistValidatorForPath 13 | org.blackdread.lib.restfilter.validation.file.internal.FilenamePatternValidatorForFile 14 | org.blackdread.lib.restfilter.validation.file.internal.FilenamePatternValidatorForPath 15 | org.blackdread.lib.restfilter.validation.file.internal.FilenamePatternValidatorForPart 16 | org.blackdread.lib.restfilter.validation.file.internal.FilenamePatternValidatorForMultipartFile 17 | org.blackdread.lib.restfilter.validation.nullability.internal.AllOrNoneNullValidator 18 | org.blackdread.lib.restfilter.validation.nullability.internal.NullOrMaxNotNullValidator 19 | org.blackdread.lib.restfilter.validation.nullability.internal.OneNotNullValidator 20 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/ChildRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.demo; 25 | 26 | import org.springframework.data.jpa.repository.JpaRepository; 27 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 28 | import org.springframework.stereotype.Repository; 29 | 30 | /** 31 | *

Created on 2019/07/09.

32 | * 33 | * @author Yoann CAPLAIN 34 | */ 35 | @Repository 36 | public interface ChildRepository extends JpaRepository, JpaSpecificationExecutor { 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/ParentRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.demo; 25 | 26 | import org.springframework.data.jpa.repository.JpaRepository; 27 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 28 | import org.springframework.stereotype.Repository; 29 | 30 | /** 31 | *

Created on 2019/07/24.

32 | * 33 | * @author Yoann CAPLAIN 34 | */ 35 | @Repository 36 | public interface ParentRepository extends JpaRepository, JpaSpecificationExecutor { 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/SpringTestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.demo; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | import org.springframework.boot.autoconfigure.SpringBootApplication; 29 | 30 | /** 31 | *

Created on 2019/07/09.

32 | * 33 | * @author Yoann CAPLAIN 34 | */ 35 | @SpringBootApplication 36 | public class SpringTestApp { 37 | 38 | private static final Logger log = LoggerFactory.getLogger(SpringTestApp.class); 39 | 40 | public static void main(String[] args) { 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/ChildEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.demo; 25 | 26 | import javax.persistence.Entity; 27 | import javax.persistence.FetchType; 28 | import javax.persistence.ManyToOne; 29 | 30 | @Entity 31 | public class ChildEntity extends BaseEntity { 32 | 33 | @ManyToOne(optional = true, fetch = FetchType.LAZY) 34 | private ParentEntity parent; 35 | 36 | public ParentEntity getParent() { 37 | return parent; 38 | } 39 | 40 | public void setParent(ParentEntity parent) { 41 | this.parent = parent; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/filter/QueryServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.spring.filter; 25 | 26 | import org.springframework.transaction.annotation.Transactional; 27 | 28 | /** 29 | * An empty implementation of QueryService, can be used as composition instead of inheritance. 30 | * (Do not instantiate yourself otherwise Transactional will do nothing, unless you call it from a Transactional service already) 31 | * 32 | * @param the type of the entity which is queried. 33 | */ 34 | @Transactional(readOnly = true) 35 | public class QueryServiceImpl implements QueryService { 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/ParentEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.demo; 25 | 26 | import javax.persistence.Entity; 27 | import javax.persistence.FetchType; 28 | import javax.persistence.OneToMany; 29 | import java.util.HashSet; 30 | import java.util.Set; 31 | 32 | @Entity 33 | public class ParentEntity extends BaseEntity { 34 | @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) 35 | private Set child1s = new HashSet<>(); 36 | 37 | public Set getChild1s() { 38 | return child1s; 39 | } 40 | 41 | public void setChild1s(Set child1s) { 42 | this.child1s = child1s; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/filter/JooqQueryServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.spring.filter; 25 | 26 | import org.springframework.transaction.annotation.Transactional; 27 | 28 | /** 29 | * An empty implementation of JooqQueryService, can be used as composition instead of inheritance. 30 | * (Do not instantiate yourself otherwise Transactional will do nothing, unless you call it from a Transactional service already) 31 | *

Created on 2019/07/23

32 | * 33 | * @author Yoann CAPLAIN 34 | * @deprecated not sure of API, better to use directly the util class with the static methods 35 | */ 36 | @Transactional(readOnly = true) 37 | public class JooqQueryServiceImpl implements JooqQueryService { 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FilenamePatternValidator.java: -------------------------------------------------------------------------------- 1 | package org.blackdread.lib.restfilter.validation.file.internal; 2 | 3 | import org.blackdread.lib.restfilter.validation.file.FilenamePattern; 4 | 5 | import javax.validation.ConstraintValidatorContext; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | import java.util.regex.PatternSyntaxException; 9 | 10 | /** 11 | * Code copied from {@code PatternValidator} of Hibernate, modified to fit this new constraint 12 | *

Created on 2019/12/27.

13 | * 14 | * @author Yoann CAPLAIN 15 | */ 16 | public abstract class FilenamePatternValidator { 17 | 18 | private Pattern pattern; 19 | 20 | // private String escapedRegexp; 21 | 22 | public void initialize(FilenamePattern parameters) { 23 | final FilenamePattern.Flag[] flags = parameters.flags(); 24 | int intFlag = 0; 25 | for (FilenamePattern.Flag flag : flags) { 26 | intFlag = intFlag | flag.getValue(); 27 | } 28 | 29 | try { 30 | pattern = Pattern.compile(parameters.regexp(), intFlag); 31 | } catch (PatternSyntaxException e) { 32 | throw new IllegalArgumentException(e); 33 | } 34 | 35 | // escapedRegexp = InterpolationHelper.escapeMessageParameter(parameters.regexp()); 36 | } 37 | 38 | public boolean isValid(String filename, ConstraintValidatorContext constraintValidatorContext) { 39 | if (filename == null) { 40 | return true; 41 | } 42 | 43 | // 44 | // if ( constraintValidatorContext instanceof HibernateConstraintValidatorContext ) { 45 | // constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class ).addMessageParameter( "regexp", escapedRegexp ); 46 | // } 47 | 48 | Matcher m = pattern.matcher(filename); 49 | return m.matches(); 50 | } 51 | 52 | // protected Pattern getPattern() { 53 | // return pattern; 54 | // } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/resources/ValidationMessages_fr.properties: -------------------------------------------------------------------------------- 1 | org.blackdread.lib.restfilter.validation.EqualsForbidden.message=Le filtre Equals n'est pas autoris\u00E9 2 | org.blackdread.lib.restfilter.validation.NotEqualsForbidden.message=Le filtre NotEquals n'est pas autoris\u00E9 3 | org.blackdread.lib.restfilter.validation.InForbidden.message=Le filtre In n'est pas autoris\u00E9 4 | org.blackdread.lib.restfilter.validation.NotInForbidden.message=Le filtre NotIn n'est pas autoris\u00E9 5 | org.blackdread.lib.restfilter.validation.SpecifiedForbidden.message=Le filtre Specified n'est pas autoris\u00E9 6 | org.blackdread.lib.restfilter.validation.GreaterThanForbidden.message=Le filtre GreaterThan n'est pas autoris\u00E9 7 | org.blackdread.lib.restfilter.validation.GreaterThanOrEqualForbidden.message=Le filtre GreaterThanOrEqual n'est pas autoris\u00E9 8 | org.blackdread.lib.restfilter.validation.LessThanForbidden.message=Le filtre LessThan n'est pas autoris\u00E9 9 | org.blackdread.lib.restfilter.validation.LessThanOrEqualForbidden.message=Le filtre LessThanOrEqual n'est pas autoris\u00E9 10 | org.blackdread.lib.restfilter.validation.ContainsForbidden.message=Le filtre Contains n'est pas autoris\u00E9 11 | org.blackdread.lib.restfilter.validation.NotContainsForbidden.message=Le filtre NotContains n'est pas autoris\u00E9 12 | org.blackdread.lib.restfilter.validation.FileSize.message=File size must be higher than {min} ${min > 0 ? 'bytes' : 'byte'} (inclusive) and lower than {max} bytes (inclusive). 13 | org.blackdread.lib.restfilter.validation.FileMimeType.message= 14 | org.blackdread.lib.restfilter.validation.FileExtension.message= 15 | org.blackdread.lib.restfilter.validation.FileNotEmpty.message= 16 | org.blackdread.lib.restfilter.validation.IsAbsolutePath.message= 17 | org.blackdread.lib.restfilter.validation.PathExist.message= 18 | org.blackdread.lib.restfilter.validation.OneNotNull.message=Only one of {value} must not be null 19 | org.blackdread.lib.restfilter.validation.AllOrNoneNull.message= 20 | org.blackdread.lib.restfilter.validation.NullOrMaxNotNull.message= 21 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileSizeValidatorForFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileSize; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.io.File; 31 | 32 | /** 33 | *

Created on 2017/4/22.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class FileSizeValidatorForFile extends FileSizeValidator implements ConstraintValidator { 38 | 39 | @Override 40 | public boolean isValid(final File value, final ConstraintValidatorContext context) { 41 | if (value == null) 42 | return true; 43 | final long fileSize = value.length(); 44 | return fileSize >= minBytes && fileSize <= maxBytes; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/PathExistValidatorForFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.PathExist; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.io.File; 31 | 32 | /** 33 | *

Created on 2017/4/4.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class PathExistValidatorForFile implements ConstraintValidator { 38 | 39 | @Override 40 | public void initialize(final PathExist constraintAnnotation) { 41 | } 42 | 43 | @Override 44 | public boolean isValid(final File value, final ConstraintValidatorContext context) { 45 | if (value == null) 46 | return true; 47 | return value.exists(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileSizeValidatorForPart.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileSize; 27 | 28 | import javax.servlet.http.Part; 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2017/4/22.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class FileSizeValidatorForPart extends FileSizeValidator implements ConstraintValidator { 38 | 39 | @Override 40 | public boolean isValid(final Part value, final ConstraintValidatorContext context) { 41 | if (value == null) 42 | return true; 43 | final long fileSize = value.getSize(); 44 | return fileSize >= minBytes && fileSize <= maxBytes; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/IsAbsolutePathValidatorForFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.IsAbsolutePath; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.io.File; 31 | 32 | /** 33 | *

Created on 2017/4/4.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class IsAbsolutePathValidatorForFile implements ConstraintValidator { 38 | 39 | @Override 40 | public void initialize(final IsAbsolutePath constraintAnnotation) { 41 | } 42 | 43 | @Override 44 | public boolean isValid(final File value, final ConstraintValidatorContext context) { 45 | if (value == null) 46 | return true; 47 | return value.isAbsolute(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/IsAbsolutePathValidatorForPath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.IsAbsolutePath; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.nio.file.Path; 31 | 32 | /** 33 | *

Created on 2017/4/4.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class IsAbsolutePathValidatorForPath implements ConstraintValidator { 38 | 39 | @Override 40 | public void initialize(final IsAbsolutePath constraintAnnotation) { 41 | } 42 | 43 | @Override 44 | public boolean isValid(final Path value, final ConstraintValidatorContext context) { 45 | if (value == null) 46 | return true; 47 | return value.isAbsolute(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/PathExistValidatorForPath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.PathExist; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.nio.file.Files; 31 | import java.nio.file.Path; 32 | 33 | /** 34 | *

Created on 2017/4/4.

35 | * 36 | * @author Yoann CAPLAIN 37 | */ 38 | public class PathExistValidatorForPath implements ConstraintValidator { 39 | 40 | @Override 41 | public void initialize(final PathExist constraintAnnotation) { 42 | } 43 | 44 | @Override 45 | public boolean isValid(final Path value, final ConstraintValidatorContext context) { 46 | if (value == null) 47 | return true; 48 | return Files.exists(value); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/criteria/FilterQueryParamFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | 28 | import java.util.List; 29 | 30 | /** 31 | *

Created on 2019/10/21.

32 | * 33 | * @author Yoann CAPLAIN 34 | */ 35 | @FunctionalInterface 36 | interface FilterQueryParamFormatter { 37 | 38 | /** 39 | * Build query params from a filter 40 | *

41 | * todo: might return QueryParam instead so not only related to filters -> not sure, any field in a filter should be part of a filter in back-end as well 42 | * 43 | * @param filterName name of filter (from field/method/alias/etc) 44 | * @param filter filter to extract FilterQueryParam (never null) 45 | * @return query params, may be empty 46 | */ 47 | List getFilterQueryParams(String filterName, Filter filter); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/converter/InstantConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.demo.converter; 25 | 26 | import org.jooq.Converter; 27 | 28 | import java.time.Instant; 29 | import java.time.OffsetDateTime; 30 | import java.time.ZoneOffset; 31 | 32 | public class InstantConverter implements Converter { 33 | @Override 34 | public Instant from(OffsetDateTime databaseObject) { 35 | return databaseObject == null ? null : databaseObject.toInstant(); 36 | } 37 | 38 | @Override 39 | public OffsetDateTime to(Instant userObject) { 40 | return userObject == null ? null : userObject.atOffset(ZoneOffset.UTC); 41 | } 42 | 43 | @Override 44 | public Class fromType() { 45 | return OffsetDateTime.class; 46 | } 47 | 48 | @Override 49 | public Class toType() { 50 | return Instant.class; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileSizeValidatorForMultipartFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileSize; 27 | import org.springframework.web.multipart.MultipartFile; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2017/4/22.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class FileSizeValidatorForMultipartFile extends FileSizeValidator implements ConstraintValidator { 38 | 39 | @Override 40 | public boolean isValid(final MultipartFile value, final ConstraintValidatorContext context) { 41 | if (value == null) 42 | return true; 43 | final long fileSize = value.getSize(); 44 | return fileSize >= minBytes && fileSize <= maxBytes; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/resources/ValidationMessages.properties: -------------------------------------------------------------------------------- 1 | org.blackdread.lib.restfilter.validation.EqualsForbidden.message=Equals filter is not allowed 2 | org.blackdread.lib.restfilter.validation.NotEqualsForbidden.message=NotEquals filter is not allowed 3 | org.blackdread.lib.restfilter.validation.InForbidden.message=In filter is not allowed 4 | org.blackdread.lib.restfilter.validation.NotInForbidden.message=NotIn filter is not allowed 5 | org.blackdread.lib.restfilter.validation.SpecifiedForbidden.message=Specified filter is not allowed 6 | org.blackdread.lib.restfilter.validation.GreaterThanForbidden.message=GreaterThan filter is not allowed 7 | org.blackdread.lib.restfilter.validation.GreaterThanOrEqualForbidden.message=GreaterThanOrEqual filter is not allowed 8 | org.blackdread.lib.restfilter.validation.LessThanForbidden.message=LessThan filter is not allowed 9 | org.blackdread.lib.restfilter.validation.LessThanOrEqualForbidden.message=LessThanOrEqual filter is not allowed 10 | org.blackdread.lib.restfilter.validation.ContainsForbidden.message=Contains filter is not allowed 11 | org.blackdread.lib.restfilter.validation.NotContainsForbidden.message=NotContains filter is not allowed 12 | org.blackdread.lib.restfilter.validation.FileSize.message=File size must be higher than {min} ${min > 0 ? 'bytes' : 'byte'} (inclusive) and lower than {max} bytes (inclusive). 13 | org.blackdread.lib.restfilter.validation.FileMimeType.message= 14 | org.blackdread.lib.restfilter.validation.FileExtension.message=File extension is forbidden, authorized values are ${inverse ? 'none of ' : ''}{value}. Case is ${isIgnoreCase ? '' : 'not '}ignored 15 | org.blackdread.lib.restfilter.validation.FileNotEmpty.message=File must not be empty 16 | org.blackdread.lib.restfilter.validation.IsAbsolutePath.message=Path or file must be given as an absolute path: {value} 17 | org.blackdread.lib.restfilter.validation.PathExist.message=File must exist: {value} 18 | org.blackdread.lib.restfilter.validation.OneNotNull.message=Only one of {value} must not be null 19 | org.blackdread.lib.restfilter.validation.AllOrNoneNull.message=All of {value} must be null or all not null 20 | org.blackdread.lib.restfilter.validation.NullOrMaxNotNull.message=All of {value} must be null or a maximum of {maxNotNull} can be non null 21 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/InForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | import org.blackdread.lib.restfilter.validation.criteria.InForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class InForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(InForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(InForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(Filter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getIn() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/jooq/Tables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is generated by jOOQ. 3 | */ 4 | /* 5 | * MIT License 6 | * 7 | * Copyright (c) 2019-2020 Yoann CAPLAIN 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | */ 27 | package org.blackdread.lib.restfilter.demo.jooq; 28 | 29 | 30 | import org.blackdread.lib.restfilter.demo.jooq.tables.ChildEntity; 31 | import org.blackdread.lib.restfilter.demo.jooq.tables.ParentEntity; 32 | 33 | import javax.annotation.Generated; 34 | 35 | 36 | /** 37 | * Convenience access to all tables in PUBLIC 38 | */ 39 | @Generated( 40 | value = { 41 | "http://www.jooq.org", 42 | "jOOQ version:3.11.11" 43 | }, 44 | comments = "This class is generated by jOOQ" 45 | ) 46 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 47 | public class Tables { 48 | 49 | /** 50 | * The table PUBLIC.CHILD_ENTITY. 51 | */ 52 | public static final ChildEntity CHILD_ENTITY = ChildEntity.CHILD_ENTITY; 53 | 54 | /** 55 | * The table PUBLIC.PARENT_ENTITY. 56 | */ 57 | public static final ParentEntity PARENT_ENTITY = ParentEntity.PARENT_ENTITY; 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/NotInForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | import org.blackdread.lib.restfilter.validation.criteria.NotInForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class NotInForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(NotInForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(NotInForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(Filter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getNotIn() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/EqualsForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | import org.blackdread.lib.restfilter.validation.criteria.EqualsForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class EqualsForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(EqualsForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(EqualsForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(Filter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getEquals() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/NotEqualsForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | import org.blackdread.lib.restfilter.validation.criteria.NotEqualsForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class NotEqualsForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(NotEqualsForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(NotEqualsForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(Filter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getNotEquals() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/SpecifiedForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | import org.blackdread.lib.restfilter.validation.criteria.SpecifiedForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class SpecifiedForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(SpecifiedForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(SpecifiedForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(Filter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getSpecified() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/ContainsForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.StringFilter; 27 | import org.blackdread.lib.restfilter.validation.criteria.ContainsForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class ContainsForbiddenValidator implements ConstraintValidator { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(ContainsForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(ContainsForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(StringFilter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getContains() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/LessThanForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.RangeFilter; 27 | import org.blackdread.lib.restfilter.validation.criteria.LessThanForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class LessThanForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(LessThanForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(LessThanForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(RangeFilter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getLessThan() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/criteria/UnsupportedFilterForQueryParamException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | 28 | /** 29 | * Thrown when {@link CriteriaQueryParam} could not format a filter type. 30 | *

Created on 2019/10/21.

31 | * 32 | * @author Yoann CAPLAIN 33 | */ 34 | public class UnsupportedFilterForQueryParamException extends IllegalArgumentException { 35 | 36 | private static final long serialVersionUID = 1L; 37 | 38 | private final String criteriaName; 39 | private final Filter filter; 40 | 41 | public UnsupportedFilterForQueryParamException(final String criteriaName, final Filter filter) { 42 | super("Field/method '" + criteriaName + "' did not match any formatter for filter '" + filter.getClass() + "'"); 43 | this.criteriaName = criteriaName; 44 | this.filter = filter; 45 | } 46 | 47 | public String getCriteriaName() { 48 | return criteriaName; 49 | } 50 | 51 | public Filter getFilter() { 52 | return filter; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/GreaterThanForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.RangeFilter; 27 | import org.blackdread.lib.restfilter.validation.criteria.GreaterThanForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class GreaterThanForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(GreaterThanForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(GreaterThanForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(RangeFilter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getGreaterThan() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/NotContainsForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.StringFilter; 27 | import org.blackdread.lib.restfilter.validation.criteria.NotContainsForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class NotContainsForbiddenValidator implements ConstraintValidator { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(NotContainsForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(NotContainsForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(StringFilter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getNotContains() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/LessThanOrEqualForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.RangeFilter; 27 | import org.blackdread.lib.restfilter.validation.criteria.LessThanOrEqualForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class LessThanOrEqualForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(LessThanOrEqualForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(LessThanOrEqualForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(RangeFilter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getLessThanOrEqual() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/internal/GreaterThanOrEqualForbiddenValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria.internal; 25 | 26 | import org.blackdread.lib.restfilter.filter.RangeFilter; 27 | import org.blackdread.lib.restfilter.validation.criteria.GreaterThanOrEqualForbidden; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | 32 | /** 33 | *

Created on 2019/10/06.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public class GreaterThanOrEqualForbiddenValidator implements ConstraintValidator> { 38 | 39 | // private static final Logger log = LoggerFactory.getLogger(GreaterThanOrEqualForbiddenValidator.class); 40 | 41 | @Override 42 | public void initialize(GreaterThanOrEqualForbidden constraintAnnotation) { 43 | } 44 | 45 | @Override 46 | public boolean isValid(RangeFilter value, ConstraintValidatorContext context) { 47 | if (value == null) 48 | return true; 49 | 50 | return value.getGreaterThanOrEqual() == null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileExtensionValidatorForFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileExtension; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.io.File; 31 | import java.util.Arrays; 32 | 33 | /** 34 | *

Created on 2017/4/22.

35 | * 36 | * @author Yoann CAPLAIN 37 | */ 38 | public class FileExtensionValidatorForFile extends FileExtensionValidator implements ConstraintValidator { 39 | 40 | @Override 41 | public boolean isValid(final File value, final ConstraintValidatorContext context) { 42 | if (value == null) 43 | return true; 44 | final String extension = getExtension(value.getName()); 45 | 46 | final boolean match = Arrays.stream(allowedExtensions) 47 | .anyMatch(allowedExtension -> isIgnoreCase ? allowedExtension.equalsIgnoreCase(extension) : allowedExtension.equalsIgnoreCase(extension)); 48 | 49 | if (isInverse) 50 | return !match; 51 | return match; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileExtensionValidatorForPart.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileExtension; 27 | 28 | import javax.servlet.http.Part; 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | import java.util.Arrays; 32 | 33 | /** 34 | *

Created on 2017/4/22.

35 | * 36 | * @author Yoann CAPLAIN 37 | */ 38 | public class FileExtensionValidatorForPart extends FileExtensionValidator implements ConstraintValidator { 39 | 40 | @Override 41 | public boolean isValid(final Part value, final ConstraintValidatorContext context) { 42 | if (value == null) 43 | return true; 44 | final String extension = getExtension(value.getSubmittedFileName()); 45 | 46 | final boolean match = Arrays.stream(allowedExtensions) 47 | .anyMatch(allowedExtension -> isIgnoreCase ? allowedExtension.equalsIgnoreCase(extension) : allowedExtension.equals(extension)); 48 | 49 | if (isInverse) 50 | return !match; 51 | return match; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileExtensionValidatorForPath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileExtension; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.nio.file.Path; 31 | import java.util.Arrays; 32 | 33 | /** 34 | *

Created on 2017/4/22.

35 | * 36 | * @author Yoann CAPLAIN 37 | */ 38 | public class FileExtensionValidatorForPath extends FileExtensionValidator implements ConstraintValidator { 39 | 40 | @Override 41 | public boolean isValid(final Path value, final ConstraintValidatorContext context) { 42 | if (value == null) 43 | return true; 44 | final String extension = getExtension(value.getFileName().toString()); 45 | 46 | final boolean match = Arrays.stream(allowedExtensions) 47 | .anyMatch(allowedExtension -> isIgnoreCase ? allowedExtension.equalsIgnoreCase(extension) : allowedExtension.equalsIgnoreCase(extension)); 48 | 49 | if (isInverse) 50 | return !match; 51 | return match; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileSizeValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileSize; 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | 30 | /** 31 | *

Created on 2017/4/22.

32 | * 33 | * @author Yoann CAPLAIN 34 | */ 35 | public abstract class FileSizeValidator { 36 | 37 | private static final Logger log = LoggerFactory.getLogger(FileSizeValidator.class); 38 | 39 | protected long minBytes; 40 | 41 | protected long maxBytes; 42 | 43 | public void initialize(final FileSize constraintAnnotation) { 44 | minBytes = constraintAnnotation.min(); 45 | maxBytes = constraintAnnotation.max(); 46 | validateParameters(); 47 | } 48 | 49 | private void validateParameters() { 50 | if (minBytes < 0) 51 | throw new IllegalArgumentException("Min cannot be negative"); 52 | if(maxBytes < 0) 53 | throw new IllegalArgumentException("Max cannot be negative"); 54 | if(maxBytes < minBytes) 55 | throw new IllegalArgumentException("Max cannot be lower than minBytes"); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileExtensionValidatorForMultipartFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileExtension; 27 | import org.springframework.web.multipart.MultipartFile; 28 | 29 | import javax.validation.ConstraintValidator; 30 | import javax.validation.ConstraintValidatorContext; 31 | import java.util.Arrays; 32 | 33 | /** 34 | *

Created on 2017/4/22.

35 | * 36 | * @author Yoann CAPLAIN 37 | */ 38 | public class FileExtensionValidatorForMultipartFile extends FileExtensionValidator implements ConstraintValidator { 39 | 40 | @Override 41 | public boolean isValid(final MultipartFile value, final ConstraintValidatorContext context) { 42 | if (value == null) 43 | return true; 44 | final String extension = getExtension(value.getOriginalFilename()); 45 | 46 | final boolean match = Arrays.stream(allowedExtensions) 47 | .anyMatch(allowedExtension -> isIgnoreCase ? allowedExtension.equalsIgnoreCase(extension) : allowedExtension.equals(extension)); 48 | 49 | if (isInverse) 50 | return !match; 51 | return match; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileSizeValidatorForPath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.blackdread.lib.restfilter.validation.file.FileSize; 27 | 28 | import javax.validation.ConstraintValidator; 29 | import javax.validation.ConstraintValidatorContext; 30 | import java.io.IOException; 31 | import java.io.UncheckedIOException; 32 | import java.nio.file.Files; 33 | import java.nio.file.Path; 34 | 35 | /** 36 | *

Created on 2017/4/22.

37 | * 38 | * @author Yoann CAPLAIN 39 | */ 40 | public class FileSizeValidatorForPath extends FileSizeValidator implements ConstraintValidator { 41 | 42 | @Override 43 | public boolean isValid(final Path value, final ConstraintValidatorContext context) { 44 | if (value == null) 45 | return true; 46 | final long fileSize; 47 | try { 48 | fileSize = Files.size(value); 49 | } catch (IOException e) { 50 | context.disableDefaultConstraintViolation(); 51 | context 52 | .buildConstraintViolationWithTemplate("Failed to get file size: " + e.getMessage()) 53 | .addConstraintViolation(); 54 | return false; 55 | } 56 | return fileSize >= minBytes && fileSize <= maxBytes; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/nullability/OneNotNull.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.nullability 25 | 26 | import javax.validation.Constraint 27 | import javax.validation.Payload 28 | import kotlin.annotation.AnnotationRetention.RUNTIME 29 | import kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS 30 | import kotlin.annotation.AnnotationTarget.CLASS 31 | import kotlin.reflect.KClass 32 | 33 | /** 34 | * Check that all fields passed are null except one. 35 | * 36 | * If any field is not found by reflection, it will throw an exception. 37 | * 38 | * At least two field names must be passed otherwise throw exception. 39 | * 40 | * Same behavior can be done with [org.hibernate.validator.constraints.ScriptAssert] but here is easier 41 | * 42 | *

Created on 2019/12/22.

43 | * 44 | * @author Yoann CAPLAIN 45 | * @since 2.2.1 46 | */ 47 | @MustBeDocumented 48 | @Constraint(validatedBy = []) 49 | @Target(ANNOTATION_CLASS, CLASS) 50 | @Retention(RUNTIME) 51 | @Repeatable 52 | annotation class OneNotNull( 53 | 54 | val message: String = "{org.blackdread.lib.restfilter.validation.OneNotNull.message}", 55 | 56 | val groups: Array> = [], 57 | 58 | val payload: Array> = [], 59 | 60 | /** 61 | * Field names 62 | */ 63 | val value: Array = [] 64 | ) 65 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/nullability/AllOrNoneNull.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.nullability 25 | 26 | import javax.validation.Constraint 27 | import javax.validation.Payload 28 | import kotlin.annotation.AnnotationRetention.RUNTIME 29 | import kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS 30 | import kotlin.annotation.AnnotationTarget.CLASS 31 | import kotlin.reflect.KClass 32 | 33 | /** 34 | * Check that fields passed **are all null** or **all are not null**. 35 | * 36 | * If any field is not found by reflection, it will throw an exception. 37 | * 38 | * At least two field names must be passed otherwise throw exception. 39 | * 40 | * Same behavior can be done with [org.hibernate.validator.constraints.ScriptAssert] but here is easier 41 | * 42 | *

Created on 2019/12/22.

43 | * 44 | * @author Yoann CAPLAIN 45 | * @since 2.2.1 46 | */ 47 | @MustBeDocumented 48 | @Constraint(validatedBy = []) 49 | @Target(ANNOTATION_CLASS, CLASS) 50 | @Retention(RUNTIME) 51 | @Repeatable 52 | annotation class AllOrNoneNull( 53 | 54 | val message: String = "{org.blackdread.lib.restfilter.validation.AllOrNoneNull.message}", 55 | 56 | val groups: Array> = [], 57 | 58 | val payload: Array> = [], 59 | 60 | /** 61 | * Field names 62 | */ 63 | val value: Array = [] 64 | ) 65 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ###################### 2 | # Project Specific 3 | ###################### 4 | 5 | 6 | ###################### 7 | # Node 8 | ###################### 9 | /node/ 10 | node_tmp/ 11 | node_modules/ 12 | npm-debug.log.* 13 | /.awcache/* 14 | /.cache-loader/* 15 | 16 | ###################### 17 | # SASS 18 | ###################### 19 | .sass-cache/ 20 | 21 | ###################### 22 | # Eclipse 23 | ###################### 24 | *.pydevproject 25 | .project 26 | .metadata 27 | tmp/ 28 | tmp/**/* 29 | *.tmp 30 | *.bak 31 | *.swp 32 | *~.nib 33 | local.properties 34 | .classpath 35 | .settings/ 36 | .loadpath 37 | .factorypath 38 | /src/main/resources/rebel.xml 39 | 40 | # External tool builders 41 | .externalToolBuilders/** 42 | 43 | # Locally stored "Eclipse launch configurations" 44 | *.launch 45 | 46 | # CDT-specific 47 | .cproject 48 | 49 | # PDT-specific 50 | .buildpath 51 | 52 | ###################### 53 | # Intellij 54 | ###################### 55 | .idea/ 56 | *.iml 57 | *.iws 58 | *.ipr 59 | *.ids 60 | *.orig 61 | classes/ 62 | out/ 63 | 64 | ###################### 65 | # Visual Studio Code 66 | ###################### 67 | .vscode/ 68 | 69 | ###################### 70 | # Maven 71 | ###################### 72 | /log/ 73 | /target/ 74 | 75 | ###################### 76 | # Gradle 77 | ###################### 78 | .gradle/ 79 | /build/ 80 | 81 | ###################### 82 | # Package Files 83 | ###################### 84 | *.jar 85 | *.war 86 | *.ear 87 | *.db 88 | 89 | ###################### 90 | # Windows 91 | ###################### 92 | # Windows image file caches 93 | Thumbs.db 94 | 95 | # Folder config file 96 | Desktop.ini 97 | 98 | ###################### 99 | # Mac OSX 100 | ###################### 101 | .DS_Store 102 | .svn 103 | 104 | # Thumbnails 105 | ._* 106 | 107 | # Files that might appear on external disk 108 | .Spotlight-V100 109 | .Trashes 110 | 111 | ###################### 112 | # Directories 113 | ###################### 114 | /bin/ 115 | /deploy/ 116 | 117 | ###################### 118 | # Logs 119 | ###################### 120 | *.log* 121 | 122 | ###################### 123 | # Others 124 | ###################### 125 | *.class 126 | *.*~ 127 | *~ 128 | .merge_file* 129 | 130 | ###################### 131 | # Gradle Wrapper 132 | ###################### 133 | !gradle/wrapper/gradle-wrapper.jar 134 | 135 | ###################### 136 | # Maven Wrapper 137 | ###################### 138 | !.mvn/wrapper/maven-wrapper.jar 139 | 140 | ###################### 141 | # ESLint 142 | ###################### 143 | .eslintcache 144 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/PathExist.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file; 25 | 26 | import javax.validation.Constraint; 27 | import javax.validation.Payload; 28 | import java.lang.annotation.Documented; 29 | import java.lang.annotation.Repeatable; 30 | import java.lang.annotation.Retention; 31 | import java.lang.annotation.Target; 32 | 33 | import static java.lang.annotation.ElementType.*; 34 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 35 | 36 | /** 37 | * Asserts that the annotated File or Path exist. 38 | *

Created on 2018/04/05.

39 | * 40 | * @author Yoann CAPLAIN 41 | * @since 2.2.1 42 | */ 43 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 44 | @Retention(RUNTIME) 45 | @Constraint(validatedBy = {}) 46 | @Documented 47 | @Repeatable(PathExist.List.class) 48 | public @interface PathExist { 49 | 50 | String message() default "{org.blackdread.lib.restfilter.validation.PathExist.message}"; 51 | 52 | Class[] groups() default {}; 53 | 54 | Class[] payload() default {}; 55 | 56 | /** 57 | * Defines several {@link PathExist} annotations on the same element. 58 | * 59 | * @see PathExist 60 | */ 61 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 62 | @Retention(RUNTIME) 63 | @Documented 64 | @interface List { 65 | 66 | PathExist[] value(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/IsAbsolutePath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file; 25 | 26 | import javax.validation.Constraint; 27 | import javax.validation.Payload; 28 | import java.lang.annotation.Documented; 29 | import java.lang.annotation.Repeatable; 30 | import java.lang.annotation.Retention; 31 | import java.lang.annotation.Target; 32 | 33 | import static java.lang.annotation.ElementType.*; 34 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 35 | 36 | /** 37 | * Asserts that the annotated File or Path is an absolute path. 38 | *

Created on 2018/04/04

39 | * 40 | * @author Yoann CAPLAIN 41 | * @since 2.2.1 42 | */ 43 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 44 | @Retention(RUNTIME) 45 | @Constraint(validatedBy = {}) 46 | @Documented 47 | @Repeatable(IsAbsolutePath.List.class) 48 | public @interface IsAbsolutePath { 49 | 50 | String message() default "{org.blackdread.lib.restfilter.validation.IsAbsolutePath.message}"; 51 | 52 | Class[] groups() default {}; 53 | 54 | Class[] payload() default {}; 55 | 56 | /** 57 | * Defines several {@link IsAbsolutePath} annotations on the same element. 58 | * 59 | * @see IsAbsolutePath 60 | */ 61 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 62 | @Retention(RUNTIME) 63 | @Documented 64 | @interface List { 65 | 66 | IsAbsolutePath[] value(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/nullability/NullOrMaxNotNull.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.nullability 25 | 26 | import javax.validation.Constraint 27 | import javax.validation.Payload 28 | import kotlin.annotation.AnnotationRetention.RUNTIME 29 | import kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS 30 | import kotlin.annotation.AnnotationTarget.CLASS 31 | import kotlin.reflect.KClass 32 | 33 | /** 34 | * Check that all fields passed are null or at maximum X fields are not null. Default max is 1 field not null. 35 | * 36 | * If any field is not found by reflection, it will throw an exception. 37 | * 38 | * At least two field names must be passed otherwise throw exception. 39 | * 40 | * Same behavior can be done with [org.hibernate.validator.constraints.ScriptAssert] but here is easier 41 | * 42 | *

Created on 2019/12/22.

43 | * 44 | * @author Yoann CAPLAIN 45 | * @since 2.2.1 46 | */ 47 | @MustBeDocumented 48 | @Constraint(validatedBy = []) 49 | @Target(ANNOTATION_CLASS, CLASS) 50 | @Retention(RUNTIME) 51 | @Repeatable 52 | annotation class NullOrMaxNotNull( 53 | 54 | val message: String = "{org.blackdread.lib.restfilter.validation.NullOrMaxNotNull.message}", 55 | 56 | val groups: Array> = [], 57 | 58 | val payload: Array> = [], 59 | 60 | /** 61 | * Field names 62 | */ 63 | val value: Array = [], 64 | 65 | /** 66 | * Cannot be less than 1 67 | */ 68 | val maxNotNull: Int = 1 69 | ) 70 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/BaseEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.demo; 25 | 26 | import javax.persistence.Column; 27 | import javax.persistence.GeneratedValue; 28 | import javax.persistence.Id; 29 | import javax.persistence.MappedSuperclass; 30 | import java.math.BigDecimal; 31 | import java.time.Duration; 32 | import java.time.Instant; 33 | import java.time.LocalDate; 34 | import java.util.Objects; 35 | import java.util.UUID; 36 | 37 | @MappedSuperclass 38 | public class BaseEntity { 39 | @Id 40 | @GeneratedValue 41 | Long id; 42 | 43 | @Column(nullable = false) 44 | String name; 45 | 46 | @Column(nullable = false) 47 | Instant createTime; 48 | 49 | @Column(nullable = false) 50 | BigDecimal total; 51 | 52 | @Column(nullable = true) 53 | Integer count; 54 | 55 | @Column(nullable = false) 56 | LocalDate localDate; 57 | 58 | @Column(nullable = false) 59 | Short aShort; 60 | 61 | @Column(nullable = false) 62 | Boolean active; 63 | 64 | @Column(nullable = false) 65 | UUID uuid; 66 | 67 | @Column(nullable = false) 68 | Duration duration; 69 | 70 | @Override 71 | public boolean equals(Object o) { 72 | if (this == o) return true; 73 | if (o == null || getClass() != o.getClass()) return false; 74 | BaseEntity that = (BaseEntity) o; 75 | return Objects.equals(id, that.id); 76 | } 77 | 78 | @Override 79 | public int hashCode() { 80 | return Objects.hash(id); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/InForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.InForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.InForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = InForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface InForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.InForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @InForbidden} constraints on the same element. 60 | * 61 | * @see InForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | InForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/NotInForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.NotInForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.NotInForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = NotInForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface NotInForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.NotInForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @NotInForbidden} constraints on the same element. 60 | * 61 | * @see NotInForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | NotInForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/EqualsForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.EqualsForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.EqualsForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = EqualsForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface EqualsForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.EqualsForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @EqualsForbidden} constraints on the same element. 60 | * 61 | * @see EqualsForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | EqualsForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/ContainsForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.ContainsForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.ContainsForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = ContainsForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface ContainsForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.ContainsForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @ContainsForbidden} constraints on the same element. 60 | * 61 | * @see ContainsForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | ContainsForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/LessThanForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.LessThanForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.LessThanForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = LessThanForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface LessThanForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.LessThanForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @LessThanForbidden} constraints on the same element. 60 | * 61 | * @see LessThanForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | LessThanForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/NotEqualsForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.NotEqualsForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.NotEqualsForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = NotEqualsForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface NotEqualsForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.NotEqualsForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @NotEqualsForbidden} constraints on the same element. 60 | * 61 | * @see NotEqualsForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | NotEqualsForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/SpecifiedForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.SpecifiedForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.SpecifiedForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = SpecifiedForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface SpecifiedForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.SpecifiedForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @SpecifiedForbidden} constraints on the same element. 60 | * 61 | * @see SpecifiedForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | SpecifiedForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/query/SortQueryParamSpringUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.spring.query; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | import org.springframework.data.domain.Sort; 29 | import org.springframework.web.util.UriBuilder; 30 | 31 | import java.util.StringJoiner; 32 | 33 | /** 34 | *

Created on 2019/10/27.

35 | * 36 | * @author Yoann CAPLAIN 37 | */ 38 | public final class SortQueryParamSpringUtil { 39 | 40 | private static final Logger log = LoggerFactory.getLogger(SortQueryParamSpringUtil.class); 41 | 42 | private static final String SORT_KEY = "sort"; 43 | 44 | private SortQueryParamSpringUtil() { 45 | } 46 | 47 | // todo later will not be static so can configure better 48 | 49 | public static UriBuilder addSortQueryParams(final Sort sort, UriBuilder builder) { 50 | for (final Sort.Order order : sort) { 51 | builder = builder.queryParam(SORT_KEY, formatParamValue(order)); 52 | } 53 | return builder; 54 | } 55 | 56 | public static String formatSort(final Sort sort) { 57 | final StringJoiner builder = new StringJoiner("&"); 58 | for (final Sort.Order order : sort) { 59 | builder.add(formatOrder(order)); 60 | } 61 | return builder.toString(); 62 | } 63 | 64 | public static String formatOrder(final Sort.Order order) { 65 | return SORT_KEY + "=" + formatParamValue(order); 66 | } 67 | 68 | public static String formatParamValue(final Sort.Order order) { 69 | return order.getProperty() + "," + order.getDirection().name().toLowerCase(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/GreaterThanForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.GreaterThanForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.GreaterThanForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = GreaterThanForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface GreaterThanForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.GreaterThanForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @GreaterThanForbidden} constraints on the same element. 60 | * 61 | * @see GreaterThanForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | GreaterThanForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/NotContainsForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.NotContainsForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.NotContainsForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = NotContainsForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface NotContainsForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.NotContainsForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @NotContainsForbidden} constraints on the same element. 60 | * 61 | * @see NotContainsForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | NotContainsForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/resources/database.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE parent_entity 2 | ( 3 | id BIGINT NOT NULL PRIMARY KEY, 4 | name VARCHAR(50) NOT NULL, 5 | -- create_time DATETIME NOT NULL, 6 | create_time TIMESTAMP WITH TIMEZONE NOT NULL, 7 | total DECIMAL(20, 5) NOT NULL, 8 | count INT NOT NULL, 9 | local_date DATE NOT NULL, 10 | a_short SMALLINT NOT NULL, 11 | active BOOLEAN NOT NULL, 12 | uuid UUID NOT NULL, 13 | duration VARCHAR(50) NOT NULL 14 | ); 15 | 16 | /* 17 | TODO see "INTERVAL DAY" to do duration 18 | TODO add more type like time, etc 19 | */ 20 | 21 | CREATE TABLE child_entity 22 | ( 23 | id BIGINT NOT NULL PRIMARY KEY, 24 | name VARCHAR(50) NOT NULL, 25 | -- create_time DATETIME NOT NULL, 26 | create_time TIMESTAMP WITH TIMEZONE NOT NULL, 27 | total DECIMAL(20, 5) NOT NULL, 28 | count INT NOT NULL, 29 | local_date DATE NOT NULL, 30 | a_short SMALLINT NOT NULL, 31 | active BOOLEAN NOT NULL, 32 | uuid UUID NOT NULL, 33 | duration VARCHAR(50) NOT NULL, 34 | parent_id NUMBER(11) NULL 35 | ); 36 | 37 | /* 38 | Below is just a more complex example, not used for now 39 | 40 | CREATE TABLE language 41 | ( 42 | id NUMBER(7) NOT NULL PRIMARY KEY, 43 | cd CHAR(2) NOT NULL, 44 | description VARCHAR2(50) 45 | ); 46 | 47 | CREATE TABLE author 48 | ( 49 | id NUMBER(7) NOT NULL PRIMARY KEY, 50 | first_name VARCHAR2(50), 51 | last_name VARCHAR2(50) NOT NULL, 52 | date_of_birth DATE, 53 | year_of_birth NUMBER(7), 54 | distinguished NUMBER(1) 55 | ); 56 | 57 | CREATE TABLE book 58 | ( 59 | id NUMBER(7) NOT NULL PRIMARY KEY, 60 | author_id NUMBER(7) NOT NULL, 61 | title VARCHAR2(400) NOT NULL, 62 | published_in NUMBER(7) NOT NULL, 63 | language_id NUMBER(7) NOT NULL, 64 | 65 | CONSTRAINT fk_book_author FOREIGN KEY (author_id) REFERENCES author (id), 66 | CONSTRAINT fk_book_language FOREIGN KEY (language_id) REFERENCES language (id) 67 | ); 68 | 69 | CREATE TABLE book_store 70 | ( 71 | name VARCHAR2(400) NOT NULL UNIQUE 72 | ); 73 | 74 | CREATE TABLE book_to_book_store 75 | ( 76 | name VARCHAR2(400) NOT NULL, 77 | book_id INTEGER NOT NULL, 78 | stock INTEGER, 79 | 80 | PRIMARY KEY (name, book_id), 81 | CONSTRAINT fk_b2bs_book_store FOREIGN KEY (name) REFERENCES book_store (name) ON DELETE CASCADE, 82 | CONSTRAINT fk_b2bs_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE 83 | ); 84 | //*/ 85 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/LessThanOrEqualForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.LessThanOrEqualForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.LessThanOrEqualForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = LessThanOrEqualForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface LessThanOrEqualForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.LessThanOrEqualForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @LessThanOrEqualForbidden} constraints on the same element. 60 | * 61 | * @see LessThanOrEqualForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | LessThanOrEqualForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/query/PageableQueryParamSpringUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.spring.query; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | import org.springframework.data.domain.Pageable; 29 | import org.springframework.web.util.UriBuilder; 30 | 31 | /** 32 | *

Created on 2019/10/27.

33 | * 34 | * @author Yoann CAPLAIN 35 | */ 36 | public final class PageableQueryParamSpringUtil { 37 | 38 | private static final Logger log = LoggerFactory.getLogger(PageableQueryParamSpringUtil.class); 39 | 40 | private PageableQueryParamSpringUtil() { 41 | } 42 | 43 | // todo later will not be static so can configure param key (page & size can be different for some back-end since it can be configured) 44 | 45 | public static UriBuilder addPageAndSortQueryParams(final Pageable pageable, UriBuilder builder) { 46 | if(pageable.isUnpaged()) 47 | return builder; 48 | builder = addPageOnlyQueryParams(pageable, builder); 49 | return SortQueryParamSpringUtil.addSortQueryParams(pageable.getSort(), builder); 50 | } 51 | 52 | public static UriBuilder addPageOnlyQueryParams(final Pageable pageable, UriBuilder builder) { 53 | if(pageable.isUnpaged()) 54 | return builder; 55 | return builder 56 | .queryParam("page", pageable.getPageNumber()) 57 | .queryParam("size", pageable.getPageSize()); 58 | } 59 | 60 | public static String formatPage(final Pageable pageable) { 61 | return "page=" + pageable.getPageNumber(); 62 | } 63 | 64 | public static String formatPageSize(final Pageable pageable) { 65 | return "size=" + pageable.getPageSize(); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/criteria/GreaterThanOrEqualForbidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.criteria; 25 | 26 | import org.blackdread.lib.restfilter.validation.criteria.internal.GreaterThanOrEqualForbiddenValidator; 27 | 28 | import javax.validation.Constraint; 29 | import javax.validation.Payload; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | import static org.blackdread.lib.restfilter.validation.criteria.GreaterThanOrEqualForbidden.List; 38 | 39 | /** 40 | * 41 | *

Created on 2019/10/06.

42 | * 43 | * @author Yoann CAPLAIN 44 | */ 45 | @Documented 46 | @Constraint(validatedBy = GreaterThanOrEqualForbiddenValidator.class) 47 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 48 | @Retention(RUNTIME) 49 | @Repeatable(List.class) 50 | public @interface GreaterThanOrEqualForbidden { 51 | 52 | String message() default "{org.blackdread.lib.restfilter.validation.GreaterThanOrEqualForbidden.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | 58 | /** 59 | * Defines several {@code @GreaterThanOrEqualForbidden} constraints on the same element. 60 | * 61 | * @see GreaterThanOrEqualForbidden 62 | */ 63 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 64 | @Retention(RUNTIME) 65 | @Documented 66 | public @interface List { 67 | GreaterThanOrEqualForbidden[] value(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/FileNotEmpty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file; 25 | 26 | import javax.validation.Constraint; 27 | import javax.validation.Payload; 28 | import javax.validation.ReportAsSingleViolation; 29 | import javax.validation.constraints.NotNull; 30 | import java.lang.annotation.Documented; 31 | import java.lang.annotation.Repeatable; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.Target; 34 | 35 | import static java.lang.annotation.ElementType.*; 36 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 37 | 38 | /** 39 | * Asserts that the annotated {@link javax.servlet.http.Part}, {@link org.springframework.web.multipart.MultipartFile}, File or Path is not {@code null} or empty. 40 | *

Created on 2018/04/22

41 | * 42 | * @author Yoann CAPLAIN 43 | * @since 2.2.1 44 | */ 45 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 46 | @Retention(RUNTIME) 47 | @Constraint(validatedBy = {}) 48 | @Documented 49 | @Repeatable(FileNotEmpty.List.class) 50 | @ReportAsSingleViolation 51 | @NotNull 52 | @FileSize(min = 1, max = Long.MAX_VALUE) 53 | public @interface FileNotEmpty { 54 | 55 | String message() default "{org.blackdread.lib.restfilter.validation.FileNotEmpty.message}"; 56 | 57 | Class[] groups() default {}; 58 | 59 | Class[] payload() default {}; 60 | 61 | /** 62 | * Defines several {@link FileNotEmpty} annotations on the same element. 63 | * 64 | * @see FileNotEmpty 65 | */ 66 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 67 | @Retention(RUNTIME) 68 | @Documented 69 | @interface List { 70 | 71 | FileNotEmpty[] value(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/criteria/parser/CriteriaFieldParserUtilAllIgnoreAnnotationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria.parser; 25 | 26 | import org.blackdread.lib.restfilter.List2; 27 | import org.blackdread.lib.restfilter.criteria.parser.criteria.AllIgnoreAnnotationCriteria; 28 | import org.junit.jupiter.api.AfterEach; 29 | import org.junit.jupiter.api.BeforeEach; 30 | import org.junit.jupiter.api.Test; 31 | import org.slf4j.Logger; 32 | import org.slf4j.LoggerFactory; 33 | 34 | import static org.junit.jupiter.api.Assertions.assertEquals; 35 | import static org.junit.jupiter.api.Assertions.assertSame; 36 | 37 | /** 38 | *

Created on 2019/10/26.

39 | * 40 | * @author Yoann CAPLAIN 41 | */ 42 | class CriteriaFieldParserUtilAllIgnoreAnnotationTest { 43 | 44 | private static final Logger log = LoggerFactory.getLogger(CriteriaFieldParserUtilAllIgnoreAnnotationTest.class); 45 | 46 | @BeforeEach 47 | void setUp() { 48 | } 49 | 50 | @AfterEach 51 | void tearDown() { 52 | } 53 | 54 | @Test 55 | void classesAreCached() { 56 | final CriteriaData result = CriteriaFieldParserUtil.getCriteriaData(AllIgnoreAnnotationCriteria.class); 57 | final CriteriaData result2 = CriteriaFieldParserUtil.getCriteriaData(AllIgnoreAnnotationCriteria.class); 58 | 59 | assertEquals(result, result2); 60 | assertSame(result, result2); 61 | } 62 | 63 | @Test 64 | void emptyDataWhenAllIgnored() { 65 | final AllIgnoreAnnotationCriteria criteria = new AllIgnoreAnnotationCriteria(); 66 | 67 | final CriteriaData result = CriteriaFieldParserUtil.getCriteriaData(criteria); 68 | 69 | assertEquals(List2.of(), result.getFields()); 70 | assertEquals(List2.of(), result.getMethods()); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/criteria/CriteriaQueryParamSpringExtend.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.spring.criteria; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | /** 30 | *

Created on 2019/10/20.

31 | * 32 | * @author Yoann CAPLAIN 33 | * @deprecated not sure to have implementation with extend, anyway needed methods that were private became public so no need anymore 34 | */ 35 | class CriteriaQueryParamSpringExtend /*extends CriteriaQueryParamImpl implements CriteriaQueryParamSpring*/ { 36 | 37 | private static final Logger log = LoggerFactory.getLogger(CriteriaQueryParamSpringExtend.class); 38 | 39 | private CriteriaQueryParamSpringExtend() { 40 | 41 | } 42 | 43 | // @Override 44 | // public UriBuilder buildQueryParams(final Object criteria, final UriBuilder builder) { 45 | // final Map filtersByFieldName = CriteriaFieldParserUtil.build(criteria); 46 | // final List filterQueryParams = getFilterQueryParams(filtersByFieldName); 47 | // for (final FilterQueryParam filterQueryParam : filterQueryParams) { 48 | // builder = builder.queryParam(filterQueryParam.getParamName(), filterQueryParam.getParamValues()); 49 | // } 50 | // return builder; 51 | // } 52 | 53 | // @Override 54 | // public UriBuilder buildQueryParams(final String fieldName, final Filter filter, final UriBuilder builder) { 55 | // final List filterQueryParams = getFilterQueryParams(fieldName, filter); 56 | // for (final FilterQueryParam filterQueryParam : filterQueryParams) { 57 | // builder = builder.queryParam(filterQueryParam.getParamName(), filterQueryParam.getParamValues()); 58 | // } 59 | // return builder; 60 | // } 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/jooq/Indexes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is generated by jOOQ. 3 | */ 4 | /* 5 | * MIT License 6 | * 7 | * Copyright (c) 2019-2020 Yoann CAPLAIN 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | */ 27 | package org.blackdread.lib.restfilter.demo.jooq; 28 | 29 | 30 | import org.blackdread.lib.restfilter.demo.jooq.tables.ChildEntity; 31 | import org.blackdread.lib.restfilter.demo.jooq.tables.ParentEntity; 32 | import org.jooq.Index; 33 | import org.jooq.OrderField; 34 | import org.jooq.impl.Internal; 35 | 36 | import javax.annotation.Generated; 37 | 38 | 39 | /** 40 | * A class modelling indexes of tables of the PUBLIC schema. 41 | */ 42 | @Generated( 43 | value = { 44 | "http://www.jooq.org", 45 | "jOOQ version:3.11.11" 46 | }, 47 | comments = "This class is generated by jOOQ" 48 | ) 49 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 50 | public class Indexes { 51 | 52 | // ------------------------------------------------------------------------- 53 | // INDEX definitions 54 | // ------------------------------------------------------------------------- 55 | 56 | public static final Index PRIMARY_KEY_4 = Indexes0.PRIMARY_KEY_4; 57 | public static final Index PRIMARY_KEY_6 = Indexes0.PRIMARY_KEY_6; 58 | 59 | // ------------------------------------------------------------------------- 60 | // [#1459] distribute members to avoid static initialisers > 64kb 61 | // ------------------------------------------------------------------------- 62 | 63 | private static class Indexes0 { 64 | public static Index PRIMARY_KEY_4 = Internal.createIndex("PRIMARY_KEY_4", ChildEntity.CHILD_ENTITY, new OrderField[] { ChildEntity.CHILD_ENTITY.ID }, true); 65 | public static Index PRIMARY_KEY_6 = Internal.createIndex("PRIMARY_KEY_6", ParentEntity.PARENT_ENTITY, new OrderField[] { ParentEntity.PARENT_ENTITY.ID }, true); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/internal/FileMimeTypeValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file.internal; 25 | 26 | import org.apache.commons.lang3.NotImplementedException; 27 | import org.blackdread.lib.restfilter.validation.file.FileMimeType; 28 | import org.slf4j.Logger; 29 | import org.slf4j.LoggerFactory; 30 | 31 | import java.nio.file.Path; 32 | import java.util.regex.Pattern; 33 | 34 | /** 35 | *

Created on 2017/4/22.

36 | * 37 | * @author Yoann CAPLAIN 38 | */ 39 | public abstract class FileMimeTypeValidator { 40 | 41 | private static final Logger log = LoggerFactory.getLogger(FileMimeTypeValidator.class); 42 | 43 | // TODO to check 44 | private static final Pattern MIME_TYPE_REGEX = Pattern.compile("^[A-Za-z]+/[A-Za-z]+$"); 45 | 46 | protected String[] allowedMimeTypes; 47 | 48 | protected boolean isInverse; 49 | 50 | public void initialize(final FileMimeType constraintAnnotation) { 51 | allowedMimeTypes = constraintAnnotation.value(); 52 | isInverse = constraintAnnotation.inverse(); 53 | validateParameters(); 54 | } 55 | 56 | protected String getMimeType(final Path filePath) { 57 | throw new NotImplementedException("Not done for now, will see later"); 58 | } 59 | 60 | private void validateParameters() { 61 | if (allowedMimeTypes == null) 62 | throw new IllegalArgumentException("allowedMimeTypes cannot be null"); 63 | for (String s : allowedMimeTypes) { 64 | if (s == null) 65 | throw new IllegalArgumentException("Array of allowed mime types cannot contain null values"); 66 | if (!MIME_TYPE_REGEX.matcher(s).matches()) 67 | throw new IllegalArgumentException("Mime type passed does not respect pattern: " + MIME_TYPE_REGEX.pattern()); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/util/IterableUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.util; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | import java.util.Iterator; 30 | import java.util.Optional; 31 | 32 | /** 33 | *

Created on 2019/10/27.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | public final class IterableUtil { 38 | 39 | private static final Logger log = LoggerFactory.getLogger(IterableUtil.class); 40 | 41 | private IterableUtil() { 42 | } 43 | 44 | /** 45 | * @param iterable iterable 46 | * @return true of iterator returns false for {@link Iterator#hasNext()} 47 | */ 48 | public static boolean isEmpty(final Iterable iterable) { 49 | final Iterator iterator = iterable.iterator(); 50 | return !iterator.hasNext(); 51 | } 52 | 53 | /** 54 | * @param iterable iterable 55 | * @return true of iterator returns true for {@link Iterator#hasNext()} 56 | */ 57 | public static boolean isNotEmpty(final Iterable iterable) { 58 | final Iterator iterator = iterable.iterator(); 59 | return iterator.hasNext(); 60 | } 61 | 62 | /** 63 | * Iterable passed should have not side-effect since it will be iterated for its first value if exist 64 | * 65 | * @param iterable iterable 66 | * @return first value if present and not null 67 | */ 68 | public static Optional getFirstValue(final Iterable iterable) { 69 | final Iterator iterator = iterable.iterator(); 70 | if (iterator.hasNext()) { 71 | final T firstValue = iterator.next(); 72 | if (firstValue == null) { // null are forbidden, no further test 73 | log.warn("Got null in an iterable"); 74 | return Optional.empty(); 75 | } 76 | return Optional.of(firstValue); 77 | } 78 | return Optional.empty(); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/criteria/parser/CriteriaFieldParserUtilNoAnnotationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria.parser; 25 | 26 | import org.blackdread.lib.restfilter.List2; 27 | import org.blackdread.lib.restfilter.criteria.parser.criteria.NoAnnotationCriteria; 28 | import org.blackdread.lib.restfilter.filter.LongFilter; 29 | import org.junit.jupiter.api.AfterEach; 30 | import org.junit.jupiter.api.BeforeEach; 31 | import org.junit.jupiter.api.Test; 32 | import org.slf4j.Logger; 33 | import org.slf4j.LoggerFactory; 34 | 35 | import static org.junit.jupiter.api.Assertions.assertEquals; 36 | import static org.junit.jupiter.api.Assertions.assertSame; 37 | 38 | /** 39 | *

Created on 2019/10/26.

40 | * 41 | * @author Yoann CAPLAIN 42 | */ 43 | class CriteriaFieldParserUtilNoAnnotationTest { 44 | 45 | private static final Logger log = LoggerFactory.getLogger(CriteriaFieldParserUtilNoAnnotationTest.class); 46 | 47 | @BeforeEach 48 | void setUp() { 49 | } 50 | 51 | @AfterEach 52 | void tearDown() { 53 | } 54 | 55 | @Test 56 | void classesAreCached() { 57 | final CriteriaData result = CriteriaFieldParserUtil.getCriteriaData(NoAnnotationCriteria.class); 58 | final CriteriaData result2 = CriteriaFieldParserUtil.getCriteriaData(NoAnnotationCriteria.class); 59 | 60 | assertEquals(result, result2); 61 | assertSame(result, result2); 62 | } 63 | 64 | @Test 65 | void onlyFilterFieldsAreParsedByDefault() { 66 | final NoAnnotationCriteria criteria = new NoAnnotationCriteria(); 67 | 68 | final CriteriaData result = CriteriaFieldParserUtil.getCriteriaData(criteria); 69 | 70 | log.info("result: {}", result); 71 | 72 | assertEquals(List2.of(), result.getMethods()); 73 | 74 | assertEquals(1, result.getFields().size()); 75 | CriteriaFieldDataUtil.checkFilter(result.getFields(), 0, "longFilter", LongFilter.class, null, null); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/filter/UUIDFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Original taken from jhipster project, this class has been modified. 25 | */ 26 | package org.blackdread.lib.restfilter.filter; 27 | 28 | import java.util.List; 29 | import java.util.UUID; 30 | 31 | /** 32 | * Filter class for {@link UUID} type attributes. 33 | * 34 | * @see Filter 35 | */ 36 | public class UUIDFilter extends Filter { 37 | 38 | private static final long serialVersionUID = 1L; 39 | 40 | public UUIDFilter() { 41 | } 42 | 43 | public UUIDFilter(final UUIDFilter filter) { 44 | super(filter); 45 | } 46 | 47 | @Override 48 | public Class obtainGenericClass() { 49 | return UUID.class; 50 | } 51 | 52 | /** 53 | * {@inheritDoc} 54 | */ 55 | @Override 56 | public UUIDFilter copy() { 57 | return new UUIDFilter(this); 58 | } 59 | 60 | /** 61 | * {@inheritDoc} 62 | */ 63 | @Override 64 | public UUIDFilter setEquals(UUID equals) { 65 | super.setEquals(equals); 66 | return this; 67 | } 68 | 69 | /** 70 | * {@inheritDoc} 71 | */ 72 | @Override 73 | public UUIDFilter setNotEquals(UUID notEquals) { 74 | super.setNotEquals(notEquals); 75 | return this; 76 | } 77 | 78 | /** 79 | * {@inheritDoc} 80 | */ 81 | @Override 82 | public UUIDFilter setSpecified(Boolean specified) { 83 | super.setSpecified(specified); 84 | return this; 85 | } 86 | 87 | /** 88 | * {@inheritDoc} 89 | */ 90 | @Override 91 | public UUIDFilter setIn(List in) { 92 | super.setIn(in); 93 | return this; 94 | } 95 | 96 | /** 97 | * {@inheritDoc} 98 | */ 99 | @Override 100 | public UUIDFilter setNotIn(List notIn) { 101 | super.setNotIn(notIn); 102 | return this; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/criteria/annotation/CriteriaIgnore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria.annotation; 25 | 26 | import java.lang.annotation.Documented; 27 | import java.lang.annotation.ElementType; 28 | import java.lang.annotation.Retention; 29 | import java.lang.annotation.RetentionPolicy; 30 | import java.lang.annotation.Target; 31 | 32 | /** 33 | * Allow to exclude a field/method (filter type or not) to be used with query param creation. 34 | *

35 | * This annotation is mostly useful for field of type {@link org.blackdread.lib.restfilter.filter.Filter}, to exclude some fields from being used by the library. 36 | *

37 | * Behavior on annotated methods is unknown. 38 | *

39 | * If same element is annotated with {@link CriteriaInclude} and {@link CriteriaIgnore}, behavior is unknown. 40 | *

41 | * Examples: 42 | *

43 |  * public class MyCriteria implements Criteria {
44 |  *   @CriteriaInclude
45 |  *   private String name;
46 |  *
47 |  *   @CriteriaInclude
48 |  *   private boolean showHidden;
49 |  *
50 |  *   @CriteriaIgnore // would already not be included as CriteriaInclude is not on field
51 |  *   private boolean doNotWantToIncludeWithFieldAccess;
52 |  *
53 |  *   @CriteriaIgnore
54 |  *   private LongFilter doNotWantToIncludeWithFieldAccess2;
55 |  *
56 |  *   @CriteriaAlias("fullName.contains"})
57 |  *   @CriteriaInclude
58 |  *   public String getFullName(){
59 |  *       // ...
60 |  *   }
61 |  *
62 |  *   @CriteriaAlias("allow")
63 |  *   @CriteriaInclude
64 |  *   public boolean isAnything(){
65 |  *       // ...
66 |  *   }
67 |  * }
68 |  * 
69 | *

Created on 2019/10/26.

70 | * 71 | * @author Yoann CAPLAIN 72 | */ 73 | @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD}) 74 | @Retention(RetentionPolicy.RUNTIME) 75 | @Documented 76 | public @interface CriteriaIgnore { 77 | 78 | // similar to validation groups but not used for now, simple logic for now 79 | // Class[] groups() default {}; 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/nullability/internal/OneNotNullValidator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.nullability.internal 25 | 26 | import org.blackdread.lib.restfilter.validation.nullability.OneNotNull 27 | import org.slf4j.LoggerFactory 28 | import org.springframework.util.ReflectionUtils 29 | import javax.validation.ConstraintValidator 30 | import javax.validation.ConstraintValidatorContext 31 | 32 | /** 33 | *

Created on 2019/12/22.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | class OneNotNullValidator : ConstraintValidator { 38 | 39 | private var fieldNames: Array = emptyArray() 40 | 41 | private companion object { 42 | @JvmStatic 43 | private val log = LoggerFactory.getLogger(OneNotNullValidator::class.java) 44 | } 45 | 46 | override fun initialize(constraintAnnotation: OneNotNull) { 47 | fieldNames = constraintAnnotation.value 48 | } 49 | 50 | override fun isValid(value: Any?, context: ConstraintValidatorContext): Boolean { 51 | if (value == null) { 52 | return true 53 | } 54 | if (fieldNames.size < 2) { 55 | throw IllegalArgumentException("Require at least 2 field names passed") 56 | } 57 | var isOneNotNullFound = false 58 | for (fieldName in fieldNames) { 59 | val field = ReflectionUtils.findField(value.javaClass, fieldName) ?: throw IllegalArgumentException("Could not find field: $fieldName") 60 | try { 61 | field.isAccessible = true 62 | val fieldValue = field[value] 63 | if (fieldValue != null) { 64 | if (isOneNotNullFound) return false 65 | isOneNotNullFound = true 66 | } 67 | } catch (e: IllegalAccessException) { 68 | log.error("Failed to get field", e) 69 | throw IllegalStateException(e) 70 | } 71 | } 72 | 73 | return isOneNotNullFound 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/filter/JooqQueryService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.spring.filter; 25 | 26 | import org.blackdread.lib.restfilter.filter.Filter; 27 | import org.blackdread.lib.restfilter.filter.RangeFilter; 28 | import org.blackdread.lib.restfilter.filter.StringFilter; 29 | import org.blackdread.lib.restfilter.jooq.JooqFilterUtil; 30 | import org.jooq.Condition; 31 | import org.jooq.Field; 32 | import org.springframework.transaction.annotation.Transactional; 33 | 34 | /** 35 | * Service contract for constructing and executing complex queries with jOOQ. 36 | * Build condition based on filter passed, for more complex conditions, might require to manually write it instead of using this class. 37 | *

Created on 2019/07/23

38 | * 39 | * @author Yoann CAPLAIN 40 | * @deprecated Will not delete, just API not sure yet, better to use directly the util class with the static methods 41 | */ 42 | @Transactional(readOnly = true) 43 | public interface JooqQueryService { 44 | 45 | default > Condition buildCondition(final Filter filter, final T field) { 46 | return JooqFilterUtil.buildCondition(filter, field); 47 | } 48 | 49 | default , T extends Field> Condition buildCondition(final RangeFilter filter, final T field) { 50 | return JooqFilterUtil.buildCondition(filter, field); 51 | } 52 | 53 | default > Condition buildCondition(final StringFilter filter, final T field) { 54 | return JooqFilterUtil.buildCondition(filter, field); 55 | } 56 | 57 | /** 58 | * 59 | * @param filter filter 60 | * @param field field 61 | * @param field type 62 | * @return condition 63 | * @deprecated not sure of API, could add a boolean in StringFilter so user of API can specify ignore case in url, would not be another field in a criteria class or other to make the distinction 64 | */ 65 | default > Condition buildConditionIgnoreCase(final StringFilter filter, final T field) { 66 | return JooqFilterUtil.buildConditionIgnoreCase(filter, field); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/spring/criteria/CriteriaQueryParamSpring.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.spring.criteria; 25 | 26 | import org.blackdread.lib.restfilter.criteria.Criteria; 27 | import org.blackdread.lib.restfilter.criteria.CriteriaQueryParam; 28 | import org.blackdread.lib.restfilter.criteria.FilterQueryParam; 29 | import org.blackdread.lib.restfilter.criteria.QueryParam; 30 | import org.blackdread.lib.restfilter.filter.Filter; 31 | import org.springframework.web.util.UriBuilder; 32 | 33 | import java.util.List; 34 | 35 | /** 36 | * Same as {@link CriteriaQueryParam} but with functionality dependent on Spring. 37 | *

Created on 2019/10/20.

38 | * 39 | * @author Yoann CAPLAIN 40 | */ 41 | public interface CriteriaQueryParamSpring extends CriteriaQueryParam { 42 | 43 | static CriteriaQueryParamSpring ofDelegate(final CriteriaQueryParam delegate) { 44 | return new CriteriaQueryParamSpringDelegate(delegate); 45 | } 46 | 47 | default UriBuilder buildQueryParams(final Criteria criteria, final UriBuilder builder) { 48 | return buildQueryParams((Object) criteria, builder); 49 | } 50 | 51 | default UriBuilder buildQueryParams(final Object criteria, UriBuilder builder) { 52 | final List queryParams = buildQueryParams(criteria); 53 | for (final QueryParam queryParam : queryParams) { 54 | builder = builder.queryParam(queryParam.getParamName(), queryParam.getParamValues()); 55 | } 56 | return builder; 57 | } 58 | 59 | /** 60 | * @param paramName a 61 | * @param filter a 62 | * @param builder a 63 | * @return a 64 | * @deprecated not sure to keep as public API 65 | */ 66 | default UriBuilder buildQueryParams(final String paramName, final Filter filter, UriBuilder builder) { 67 | final List filterQueryParams = getFilterQueryParams(paramName, filter); 68 | for (final FilterQueryParam filterQueryParam : filterQueryParams) { 69 | builder = builder.queryParam(filterQueryParam.getParamName(), filterQueryParam.getParamValues()); 70 | } 71 | return builder; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/nullability/internal/AllOrNoneNullValidator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.nullability.internal 25 | 26 | import org.blackdread.lib.restfilter.validation.nullability.AllOrNoneNull 27 | import org.slf4j.LoggerFactory 28 | import org.springframework.util.ReflectionUtils 29 | import javax.validation.ConstraintValidator 30 | import javax.validation.ConstraintValidatorContext 31 | 32 | /** 33 | *

Created on 2019/12/22.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | class AllOrNoneNullValidator : ConstraintValidator { 38 | 39 | private var fieldNames: Array = emptyArray() 40 | 41 | private companion object { 42 | private val log = LoggerFactory.getLogger(AllOrNoneNullValidator::class.java) 43 | } 44 | 45 | override fun initialize(constraintAnnotation: AllOrNoneNull) { 46 | fieldNames = constraintAnnotation.value 47 | } 48 | 49 | override fun isValid(value: Any?, context: ConstraintValidatorContext): Boolean { 50 | if (value == null) { 51 | return true 52 | } 53 | if (fieldNames.size < 2) { 54 | throw IllegalArgumentException("Require at least 2 field names passed") 55 | } 56 | var isOneNotNullFound = false 57 | var isOneNullFound = false 58 | for (fieldName in fieldNames) { 59 | val field = ReflectionUtils.findField(value.javaClass, fieldName) ?: throw IllegalArgumentException("Could not find field: $fieldName") 60 | try { 61 | field.isAccessible = true 62 | val fieldValue = field[value] 63 | if (fieldValue != null) { 64 | isOneNotNullFound = true 65 | if (isOneNullFound) return false 66 | } else { 67 | isOneNullFound = true 68 | if (isOneNotNullFound) return false 69 | } 70 | } catch (e: IllegalAccessException) { 71 | log.error("Failed to get field", e) 72 | throw IllegalStateException(e) 73 | } 74 | } 75 | return true 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/demo/jooq/Public.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is generated by jOOQ. 3 | */ 4 | /* 5 | * MIT License 6 | * 7 | * Copyright (c) 2019-2020 Yoann CAPLAIN 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | */ 27 | package org.blackdread.lib.restfilter.demo.jooq; 28 | 29 | 30 | import org.blackdread.lib.restfilter.demo.jooq.tables.ChildEntity; 31 | import org.blackdread.lib.restfilter.demo.jooq.tables.ParentEntity; 32 | import org.jooq.Catalog; 33 | import org.jooq.Table; 34 | import org.jooq.impl.SchemaImpl; 35 | 36 | import javax.annotation.Generated; 37 | import java.util.ArrayList; 38 | import java.util.Arrays; 39 | import java.util.List; 40 | 41 | 42 | /** 43 | * This class is generated by jOOQ. 44 | */ 45 | @Generated( 46 | value = { 47 | "http://www.jooq.org", 48 | "jOOQ version:3.11.11" 49 | }, 50 | comments = "This class is generated by jOOQ" 51 | ) 52 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 53 | public class Public extends SchemaImpl { 54 | 55 | private static final long serialVersionUID = -1887564855; 56 | 57 | /** 58 | * The reference instance of PUBLIC 59 | */ 60 | public static final Public PUBLIC = new Public(); 61 | 62 | /** 63 | * The table PUBLIC.CHILD_ENTITY. 64 | */ 65 | public final ChildEntity CHILD_ENTITY = ChildEntity.CHILD_ENTITY; 66 | 67 | /** 68 | * The table PUBLIC.PARENT_ENTITY. 69 | */ 70 | public final ParentEntity PARENT_ENTITY = ParentEntity.PARENT_ENTITY; 71 | 72 | /** 73 | * No further instances allowed 74 | */ 75 | private Public() { 76 | super("PUBLIC", null); 77 | } 78 | 79 | 80 | /** 81 | * {@inheritDoc} 82 | */ 83 | @Override 84 | public Catalog getCatalog() { 85 | return DefaultCatalog.DEFAULT_CATALOG; 86 | } 87 | 88 | @Override 89 | public final List> getTables() { 90 | List result = new ArrayList(); 91 | result.addAll(getTables0()); 92 | return result; 93 | } 94 | 95 | private final List> getTables0() { 96 | return Arrays.>asList( 97 | ChildEntity.CHILD_ENTITY, 98 | ParentEntity.PARENT_ENTITY); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/nullability/internal/NullOrMaxNotNullValidator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.nullability.internal 25 | 26 | import org.blackdread.lib.restfilter.validation.nullability.NullOrMaxNotNull 27 | import org.slf4j.LoggerFactory 28 | import org.springframework.util.ReflectionUtils 29 | import javax.validation.ConstraintValidator 30 | import javax.validation.ConstraintValidatorContext 31 | 32 | /** 33 | *

Created on 2019/12/22.

34 | * 35 | * @author Yoann CAPLAIN 36 | */ 37 | class NullOrMaxNotNullValidator : ConstraintValidator { 38 | 39 | private var fieldNames: Array = emptyArray() 40 | 41 | private var maxNotNull: Int = 0 42 | 43 | private companion object { 44 | @JvmStatic 45 | private val log = LoggerFactory.getLogger(NullOrMaxNotNullValidator::class.java) 46 | } 47 | 48 | override fun initialize(constraintAnnotation: NullOrMaxNotNull) { 49 | fieldNames = constraintAnnotation.value 50 | require(constraintAnnotation.maxNotNull >= 1) { "MaxNotNull cannot be less than 1" } 51 | maxNotNull = constraintAnnotation.maxNotNull 52 | } 53 | 54 | override fun isValid(value: Any?, context: ConstraintValidatorContext): Boolean { 55 | if (value == null) { 56 | return true 57 | } 58 | if (fieldNames.size < 2) { 59 | throw IllegalArgumentException("Require at least 2 field names passed") 60 | } 61 | var countNotNull = 0 62 | for (fieldName in fieldNames) { 63 | val field = ReflectionUtils.findField(value.javaClass, fieldName) ?: throw IllegalArgumentException("Could not find field: $fieldName") 64 | try { 65 | field.isAccessible = true 66 | val fieldValue = field[value] 67 | if (fieldValue != null) { 68 | countNotNull++ 69 | if (countNotNull > maxNotNull) return false 70 | } 71 | } catch (e: IllegalAccessException) { 72 | log.error("Failed to get field", e) 73 | throw IllegalStateException(e) 74 | } 75 | } 76 | 77 | return true 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/org/blackdread/lib/restfilter/criteria/DebugController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria; 25 | 26 | import org.blackdread.lib.restfilter.filter.DurationFilter; 27 | import org.blackdread.lib.restfilter.filter.InstantFilter; 28 | import org.blackdread.lib.restfilter.filter.LocalDateFilter; 29 | import org.blackdread.lib.restfilter.filter.ZonedDateTimeFilter; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | import org.springframework.web.bind.annotation.GetMapping; 33 | import org.springframework.web.bind.annotation.RestController; 34 | 35 | /** 36 | *

Created on 2019/10/19.

37 | * 38 | * @author Yoann CAPLAIN 39 | */ 40 | @RestController 41 | public class DebugController { 42 | 43 | private static final Logger log = LoggerFactory.getLogger(DebugController.class); 44 | 45 | @GetMapping("/api/test") 46 | public MyDTO test(MyDTO dto) { 47 | return dto; 48 | } 49 | 50 | public static class MyDTO { 51 | private InstantFilter instantFilter; 52 | private LocalDateFilter localDateFilter; 53 | private ZonedDateTimeFilter zonedDateTimeFilter; 54 | private DurationFilter durationFilter; 55 | 56 | public InstantFilter getInstantFilter() { 57 | return instantFilter; 58 | } 59 | 60 | public void setInstantFilter(final InstantFilter instantFilter) { 61 | this.instantFilter = instantFilter; 62 | } 63 | 64 | public LocalDateFilter getLocalDateFilter() { 65 | return localDateFilter; 66 | } 67 | 68 | public void setLocalDateFilter(final LocalDateFilter localDateFilter) { 69 | this.localDateFilter = localDateFilter; 70 | } 71 | 72 | public ZonedDateTimeFilter getZonedDateTimeFilter() { 73 | return zonedDateTimeFilter; 74 | } 75 | 76 | public void setZonedDateTimeFilter(final ZonedDateTimeFilter zonedDateTimeFilter) { 77 | this.zonedDateTimeFilter = zonedDateTimeFilter; 78 | } 79 | 80 | public DurationFilter getDurationFilter() { 81 | return durationFilter; 82 | } 83 | 84 | public void setDurationFilter(final DurationFilter durationFilter) { 85 | this.durationFilter = durationFilter; 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/criteria/annotation/CriteriaAlias.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.criteria.annotation; 25 | 26 | import java.lang.annotation.Documented; 27 | import java.lang.annotation.ElementType; 28 | import java.lang.annotation.Retention; 29 | import java.lang.annotation.RetentionPolicy; 30 | import java.lang.annotation.Target; 31 | 32 | /** 33 | * Annotation that can be used to define one alternative name for 34 | * a property, accepted during query param creation as alternative to the official name. 35 | *

36 | * This annotation is not a replacement of Filters, you should prefer to use actual implementation of {@link org.blackdread.lib.restfilter.filter.Filter} to have filtering features. 37 | *

38 | * Examples: 39 | *

40 |  * public class MyCriteria implements Criteria {
41 |  *   @CriteriaInclude
42 |  *   @CriteriaAlias("userName"})
43 |  *   private String name;
44 |  *
45 |  *   @CriteriaInclude
46 |  *   @CriteriaAlias("createTime.greaterThan"}) // can use like if back-end use Filter class but client code is not
47 |  *   private Instant createdAfter;
48 |  *
49 |  *   @CriteriaAlias("updateTime"})
50 |  *   private InstantFilter updatedAt;
51 |  *
52 |  *   @CriteriaInclude
53 |  *   @CriteriaAlias("extraName"})
54 |  *   public String getFullName(){
55 |  *       // ...
56 |  *   }
57 |  * }
58 |  * 
59 | *

Created on 2019/10/23.

60 | * 61 | * @author Yoann CAPLAIN 62 | */ 63 | @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD}) 64 | @Retention(RetentionPolicy.RUNTIME) 65 | @Documented 66 | public @interface CriteriaAlias { 67 | 68 | /** 69 | * Name of query param to use. 70 | * For field, it use alias if set instead of field name. 71 | * For methods, it use alias instead of method name (method name automatically removes 72 | * 'get' or 'is' or etc depending on options set when initializing the library. 73 | *

74 | * For basic type (Boolean, String, etc), name may use '.' if back-end is using some Filter class but client code criteria is not using. 75 | *
76 | * For filter type, if '.' is used, behavior is unknown. May throw or ignore any character after first '.'. 77 | * 78 | * @return alias to use for param name 79 | */ 80 | String value(); 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/blackdread/lib/restfilter/validation/file/FileMimeType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2019-2020 Yoann CAPLAIN 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package org.blackdread.lib.restfilter.validation.file; 25 | 26 | import javax.validation.Constraint; 27 | import javax.validation.Payload; 28 | import java.lang.annotation.Documented; 29 | import java.lang.annotation.Repeatable; 30 | import java.lang.annotation.Retention; 31 | import java.lang.annotation.Target; 32 | 33 | import static java.lang.annotation.ElementType.*; 34 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 35 | 36 | /** 37 | * Asserts that the annotated {@link javax.servlet.http.Part}, {@link org.springframework.web.multipart.MultipartFile}, File or Path has mime types defined or inverse. 38 | * TODO Not sure if want to add an optional class of FileUploadValidation where it should be a single bean implementation so we can have dynamic {@code value} array of allowed mime types 39 | *

Created on 2018/04/22

40 | * 41 | * @author Yoann CAPLAIN 42 | * @since 2.2.1 43 | * @deprecated not implemented, nor tested 44 | */ 45 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 46 | @Retention(RUNTIME) 47 | // want to try to register validators via DataBinder in config 48 | @Constraint(validatedBy = {}) 49 | @Documented 50 | @Repeatable(FileMimeType.List.class) 51 | @Deprecated 52 | public @interface FileMimeType { 53 | 54 | String message() default "{org.blackdread.lib.restfilter.validation.FileMimeType.message}"; 55 | 56 | Class[] groups() default {}; 57 | 58 | Class[] payload() default {}; 59 | 60 | /* 61 | * @return Service where we can use dynamic values provided by {@link FileUploadValidation#getAuthorizedMimeTypes()} 62 | */ 63 | //Class service(); 64 | 65 | /** 66 | * @return Array of allowed mime types for the file 67 | */ 68 | String[] value(); 69 | 70 | /** 71 | * @return if true, result of isValid is inverse 72 | */ 73 | boolean inverse() default false; 74 | 75 | /** 76 | * Defines several {@link FileMimeType} annotations on the same element. 77 | * 78 | * @see FileMimeType 79 | */ 80 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 81 | @Retention(RUNTIME) 82 | @Documented 83 | @interface List { 84 | 85 | FileMimeType[] value(); 86 | } 87 | } 88 | --------------------------------------------------------------------------------