I am developing a custom Spring cloud gateway predicate factory that will take decision based on some value in the request body.
I have written the following code.
public class OperatorIdPredicateFactory extends AbstractRoutePredicateFactory<OperatorIdPredicateFactory.Config> {
public OperatorIdPredicateFactory(Class<Config> configClass) {
super(configClass);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
return (ServerWebExchange webExchange) -> {
return webExchange.getRequest().getBody().as((Flux<DataBuffer> body) ->{
var returnValue = new Object() {boolean value = false;}; // wrapper anonymous class for return value;
body.subscribe((DataBuffer buffer) ->{
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
DataBufferUtils.release(buffer);
String requestJson = charBuffer.toString();
ObjectMapper mapper = new ObjectMapper();
RouteRequestPayload requestPayload = null;
try {
requestPayload = mapper.readValue(requestJson, RouteRequestPayload.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
if(config.getPtoId().equalsIgnoreCase(requestPayload.getRouteRequest().getOperatorId()))
returnValue.value = true;
});
return returnValue.value;
});
};
}
// config class
}
But the problem is that the method is returning before the subscribe method finishes.
How can I make sure that the subscribe method finished before returning the value.