com.coreoz.plume.admin.RequestPredicate Maven / Gradle / Ivy
package com.coreoz.plume.admin;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import okhttp3.Request;
/**
* Represents a predicate of a {@link Request}
*
* This is a functional interface
* whose functional method is {@link #test(Object)}}.
*
*
The static method {@link #alwaysTrue()} initiates the predicate
*
*/
@FunctionalInterface
public interface RequestPredicate extends Predicate {
static RequestPredicate alwaysTrue() {
return request -> true;
}
/**
* Filters a request by its endpoint when starting with the argument
* @param endpointToFilter the endpoint to filter
*/
default RequestPredicate filterEndpointStartsWith(String endpointToFilter) {
return request -> test(request)
&& OkHttpMatchers.matchRequestEndpointStartsWith(request.url(), endpointToFilter);
}
/**
* Filters a request by its URL through a URL regex list
*
* filterUrlRegex(List.of(".+?/([^/]*)/world$")) will filter URL like `[base-url]/hello/world` but will allow URL `[base-url]/hello/world/allowed`
*
* @param urlsRegex : the URL regex list to be filtered
*/
default RequestPredicate filterUrlRegex(List urlsRegex) {
Objects.requireNonNull(urlsRegex);
if (urlsRegex.isEmpty()) {
return alwaysTrue();
}
Pattern compiledRegex = Pattern.compile(RegexBuilder.buildFilterUrlsRegex(urlsRegex));
return request -> test(request)
&& OkHttpMatchers.matchRequestUrlRegex(request.url(), compiledRegex);
}
/**
* Filters a request by its {@link HttpMethod} when matching the argument
* @param method the {@link HttpMethod} to filter
*/
default RequestPredicate filterMethod(HttpMethod method) {
return request -> test(request)
&& OkHttpMatchers.matchRequestMethod(request.method(), method.name());
}
}