io.vertx.up.uca.options.HttpServerVisitor Maven / Gradle / Ivy
package io.vertx.up.uca.options;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.up.atom.Ruler;
import io.vertx.up.eon.Info;
import io.vertx.up.eon.Plugins;
import io.vertx.up.eon.em.ServerType;
import io.vertx.up.exception.ZeroException;
import io.vertx.up.exception.demon.ServerConfigException;
import io.vertx.up.fn.Fn;
import io.vertx.up.log.Annal;
import io.vertx.up.uca.marshal.HttpServerStrada;
import io.vertx.up.uca.marshal.Transformer;
import io.vertx.up.uca.yaml.Node;
import io.vertx.up.util.Ut;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @author lang
* Http options only, it's standard
*/
public class HttpServerVisitor implements ServerVisitor {
protected transient final Transformer
transformer = Ut.singleton(HttpServerStrada.class);
private transient final Node NODE = Node.infix(Plugins.SERVER);
/**
* @return Server config to generate HttpServerOptions by port
* @throws ZeroException ServerConfigException
*/
@Override
public ConcurrentMap visit(final String... key)
throws ZeroException {
// 1. Must be the first line, fixed position.
Ut.ensureEqualLength(getClass(), 0, (Object[]) key);
// 2. Visit the node for server, http
final JsonObject data = NODE.read();
Fn.outZero(null == data || !data.containsKey(KEY), getLogger(),
ServerConfigException.class,
getClass(), null == data ? null : data.encode());
return visit(data.getJsonArray(KEY));
}
private ConcurrentMap visit(final JsonArray serverData)
throws ZeroException {
getLogger().info(Info.INF_B_VERIFY, KEY, getType(), serverData.encode());
Ruler.verify(KEY, serverData);
final ConcurrentMap map =
new ConcurrentHashMap<>();
extract(serverData, map);
getLogger().info(Info.INF_A_VERIFY, KEY, getType(), map.keySet());
return map;
}
protected void extract(final JsonArray serverData, final ConcurrentMap map) {
Ut.itJArray(serverData, JsonObject.class, (item, index) -> {
if (isServer(item)) {
// 1. Extract port
final int port = extractPort(item.getJsonObject(YKEY_CONFIG));
// 2. Convert JsonObject to HttpServerOptions
final HttpServerOptions options = transformer.transform(item);
Fn.safeNull(() -> {
// 3. Add to map;
map.put(port, options);
}, port, options);
}
});
}
protected boolean isServer(final JsonObject item) {
return getType().match(item.getString(YKEY_TYPE));
}
protected int extractPort(final JsonObject config) {
if (null != config) {
return config.getInteger("port", HttpServerOptions.DEFAULT_PORT);
}
return HttpServerOptions.DEFAULT_PORT;
}
protected ServerType getType() {
return ServerType.HTTP;
}
protected Annal getLogger() {
return Annal.get(getClass());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy