+ public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws IOException, SAXException {
+ if (in.isReadable()) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
+ }
+
+ /* According to the XML 1.0 specifications, when there is an XML declaration
+ * at the beginning of an XML document, it is invalid to have
+ * white spaces before that declaration (reminder: a XML declaration looks like:
+ * <?xml version="1.0" encoding="UTF-8"?>). In contrast, when there is no XML declaration,
+ * it is valid to have white spaces at the beginning of the document.
+ *
+ * When they send a NETCONF message, several NETCONF servers start with a new line (either
+ * LF or CRLF), presumably to improve readability in interactive sessions with a human being.
+ * Some NETCONF servers send an XML declaration, some others do not.
+ *
+ * If a server starts a NETCONF message with white spaces and follows with an XML
+ * declaration, XmlUtil.readXmlToDocument() will fail because this is invalid XML.
+ * But in the spirit of the "NETCONF over SSH" RFC 4742 and to improve interoperability, we want
+ * to accept those messages.
+ *
+ * To do this, the following code strips the leading bytes before the start of the XML messages.
+ */