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 Jun 24, 2018
1 parent 95b94d3 commit 13374df
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 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,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 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 instanceof InetSocketAddress) {
InetSocketAddress inetremote = (InetSocketAddress)remote;
remotehost = inetremote.getAddress().getHostAddress() + ":" + inetremote.getPort();
} else {
remotehost = "undefined";
}

Expand Down

0 comments on commit 13374df

Please sign in to comment.