[Java] Allow configuration of OkHttpClient.Builder (#23822)
* start * comment * remove ctor * unused import * fb * Update src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HttpHubConnectionBuilder.java * remove unused param
This commit is contained in:
parent
0a61165698
commit
c18571c820
|
|
@ -19,14 +19,14 @@ import okhttp3.*;
|
|||
final class DefaultHttpClient extends HttpClient {
|
||||
private OkHttpClient client = null;
|
||||
|
||||
public DefaultHttpClient() {
|
||||
this(0, null);
|
||||
public DefaultHttpClient(Action1<OkHttpClient.Builder> configureBuilder) {
|
||||
this(null, configureBuilder);
|
||||
}
|
||||
|
||||
public DefaultHttpClient cloneWithTimeOut(int timeoutInMilliseconds) {
|
||||
OkHttpClient newClient = client.newBuilder().readTimeout(timeoutInMilliseconds, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
return new DefaultHttpClient(timeoutInMilliseconds, newClient);
|
||||
return new DefaultHttpClient(newClient, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -36,7 +36,7 @@ final class DefaultHttpClient extends HttpClient {
|
|||
}
|
||||
}
|
||||
|
||||
public DefaultHttpClient(int timeoutInMilliseconds, OkHttpClient client) {
|
||||
public DefaultHttpClient(OkHttpClient client, Action1<OkHttpClient.Builder> configureBuilder) {
|
||||
if (client != null) {
|
||||
this.client = client;
|
||||
} else {
|
||||
|
|
@ -90,9 +90,10 @@ final class DefaultHttpClient extends HttpClient {
|
|||
}
|
||||
});
|
||||
|
||||
if (timeoutInMilliseconds > 0) {
|
||||
builder.readTimeout(timeoutInMilliseconds, TimeUnit.MILLISECONDS);
|
||||
if (configureBuilder != null) {
|
||||
configureBuilder.invoke(builder);
|
||||
}
|
||||
|
||||
this.client = builder.build();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
import io.reactivex.Single;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
/**
|
||||
* A builder for configuring {@link HubConnection} instances.
|
||||
|
|
@ -20,6 +21,7 @@ public class HttpHubConnectionBuilder {
|
|||
private long handshakeResponseTimeout = 0;
|
||||
private Map<String, String> headers;
|
||||
private TransportEnum transportEnum;
|
||||
private Action1<OkHttpClient.Builder> configureBuilder;
|
||||
|
||||
HttpHubConnectionBuilder(String url) {
|
||||
this.url = url;
|
||||
|
|
@ -113,12 +115,25 @@ public class HttpHubConnectionBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a method that will be called when constructing the HttpClient to allow customization such as certificate validation, proxies, and cookies.
|
||||
* By default the client will have a cookie jar added and a read timeout for LongPolling.
|
||||
*
|
||||
* @param configureBuilder Callback for configuring the OkHttpClient.Builder.
|
||||
* @return This instance of the HttpHubConnectionBuilder.
|
||||
*/
|
||||
public HttpHubConnectionBuilder setHttpClientBuilderCallback(Action1<OkHttpClient.Builder> configureBuilder) {
|
||||
this.configureBuilder = configureBuilder;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a new instance of {@link HubConnection}.
|
||||
*
|
||||
* @return A new instance of {@link HubConnection}.
|
||||
*/
|
||||
public HubConnection build() {
|
||||
return new HubConnection(url, transport, skipNegotiate, httpClient, accessTokenProvider, handshakeResponseTimeout, headers, transportEnum);
|
||||
return new HubConnection(url, transport, skipNegotiate, httpClient, accessTokenProvider,
|
||||
handshakeResponseTimeout, headers, transportEnum, configureBuilder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import io.reactivex.Completable;
|
|||
import io.reactivex.Observable;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.subjects.*;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
/**
|
||||
* A connection used to invoke hub methods on a SignalR Server.
|
||||
|
|
@ -126,7 +127,8 @@ public class HubConnection implements AutoCloseable {
|
|||
}
|
||||
|
||||
HubConnection(String url, Transport transport, boolean skipNegotiate, HttpClient httpClient,
|
||||
Single<String> accessTokenProvider, long handshakeResponseTimeout, Map<String, String> headers, TransportEnum transportEnum) {
|
||||
Single<String> accessTokenProvider, long handshakeResponseTimeout, Map<String, String> headers, TransportEnum transportEnum,
|
||||
Action1<OkHttpClient.Builder> configureBuilder) {
|
||||
if (url == null || url.isEmpty()) {
|
||||
throw new IllegalArgumentException("A valid url is required.");
|
||||
}
|
||||
|
|
@ -143,7 +145,7 @@ public class HubConnection implements AutoCloseable {
|
|||
if (httpClient != null) {
|
||||
this.httpClient = httpClient;
|
||||
} else {
|
||||
this.httpClient = new DefaultHttpClient();
|
||||
this.httpClient = new DefaultHttpClient(configureBuilder);
|
||||
}
|
||||
|
||||
if (transport != null) {
|
||||
|
|
|
|||
|
|
@ -16,13 +16,6 @@ import io.reactivex.Completable;
|
|||
import io.reactivex.Single;
|
||||
|
||||
class WebSocketTransportTest {
|
||||
// @Test Skipping until we add functional test support
|
||||
public void WebSocketThrowsIfItCantConnect() {
|
||||
Transport transport = new WebSocketTransport(new HashMap<>(), new DefaultHttpClient());
|
||||
RuntimeException exception = assertThrows(RuntimeException.class, () -> transport.start("http://url.fake.example").blockingAwait(1, TimeUnit.SECONDS));
|
||||
assertEquals("There was an error starting the WebSocket transport.", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void CanPassNullExitCodeToOnClosed() {
|
||||
WebSocketTransport transport = new WebSocketTransport(new HashMap<>(), new WebSocketTestHttpClient());
|
||||
|
|
|
|||
Loading…
Reference in New Issue