Suppress unwanted output to System.err when pretty printing content

Previously, when the XML pretty printer attempted to pretty print
the content of a request or response it would output an error to
System.err if the content was not valid XML. While, benign, this
output was distracting.

This commit updates the XML pretty printer to suppress its output to
System.err by configuring it with an ErrorHandler and an ErrorListener
that swallow any errors of which they are notified.

Closes gh-153
This commit is contained in:
Andy Wilkinson
2015-10-22 09:42:26 +01:00
parent 9a3e4c5f70
commit 75085477b6
3 changed files with 233 additions and 8 deletions

View File

@@ -23,13 +23,23 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.http.MediaType;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -47,12 +57,14 @@ public class PrettyPrintingContentModifier implements ContentModifier {
@Override
public byte[] modifyContent(byte[] originalContent, MediaType contentType) {
for (PrettyPrinter prettyPrinter : PRETTY_PRINTERS) {
try {
return prettyPrinter.prettyPrint(originalContent).getBytes();
}
catch (Exception ex) {
// Continue
if (originalContent.length > 0) {
for (PrettyPrinter prettyPrinter : PRETTY_PRINTERS) {
try {
return prettyPrinter.prettyPrint(originalContent).getBytes();
}
catch (Exception ex) {
// Continue
}
}
}
return originalContent;
@@ -74,10 +86,61 @@ public class PrettyPrintingContentModifier implements ContentModifier {
"4");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
StringWriter transformed = new StringWriter();
transformer.transform(new StreamSource(new ByteArrayInputStream(original)),
transformer.setErrorListener(new SilentErrorListener());
transformer.transform(createSaxSource(original),
new StreamResult(transformed));
return transformed.toString();
}
private SAXSource createSaxSource(byte[] original)
throws ParserConfigurationException, SAXException {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setErrorHandler(new SilentErrorHandler());
SAXSource xmlInput = new SAXSource(xmlReader, new InputSource(
new ByteArrayInputStream(original)));
return xmlInput;
}
private static final class SilentErrorListener implements ErrorListener {
@Override
public void warning(TransformerException exception)
throws TransformerException {
}
@Override
public void error(TransformerException exception) throws TransformerException {
}
@Override
public void fatalError(TransformerException exception)
throws TransformerException {
}
}
private static final class SilentErrorHandler implements ErrorHandler {
@Override
public void warning(SAXParseException exception) throws SAXException {
}
@Override
public void error(SAXParseException exception) throws SAXException {
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
}
}
}
private static final class JsonPrettyPrinter implements PrettyPrinter {