/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.netconf.util.handler; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import java.io.InputStream; import java.util.List; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.openexi.sax.EXIReader; import org.w3c.dom.Document; import org.xml.sax.InputSource; import com.google.common.base.Preconditions; public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder { private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); // FIXME: is this needed? // private static final SAXParserFactory saxParserFactory; // static { // saxParserFactory = SAXParserFactory.newInstance(); // saxParserFactory.setNamespaceAware(true); // } private final NetconfEXICodec codec; public NetconfEXIToMessageDecoder(final NetconfEXICodec codec) { this.codec = Preconditions.checkNotNull(codec); } @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { /* * Note that we could loop here and process all the messages, but we can't do that. * The reason is operation, which has the contract of immediately stopping * the use of EXI, which means the next message needs to be decoded not by us, but rather * by the XML decoder. */ final DOMResult result = new DOMResult(); final EXIReader r = codec.getReader(); final TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler(); transformerHandler.setResult(result); r.setContentHandler(transformerHandler); try (final InputStream is = new ByteBufInputStream(in)) { r.parse(new InputSource(is)); } out.add(new NetconfMessage((Document) result.getNode())); } }