Skip to content

Commit

Permalink
channel().localAddress() not always return an InetSocketAddress, for …
Browse files Browse the repository at this point in the history
…example during unit tests.

So check they are the right type before type cast.
  • Loading branch information
fbacchella committed Jul 2, 2018
1 parent 95b94d3 commit 8600bf9
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/main/java/org/logstash/beats/BeatsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Batch> {
Expand Down Expand Up @@ -114,23 +116,21 @@ 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{
localhost = "undefined";
}
SocketAddress local = context.channel().localAddress();
SocketAddress remote = context.channel().remoteAddress();

String remotehost;
if(remote != null) {
remotehost = remote.getAddress().getHostAddress() + ":" + remote.getPort();
} else{
remotehost = "undefined";
}
String localhost = addressToString(local);
String remotehost = addressToString(remote);

return "[local: " + localhost + ", remote: " + remotehost + "] " + message;
}

private String addressToString(SocketAddress saddr) {
if (saddr instanceof InetSocketAddress) {
InetSocketAddress inetaddr = (InetSocketAddress) saddr;
return inetaddr.getAddress().getHostAddress() + ":" + inetaddr.getPort();
} else {
return "undefined";
}
}
}

0 comments on commit 8600bf9

Please sign in to comment.