Skip to content

Commit b7cab91

Browse files
committed
Added a default SSLFactory for the client
1 parent 587ef3f commit b7cab91

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

example/SSLClientExample.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import java.io.BufferedReader;
22
import java.io.File;
33
import java.io.FileInputStream;
4-
import java.io.IOException;
54
import java.io.InputStreamReader;
6-
import java.net.Socket;
75
import java.net.URI;
8-
import java.nio.channels.ByteChannel;
9-
import java.nio.channels.SelectionKey;
106
import java.security.KeyStore;
11-
import java.util.List;
12-
import java.util.concurrent.ExecutorService;
13-
import java.util.concurrent.Executors;
147

158
import javax.net.ssl.KeyManagerFactory;
169
import javax.net.ssl.SSLContext;
17-
import javax.net.ssl.SSLEngine;
1810
import javax.net.ssl.TrustManagerFactory;
1911

20-
import org.java_websocket.SSLSocketChannel2;
2112
import org.java_websocket.WebSocket;
22-
import org.java_websocket.WebSocketAdapter;
23-
import org.java_websocket.WebSocketFactory;
24-
import org.java_websocket.WebSocketImpl;
13+
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
2514
import org.java_websocket.client.WebSocketClient;
26-
import org.java_websocket.drafts.Draft;
2715
import org.java_websocket.handshake.ServerHandshake;
2816

2917
class WebSocketChatClient extends WebSocketClient {
@@ -90,7 +78,7 @@ public static void main( String[] args ) throws Exception {
9078
sslContext = SSLContext.getInstance( "TLS" );
9179
sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
9280

93-
chatclient.setWebSocketFactory( new SSLWebSocketClientFactory( sslContext ) );
81+
chatclient.setWebSocketFactory( new DefaultSSLWebSocketClientFactory( sslContext ) );
9482

9583
chatclient.connect();
9684

@@ -101,29 +89,3 @@ public static void main( String[] args ) throws Exception {
10189

10290
}
10391
}
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-
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.java_websocket.client;
2+
import java.io.IOException;
3+
import java.net.Socket;
4+
import java.nio.channels.ByteChannel;
5+
import java.nio.channels.SelectionKey;
6+
import java.util.List;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
10+
import javax.net.ssl.SSLContext;
11+
import javax.net.ssl.SSLEngine;
12+
13+
import org.java_websocket.SSLSocketChannel2;
14+
import org.java_websocket.WebSocketAdapter;
15+
import org.java_websocket.WebSocketImpl;
16+
import org.java_websocket.client.WebSocketClient.WebSocketClientFactory;
17+
import org.java_websocket.drafts.Draft;
18+
19+
20+
public class DefaultSSLWebSocketClientFactory implements WebSocketClientFactory {
21+
protected SSLContext sslcontext;
22+
protected ExecutorService exec;
23+
24+
public DefaultSSLWebSocketClientFactory( SSLContext sslContext ) {
25+
this.sslcontext = sslContext;
26+
exec = Executors.newSingleThreadScheduledExecutor();
27+
}
28+
29+
public DefaultSSLWebSocketClientFactory( SSLContext sslContext , ExecutorService exec ) {
30+
this.sslcontext = sslContext;
31+
this.exec = exec;
32+
}
33+
34+
@Override
35+
public ByteChannel wrapChannel( SelectionKey c, String host, int port ) throws IOException {
36+
SSLEngine e = sslcontext.createSSLEngine( host, port );
37+
e.setUseClientMode( true );
38+
return new SSLSocketChannel2( c, e, exec );
39+
}
40+
41+
@Override
42+
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) {
43+
return new WebSocketImpl( a, d, c );
44+
}
45+
46+
@Override
47+
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
48+
return new WebSocketImpl( a, d, s );
49+
}
50+
}

0 commit comments

Comments
 (0)