Writing Custom Route Predicate Factories and Custom Gateway Filter Factories for Spring Cloud Gateway

Reading Time: 2 minutes

Route Predicate Factories

The Spring Cloud Gateway compares the routes of the incoming request as part of Spring WebFlux HandlerMapping infrastructure. It has many pre-defined route predicate factories. The predicates compares on the basis of different attributes passed on the HTTP request.

Gateway Filter Factories

The Spring Cloud Gateway Filter Factories provide the modifications of the incoming HTTP request or outgoing HTTP response in some particular pattern. The filters are designed to scope to a particular route.

The Spring Cloud Gateway provides a developer guide which can be followed in the manner so that the developer can design their custom route predicate factories as well as the custom gateway filter factories based upon the requirements.

Writing Custom Route Predicate Factories

For designing custom route predicate factories the first step is to implement the RoutePredicateFactory. The developer can extend the abstract class called as AbstractRoutePredicateFactory. Below is an example of a java class for the custom route predicate factories :

public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<HeaderRoutePredicateFactory.Config> {

    public MyRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        // grab configuration from Config object
        return exchange -> {
            //grab the request
            ServerHttpRequest request = exchange.getRequest();
            //take information from the request to see if it
            //matches configuration.
            return matches(config, request);
        };
    }

    public static class Config {
        //Put the configuration properties for your filter here
    }

}

Writing Custom GatewayFilter Factories

For designing the custom GatewayFilter factories the developer needs to implement GatewayFilterFactory. The developer can extend the abstract class called as AbstractGatewayFilterFactory. Below is an example java class for the implementation of custom GatewayFilter factories :

public class PreGatewayFilterFactory extends AbstractGatewayFilterFactory<PreGatewayFilterFactory.Config> {

    public PreGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        // grab configuration from Config object
        return (exchange, chain) -> {
            //If you want to build a "pre" filter you need to manipulate the
            //request before calling chain.filter
            ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
            //use builder to manipulate the request
            return chain.filter(exchange.mutate().request(builder.build()).build());
        };
    }

    public static class Config {
        //Put the configuration properties for your filter here
    }

}

public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory<PostGatewayFilterFactory.Config> {

    public PostGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        // grab configuration from Config object
        return (exchange, chain) -> {
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                ServerHttpResponse response = exchange.getResponse();
                //Manipulate the response in some way
            }));
        };
    }

    public static class Config {
        //Put the configuration properties for your filter here
    }

}

Naming Convention for Custom Filters and References in Configuration

The naming convention for the custom filter suggests the class name should end in GatewayFilterFactory.

Example, if a filter name is Sample in configuration files, than the filter must be in a class named as SampleGatewayFilterFactory.

Conclusion

The Route Predicate Factories and the Gateway filter factories have many in-built predicates and filters which provide a wide range of functionality to the developer to make their work more efficient. With the help of the custom Predicate factories and Gateway factories the scope of the developer widens and they are able to use the designed codes in a manner so that the throughput of the code can be increased exponentially. 

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading