Skip to content

Commit

Permalink
Allow for SSLParamaters
Browse files Browse the repository at this point in the history
  • Loading branch information
philip-doctor committed Apr 11, 2017
1 parent b79c223 commit 6b8697b
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/org/jgroups/client/StompConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -63,6 +65,8 @@ public class StompConnection implements Runnable {

protected final Log log=LogFactory.getLog(getClass());

protected SSLParameters sslParameters;

/**
* @param dest IP address + ':' + port, e.g. "192.168.1.5:8787"
*/
Expand All @@ -87,6 +91,7 @@ public StompConnection(String dest, boolean reconnect, SSLContext ssl) {
socket_factory = SSLSocketFactory.getDefault();
else
socket_factory = SocketFactory.getDefault();
this.sslParameters = null;
}

public StompConnection(String dest, String userid, String password, boolean reconnect, SSLContext sslcontext) {;
Expand All @@ -95,6 +100,17 @@ public StompConnection(String dest, boolean reconnect, SSLContext ssl) {
this.password = password;
this.reconnect = reconnect;
socket_factory = sslcontext.getSocketFactory();
this.sslParameters = null;
}

public StompConnection(String dest, String userid, String password, boolean reconnect, SSLContext sslcontext,
SSLParameters sslParameters) {;
server_destinations.add(dest);
this.userid = userid;
this.password = password;
this.reconnect = reconnect;
socket_factory = sslcontext.getSocketFactory();
this.sslParameters = sslParameters;
}

public String getSessionId() {return session_id;}
Expand Down Expand Up @@ -127,7 +143,7 @@ protected synchronized void startRunner() {
}
}

protected void sendConnect() {
protected void sendConnect() throws IOException {
StringBuilder sb=new StringBuilder();
sb.append(STOMP.ClientVerb.CONNECT.name()).append("\n");
if(userid != null)
Expand All @@ -145,6 +161,7 @@ protected void sendConnect() {
}
catch(IOException ex) {
log.error(Util.getMessage("FailedToSendConnectMessage"), ex);
throw ex;
}
}

Expand Down Expand Up @@ -375,13 +392,27 @@ public void startReconnectingClient() {
startRunner();
}

protected Socket buildSocket(String host, int port) throws IOException {
// both SocketFactory and SSLSocketFactory return abstract class Socket
// on createSocket calls, unfortunately we need to configure
// SSLSocket with SSLParameters, so we need to check if the socket is
// and instance of SSLSocket or not before we cast and modify
sock=socket_factory.createSocket(host, port);

if (sock instanceof SSLSocket && this.sslParameters != null) {
((SSLSocket) sock).setSSLParameters(this.sslParameters);
}

return sock;
}

protected void connectToDestination(String dest) throws IOException {
// parse destination
int index=dest.lastIndexOf(':');
String host=dest.substring(0, index);
int port=Integer.parseInt(dest.substring(index+1));

sock=socket_factory.createSocket(host, port);
sock = buildSocket(host, port);

in=new DataInputStream(sock.getInputStream());
out=new DataOutputStream(sock.getOutputStream());
Expand Down

0 comments on commit 6b8697b

Please sign in to comment.