Skip to content
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

[Issue-102] Added support of HTTP headers, timeout, followRedirects to Http Configuration store #103

Merged
merged 1 commit into from
Jan 23, 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
4 changes: 3 additions & 1 deletion vertx-config/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ any supported format.
It creates a Vert.x HTTP Client with the store configuration (see next snippet). To
ease the configuration; you can also configure the `host`, `port` and `path` with the
`host`, `port` and `path`
properties.
properties. You can also configure optional HTTP request headers with `headers` property,
the timeout (in milliseconds, 3000 by default) to retrieve the configuration with `timeout` property,
if following redirects (false by default) with `followRedirects` property.

[source, $lang]
----
Expand Down
3 changes: 2 additions & 1 deletion vertx-config/src/main/java/examples/ConfigExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public void http2() {
.put("defaultHost", "localhost")
.put("defaultPort", 8080)
.put("ssl", true)
.put("path", "/A"));
.put("path", "/A")
.put("headers", new JsonObject().put("Accept", "application/json")));
}

public void eb() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.json.JsonObject;

/**
* A configuration store retrieving the configuration from a HTTP location
Expand All @@ -32,21 +36,30 @@
*/
public class HttpConfigStore implements ConfigStore {

private final String host;
private final int port;
private final String path;
private final HttpClient client;
private final RequestOptions requestOptions;

public HttpConfigStore(String host, int port, String path, HttpClient client) {
this.host = host;
this.port = port;
this.path = path;
this.client = client;
public HttpConfigStore(Vertx vertx, JsonObject configuration) {
String host = configuration.getString("host");
vietj marked this conversation as resolved.
Show resolved Hide resolved
int port = configuration.getInteger("port", 80);
String path = configuration.getString("path", "/");
long timeout = configuration.getLong("timeout", 3000L);
boolean followRedirects = configuration.getBoolean("followRedirects", false);
this.client = vertx.createHttpClient(new HttpClientOptions(configuration));
this.requestOptions = new RequestOptions()
.setHost(host)
.setPort(port)
.setURI(path)
.setTimeout(timeout)
.setFollowRedirects(followRedirects);
configuration.getJsonObject("headers", new JsonObject()).stream()
.filter(h -> h.getValue() != null)
.forEach(h -> requestOptions.addHeader(h.getKey(), h.getValue().toString()));
}

@Override
public void get(Handler<AsyncResult<Buffer>> completionHandler) {
client.get(port, host, path, ar -> {
client.get(requestOptions, ar -> {
if (ar.succeeded()) {
HttpClientResponse response = ar.result();
response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
package io.vertx.config.impl.spi;

import io.vertx.config.spi.ConfigStore;
import io.vertx.config.spi.ConfigStoreFactory;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.config.spi.ConfigStoreFactory;

import java.util.Objects;

/**
* The factory creating Json File configuration stores.
Expand All @@ -40,13 +36,6 @@ public String name() {

@Override
public ConfigStore create(Vertx vertx, JsonObject configuration) {
HttpClient client = vertx.createHttpClient(new HttpClientOptions(configuration));
String host = configuration.getString("host");
int port = configuration.getInteger("port", 80);
String path = configuration.getString("path", "/");

Objects.requireNonNull(host);

return new HttpConfigStore(host, port, path, client);
return new HttpConfigStore(vertx, configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ public void init() {
// Properties
request.response().end("#some properties\nfoo=bar\nkey=value");
}
if (request.path().endsWith("/D")) {
if (!"application/json".equalsIgnoreCase(request.getHeader("Accept"))) {
request.response().setStatusCode(406).setStatusMessage("Not Acceptable").end();
} else {
request.response().end(new JsonObject(JSON).encodePrettily());
}
}
if (request.path().endsWith("/E")) {
// not found
request.response().setStatusCode(404).setStatusMessage("Not Found").end();
}
if (request.path().endsWith("/F")) {
// send redirects to /A
request.response().setStatusCode(302).putHeader("Location", "/A").end();
}
})
.listen(8080, s -> {
done.set(true);
Expand Down Expand Up @@ -129,4 +144,65 @@ public void testWrongServer(TestContext tc) {
});
}

@Test
public void testJsonConfWithHeaders(TestContext tc) {
Async async = tc.async();
store = factory.create(vertx, new JsonObject()
.put("host", "localhost")
.put("port", 8080)
.put("path", "/D")
.put("headers", new JsonObject().put("Accept", "application/json"))

);

getJsonConfiguration(vertx, store, ar -> {
ConfigChecker.check(ar);
async.complete();
});
}

@Test
public void testJsonConfNotFollowingRedirects(TestContext tc) {
Async async = tc.async();
store = factory.create(vertx, new JsonObject()
.put("port", 8080)
.put("path", "/F")
.put("followRedirects", false)
);

getJsonConfiguration(vertx, store, ar -> {
assertThat(ar.failed()).isTrue();
async.complete();
});
}

@Test
public void testJsonConfFollowingRedirects(TestContext tc) {
Async async = tc.async();
store = factory.create(vertx, new JsonObject()
.put("port", 8080)
.put("path", "/F")
.put("followRedirects", true)
);

getJsonConfiguration(vertx, store, ar -> {
ConfigChecker.check(ar);
async.complete();
});
}

@Test
public void testJsonConf404(TestContext tc) {
Async async = tc.async();
store = factory.create(vertx, new JsonObject()
.put("host", "localhost")
.put("port", 8080)
.put("path", "/E")
);

getJsonConfiguration(vertx, store, ar -> {
assertThat(ar.failed()).isTrue();
async.complete();
});
}
}