3

Trying to log request and response while proxying soap service using Spring cloud gateway. Below is the code snippet... which is throwing IllegalStateException.

java.lang.IllegalStateException: Only one connection receive subscriber allowed. at reactor.netty.channel.FluxReceive.startReceiver(FluxReceive.java:271) [reactor-netty-0.8.4.RELEASE.jar:0.8.4.RELEASE] at reactor.netty.channel.FluxReceive.subscribe(FluxReceive.java:121) [reactor-netty-0.8.4.RELEASE.jar:0.8.4.RELEASE]

--- Spring Versions ---

SpringBoot                  : 2.1.2.RELEASE
spring-cloud-gateway        : 2.1.1.BUILD-SNAPSHOT
spring-boot-starter-webflux : 2.1.2.RELEASE
reactor-core                : 3.2.5.RELEASE

GatewaySampleApplication

public class  GatewaySampleApplication {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes() 
.route(
r ->
r.path("/security/getToken")
.filters( f -> f.preserveHostHeader()
.filter(new PolicyHandlerReactive()))
.uri("http://targetendpoint.xyz.com")
)
.build();
}
}

PolicyHandlerReactive :

PolicyHandlerReactive implements GatewayFilter  {

@Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) 
    {
        ServerHttpRequest request = (ServerHttpRequest) exchange.getRequest();
                Flux<DataBuffer> xmlReqFlux = request.getBody();    
                Mono<List<DataBuffer>> monoDataList = xmlReqFlux.collectList();
                monoDataList.map(dataA -> logRequest2(dataA)).subscribe();
        return chain.filter(exchange);
        }
}
private List<DataBuffer> logRequest(List<DataBuffer> dataA) {           

    ListIterator<DataBuffer> dataAList = dataA.listIterator();

    while(dataAList.hasNext() ) {
        DataBuffer dataBuffer = dataAList.next();
        InputStream is = dataBuffer.asInputStream();
        System.out.println(convertStreamToString(is));
    }       
    return dataA;
}   
dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34
shatk
  • 465
  • 5
  • 16

1 Answers1

0

able to sort this out using .modifyRequestBody instead of filters... updated java DSL below...

    .route(
                    r ->
                    r.path("external/v2/security/")
                    .filters( f -> f
                            .preserveHostHeader()
                            .modifyRequestBody(String.class, String.class, 
                            (exchange, s) -> {
                                Mono<String> dataA = Mono.just(s); 
                                dataA.subscribe(System.out::print); 
                                return dataA; 
                            })                              
                         )                                                                                               
                    .uri("http://targetendpoint.xyz.com")
                    )
shatk
  • 465
  • 5
  • 16
  • Thanks for sharing this. May I suggest one improvement on your code? You don't need to subscribe the `Mono` to just print its value. `Mono` comes with the [log](https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#log--) method. – dbaltor Jun 17 '21 at 17:16