Search in sources :

Example 1 with ConfigValue

use of io.helidon.config.ConfigValue in project helidon by oracle.

the class ClientMain method main.

/**
 * Executes WebClient examples.
 *
 * If no argument provided it will take server port from configuration server.port.
 *
 * User can override port from configuration by main method parameter with the specific port.
 *
 * @param args main method
 */
public static void main(String[] args) {
    Config config = Config.create();
    String url;
    if (args.length == 0) {
        ConfigValue<Integer> port = config.get("server.port").asInt();
        if (!port.isPresent() || port.get() == -1) {
            throw new IllegalStateException("Unknown port! Please specify port as a main method parameter " + "or directly to config server.port");
        }
        url = "http://localhost:" + port.get() + "/greet";
    } else {
        url = "http://localhost:" + Integer.parseInt(args[0]) + "/greet";
    }
    WebClient webClient = WebClient.builder().baseUri(url).config(config.get("client")).addMediaSupport(JsonpSupport.create()).build();
    performPutMethod(webClient).flatMapSingle(it -> performGetMethod(webClient)).flatMapSingle(it -> followRedirects(webClient)).flatMapSingle(it -> getResponseAsAnJsonObject(webClient)).flatMapSingle(it -> saveResponseToFile(webClient)).flatMapSingle(it -> clientMetricsExample(url, config)).await();
}
Also used : Arrays(java.util.Arrays) WebClient(io.helidon.webclient.WebClient) WebClientService(io.helidon.webclient.spi.WebClientService) Files(java.nio.file.Files) IoMulti(io.helidon.common.reactive.IoMulti) Config(io.helidon.config.Config) WebClientResponse(io.helidon.webclient.WebClientResponse) DataChunk(io.helidon.common.http.DataChunk) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) IOException(java.io.IOException) ConfigValue(io.helidon.config.ConfigValue) Json(jakarta.json.Json) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Counter(org.eclipse.microprofile.metrics.Counter) Paths(java.nio.file.Paths) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Http(io.helidon.common.http.Http) Path(java.nio.file.Path) Collections(java.util.Collections) RegistryFactory(io.helidon.metrics.RegistryFactory) WebClientMetrics(io.helidon.webclient.metrics.WebClientMetrics) Config(io.helidon.config.Config) WebClient(io.helidon.webclient.WebClient)

Example 2 with ConfigValue

use of io.helidon.config.ConfigValue in project helidon by oracle.

the class WithSourcesExample method main.

/**
 * Executes the example.
 *
 * @param args arguments
 */
public static void main(String... args) {
    /*
           Creates a config source composed of following sources:
           - conf/dev.yaml - developer specific configuration, should not be placed in VCS;
           - conf/config.yaml - deployment dependent configuration, for example prod, stage, etc;
           - default.yaml - application default values, loaded form classpath;
           with a filter which convert values with keys ending with "level" to upper case
         */
    Config config = Config.builder(file("conf/dev.yaml").optional(), file("conf/config.yaml").optional(), classpath("default.yaml")).addFilter((key, stringValue) -> key.name().equals("level") ? stringValue.toUpperCase() : stringValue).build();
    // Environment type, from dev.yaml:
    ConfigValue<String> env = config.get("meta.env").asString();
    env.ifPresent(e -> System.out.println("Environment: " + e));
    assert env.get().equals("DEV");
    // Default value (default.yaml): Config Sources Example
    String appName = config.get("app.name").asString().get();
    System.out.println("Name: " + appName);
    assert appName.equals("Config Sources Example");
    // Page size, from config.yaml: 10
    int pageSize = config.get("app.page-size").asInt().get();
    System.out.println("Page size: " + pageSize);
    assert pageSize == 10;
    // Applied filter (uppercase logging level), from dev.yaml: finest -> FINEST
    String level = config.get("component.audit.logging.level").asString().get();
    System.out.println("Level: " + level);
    assert level.equals("FINE");
}
Also used : ConfigSources.file(io.helidon.config.ConfigSources.file) Config(io.helidon.config.Config) ConfigValue(io.helidon.config.ConfigValue) ConfigSources.classpath(io.helidon.config.ConfigSources.classpath) Config(io.helidon.config.Config)

Example 3 with ConfigValue

use of io.helidon.config.ConfigValue in project helidon by oracle.

the class WebSecurity method registerRouting.

private void registerRouting(Routing.Rules routing) {
    Config wsConfig = config.get("web-server");
    SecurityHandler defaults = SecurityHandler.create(wsConfig.get("defaults"), defaultHandler);
    wsConfig.get("paths").asNodeList().ifPresent(configs -> {
        for (Config pathConfig : configs) {
            List<Http.RequestMethod> methods = pathConfig.get("methods").asNodeList().orElse(List.of()).stream().map(Config::asString).map(ConfigValue::get).map(Http.RequestMethod::create).collect(Collectors.toList());
            String path = pathConfig.get("path").asString().orElseThrow(() -> new SecurityException(pathConfig.key() + " must contain path key with a path to " + "register to web server"));
            if (methods.isEmpty()) {
                routing.any(path, SecurityHandler.create(pathConfig, defaults));
            } else {
                routing.anyOf(methods, path, SecurityHandler.create(pathConfig, defaults));
            }
        }
    });
}
Also used : ConfigValue(io.helidon.config.ConfigValue) Config(io.helidon.config.Config) EndpointConfig(io.helidon.security.EndpointConfig) Http(io.helidon.common.http.Http)

Example 4 with ConfigValue

use of io.helidon.config.ConfigValue in project helidon by oracle.

the class LoadSourcesExample method main.

/**
 * Executes the example.
 *
 * @param args arguments
 */
public static void main(String... args) {
    /*
           Creates a configuration from list of config sources loaded from meta sources:
           - conf/meta-config.yaml - - deployment dependent meta-config file, loaded from file on filesystem;
           - meta-config.yaml - application default meta-config file, loaded form classpath;
           with a filter which convert values with keys ending with "level" to upper case
         */
    Config metaConfig = Config.create(file("conf/meta-config.yaml").optional(), classpath("meta-config.yaml"));
    Config config = Config.builder().config(metaConfig).addFilter((key, stringValue) -> key.name().equals("level") ? stringValue.toUpperCase() : stringValue).build();
    // Optional environment type, from dev.yaml:
    ConfigValue<String> env = config.get("meta.env").asString();
    env.ifPresent(e -> System.out.println("Environment: " + e));
    assert env.get().equals("DEV");
    // Default value (default.yaml): Config Sources Example
    String appName = config.get("app.name").asString().get();
    System.out.println("Name: " + appName);
    assert appName.equals("Config Sources Example");
    // Page size, from config.yaml: 10
    int pageSize = config.get("app.page-size").asInt().get();
    System.out.println("Page size: " + pageSize);
    assert pageSize == 10;
    // Applied filter (uppercase logging level), from dev.yaml: finest -> FINEST
    String level = config.get("component.audit.logging.level").asString().get();
    System.out.println("Level: " + level);
    assert level.equals("FINE");
}
Also used : ConfigSources.file(io.helidon.config.ConfigSources.file) Config(io.helidon.config.Config) ConfigValue(io.helidon.config.ConfigValue) ConfigSources.classpath(io.helidon.config.ConfigSources.classpath) Config(io.helidon.config.Config)

Aggregations

Config (io.helidon.config.Config)4 ConfigValue (io.helidon.config.ConfigValue)4 Http (io.helidon.common.http.Http)2 ConfigSources.classpath (io.helidon.config.ConfigSources.classpath)2 ConfigSources.file (io.helidon.config.ConfigSources.file)2 DataChunk (io.helidon.common.http.DataChunk)1 IoMulti (io.helidon.common.reactive.IoMulti)1 Single (io.helidon.common.reactive.Single)1 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)1 RegistryFactory (io.helidon.metrics.RegistryFactory)1 EndpointConfig (io.helidon.security.EndpointConfig)1 WebClient (io.helidon.webclient.WebClient)1 WebClientResponse (io.helidon.webclient.WebClientResponse)1 WebClientMetrics (io.helidon.webclient.metrics.WebClientMetrics)1 WebClientService (io.helidon.webclient.spi.WebClientService)1 Json (jakarta.json.Json)1 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)1 JsonObject (jakarta.json.JsonObject)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1