add new Options for feeding custom OkHttpClient
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -64,6 +64,11 @@
|
|||||||
<artifactId>okhttp-ws</artifactId>
|
<artifactId>okhttp-ws</artifactId>
|
||||||
<version>2.3.0</version>
|
<version>2.3.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>mockwebserver</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import com.github.nkzawa.engineio.parser.Packet;
|
|||||||
import com.github.nkzawa.engineio.parser.Parser;
|
import com.github.nkzawa.engineio.parser.Parser;
|
||||||
import com.github.nkzawa.parseqs.ParseQS;
|
import com.github.nkzawa.parseqs.ParseQS;
|
||||||
import com.github.nkzawa.thread.EventThread;
|
import com.github.nkzawa.thread.EventThread;
|
||||||
|
import com.squareup.okhttp.OkHttpClient;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
@@ -121,6 +123,7 @@ public class Socket extends Emitter {
|
|||||||
private Future pingTimeoutTimer;
|
private Future pingTimeoutTimer;
|
||||||
private Future pingIntervalTimer;
|
private Future pingIntervalTimer;
|
||||||
private SSLContext sslContext;
|
private SSLContext sslContext;
|
||||||
|
private OkHttpClient okHttpClient;
|
||||||
|
|
||||||
private ReadyState readyState;
|
private ReadyState readyState;
|
||||||
private ScheduledExecutorService heartbeatScheduler;
|
private ScheduledExecutorService heartbeatScheduler;
|
||||||
@@ -197,6 +200,7 @@ public class Socket extends Emitter {
|
|||||||
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
|
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
|
||||||
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
||||||
this.rememberUpgrade = opts.rememberUpgrade;
|
this.rememberUpgrade = opts.rememberUpgrade;
|
||||||
|
this.okHttpClient = opts.okHttpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -254,6 +258,7 @@ public class Socket extends Emitter {
|
|||||||
opts.timestampParam = this.timestampParam;
|
opts.timestampParam = this.timestampParam;
|
||||||
opts.policyPort = this.policyPort;
|
opts.policyPort = this.policyPort;
|
||||||
opts.socket = this;
|
opts.socket = this;
|
||||||
|
opts.okHttpClient = this.okHttpClient;
|
||||||
|
|
||||||
if (WebSocket.NAME.equals(name)) {
|
if (WebSocket.NAME.equals(name)) {
|
||||||
return new WebSocket(opts);
|
return new WebSocket(opts);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.github.nkzawa.emitter.Emitter;
|
|||||||
import com.github.nkzawa.engineio.parser.Packet;
|
import com.github.nkzawa.engineio.parser.Packet;
|
||||||
import com.github.nkzawa.engineio.parser.Parser;
|
import com.github.nkzawa.engineio.parser.Parser;
|
||||||
import com.github.nkzawa.thread.EventThread;
|
import com.github.nkzawa.thread.EventThread;
|
||||||
|
import com.squareup.okhttp.OkHttpClient;
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -42,6 +43,7 @@ public abstract class Transport extends Emitter {
|
|||||||
protected String timestampParam;
|
protected String timestampParam;
|
||||||
protected SSLContext sslContext;
|
protected SSLContext sslContext;
|
||||||
protected Socket socket;
|
protected Socket socket;
|
||||||
|
protected OkHttpClient okHttpClient;
|
||||||
|
|
||||||
protected ReadyState readyState;
|
protected ReadyState readyState;
|
||||||
|
|
||||||
@@ -55,6 +57,7 @@ public abstract class Transport extends Emitter {
|
|||||||
this.timestampRequests = opts.timestampRequests;
|
this.timestampRequests = opts.timestampRequests;
|
||||||
this.sslContext = opts.sslContext;
|
this.sslContext = opts.sslContext;
|
||||||
this.socket = opts.socket;
|
this.socket = opts.socket;
|
||||||
|
this.okHttpClient = opts.okHttpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Transport onError(String msg, Exception desc) {
|
protected Transport onError(String msg, Exception desc) {
|
||||||
@@ -144,6 +147,7 @@ public abstract class Transport extends Emitter {
|
|||||||
public int policyPort = -1;
|
public int policyPort = -1;
|
||||||
public Map<String, String> query;
|
public Map<String, String> query;
|
||||||
public SSLContext sslContext;
|
public SSLContext sslContext;
|
||||||
|
public OkHttpClient okHttpClient;
|
||||||
protected Socket socket;
|
protected Socket socket;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class WebSocket extends Transport {
|
|||||||
this.emit(EVENT_REQUEST_HEADERS, headers);
|
this.emit(EVENT_REQUEST_HEADERS, headers);
|
||||||
|
|
||||||
final WebSocket self = this;
|
final WebSocket self = this;
|
||||||
final OkHttpClient client = new OkHttpClient();
|
final OkHttpClient client = this.okHttpClient != null ? this.okHttpClient : new OkHttpClient();
|
||||||
if (this.sslContext != null) {
|
if (this.sslContext != null) {
|
||||||
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
|
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
|
||||||
client.setSslSocketFactory(factory);
|
client.setSslSocketFactory(factory);
|
||||||
|
|||||||
@@ -1,37 +1,31 @@
|
|||||||
package com.github.nkzawa.engineio.client;
|
package com.github.nkzawa.engineio.client;
|
||||||
|
|
||||||
import com.github.nkzawa.emitter.Emitter;
|
import com.github.nkzawa.emitter.Emitter;
|
||||||
|
import com.squareup.okhttp.OkHttpClient;
|
||||||
|
import com.squareup.okhttp.internal.SslContextBuilder;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
||||||
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.security.GeneralSecurityException;
|
|
||||||
import java.security.KeyStore;
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
|
import javax.net.ssl.HostnameVerifier;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class SSLConnectionTest extends Connection {
|
public class SSLConnectionTest extends Connection {
|
||||||
|
|
||||||
static {
|
static HostnameVerifier hostnameVerifier = new javax.net.ssl.HostnameVerifier(){
|
||||||
// for test on localhost
|
|
||||||
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
|
|
||||||
new javax.net.ssl.HostnameVerifier(){
|
|
||||||
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
|
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
|
||||||
return hostname.equals("localhost");
|
return true;
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
|
|
||||||
@@ -52,28 +46,13 @@ public class SSLConnectionTest extends Connection {
|
|||||||
return new String[] {"DEBUG=engine*", "PORT=" + PORT, "SSL=1"};
|
return new String[] {"DEBUG=engine*", "PORT=" + PORT, "SSL=1"};
|
||||||
}
|
}
|
||||||
|
|
||||||
SSLContext createSSLContext() throws GeneralSecurityException, IOException {
|
|
||||||
KeyStore ks = KeyStore.getInstance("JKS");
|
|
||||||
File file = new File("src/test/resources/keystore.jks");
|
|
||||||
ks.load(new FileInputStream(file), "password".toCharArray());
|
|
||||||
|
|
||||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
|
||||||
kmf.init(ks, "password".toCharArray());
|
|
||||||
|
|
||||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
|
|
||||||
tmf.init(ks);
|
|
||||||
|
|
||||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
||||||
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
|
||||||
return sslContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(timeout = TIMEOUT)
|
@Test(timeout = TIMEOUT)
|
||||||
public void connect() throws Exception {
|
public void connect() throws Exception {
|
||||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
|
|
||||||
Socket.Options opts = createOptions();
|
Socket.Options opts = createOptions();
|
||||||
opts.sslContext = createSSLContext();
|
opts.sslContext = SslContextBuilder.localhost();
|
||||||
|
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
|
||||||
socket = new Socket(opts);
|
socket = new Socket(opts);
|
||||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -97,7 +76,8 @@ public class SSLConnectionTest extends Connection {
|
|||||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
|
|
||||||
Socket.Options opts = createOptions();
|
Socket.Options opts = createOptions();
|
||||||
opts.sslContext = createSSLContext();
|
opts.sslContext = SslContextBuilder.localhost();
|
||||||
|
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
|
||||||
socket = new Socket(opts);
|
socket = new Socket(opts);
|
||||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -126,8 +106,10 @@ public class SSLConnectionTest extends Connection {
|
|||||||
public void defaultSSLContext() throws Exception {
|
public void defaultSSLContext() throws Exception {
|
||||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
|
|
||||||
Socket.setDefaultSSLContext(createSSLContext());
|
Socket.Options opts = createOptions();
|
||||||
socket = new Socket(createOptions());
|
Socket.setDefaultSSLContext(SslContextBuilder.localhost());
|
||||||
|
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
|
||||||
|
socket = new Socket(opts);
|
||||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
|
|||||||
Reference in New Issue
Block a user