Skip to content

Commit

Permalink
Fix null-pointer issue while checking the server for ssl enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Avishka-Shamendra committed Apr 18, 2024
1 parent 5258c9a commit fa1755e
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.Socket;
Expand Down Expand Up @@ -78,6 +80,8 @@ public class DataEndpointGroup implements DataEndpointFailureCallback {

private ObjectName mbeanEventQueue;

private static final String SSL_PROTOCOL = "ssl";

public enum HAType {
FAILOVER, LOADBALANCE
}
Expand Down Expand Up @@ -459,7 +463,7 @@ public void run() {
isEPCheckedGap = System.currentTimeMillis() + addedDelayForEPCheck;
String[] urlElements = DataPublisherUtil.getProtocolHostPort(
dataEndpoint.getDataEndpointConfiguration().getReceiverURL());
if (!isServerExists(urlElements[1], Integer.parseInt(urlElements[2]))) {
if (!isServerExists(urlElements[1], Integer.parseInt(urlElements[2]), urlElements[0])) {
dataEndpoint.deactivate();
}
}
Expand All @@ -474,17 +478,37 @@ public void run() {
}
}

private boolean isServerExists(String ip, int port) {
private boolean isServerExists(String ip, int port, String protocol) {
Socket socket = null;
SSLSocket sslSocket = null;
try {
Socket socket = new Socket(ip, port);
socket.close();
if (SSL_PROTOCOL.equals(protocol)) {
sslSocket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(ip, port);
sslSocket.startHandshake();
} else {
socket = new Socket(ip, port);
}
return true;
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
} catch (Exception e) {
return false;
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
log.error("Error closing socket: " + e.getMessage());
}
} else if (sslSocket != null) {
try {
sslSocket.close();
} catch (IOException e) {
log.error("Error closing socket: " + e.getMessage());
}
}
}
}

Expand Down

0 comments on commit fa1755e

Please sign in to comment.