Skip to content

Commit b44340a

Browse files
committed
only reformatted code
1 parent 684db37 commit b44340a

1 file changed

Lines changed: 90 additions & 97 deletions

File tree

‎example/SSLServer.java‎

Lines changed: 90 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -27,127 +27,120 @@
2727
/*
2828
* Create the appropriate websocket server.
2929
*/
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 {
3937
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
7268
WebSocketSource wsgateway = new WebSocketSource( 8887, InetAddress.getByName( "localhost" ) );
73-
wsgateway.setWebSocketFactory(this);
74-
wsgateway.start();
75-
}
69+
wsgateway.setWebSocketFactory( this );
70+
wsgateway.start();
71+
}
7672

77-
@Override
73+
@Override
7874
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) {
7975

8076
return new WebSocketImpl( a, d, c );
81-
}
82-
83-
@Override
77+
}
78+
79+
@Override
8480
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 );
8885
return new WebSocketImpl( a, d, s );
8986
} catch ( Exception e1 ) {
9087
}
9188
return new WebSocketImpl( a, d, s );
92-
}
93-
89+
}
90+
9491
@Override
9592
public ByteChannel wrapChannel( SocketChannel c ) throws IOException {
9693
if( sslContext == null )
97-
throw new IllegalArgumentException( "sslContext not initialized");
94+
throw new IllegalArgumentException( "sslContext not initialized" );
9895
SSLEngine e = sslContext.createSSLEngine();
9996
e.setUseClientMode( false );
10097
return new SSLSocketChannel( c, e );
10198
}
10299

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+
}
111106

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+
}
118113

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+
}
123118

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+
}
134125

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+
}
149143
}
150-
}
151144
}
152145

153146
//@formatter:on

0 commit comments

Comments
 (0)