
org.zalando.logbook.Conditions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of logbook-core Show documentation
Show all versions of logbook-core Show documentation
HTTP request and response logging
package org.zalando.logbook;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import static java.util.Collections.emptyList;
import static org.zalando.logbook.RequestURI.Component.AUTHORITY;
import static org.zalando.logbook.RequestURI.Component.PATH;
import static org.zalando.logbook.RequestURI.Component.SCHEME;
import static org.zalando.logbook.RequestURI.reconstruct;
public final class Conditions {
Conditions() {
// package private so we can trick code coverage
}
@SafeVarargs
public static Predicate exclude(final Predicate... predicates) {
return exclude(Arrays.asList(predicates));
}
public static Predicate exclude(final Collection> predicates) {
return predicates.stream()
.map(Predicate::negate)
.reduce(Predicate::and)
.orElse($ -> true);
}
public static Predicate requestTo(final String pattern) {
final Predicate predicate = Glob.compile(pattern);
return pattern.startsWith("/") ?
requestTo(BaseHttpRequest::getPath, predicate) :
requestTo(request -> reconstruct(request, SCHEME, AUTHORITY, PATH), predicate);
}
private static Predicate requestTo(final Function extractor,
final Predicate predicate) {
return request -> predicate.test(extractor.apply(request));
}
public static Predicate contentType(final String... contentTypes) {
final Predicate query = MediaTypeQuery.compile(contentTypes);
return message ->
query.test(message.getContentType());
}
public static Predicate header(final String key, final String value) {
return message ->
message.getHeaders().getOrDefault(key, emptyList()).contains(value);
}
public static Predicate header(final String key, final Predicate predicate) {
return message ->
message.getHeaders().get(key).stream().anyMatch(predicate);
}
public static Predicate header(final BiPredicate predicate) {
return message ->
message.getHeaders().entrySet().stream()
.anyMatch(e ->
e.getValue().stream().anyMatch(v -> predicate.test(e.getKey(), v)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy