Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ bin
/doc
*.xml
.idea/.name
*.jks
lib/junit-4.7.jar
*.jar
2 changes: 1 addition & 1 deletion src/main/example/SSLClientExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void main( String[] args ) throws Exception {

SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();

chatclient.setSocket( factory.createSocket() );
chatclient.setSocketFactory( factory );

chatclient.connectBlocking();

Expand Down
24 changes: 22 additions & 2 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -79,6 +80,12 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna
*/
private Socket socket = null;

/**
* The SocketFactory for this WebSocketClient
* @since 1.4.0
*/
private SocketFactory socketFactory = null;

/**
* The used OutputStream
*/
Expand Down Expand Up @@ -372,8 +379,9 @@ public void run() {
InputStream istream;
try {
boolean isNewSocket = false;

if( socket == null ) {
if (socketFactory != null) {
socket = socketFactory.createSocket();
} else if( socket == null ) {
socket = new Socket( proxy );
isNewSocket = true;

Expand Down Expand Up @@ -691,14 +699,26 @@ public void setProxy( Proxy proxy ) {
* This method must be called before <code>connect</code>.
* If the given socket is not yet bound it will be bound to the uri specified in the constructor.
* @param socket The socket which should be used for the connection
* @deprecated use setSocketFactory
*/
@Deprecated
public void setSocket( Socket socket ) {
if( this.socket != null ) {
throw new IllegalStateException( "socket has already been set" );
}
this.socket = socket;
}

/**
* Accepts a SocketFactory.<br>
* This method must be called before <code>connect</code>.
* The socket will be bound to the uri specified in the constructor.
* @param socketFactory The socket factory which should be used for the connection.
*/
public void setSocketFactory(SocketFactory socketFactory) {
this.socketFactory = socketFactory;
}

@Override
public void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin ) {
engine.sendFragmentedFrame( op, buffer, fin );
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/java_websocket/issues/AllIssueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
org.java_websocket.issues.Issue661Test.class,
org.java_websocket.issues.Issue666Test.class,
org.java_websocket.issues.Issue677Test.class,
org.java_websocket.issues.Issue732Test.class
org.java_websocket.issues.Issue732Test.class,
org.java_websocket.issues.Issue764Test.class,
org.java_websocket.issues.Issue765Test.class
})
/**
* Start all tests for issues
Expand Down
142 changes: 142 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue764Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright (c) 2010-2018 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/

package org.java_websocket.issues;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SocketUtil;
import org.junit.Test;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.concurrent.CountDownLatch;

public class Issue764Test {
private CountDownLatch countClientDownLatch = new CountDownLatch(2);
private CountDownLatch countServerDownLatch = new CountDownLatch(1);

@Test(timeout = 2000)
public void testIssue() throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException {
int port = SocketUtil.getAvailablePort();
final WebSocketClient webSocket = new WebSocketClient(new URI("wss://localhost:" + port)) {
@Override
public void onOpen(ServerHandshake handshakedata) {
countClientDownLatch.countDown();
countServerDownLatch.countDown();
}

@Override
public void onMessage(String message) {
}

@Override
public void onClose(int code, String reason, boolean remote) {
}

@Override
public void onError(Exception ex) {
}
};
WebSocketServer server = new MyWebSocketServer(port, webSocket, countServerDownLatch);

// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

KeyStore ks = KeyStore.getInstance(STORETYPE);
File kf = new File(KEYSTORE);
ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray());

KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, KEYPASSWORD.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);

SSLContext sslContext = null;
sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
webSocket.setSocketFactory(sslContext.getSocketFactory());
server.start();
countServerDownLatch.await();
webSocket.connectBlocking();
webSocket.reconnectBlocking();
countClientDownLatch.await();
}


private static class MyWebSocketServer extends WebSocketServer {
private final WebSocketClient webSocket;
private final CountDownLatch countServerDownLatch;


public MyWebSocketServer(int port, WebSocketClient webSocket, CountDownLatch countServerDownLatch) {
super(new InetSocketAddress(port));
this.webSocket = webSocket;
this.countServerDownLatch = countServerDownLatch;
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}

@Override
public void onMessage(WebSocket conn, String message) {

}

@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
}

@Override
public void onStart() {
countServerDownLatch.countDown();
}
}
}
Binary file added src/test/java/org/java_websocket/keystore.jks
Binary file not shown.