X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fhandler%2FNetconfEXIToMessageDecoder.java;fp=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fhandler%2FNetconfEXIToMessageDecoder.java;h=e4c14abeaaf3dbc8bc463e2fbb93ef056c65cf8a;hp=0000000000000000000000000000000000000000;hb=7451e256fd9a658db243666bc7e734204519ceee;hpb=1e81fa45ab0ced56eef0518c7e7f87857724985b diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfEXIToMessageDecoder.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfEXIToMessageDecoder.java new file mode 100644 index 0000000000..e4c14abeaa --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfEXIToMessageDecoder.java @@ -0,0 +1,65 @@ +/* + * 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())); + } +}