package gaf3.core.gateway.handler.predicate

import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate
import org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory
import org.springframework.web.server.ServerWebExchange
import java.util.function.Predicate

class ExcludesRoutePredicateFactory: PathRoutePredicateFactory() {

    override fun apply(config: Config): Predicate<ServerWebExchange> {
        val p: Predicate<ServerWebExchange> = super.apply(config)
        return object : GatewayPredicate {
            override fun test(exchange: ServerWebExchange): Boolean {
                return !p.test(exchange)
            }

            override fun toString(): String {
                return String.format("Excludes: %s, match trailing slash: %b",
                        config.patterns, config.isMatchOptionalTrailingSeparator)
            }
        }
    }
}