Skip to content

Commit

Permalink
Merge pull request #4 from Edwin9292/optional-jakarta-dependency-pred…
Browse files Browse the repository at this point in the history
…icate-validation

Refactor to make Jakarta Servlet dependency optional
  • Loading branch information
Edwin9292 authored Dec 15, 2023
2 parents f64e445 + 28d73d4 commit 72bcaa9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 2 additions & 0 deletions bucket4j-spring-boot-starter-context/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.GenericTypeResolver;
import org.springframework.http.server.reactive.ServerHttpRequest;

import com.giffing.bucket4j.spring.boot.starter.context.ExecutePredicate;
Expand All @@ -23,9 +23,18 @@ public class Bucket4JConfigurationPredicateNameValidator implements ConstraintVa
private final Map<FilterMethod, Map<String, ExecutePredicate<?>>> filterPredicates = new HashMap<>();

@Autowired
public Bucket4JConfigurationPredicateNameValidator(
List<ExecutePredicate<HttpServletRequest>> servletPredicates,
List<ExecutePredicate<ServerHttpRequest>> webfluxPredicates) {
public Bucket4JConfigurationPredicateNameValidator(List<ExecutePredicate<?>> executePredicates) {
List<ExecutePredicate<?>> servletPredicates = new ArrayList<>();
List<ExecutePredicate<?>> webfluxPredicates = new ArrayList<>();
executePredicates.forEach(x -> {
Class<?> genericType = GenericTypeResolver.resolveTypeArgument(x.getClass(), ExecutePredicate.class);
if(genericType == null) return;
if(genericType.getName().equals("jakarta.servlet.http.HttpServletRequest")){
servletPredicates.add(x);
} else if (genericType == ServerHttpRequest.class){
webfluxPredicates.add(x);
}
});

filterPredicates.put(FilterMethod.SERVLET, servletPredicates.stream()
.collect(Collectors.toMap(ExecutePredicate::name, Function.identity())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@

class ConfigPredicateNameValidatorTest {

List<ExecutePredicate<HttpServletRequest>> servletPredicates;
List<ExecutePredicate<ServerHttpRequest>> webfluxPredicates;
List<ExecutePredicate<?>> executePredicates;
Bucket4JConfigurationPredicateNameValidator validator;

@BeforeEach
void setup() {
servletPredicates = Arrays.asList(new ServletPathExecutePredicate(), new ServletMethodPredicate());
webfluxPredicates = Arrays.asList(new WebfluxPathExecutePredicate(), new WebfluxHeaderExecutePredicate());
validator = new Bucket4JConfigurationPredicateNameValidator(servletPredicates, webfluxPredicates);
executePredicates = Arrays.asList(
//Servlet predicates
new ServletPathExecutePredicate(), new ServletMethodPredicate(),
//Webflux predicates
new WebfluxPathExecutePredicate(), new WebfluxHeaderExecutePredicate()
);
validator = new Bucket4JConfigurationPredicateNameValidator(executePredicates);
}

/**
Expand Down Expand Up @@ -144,10 +147,11 @@ void testInvalidWebfluxWithServletPredicate() {
*/
@Test
void customPredicateTest() {
List<ExecutePredicate<HttpServletRequest>> customServletPredicates = new ArrayList<>(this.servletPredicates);
customServletPredicates.add(new CustomTestPredicate());
List<ExecutePredicate<?>> includingCustomPredicate = new ArrayList<>(this.executePredicates);
includingCustomPredicate.add(new CustomTestPredicate());

Bucket4JConfigurationPredicateNameValidator customPredicateValidator =
new Bucket4JConfigurationPredicateNameValidator(customServletPredicates, webfluxPredicates);
new Bucket4JConfigurationPredicateNameValidator(includingCustomPredicate);

List<String> executePredicates = List.of("CUSTOM-QUERY=custom-servlet");
List<String> skipPredicates = List.of();
Expand Down

0 comments on commit 72bcaa9

Please sign in to comment.