From 4ac6c4964dbc3f6d4e60acebf773723b463bac85 Mon Sep 17 00:00:00 2001 From: Fabrice Bacchella Date: Wed, 20 Jun 2018 23:52:22 +0200 Subject: [PATCH] channel().localAddress() not always return an InetSocketAddress, for example during unit tests. So check they are the right type before type cast. --- .../java/org/logstash/beats/BeatsHandler.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/logstash/beats/BeatsHandler.java b/src/main/java/org/logstash/beats/BeatsHandler.java index 8120aa0d..a932d143 100644 --- a/src/main/java/org/logstash/beats/BeatsHandler.java +++ b/src/main/java/org/logstash/beats/BeatsHandler.java @@ -7,6 +7,8 @@ import org.apache.logging.log4j.Logger; import java.net.InetSocketAddress; +import java.net.SocketAddress; + import javax.net.ssl.SSLHandshakeException; public class BeatsHandler extends SimpleChannelInboundHandler { @@ -114,20 +116,22 @@ private void writeAck(ChannelHandlerContext ctx, byte protocol, int sequence) { * we will use similar logic than Netty's LoggingHandler */ private String format(String message) { - InetSocketAddress local = (InetSocketAddress) context.channel().localAddress(); - InetSocketAddress remote = (InetSocketAddress) context.channel().remoteAddress(); - - String localhost; - if(local != null) { - localhost = local.getAddress().getHostAddress() + ":" + local.getPort(); - } else{ + SocketAddress local = context.channel().localAddress(); + SocketAddress remote = context.channel().remoteAddress(); + + String localhost ; + if (local != null && local instanceof InetSocketAddress) { + InetSocketAddress inetlocal = (InetSocketAddress)local; + localhost = inetlocal.getAddress().getHostAddress() + ":" + inetlocal.getPort(); + } else { localhost = "undefined"; } String remotehost; - if(remote != null) { - remotehost = remote.getAddress().getHostAddress() + ":" + remote.getPort(); - } else{ + if (remote != null && remote instanceof InetSocketAddress) { + InetSocketAddress inetremote = (InetSocketAddress)local; + remotehost = inetremote.getAddress().getHostAddress() + ":" + inetremote.getPort(); + } else { remotehost = "undefined"; }