Remove HttpConnectionOptions (#3101)
This commit is contained in:
parent
f87d8b4aaf
commit
b63c3816d5
|
|
@ -1,79 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
package com.microsoft.signalr;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class HttpConnectionOptions {
|
||||
private Transport transport;
|
||||
private LogLevel loglevel;
|
||||
private Logger logger;
|
||||
private boolean skipNegotiate;
|
||||
private Supplier<CompletableFuture<String>> accessTokenProvider;
|
||||
private HttpClient client;
|
||||
|
||||
public HttpConnectionOptions() {}
|
||||
|
||||
public HttpConnectionOptions(Transport transport, LogLevel logLevel, boolean skipNegotiate) {
|
||||
this.transport = transport;
|
||||
this.skipNegotiate = skipNegotiate;
|
||||
this.loglevel = logLevel;
|
||||
}
|
||||
|
||||
public HttpConnectionOptions(Transport transport, Logger logger, boolean skipNegotiate) {
|
||||
this.transport = transport;
|
||||
this.skipNegotiate = skipNegotiate;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public void setTransport(Transport transport) {
|
||||
this.transport = transport;
|
||||
}
|
||||
|
||||
public void setLoglevel(LogLevel loglevel) {
|
||||
this.loglevel = loglevel;
|
||||
}
|
||||
|
||||
public void setSkipNegotiate(boolean skipNegotiate) {
|
||||
this.skipNegotiate = skipNegotiate;
|
||||
}
|
||||
|
||||
public Transport getTransport() {
|
||||
return transport;
|
||||
}
|
||||
|
||||
public LogLevel getLoglevel() {
|
||||
return loglevel;
|
||||
}
|
||||
|
||||
public boolean getSkipNegotiate() {
|
||||
return skipNegotiate;
|
||||
}
|
||||
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public void setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public void setAccessTokenProvider(Supplier<CompletableFuture<String>> accessTokenProvider) {
|
||||
this.accessTokenProvider = accessTokenProvider;
|
||||
}
|
||||
|
||||
public Supplier<CompletableFuture<String>> getAccessTokenProvider() {
|
||||
return accessTokenProvider;
|
||||
}
|
||||
|
||||
// For testing purposes only
|
||||
void setHttpClient(HttpClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
HttpClient getHttpClient() {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
package com.microsoft.signalr;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class HttpHubConnectionBuilder {
|
||||
private String url;
|
||||
private Transport transport;
|
||||
private Logger logger;
|
||||
private HttpClient httpClient;
|
||||
private boolean skipNegotiate;
|
||||
private Supplier<CompletableFuture<String>> accessTokenProvider;
|
||||
|
||||
HttpHubConnectionBuilder(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public HttpHubConnectionBuilder withTransport(Transport transport) {
|
||||
this.transport = transport;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public HttpHubConnectionBuilder withHttpClient(HttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHubConnectionBuilder configureLogging(LogLevel logLevel) {
|
||||
this.logger = new ConsoleLogger(logLevel);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHubConnectionBuilder shouldSkipNegotiate(boolean skipNegotiate) {
|
||||
this.skipNegotiate = skipNegotiate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHubConnectionBuilder withAccessTokenProvider(Supplier<CompletableFuture<String>> accessTokenProvider) {
|
||||
this.accessTokenProvider = accessTokenProvider;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHubConnectionBuilder configureLogging(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHubConnectionBuilder withLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HubConnection build() {
|
||||
return new HubConnection(url, transport, skipNegotiate, logger, httpClient, accessTokenProvider);
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ public class HubConnection {
|
|||
private HttpClient httpClient;
|
||||
private String stopError;
|
||||
|
||||
HubConnection(String url, HttpConnectionOptions options) {
|
||||
HubConnection(String url, Transport transport, boolean skipNegotiate, Logger logger, HttpClient httpClient, Supplier<CompletableFuture<String>> accessTokenProvider) {
|
||||
if (url == null || url.isEmpty()) {
|
||||
throw new IllegalArgumentException("A valid url is required.");
|
||||
}
|
||||
|
|
@ -46,29 +46,29 @@ public class HubConnection {
|
|||
this.baseUrl = url;
|
||||
this.protocol = new JsonHubProtocol();
|
||||
|
||||
if (options.getAccessTokenProvider() != null) {
|
||||
this.accessTokenProvider = options.getAccessTokenProvider();
|
||||
if (accessTokenProvider != null) {
|
||||
this.accessTokenProvider = accessTokenProvider;
|
||||
} else {
|
||||
this.accessTokenProvider = () -> CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
if (options.getLogger() != null) {
|
||||
this.logger = options.getLogger();
|
||||
} else {
|
||||
this.logger = new NullLogger();
|
||||
}
|
||||
|
||||
if (options.getHttpClient() != null) {
|
||||
this.httpClient = options.getHttpClient();
|
||||
if (httpClient != null) {
|
||||
this.httpClient = httpClient;
|
||||
} else {
|
||||
this.httpClient = new DefaultHttpClient(this.logger);
|
||||
}
|
||||
|
||||
if (options.getTransport() != null) {
|
||||
this.transport = options.getTransport();
|
||||
if (logger != null) {
|
||||
this.logger = logger;
|
||||
} else {
|
||||
this.logger = new NullLogger();
|
||||
}
|
||||
|
||||
this.skipNegotiate = options.getSkipNegotiate();
|
||||
if (transport != null) {
|
||||
this.transport = transport;
|
||||
}
|
||||
|
||||
this.skipNegotiate = skipNegotiate;
|
||||
|
||||
this.callback = (payload) -> {
|
||||
if (!handshakeReceived) {
|
||||
|
|
|
|||
|
|
@ -3,57 +3,14 @@
|
|||
|
||||
package com.microsoft.signalr;
|
||||
|
||||
public class HubConnectionBuilder {
|
||||
private String url;
|
||||
private Transport transport;
|
||||
private Logger logger;
|
||||
private HttpConnectionOptions options = null;
|
||||
public abstract class HubConnectionBuilder {
|
||||
|
||||
private HubConnectionBuilder(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public static HubConnectionBuilder create(String url) {
|
||||
public static HttpHubConnectionBuilder create(String url) {
|
||||
if (url == null || url.isEmpty()) {
|
||||
throw new IllegalArgumentException("A valid url is required.");
|
||||
}
|
||||
return new HubConnectionBuilder(url);
|
||||
return new HttpHubConnectionBuilder(url);
|
||||
}
|
||||
|
||||
public HubConnectionBuilder withTransport(Transport transport) {
|
||||
this.transport = transport;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HubConnectionBuilder withOptions(HttpConnectionOptions options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HubConnectionBuilder configureLogging(LogLevel logLevel) {
|
||||
this.logger = new ConsoleLogger(logLevel);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HubConnectionBuilder configureLogging(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HubConnection build() {
|
||||
if (options == null) {
|
||||
options = new HttpConnectionOptions();
|
||||
}
|
||||
if (options.getTransport() == null && this.transport != null) {
|
||||
options.setTransport(this.transport);
|
||||
}
|
||||
if (options.getLogger() == null && options.getLoglevel() != null) {
|
||||
options.setLogger(new ConsoleLogger(options.getLoglevel()));
|
||||
}
|
||||
if (options.getLogger() == null && this.logger != null) {
|
||||
options.setLogger(this.logger);
|
||||
}
|
||||
|
||||
return new HubConnection(url, options);
|
||||
}
|
||||
public abstract HubConnection build();
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
package com.microsoft.signalr;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HttpConnectionOptionsTest {
|
||||
@Test
|
||||
public void CheckHttpConnectionOptionsFields() {
|
||||
Transport mockTransport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions(mockTransport, LogLevel.Information, true);
|
||||
assertEquals(LogLevel.Information, options.getLoglevel());
|
||||
assertTrue(options.getSkipNegotiate());
|
||||
assertNotNull(options.getTransport());
|
||||
}
|
||||
}
|
||||
|
|
@ -61,9 +61,6 @@ class HubConnectionTest {
|
|||
@Test
|
||||
public void constructHubConnectionWithHttpConnectionOptions() throws Exception {
|
||||
Transport mockTransport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(mockTransport);
|
||||
options.setSkipNegotiate(true);
|
||||
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
|
||||
|
||||
hubConnection.start();
|
||||
|
|
@ -910,11 +907,9 @@ class HubConnectionTest {
|
|||
TestHttpClient client = new TestHttpClient()
|
||||
.on("POST", (req) -> CompletableFuture.completedFuture(new HttpResponse(404, "", "")));
|
||||
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withHttpClient(client)
|
||||
.build();
|
||||
|
||||
try {
|
||||
|
|
@ -931,11 +926,9 @@ class HubConnectionTest {
|
|||
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate",
|
||||
(req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"url\":\"http://example.com\"}")));
|
||||
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withHttpClient(client)
|
||||
.build();
|
||||
|
||||
ExecutionException exception = assertThrows(ExecutionException.class, () -> hubConnection.start().get(1000, TimeUnit.MILLISECONDS));
|
||||
|
|
@ -950,12 +943,10 @@ class HubConnectionTest {
|
|||
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}")));
|
||||
|
||||
MockTransport transport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withTransport(transport)
|
||||
.withHttpClient(client)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
|
@ -971,12 +962,10 @@ class HubConnectionTest {
|
|||
(req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"error\":\"Test error.\"}")));
|
||||
|
||||
MockTransport transport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withHttpClient(client)
|
||||
.withTransport(transport)
|
||||
.build();
|
||||
|
||||
ExecutionException exception = assertThrows(ExecutionException.class, () -> hubConnection.start().get(1000, TimeUnit.MILLISECONDS));
|
||||
|
|
@ -992,12 +981,10 @@ class HubConnectionTest {
|
|||
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}")));
|
||||
|
||||
MockTransport transport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withTransport(transport)
|
||||
.withHttpClient(client)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
|
@ -1019,13 +1006,11 @@ class HubConnectionTest {
|
|||
});
|
||||
|
||||
MockTransport transport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
options.setAccessTokenProvider(() -> CompletableFuture.completedFuture("secretToken"));
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withTransport(transport)
|
||||
.withHttpClient(client)
|
||||
.withAccessTokenProvider(() -> CompletableFuture.completedFuture("secretToken"))
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
|
@ -1035,8 +1020,7 @@ class HubConnectionTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void accessTokenProviderIsOverriddenFromRedirectNegotiate()
|
||||
throws InterruptedException, ExecutionException, TimeoutException, Exception {
|
||||
public void accessTokenProviderIsOverriddenFromRedirectNegotiate() throws Exception {
|
||||
AtomicReference<String> token = new AtomicReference<>();
|
||||
TestHttpClient client = new TestHttpClient()
|
||||
.on("POST", "http://example.com/negotiate", (req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"url\":\"http://testexample.com/\",\"accessToken\":\"newToken\"}")))
|
||||
|
|
@ -1048,13 +1032,11 @@ class HubConnectionTest {
|
|||
});
|
||||
|
||||
MockTransport transport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
options.setAccessTokenProvider(() -> CompletableFuture.completedFuture("secretToken"));
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withTransport(transport)
|
||||
.withHttpClient(client)
|
||||
.withAccessTokenProvider(() -> CompletableFuture.completedFuture("secretToken"))
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
|
@ -1067,12 +1049,10 @@ class HubConnectionTest {
|
|||
@Test
|
||||
public void hubConnectionCanBeStartedAfterBeingStopped() throws Exception {
|
||||
MockTransport transport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setSkipNegotiate(true);
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withTransport(transport)
|
||||
.shouldSkipNegotiate(true)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
|
@ -1094,12 +1074,11 @@ class HubConnectionTest {
|
|||
.on("POST", "http://testexample.com/negotiate", (req) -> CompletableFuture
|
||||
.completedFuture(new HttpResponse(200, "", "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
|
||||
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}")));
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(mockTransport);
|
||||
options.setHttpClient(client);
|
||||
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withTransport(mockTransport)
|
||||
.withHttpClient(client)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
|
@ -1120,12 +1099,10 @@ class HubConnectionTest {
|
|||
.completedFuture(new HttpResponse(500, "Internal server error", "")));
|
||||
|
||||
MockTransport transport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.withTransport(transport)
|
||||
.withHttpClient(client)
|
||||
.build();
|
||||
|
||||
ExecutionException exception = assertThrows(ExecutionException.class, () -> hubConnection.start().get(1000, TimeUnit.MILLISECONDS));
|
||||
|
|
|
|||
|
|
@ -13,12 +13,12 @@ class TestUtils {
|
|||
}
|
||||
|
||||
static HubConnection createHubConnection(String url, Transport transport, Logger logger, boolean skipNegotiate, HttpClient client) {
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setLogger(logger);
|
||||
options.setSkipNegotiate(skipNegotiate);
|
||||
options.setHttpClient(client);
|
||||
HubConnectionBuilder builder = HubConnectionBuilder.create(url);
|
||||
return builder.withOptions(options).build();
|
||||
HttpHubConnectionBuilder builder = HubConnectionBuilder.create(url)
|
||||
.withTransport(transport)
|
||||
.withHttpClient(client)
|
||||
.shouldSkipNegotiate(skipNegotiate)
|
||||
.withLogger(logger);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue