Binary data shall be emitted in the form of hexa numbers.
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / ssh / client / AsyncSshHandlerWriter.java
index e9ade8e474e3d22d9b78be9a07383428da2f10df..549bbe18cb96dc67e99bce2dff6134717f82601e 100644 (file)
@@ -17,6 +17,8 @@ import java.nio.charset.StandardCharsets;
 import java.util.Deque;
 import java.util.LinkedList;
 import java.util.Queue;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import org.checkerframework.checker.lock.qual.GuardedBy;
 import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
 import org.opendaylight.netconf.shaded.sshd.common.io.WritePendingException;
@@ -34,6 +36,8 @@ public final class AsyncSshHandlerWriter implements AutoCloseable {
     private static final Logger LOG = LoggerFactory
             .getLogger(AsyncSshHandlerWriter.class);
 
+    private static final Pattern NON_ASCII = Pattern.compile("([^\\x20-\\x7E\\x0D\\x0A])+");
+
     // public static final int MAX_PENDING_WRITES = 1000;
     // TODO implement Limiting mechanism for pending writes
     // But there is a possible issue with limiting:
@@ -156,9 +160,18 @@ public final class AsyncSshHandlerWriter implements AutoCloseable {
     }
 
     public static String byteBufToString(final ByteBuf msg) {
-        final String s = msg.toString(StandardCharsets.UTF_8);
+        final String message = msg.toString(StandardCharsets.UTF_8);
         msg.resetReaderIndex();
-        return s;
+        Matcher matcher = NON_ASCII.matcher(message);
+        return matcher.replaceAll((data) -> {
+            StringBuilder buf = new StringBuilder();
+            buf.append("\"");
+            for (byte b : data.group().getBytes(StandardCharsets.US_ASCII)) {
+                buf.append(String.format("%02X", b));
+            }
+            buf.append("\"");
+            return buf.toString();
+        });
     }
 
     private void queueRequest(final ChannelHandlerContext ctx, final ByteBuf msg, final ChannelPromise promise) {