Merge "Removing { } from NormalizedNodeJsonBodyWriter"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / 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.nettyutil.handler;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufOutputStream;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.MessageToByteEncoder;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import javax.xml.transform.Transformer;
18 import javax.xml.transform.TransformerException;
19 import javax.xml.transform.dom.DOMSource;
20 import javax.xml.transform.sax.SAXResult;
21 import org.opendaylight.controller.netconf.api.NetconfMessage;
22 import org.openexi.proc.common.EXIOptionsException;
23 import org.openexi.sax.Transmogrifier;
24 import org.openexi.sax.TransmogrifierException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.xml.sax.ContentHandler;
28
29 public final class NetconfMessageToEXIEncoder extends MessageToByteEncoder<NetconfMessage> {
30     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToEXIEncoder.class);
31     /**
32      * This class is not marked as shared, so it can be attached to only a single channel,
33      * which means that {@link #encode(io.netty.channel.ChannelHandlerContext, org.opendaylight.controller.netconf.api.NetconfMessage, io.netty.buffer.ByteBuf)}
34      * cannot be invoked concurrently. Hence we can reuse the transmogrifier.
35      */
36     private final NetconfEXICodec codec;
37
38     private NetconfMessageToEXIEncoder(final NetconfEXICodec codec) {
39         this.codec = Preconditions.checkNotNull(codec);
40     }
41
42     public static NetconfMessageToEXIEncoder create(final NetconfEXICodec codec) throws EXIOptionsException, TransmogrifierException {
43         return new NetconfMessageToEXIEncoder(codec);
44     }
45
46     @Override
47     protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws EXIOptionsException, IOException, TransformerException, TransmogrifierException {
48         LOG.trace("Sent to encode : {}", msg);
49
50         // TODO Workaround for bug 2679, recreate transmogrifier every time
51         // If the transmogrifier is reused, encoded xml can become non valid according to EXI decoder
52         // Seems like a bug in the nagasena library (try newer version of the library or fix the bug inside of it)
53         // Related bugs 2459: reuse nagasena resources, 2458: upgrade nagasena to newest version
54         final Transmogrifier transmogrifier = codec.getTransmogrifier();
55
56         try (final OutputStream os = new ByteBufOutputStream(out)) {
57             transmogrifier.setOutputStream(os);
58             final ContentHandler handler = transmogrifier.getSAXTransmogrifier();
59             final Transformer transformer = ThreadLocalTransformers.getDefaultTransformer();
60             transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(handler));
61         } finally {
62             // Make sure we do not retain any reference to state by removing
63             // the output stream reference and resetting internal state.
64             transmogrifier.setOutputStream(null);
65             transmogrifier.getSAXTransmogrifier();
66         }
67     }
68 }