Revert "add new Options for feeding custom OkHttpClient"

This reverts commit 464b7302d4.
This commit is contained in:
Yu-Hsuan Lin
2015-05-02 00:59:34 +08:00
parent ae1c521645
commit 822aaebc35
5 changed files with 38 additions and 34 deletions

View File

@@ -64,11 +64,6 @@
<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>

View File

@@ -8,8 +8,6 @@ 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;
@@ -123,7 +121,6 @@ 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;
@@ -200,7 +197,6 @@ 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;
} }
/** /**
@@ -258,7 +254,6 @@ 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;
Transport transport; Transport transport;
if (WebSocket.NAME.equals(name)) { if (WebSocket.NAME.equals(name)) {

View File

@@ -5,7 +5,6 @@ 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;
@@ -43,7 +42,6 @@ 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;
@@ -57,7 +55,6 @@ 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) {
@@ -147,7 +144,6 @@ 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;
} }
} }

View File

@@ -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 = this.okHttpClient != null ? this.okHttpClient : new OkHttpClient(); final OkHttpClient client = 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);

View File

@@ -1,31 +1,37 @@
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 HostnameVerifier hostnameVerifier = new javax.net.ssl.HostnameVerifier(){ static {
// 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 true; return hostname.equals("localhost");
}
});
} }
};
private Socket socket; private Socket socket;
@@ -46,13 +52,28 @@ 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 = SslContextBuilder.localhost(); opts.sslContext = createSSLContext();
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
@@ -76,8 +97,7 @@ 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 = SslContextBuilder.localhost(); opts.sslContext = createSSLContext();
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
@@ -106,10 +126,8 @@ 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.Options opts = createOptions(); Socket.setDefaultSSLContext(createSSLContext());
Socket.setDefaultSSLContext(SslContextBuilder.localhost()); socket = new Socket(createOptions());
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) {