Skip to content

Commit c59f9c0

Browse files
committed
created SSLClientExample.java
1 parent aa46b73 commit c59f9c0

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

‎example/SSLClientExample.java‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import java.io.BufferedReader;
2+
import java.io.File;
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.Socket;
7+
import java.net.URI;
8+
import java.nio.channels.ByteChannel;
9+
import java.nio.channels.SelectionKey;
10+
import java.security.KeyStore;
11+
import java.util.List;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.Executors;
14+
15+
import javax.net.ssl.KeyManagerFactory;
16+
import javax.net.ssl.SSLContext;
17+
import javax.net.ssl.SSLEngine;
18+
import javax.net.ssl.TrustManagerFactory;
19+
20+
import org.java_websocket.SSLSocketChannel2;
21+
import org.java_websocket.WebSocket;
22+
import org.java_websocket.WebSocketAdapter;
23+
import org.java_websocket.WebSocketFactory;
24+
import org.java_websocket.WebSocketImpl;
25+
import org.java_websocket.client.WebSocketClient;
26+
import org.java_websocket.drafts.Draft;
27+
import org.java_websocket.handshake.ServerHandshake;
28+
29+
class WebSocketChatClient extends WebSocketClient {
30+
31+
public WebSocketChatClient( URI serverUri ) {
32+
super( serverUri );
33+
}
34+
35+
@Override
36+
public void onOpen( ServerHandshake handshakedata ) {
37+
System.out.println( "Connected" );
38+
39+
}
40+
41+
@Override
42+
public void onMessage( String message ) {
43+
System.out.println( "got: " + message );
44+
45+
}
46+
47+
@Override
48+
public void onClose( int code, String reason, boolean remote ) {
49+
System.out.println( "Disconnected" );
50+
System.exit( 0 );
51+
52+
}
53+
54+
@Override
55+
public void onError( Exception ex ) {
56+
ex.printStackTrace();
57+
58+
}
59+
60+
}
61+
62+
public class SSLClientExample {
63+
64+
/*
65+
* Keystore with certificate created like so (in JKS format):
66+
*
67+
*keytool -genkey -validity 3650 -keystore "keystore.jks" -storepass "storepassword" -keypass "keypassword" -alias "default" -dname "CN=127.0.0.1, OU=MyOrgUnit, O=MyOrg, L=MyCity, S=MyRegion, C=MyCountry"
68+
*/
69+
public static void main( String[] args ) throws Exception {
70+
WebSocket.DEBUG = true;
71+
72+
WebSocketChatClient chatclient = new WebSocketChatClient( new URI( "wss://localhost:8887" ) );
73+
74+
// load up the key store
75+
String STORETYPE = "JKS";
76+
String KEYSTORE = "keystore.jks";
77+
String STOREPASSWORD = "storepassword";
78+
String KEYPASSWORD = "keypassword";
79+
80+
KeyStore ks = KeyStore.getInstance( STORETYPE );
81+
File kf = new File( KEYSTORE );
82+
ks.load( new FileInputStream( kf ), STOREPASSWORD.toCharArray() );
83+
84+
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
85+
kmf.init( ks, KEYPASSWORD.toCharArray() );
86+
TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
87+
tmf.init( ks );
88+
89+
SSLContext sslContext = null;
90+
sslContext = SSLContext.getInstance( "TLS" );
91+
sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
92+
93+
chatclient.setWebSocketFactory( new SSLWebSocketClientFactory( sslContext ) );
94+
95+
chatclient.connect();
96+
97+
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
98+
while ( true ) {
99+
chatclient.send( reader.readLine() );
100+
}
101+
102+
}
103+
}
104+
105+
class SSLWebSocketClientFactory implements WebSocketFactory {
106+
private SSLContext sslcontext;
107+
private ExecutorService exec = Executors.newSingleThreadScheduledExecutor();
108+
109+
SSLWebSocketClientFactory( SSLContext sslContext ) {
110+
this.sslcontext = sslContext;
111+
}
112+
113+
@Override
114+
public ByteChannel wrapChannel( SelectionKey c ) throws IOException {
115+
SSLEngine e = sslcontext.createSSLEngine();
116+
e.setUseClientMode( true );
117+
return new SSLSocketChannel2( c, e, exec );
118+
}
119+
120+
@Override
121+
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) {
122+
return new WebSocketImpl( a, d, c );
123+
}
124+
125+
@Override
126+
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
127+
return new WebSocketImpl( a, d, s );
128+
}
129+
}

0 commit comments

Comments
 (0)