0

I have to log a response from a ClientResponse, I wrote this function:

 protected Mono<ClientResponse> logResponse(ContextService ctx,ClientResponse resp, String method){

        return resp.bodyToMono(String.class)
            .map(responseBody -> {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = null;
                try {
                    builder = factory.newDocumentBuilder();
                    Document document = builder.parse(new InputSource(new StringReader(responseBody)));
                    NodeList uploadDate = document.getElementsByTagName("uploadDate");
                    NodeList outcome = document.getElementsByTagName("outcome");
                    NodeList userPdaId = document.getElementsByTagName("userPdaId");
                    NodeList errorCode = document.getElementsByTagName("errorCode");
                    NodeList errorMessage = document.getElementsByTagName("errorMessage");

                    StringBuilder message = new StringBuilder(" \n");

                    message.append(uploadDate.item(0).getTextContent() + " \n");
                    message.append(outcome.item(0).getTextContent()+ " \n");
                    message.append(userPdaId.item(0).getTextContent()+ " \n");
                    message.append(errorCode.item(0).getTextContent()+ " \n");
                    message.append(errorMessage.item(0).getTextContent()+ " \n");
                    logService.logInfo(ctx,applicationName,getClass().getSimpleName()+ ":" + method,null,message.toString(),null,null);
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return resp;
            });
}

I call the function after I got the response:

 bucketService.uploadDocumentAsync(Mono.just(doc))
                            .flatMap(clientResponse -> logResponse(ctx,clientResponse,"uploadSync"))
                            .flatMap(resp -> {
                                if (resp.statusCode().equals(HttpStatus.BAD_REQUEST)){
                                    return manageNotStructuredResponse(resp);
                                }else {
                                    return ServerResponse
                                            .status(resp.statusCode())
                                            .headers(fromClientResponse(resp))
                                            .body(BodyInserters.fromDataBuffers(resp.bodyToFlux(DataBuffer.class)));
                                }
                            }
                        )
                   ).switchIfEmpty(ServerResponse.badRequest().build())

The problem is that I get stuck inside my function and I never left it (in Postman I'm on stasys and I never get a response). Someone can help me? I think the map inside the bodyToMono is the problem but i don't know how extract the response. I cannot use subscribe() since there is another active and cannot use block().

As you can see, after my log a ServerResponse is made by the ClientResponse. Maybe I can extract the body of response from the ServerResponse? Any suggestion accepted

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Removed
  • 99
  • 3
  • 17

0 Answers0