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.controller.netconf.util.handler;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufInputStream;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.io.InputStream;
16 import java.util.List;
18 import javax.xml.transform.dom.DOMResult;
19 import javax.xml.transform.sax.SAXTransformerFactory;
20 import javax.xml.transform.sax.TransformerHandler;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.openexi.sax.EXIReader;
24 import org.w3c.dom.Document;
25 import org.xml.sax.InputSource;
27 import com.google.common.base.Preconditions;
29 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
30 private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
32 // FIXME: is this needed?
33 // private static final SAXParserFactory saxParserFactory;
35 // saxParserFactory = SAXParserFactory.newInstance();
36 // saxParserFactory.setNamespaceAware(true);
39 private final NetconfEXICodec codec;
41 public NetconfEXIToMessageDecoder(final NetconfEXICodec codec) {
42 this.codec = Preconditions.checkNotNull(codec);
46 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
48 * Note that we could loop here and process all the messages, but we can't do that.
49 * The reason is <stop-exi> operation, which has the contract of immediately stopping
50 * the use of EXI, which means the next message needs to be decoded not by us, but rather
53 final DOMResult result = new DOMResult();
54 final EXIReader r = codec.getReader();
56 final TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
57 transformerHandler.setResult(result);
58 r.setContentHandler(transformerHandler);
60 try (final InputStream is = new ByteBufInputStream(in)) {
61 r.parse(new InputSource(is));
63 out.add(new NetconfMessage((Document) result.getNode()));