X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=tests%2Fhoneynode%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fnettyutil%2Fhandler%2FNetconfMessageToXMLEncoder.java;fp=tests%2Fhoneynode%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fnettyutil%2Fhandler%2FNetconfMessageToXMLEncoder.java;h=f033e0efe3406eb1f03edbdf323502e1018dbcfd;hb=73d276ca887159c41a0877c2250d54b42e7fd64c;hp=0000000000000000000000000000000000000000;hpb=57e20793c55e43ed6bbec42e3304b9a37a1ff2f5;p=transportpce.git diff --git a/tests/honeynode/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfMessageToXMLEncoder.java b/tests/honeynode/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfMessageToXMLEncoder.java new file mode 100644 index 000000000..f033e0efe --- /dev/null +++ b/tests/honeynode/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfMessageToXMLEncoder.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.nettyutil.handler; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Optional; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufOutputStream; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import javax.xml.transform.TransformerException; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.opendaylight.netconf.api.NetconfMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Comment; + +public class NetconfMessageToXMLEncoder extends MessageToByteEncoder { + private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class); + + private final Optional clientId; + + public NetconfMessageToXMLEncoder() { + this(Optional.absent()); + } + + public NetconfMessageToXMLEncoder(final Optional clientId) { + this.clientId = clientId; + } + + @Override + @VisibleForTesting + public void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) + throws IOException, TransformerException { + LOG.trace("Sent to encode : {}", msg); + + if (clientId.isPresent()) { + Comment comment = msg.getDocument().createComment("clientId:" + clientId.get()); + msg.getDocument().appendChild(comment); + } + + try (OutputStream os = new ByteBufOutputStream(out)) { + // Wrap OutputStreamWriter with BufferedWriter as suggested in javadoc for OutputStreamWriter + + // Using custom BufferedWriter that does not provide newLine method as performance improvement + // see javadoc for BufferedWriter + StreamResult result = + new StreamResult(new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))); + DOMSource source = new DOMSource(msg.getDocument()); + ThreadLocalTransformers.getPrettyTransformer().transform(source, result); + } + } +}