|
27 | 27 | /* |
28 | 28 | * Create the appropriate websocket server. |
29 | 29 | */ |
30 | | -public class SSLServer implements WebSocketServer.WebSocketServerFactory |
31 | | -{ |
32 | | - private static final String STORETYPE = "JKS"; |
33 | | - private static final String KEYSTORE = "keystore.jks"; |
34 | | - private static final String STOREPASSWORD = "storepassword"; |
35 | | - private static final String KEYPASSWORD = "keypassword"; |
36 | | - |
37 | | - public static void main(String[] args) throws Exception |
38 | | - { |
| 30 | +public class SSLServer implements WebSocketServer.WebSocketServerFactory { |
| 31 | + private static final String STORETYPE = "JKS"; |
| 32 | + private static final String KEYSTORE = "keystore.jks"; |
| 33 | + private static final String STOREPASSWORD = "storepassword"; |
| 34 | + private static final String KEYPASSWORD = "keypassword"; |
| 35 | + |
| 36 | + public static void main( String[] args ) throws Exception { |
39 | 37 | WebSocket.DEBUG = true; |
40 | | - new SSLServer(); |
41 | | - } |
42 | | - |
43 | | - private SSLContext sslContext; |
44 | | - |
45 | | - void loadFromFile() throws Exception |
46 | | - { |
47 | | - // load up the key store |
48 | | - KeyStore ks = KeyStore.getInstance(STORETYPE); |
49 | | - File kf = new File(KEYSTORE); |
50 | | - ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray()); |
51 | | - |
52 | | - KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
53 | | - kmf.init(ks, KEYPASSWORD.toCharArray()); |
54 | | - TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
55 | | - tmf.init(ks); |
56 | | - |
57 | | - sslContext = SSLContext.getInstance("TLS"); |
58 | | - sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
59 | | - } |
60 | | - |
61 | | - /* |
62 | | - * Keystore with certificate created like so (in JKS format): |
63 | | - * |
64 | | - 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" |
65 | | - */ |
66 | | - SSLServer() throws Exception |
67 | | - { |
68 | | - sslContext = null; |
69 | | - loadFromFile(); |
70 | | - |
71 | | - // create the web socket server |
| 38 | + new SSLServer(); |
| 39 | + } |
| 40 | + |
| 41 | + private SSLContext sslContext; |
| 42 | + |
| 43 | + void loadFromFile() throws Exception { |
| 44 | + // load up the key store |
| 45 | + KeyStore ks = KeyStore.getInstance( STORETYPE ); |
| 46 | + File kf = new File( KEYSTORE ); |
| 47 | + ks.load( new FileInputStream( kf ), STOREPASSWORD.toCharArray() ); |
| 48 | + |
| 49 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" ); |
| 50 | + kmf.init( ks, KEYPASSWORD.toCharArray() ); |
| 51 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" ); |
| 52 | + tmf.init( ks ); |
| 53 | + |
| 54 | + sslContext = SSLContext.getInstance( "TLS" ); |
| 55 | + sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null ); |
| 56 | + } |
| 57 | + |
| 58 | + /* |
| 59 | + * Keystore with certificate created like so (in JKS format): |
| 60 | + * |
| 61 | + *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" |
| 62 | + */ |
| 63 | + SSLServer() throws Exception { |
| 64 | + sslContext = null; |
| 65 | + loadFromFile(); |
| 66 | + |
| 67 | + // create the web socket server |
72 | 68 | WebSocketSource wsgateway = new WebSocketSource( 8887, InetAddress.getByName( "localhost" ) ); |
73 | | - wsgateway.setWebSocketFactory(this); |
74 | | - wsgateway.start(); |
75 | | - } |
| 69 | + wsgateway.setWebSocketFactory( this ); |
| 70 | + wsgateway.start(); |
| 71 | + } |
76 | 72 |
|
77 | | - @Override |
| 73 | + @Override |
78 | 74 | public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) { |
79 | 75 |
|
80 | 76 | return new WebSocketImpl( a, d, c ); |
81 | | - } |
82 | | - |
83 | | - @Override |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
84 | 80 | public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) { |
85 | | - if(sslContext != null) try{ |
86 | | - SSLEngine e = sslContext.createSSLEngine(); |
87 | | - e.setUseClientMode(false); |
| 81 | + if( sslContext != null ) |
| 82 | + try { |
| 83 | + SSLEngine e = sslContext.createSSLEngine(); |
| 84 | + e.setUseClientMode( false ); |
88 | 85 | return new WebSocketImpl( a, d, s ); |
89 | 86 | } catch ( Exception e1 ) { |
90 | 87 | } |
91 | 88 | return new WebSocketImpl( a, d, s ); |
92 | | - } |
93 | | - |
| 89 | + } |
| 90 | + |
94 | 91 | @Override |
95 | 92 | public ByteChannel wrapChannel( SocketChannel c ) throws IOException { |
96 | 93 | if( sslContext == null ) |
97 | | - throw new IllegalArgumentException( "sslContext not initialized"); |
| 94 | + throw new IllegalArgumentException( "sslContext not initialized" ); |
98 | 95 | SSLEngine e = sslContext.createSSLEngine(); |
99 | 96 | e.setUseClientMode( false ); |
100 | 97 | return new SSLSocketChannel( c, e ); |
101 | 98 | } |
102 | 99 |
|
103 | | - class WebSocketSource extends WebSocketServer |
104 | | - { |
105 | | - private WebSocket handle; |
106 | | - WebSocketSource(int port, InetAddress addr) |
107 | | - { |
108 | | - super(new InetSocketAddress(addr, port)); |
109 | | - handle = null; |
110 | | - } |
| 100 | + class WebSocketSource extends WebSocketServer { |
| 101 | + private WebSocket handle; |
| 102 | + WebSocketSource( int port , InetAddress addr ) { |
| 103 | + super( new InetSocketAddress( addr, port ) ); |
| 104 | + handle = null; |
| 105 | + } |
111 | 106 |
|
112 | | - @Override |
113 | | - public void onClose(WebSocket arg0, int arg1, String arg2, boolean arg3) |
114 | | - { |
115 | | - System.err.println("---------------------------->Closed"); |
116 | | - if(arg0 == handle) handle = null; |
117 | | - } |
| 107 | + @Override |
| 108 | + public void onClose( WebSocket arg0, int arg1, String arg2, boolean arg3 ) { |
| 109 | + System.err.println( "---------------------------->Closed" ); |
| 110 | + if( arg0 == handle ) |
| 111 | + handle = null; |
| 112 | + } |
118 | 113 |
|
119 | | - @Override |
120 | | - public void onError(WebSocket arg0, Exception arg1) { |
121 | | - // TODO Auto-generated method stub |
122 | | - } |
| 114 | + @Override |
| 115 | + public void onError( WebSocket arg0, Exception arg1 ) { |
| 116 | + // TODO Auto-generated method stub |
| 117 | + } |
123 | 118 |
|
124 | | - @Override |
125 | | - public void onMessage(WebSocket arg0, String arg1) |
126 | | - { |
127 | | - if(arg0 != handle){ |
128 | | - arg0.close(org.java_websocket.framing.CloseFrame.NORMAL); |
129 | | - return; |
130 | | - } |
131 | | - |
132 | | - System.out.println("--------->["+arg1+"]"); |
133 | | - } |
| 119 | + @Override |
| 120 | + public void onMessage( WebSocket arg0, String arg1 ) { |
| 121 | + if( arg0 != handle ) { |
| 122 | + arg0.close( org.java_websocket.framing.CloseFrame.NORMAL ); |
| 123 | + return; |
| 124 | + } |
134 | 125 |
|
135 | | - @Override |
136 | | - public void onOpen(WebSocket arg0, ClientHandshake arg1) |
137 | | - { |
138 | | - // nothing to see just yet |
139 | | - if(handle == null){ |
140 | | - handle = arg0; |
141 | | - }else if(handle != arg0){ |
142 | | - arg0.close(org.java_websocket.framing.CloseFrame.NORMAL); |
143 | | - } |
144 | | - } |
145 | | - |
146 | | - void done() |
147 | | - { |
148 | | - if(handle != null) handle.close(org.java_websocket.framing.CloseFrame.NORMAL); |
| 126 | + System.out.println( "--------->[" + arg1 + "]" ); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onOpen( WebSocket arg0, ClientHandshake arg1 ) { |
| 131 | + // nothing to see just yet |
| 132 | + if( handle == null ) { |
| 133 | + handle = arg0; |
| 134 | + } else if( handle != arg0 ) { |
| 135 | + arg0.close( org.java_websocket.framing.CloseFrame.NORMAL ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + void done() { |
| 140 | + if( handle != null ) |
| 141 | + handle.close( org.java_websocket.framing.CloseFrame.NORMAL ); |
| 142 | + } |
149 | 143 | } |
150 | | - } |
151 | 144 | } |
152 | 145 |
|
153 | 146 | //@formatter:on |
0 commit comments