Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #759 from mirvnillith/feature/httpclient-improved-…
Browse files Browse the repository at this point in the history
…config

Improved HttpClientConfig
  • Loading branch information
softqwewasd authored Sep 5, 2024
2 parents a56d84d + 4f90223 commit 8a36888
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static reactor.core.Exceptions.isRetryExhausted;
import static reactor.core.publisher.Mono.just;
import static se.fortnox.reactivewizard.client.HttpClientConfig.DEFAULT_TIMEOUT_MS;

public class HttpClient implements InvocationHandler {
private static final Logger LOG = LoggerFactory.getLogger(HttpClient.class);
Expand All @@ -103,8 +104,8 @@ public class HttpClient implements InvocationHandler {
private final RequestLogger requestLogger;
private final Map<Class<?>, List<HttpClient.BeanParamProperty>> beanParamCache = new ConcurrentHashMap<>();
private final Map<Method, JaxRsMeta> jaxRsMetaMap = new ConcurrentHashMap<>();
private int timeout = 10;
private TemporalUnit timeoutUnit = ChronoUnit.SECONDS;
private int timeout = DEFAULT_TIMEOUT_MS;
private TemporalUnit timeoutUnit = ChronoUnit.MILLIS;
private final Duration retryDuration;

@Inject
Expand All @@ -131,6 +132,7 @@ public HttpClient(HttpClientConfig config,
collector = new ByteBufCollector(config.getMaxResponseSize());
this.preRequestHooks = preRequestHooks;
this.retryDuration = Duration.ofMillis(config.getRetryDelayMs());
setTimeout(config.getTimeoutMs(), ChronoUnit.MILLIS);
}

public HttpClient(HttpClientConfig config) {
Expand Down Expand Up @@ -534,7 +536,22 @@ protected RequestBuilder createRequest(Method method, Object[] arguments) {
JaxRsMeta meta = getJaxRsMeta(method);

RequestBuilder request = new RequestBuilder(serverInfo, meta.getHttpMethod(), meta.getFullPath());
request.setUri(getPath(method, arguments, meta));

String root = config.getRoot();
String path = getPath(method, arguments, meta);
if (root == null || root.isEmpty()) {
request.setUri(path);
} else {
if (root.endsWith("/")) {
root = root.substring(0, root.length() - 1);
}
if (path.startsWith("/")) {
request.setUri(root + path);
} else {
request.setUri(root + "/" + path);
}
}

setHeaderParams(request, method, arguments);
addCustomParams(request, method, arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@Config("httpClient")
public class HttpClientConfig {

public static final int DEFAULT_TIMEOUT_MS = 10_000;

@Valid
@JsonProperty("port")
private int port = 80;
Expand All @@ -33,6 +35,7 @@ public class HttpClientConfig {
private int maxConnections = 1000;

private String url;
private String root;
private InetSocketAddress devServerInfo;
@Size(min = 1)
private String devCookie;
Expand All @@ -44,10 +47,12 @@ public class HttpClientConfig {
private boolean isHttps;
private int retryCount = 3;
private int retryDelayMs = 1000;
private int timeoutMs = DEFAULT_TIMEOUT_MS;
private int readTimeoutMs = 10000;
private int poolAcquireTimeoutMs = 10000;
@JsonProperty("validateCertificates")
private boolean isValidateCertificates = true;
private boolean followRedirect = false;

private long connectionMaxIdleTimeInMs = TimeUnit.MILLISECONDS.convert(10, MINUTES);
private int numberOfConnectionFailuresAllowed = 10;
Expand Down Expand Up @@ -88,6 +93,7 @@ public void setUrl(String url) throws URISyntaxException {
}
URI uri = new URI(this.url);
setHost(uri.getHost());
setRoot(uri.getPath());

isHttps = "https".equals(uri.getScheme());
port = uri.getPort();
Expand All @@ -100,6 +106,14 @@ public void setUrl(String url) throws URISyntaxException {
}
}

public void setRoot(String root) {
this.root = root;
}

public String getRoot() {
return root;
}

public InetSocketAddress getDevServerInfo() {
return devServerInfo;
}
Expand Down Expand Up @@ -161,6 +175,14 @@ public void setHost(String host) {
}
}

public int getTimeoutMs() {
return timeoutMs;
}

public void setTimeoutMs(int timeoutMs) {
this.timeoutMs = timeoutMs;
}

public int getReadTimeoutMs() {
return readTimeoutMs;
}
Expand All @@ -177,6 +199,14 @@ public void setValidateCertificates(boolean value) {
isValidateCertificates = value;
}

public boolean isFollowRedirect() {
return followRedirect;
}

public void setFollowRedirect(boolean value) {
followRedirect = value;
}

public BasicAuthConfig getBasicAuth() {
return basicAuth;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private HttpClient buildClient(InetSocketAddress socketAddress) {
.doOnError((httpClientRequest, throwable) -> {
healthRecorder.logStatus(connectionProvider, errorCount.incrementAndGet() <= config.getNumberOfConnectionFailuresAllowed());
}, (httpClientResponse, throwable) -> { })
.followRedirect(false);
.followRedirect(config.isFollowRedirect());

if (config.isHttps()) {
return setupSsl(client, config.isValidateCertificates());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ void shouldInitializeFromConfig() {
assertThat(config.getRetryCount()).isEqualTo(15);
assertThat(config.getRetryDelayMs()).isEqualTo(200);
assertThat(config.getMaxResponseSize()).isEqualTo(512);
assertThat(config.getTimeoutMs()).isEqualTo(60 * 1000);
assertThat(config.isFollowRedirect()).isTrue();

assertThat(config.getDevCookie()).isEqualTo("TEST=123");
assertThat(config.getDevServerInfo().getHostString()).isEqualTo("mymachine");
assertThat(config.getDevServerInfo().getPort()).isEqualTo(9090);
assertThat(config.getDevHeaders()).contains(entry("Host", "localhost"));

}

@Test
Expand Down Expand Up @@ -83,6 +86,14 @@ void shouldSetPort() throws URISyntaxException {
assertThat(config.getHost()).isEqualTo("localhost");
}

@Test
void shouldSetRoot() throws URISyntaxException {
assertThat(new HttpClientConfig("http://localhost:8080").getRoot()).isEqualTo("");
assertThat(new HttpClientConfig("http://localhost:8080/").getRoot()).isEqualTo("/");
assertThat(new HttpClientConfig("http://localhost:8080/root/path").getRoot()).isEqualTo("/root/path");
assertThat(new HttpClientConfig("http://localhost:8080/root/path/").getRoot()).isEqualTo("/root/path/");
}

@Test
void shouldSetHttpsFromProtocolEvenIfPortIsSupplied() throws URISyntaxException {
HttpClientConfig config = new HttpClientConfig("https://localhost:8080");
Expand Down Expand Up @@ -121,4 +132,16 @@ void shouldNotBeInsecureByDefault() throws URISyntaxException {
assertThat(httpClientConfig.isHttps()).isTrue();
assertThat(httpClientConfig.isValidateCertificates()).isTrue();
}

@Test
void shouldNotFollowRedirectByDefault() throws URISyntaxException {
HttpClientConfig httpClientConfig = new HttpClientConfig("http://example.com");
assertThat(httpClientConfig.isFollowRedirect()).isFalse();
}

@Test
void shouldDefaultToTenSecondTimeout() throws URISyntaxException {
HttpClientConfig httpClientConfig = new HttpClientConfig("http://example.com");
assertThat(httpClientConfig.getTimeoutMs()).isEqualTo(10 * 1000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,23 @@ protected JaxRsMeta getJaxRsMeta(Method method) {
assertThat(jaxRsMetas.get(2)).isSameAs(jaxRsMetas.get(3));
}

@Test
void shouldAddRootToRequestUri() throws URISyntaxException, NoSuchMethodException {

assertAddedRoot("localhost", "/hello");
assertAddedRoot("localhost/", "/hello");
assertAddedRoot("localhost/root", "/root/hello");
assertAddedRoot("localhost/root/", "/root/hello");
}

private void assertAddedRoot(String url, String expected) throws URISyntaxException, NoSuchMethodException {
HttpClient httpClient = new HttpClient(new HttpClientConfig(url));

Method getHello = TestResource.class.getMethod("getHello");

assertThat(httpClient.createRequest(getHello, new Object[0]).getUri()).isEqualTo(expected);
}

@Test
void shouldRetryIfEmptyReturnedOnGet() {

Expand Down
2 changes: 2 additions & 0 deletions client/src/test/resources/httpconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ httpClient:
retryCount: 15
retryDelayMs: 200
maxResponseSize: 512
timeoutMs: 60000
followRedirect: true

0 comments on commit 8a36888

Please sign in to comment.