2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.nettyutil.handler;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufInputStream;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.handler.codec.ByteToMessageDecoder;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.List;
19 import javax.xml.transform.TransformerConfigurationException;
20 import javax.xml.transform.TransformerFactory;
21 import javax.xml.transform.TransformerFactoryConfigurationError;
22 import javax.xml.transform.dom.DOMResult;
23 import javax.xml.transform.sax.SAXTransformerFactory;
24 import javax.xml.transform.sax.TransformerHandler;
25 import org.opendaylight.netconf.api.NetconfMessage;
26 import org.openexi.proc.common.EXIOptionsException;
27 import org.openexi.sax.EXIReader;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
34 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
36 private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
37 private static final SAXTransformerFactory FACTORY;
39 final TransformerFactory f = SAXTransformerFactory.newInstance();
40 if (!f.getFeature(SAXTransformerFactory.FEATURE)) {
41 throw new TransformerFactoryConfigurationError(String.format("Factory %s is not a SAXTransformerFactory", f));
44 FACTORY = (SAXTransformerFactory)f;
48 * This class is not marked as shared, so it can be attached to only a single channel,
49 * which means that {@link #decode(ChannelHandlerContext, ByteBuf, List)}
50 * cannot be invoked concurrently. Hence we can reuse the reader.
52 private final EXIReader reader;
54 private NetconfEXIToMessageDecoder(final EXIReader reader) {
55 this.reader = Preconditions.checkNotNull(reader);
58 public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIOptionsException {
59 return new NetconfEXIToMessageDecoder(codec.getReader());
63 protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws EXIOptionsException, IOException, SAXException, TransformerConfigurationException {
65 * Note that we could loop here and process all the messages, but we can't do that.
66 * The reason is <stop-exi> operation, which has the contract of immediately stopping
67 * the use of EXI, which means the next message needs to be decoded not by us, but rather
71 // If empty Byte buffer is passed to r.parse, EOFException is thrown
72 if (!in.isReadable()) {
73 LOG.debug("No more content in incoming buffer.");
77 if (LOG.isTraceEnabled()) {
78 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
81 final TransformerHandler handler = FACTORY.newTransformerHandler();
82 reader.setContentHandler(handler);
84 final DOMResult domResult = new DOMResult();
85 handler.setResult(domResult);
87 try (final InputStream is = new ByteBufInputStream(in)) {
88 // Performs internal reset before doing anything
89 reader.parse(new InputSource(is));
92 out.add(new NetconfMessage((Document) domResult.getNode()));