When you are working with pseudo web services (without a WSDL and not REST) is useful to log the content of the response. 

The following code does the trick:

import org.apache.commons.io.input.TeeInputStream;
(...)

private static final Logger LOG = Logger.getLogger(AbstractWebServiceClient.class);

(...)
InputStream responseStream = response.getEntity().getContent(); //We get the inputStream that we want to log.
try { if (LOG.isDebugEnabled()) { responseStream = new TeeInputStream(responseStream, new ByteArrayOutputStream() { @Override public void close() throws IOException { LOG.debug("Received response: " + this.toString()); super.close(); } }, true); //We close the outputstream when the original responseStream is closed. }
return pResponseParser.parse(responseStream); //We work with the response.
} finally { responseStream.close(); //We make sure that the stream is always close. }

The TeeInputStream class in part of the Commons IO package which I usually find in my projects. I am currently using version 2.1

 

Add comment


Security code
Refresh