Skip to content

[Java] Allow configuration of OkHttpClient.Builder #23822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 20, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
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
Expand All @@ -36,7 +36,7 @@ public void close() {
}
}

public DefaultHttpClient(int timeoutInMilliseconds, OkHttpClient client) {
public DefaultHttpClient(OkHttpClient client, Action1<OkHttpClient.Builder> configureBuilder) {
if (client != null) {
this.client = client;
} else {
Expand Down Expand Up @@ -90,9 +90,10 @@ public List<Cookie> loadForRequest(HttpUrl url) {
}
});

if (timeoutInMilliseconds > 0) {
builder.readTimeout(timeoutInMilliseconds, TimeUnit.MILLISECONDS);
if (configureBuilder != null) {
configureBuilder.invoke(builder);
}

this.client = builder.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;

import io.reactivex.Single;
import okhttp3.OkHttpClient;

/**
* A builder for configuring {@link HubConnection} instances.
Expand All @@ -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;
Expand Down Expand Up @@ -113,12 +115,25 @@ public HttpHubConnectionBuilder withHeader(String name, String value) {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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.
Expand Down Expand Up @@ -126,7 +127,8 @@ Transport getTransport() {
}

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.");
}
Expand All @@ -143,7 +145,7 @@ Transport getTransport() {
if (httpClient != null) {
this.httpClient = httpClient;
} else {
this.httpClient = new DefaultHttpClient();
this.httpClient = new DefaultHttpClient(configureBuilder);
}

if (transport != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
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());
Expand Down