How do I conditionally return from a Mono within a WebFilter?

Not sure I’m asking this right so here’s an example of what I want to do (in imperative style with blocking)

public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  Mono<MyThing> myThingMono = getMyThingMono();
  // I Know this is not right but it's effectively what I want to do
  if( myThingMono is empty ) {
    return chain.filter(exchange);
  } else {
    MyThing thing = myThingMono.block(); // I know I can't do this
    switch(thing.status) {
      case A:
        exchange.getResponse().setStatus(HttpStatus.BAD_REQUEST);
        return exchange.getResponse().setComplete();
      default: 
        return chain.filter(exchange);
    }
  }
}

This is the closest I’ve gotten in an reactive way

public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  Mono<MyThing> myThingMono = getMyThingMono();
  myThingMono.map(thing -> {
    switch(thing.status) {
      case A: return HttpStatus.BAD_REQUEST;
      default: return HttpStatus.OK;
    }        
  })
  .defaultIfEmpty(HttpStatus.OK) // in case myThingMono is empty
  .map(status -> {
    switch(status) {
      case HttpStatus.OK:
        return chain.filter(exchange);
      default:
        exchange.getResponse().setStatusCode(status);
        return exchange.getResponse().setComplete();
    }        
  })
  .then();

The myThingMono wants to return a a Mono<Object> but my filter is expecting a Mono<Void> so I just jammed a .then() into it. I’m sure that’s not right.

My code compiles and is reaching the final switch statement and calling the right return statement, but the request is not reaching my controller. The webfilter is not forwarding the request correctly. I’m just getting a 200 status back without the handler’s result.

What is the right reactive way to do this?

Answer

I meant something like this. I used .switchIfEmpty(Mono.empty()) to return something if your code does not enter on the first transformation. You can also create a default Mono.just(new MyThing()).

Then I used the flatMap to place all the logic switch ... inside it.

enum MyThingEnum { A }

class MyThing { public MyThingEnum status; }

public class Test {
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        Mono<MyThing> myThingMono = getMyThingMono();
        return myThingMono.flatMap(thing -> {
            switch (thing.status) {
                case A:
                    exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
                    return exchange.getResponse().setComplete();
                default:
                    exchange.getResponse().setStatusCode(HttpStatus.OK);
                    return chain.filter(exchange);
            }
        })
        .switchIfEmpty(Mono.empty());
    }

    private Mono<MyThing> getMyThingMono() {
        return Mono.just(new MyThing());
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *