I am trying to log an outgoing SOAP call for which I implemented an interceptor. When I am trying to read content from the xml stream, it is causing disruption in the call flow and the soap request is not reaching the client.
Please look at the code. Even if I use just log.info() it will lead to the same issue.
Note: This SOAP call with execute successfully when I remove/exclude the changes.
With below approach, SOAP request is getting logged but it disrupts the API flow that leads to readtimeout.
I want to log my outgoing soap requests.
public class WSLoggingOutInterceptor extends AbstractSoapInterceptor {
public static final Logger log = LoggerFactory.getLogger(WSLoggingOutInterceptor.class);
public WSLoggingOutInterceptor() {
super(Phase.PRE_STREAM);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
OutputStream os = message.getContent(OutputStream.class);
CachedOutputStream cos = new CachedOutputStream();
try {
message.setContent(OutputStream.class, cos);
message.getInterceptorChain().doIntercept(message);
cos.flush();
InputStream in = cos.getInputStream();
//converting InputStream data to string
String soapMessage = IOUtils.toString(in);
log.info("SOAP envelope : {}", soapMessage.toString())
} catch (IOException e) {
throw new Fault(e);
} finally {
//restoring original OutputStream
message.setContent(OutputStream.class, os);
try {
//writing captured content back into the original OutputStream
cos.writeCacheTo(os);
os.flush();
} catch (IOException e) {
log.error("Error while writing back the cached SOAP message", e);
}
//closing CachedOutputStream
try {
cos.close();
} catch (IOException e) {
log.error("Error closing CachedOutputStream", e);
}
}
}
@Override
public void handleFault(SoapMessage message) throws Fault {
//implementation
}
}