Skip to content

Commit 4ac6c49

Browse files
committed
channel().localAddress() not always return an InetSocketAddress, for example during unit tests.
So check they are the right type before type cast.
1 parent 95b94d3 commit 4ac6c49

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/main/java/org/logstash/beats/BeatsHandler.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.apache.logging.log4j.Logger;
88

99
import java.net.InetSocketAddress;
10+
import java.net.SocketAddress;
11+
1012
import javax.net.ssl.SSLHandshakeException;
1113

1214
public class BeatsHandler extends SimpleChannelInboundHandler<Batch> {
@@ -114,20 +116,22 @@ private void writeAck(ChannelHandlerContext ctx, byte protocol, int sequence) {
114116
* we will use similar logic than Netty's LoggingHandler
115117
*/
116118
private String format(String message) {
117-
InetSocketAddress local = (InetSocketAddress) context.channel().localAddress();
118-
InetSocketAddress remote = (InetSocketAddress) context.channel().remoteAddress();
119-
120-
String localhost;
121-
if(local != null) {
122-
localhost = local.getAddress().getHostAddress() + ":" + local.getPort();
123-
} else{
119+
SocketAddress local = context.channel().localAddress();
120+
SocketAddress remote = context.channel().remoteAddress();
121+
122+
String localhost ;
123+
if (local != null && local instanceof InetSocketAddress) {
124+
InetSocketAddress inetlocal = (InetSocketAddress)local;
125+
localhost = inetlocal.getAddress().getHostAddress() + ":" + inetlocal.getPort();
126+
} else {
124127
localhost = "undefined";
125128
}
126129

127130
String remotehost;
128-
if(remote != null) {
129-
remotehost = remote.getAddress().getHostAddress() + ":" + remote.getPort();
130-
} else{
131+
if (remote != null && remote instanceof InetSocketAddress) {
132+
InetSocketAddress inetremote = (InetSocketAddress)local;
133+
remotehost = inetremote.getAddress().getHostAddress() + ":" + inetremote.getPort();
134+
} else {
131135
remotehost = "undefined";
132136
}
133137

0 commit comments

Comments
 (0)