5edec0d1bdae2fb252b185057bc46ca4f6079e67
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / handler / NetconfMessageToEXIEncoder.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.util.handler;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.OutputStream;
12
13 import org.opendaylight.controller.netconf.api.NetconfMessage;
14 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
15 import org.openexi.sax.Transmogrifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.xml.sax.InputSource;
19
20 import com.google.common.base.Preconditions;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.ByteBufOutputStream;
24 import io.netty.channel.ChannelHandlerContext;
25 import io.netty.handler.codec.MessageToByteEncoder;
26
27 public final class NetconfMessageToEXIEncoder extends MessageToByteEncoder<NetconfMessage> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToEXIEncoder.class);
30
31     //private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
32     private final NetconfEXICodec codec;
33
34     public NetconfMessageToEXIEncoder(final NetconfEXICodec codec) {
35         this.codec = Preconditions.checkNotNull(codec);
36     }
37
38     @Override
39     protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws Exception {
40         LOG.trace("Sent to encode : {}", XmlUtil.toString(msg.getDocument()));
41
42         try (final OutputStream os = new ByteBufOutputStream(out)) {
43             final Transmogrifier transmogrifier = codec.getTransmogrifier();
44             transmogrifier.setOutputStream(os);
45
46             // FIXME transformer not working, see EXILibTest
47             transmogrifier.encode(new InputSource(new ByteArrayInputStream(XmlUtil.toString(msg.getDocument()).getBytes())));
48             //final Transformer transformer = saxTransformerFactory.newTransformer();
49             //transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(transmogrifier.getSAXTransmogrifier()));
50         }
51     }
52 }