X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2Fhandler%2FNetconfEXIToMessageDecoder.java;h=db265dee4096d10a141f876038603b52c7c10479;hb=1e80b656857bf829d8ae3cae21b0b726190b96ea;hp=db3dcafbdef93eb4a007fb4e1189daa1ad9e3957;hpb=47ad11bd477096d9ffcf568e071ba68baabdbe6e;p=controller.git diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXIToMessageDecoder.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXIToMessageDecoder.java index db3dcafbde..db265dee40 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXIToMessageDecoder.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXIToMessageDecoder.java @@ -13,31 +13,43 @@ import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; +import java.io.IOException; import java.io.InputStream; import java.util.List; -import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerConfigurationException; 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.proc.common.EXIOptionsException; import org.openexi.sax.EXIReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; +import org.xml.sax.SAXException; public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder { private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class); + private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); + /** + * This class is not marked as shared, so it can be attached to only a single channel, + * which means that {@link #decode(ChannelHandlerContext, ByteBuf, List)} + * cannot be invoked concurrently. Hence we can reuse the reader. + */ + private final EXIReader reader; - private final NetconfEXICodec codec; + private NetconfEXIToMessageDecoder(final EXIReader reader) { + this.reader = Preconditions.checkNotNull(reader); + } - public NetconfEXIToMessageDecoder(final NetconfEXICodec codec) { - this.codec = Preconditions.checkNotNull(codec); + public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIOptionsException { + return new NetconfEXIToMessageDecoder(codec.getReader()); } @Override - protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List out) throws Exception { + protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List out) throws EXIOptionsException, IOException, SAXException, TransformerConfigurationException { /* * 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 @@ -46,25 +58,24 @@ public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder { */ // If empty Byte buffer is passed to r.parse, EOFException is thrown - if (in.isReadable() == false) { + if (!in.isReadable()) { LOG.debug("No more content in incoming buffer."); return; } - LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); - - final EXIReader r = codec.getReader(); + if (LOG.isTraceEnabled()) { + LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); + } - final SAXTransformerFactory transformerFactory - = (SAXTransformerFactory) TransformerFactory.newInstance(); - final TransformerHandler handler = transformerFactory.newTransformerHandler(); - r.setContentHandler(handler); + final TransformerHandler handler = FACTORY.newTransformerHandler(); + reader.setContentHandler(handler); final DOMResult domResult = new DOMResult(); handler.setResult(domResult); try (final InputStream is = new ByteBufInputStream(in)) { - r.parse(new InputSource(is)); + // Performs internal reset before doing anything + reader.parse(new InputSource(is)); } out.add(new NetconfMessage((Document) domResult.getNode()));