Skip to content

Commit 29fc4ea

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 c9f5488 commit 29fc4ea

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

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

+15-15
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,23 +116,21 @@ 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{
124-
localhost = "undefined";
125-
}
119+
SocketAddress local = context.channel().localAddress();
120+
SocketAddress remote = context.channel().remoteAddress();
126121

127-
String remotehost;
128-
if(remote != null) {
129-
remotehost = remote.getAddress().getHostAddress() + ":" + remote.getPort();
130-
} else{
131-
remotehost = "undefined";
132-
}
122+
String localhost = addressToString(local);
123+
String remotehost = addressToString(remote);
133124

134125
return "[local: " + localhost + ", remote: " + remotehost + "] " + message;
135126
}
127+
128+
private String addressToString(SocketAddress saddr) {
129+
if (saddr instanceof InetSocketAddress) {
130+
InetSocketAddress inetaddr = (InetSocketAddress) saddr;
131+
return inetaddr.getAddress().getHostAddress() + ":" + inetaddr.getPort();
132+
} else {
133+
return "undefined";
134+
}
135+
}
136136
}

0 commit comments

Comments
 (0)