d109e2df4a9a1f7501f6e96a308514b41061617c
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfMessageToXMLEncoder.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.annotations.VisibleForTesting;
11 import com.google.common.base.Optional;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufOutputStream;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.handler.codec.MessageToByteEncoder;
16 import java.io.BufferedWriter;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.io.OutputStreamWriter;
20 import javax.xml.transform.OutputKeys;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerException;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.stream.StreamResult;
26 import org.opendaylight.controller.netconf.api.NetconfMessage;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Comment;
30
31 public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMessage> {
32     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class);
33     private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
34
35     private final Optional<String> clientId;
36
37     public NetconfMessageToXMLEncoder() {
38         this(Optional.<String>absent());
39     }
40
41     public NetconfMessageToXMLEncoder(Optional<String> clientId) {
42         this.clientId = clientId;
43     }
44
45     @Override
46     @VisibleForTesting
47     public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
48         LOG.trace("Sent to encode : {}", msg);
49
50         if (clientId.isPresent()) {
51             Comment comment = msg.getDocument().createComment("clientId:" + clientId.get());
52             msg.getDocument().appendChild(comment);
53         }
54
55         try (OutputStream os = new ByteBufOutputStream(out)) {
56             Transformer transformer = FACTORY.newTransformer();
57             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
58             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
59
60             // Wrap OutputStreamWriter with BufferedWriter as suggested in javadoc for OutputStreamWriter
61             StreamResult result = new StreamResult(new BufferedWriter(new OutputStreamWriter(os)));
62             DOMSource source = new DOMSource(msg.getDocument());
63             transformer.transform(source, result);
64         }
65     }
66 }