Search in sources :

Example 26 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class MetricsSupport method setUpFullFeaturedEndpoint.

private void setUpFullFeaturedEndpoint(Routing.Rules serviceEndpointRoutingRules, io.helidon.metrics.RegistryFactory rf) {
    Registry base = rf.getARegistry(MetricRegistry.Type.BASE);
    Registry vendor = rf.getARegistry(MetricRegistry.Type.VENDOR);
    Registry app = rf.getARegistry(MetricRegistry.Type.APPLICATION);
    // routing to root of metrics
    serviceEndpointRoutingRules.get(context(), (req, res) -> getMultiple(req, res, base, app, vendor)).options(context(), (req, res) -> optionsMultiple(req, res, base, app, vendor));
    // routing to each scope
    Stream.of(app, base, vendor).forEach(registry -> {
        String type = registry.type();
        serviceEndpointRoutingRules.get(context() + "/" + type, (req, res) -> getAll(req, res, registry)).get(context() + "/" + type + "/{metric}", (req, res) -> getByName(req, res, registry)).options(context() + "/" + type, (req, res) -> optionsAll(req, res, registry)).options(context() + "/" + type + "/{metric}", (req, res) -> optionsOne(req, res, registry));
    });
}
Also used : Arrays(java.util.Arrays) JsonValue(jakarta.json.JsonValue) KeyPerformanceIndicatorSupport(io.helidon.webserver.KeyPerformanceIndicatorSupport) HelidonRestServiceSupport(io.helidon.servicecommon.rest.HelidonRestServiceSupport) DeprecatedConfig(io.helidon.config.DeprecatedConfig) MediaType(io.helidon.common.http.MediaType) BigDecimal(java.math.BigDecimal) PostRequestMetricsSupport(io.helidon.metrics.serviceapi.PostRequestMetricsSupport) Map(java.util.Map) JsonObject(jakarta.json.JsonObject) BigInteger(java.math.BigInteger) RestServiceSettings(io.helidon.servicecommon.rest.RestServiceSettings) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Set(java.util.Set) Logger(java.util.logging.Logger) MetricID(org.eclipse.microprofile.metrics.MetricID) Collectors(java.util.stream.Collectors) MetricsSettings(io.helidon.metrics.api.MetricsSettings) ServerRequest(io.helidon.webserver.ServerRequest) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Handler(io.helidon.webserver.Handler) Optional(java.util.Optional) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) JsonArray(jakarta.json.JsonArray) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) Metric(org.eclipse.microprofile.metrics.Metric) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) JsonStructure(jakarta.json.JsonStructure) BiConsumer(java.util.function.BiConsumer) ServerResponse(io.helidon.webserver.ServerResponse) Http(io.helidon.common.http.Http) MessageBodyWriter(io.helidon.media.common.MessageBodyWriter) Config(io.helidon.config.Config) MinimalMetricsSupport(io.helidon.metrics.serviceapi.MinimalMetricsSupport) RequestHeaders(io.helidon.webserver.RequestHeaders) Json(jakarta.json.Json) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) KeyPerformanceIndicatorMetricsSettings(io.helidon.metrics.api.KeyPerformanceIndicatorMetricsSettings) RegistryFactory(io.helidon.metrics.api.RegistryFactory) SystemTagsManager(io.helidon.metrics.api.SystemTagsManager) Comparator(java.util.Comparator) Routing(io.helidon.webserver.Routing) Collections(java.util.Collections) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry)

Example 27 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class Server method main.

/**
 * The main program entry point.
 *
 * @param args  the program arguments
 */
public static void main(String[] args) {
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    // load logging configuration
    LogConfig.configureRuntime();
    // Get gRPC server config from the "grpc" section of application.yaml
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder(config.get("grpc")).build();
    GrpcServer grpcServer = GrpcServer.create(serverConfig, createRouting(config));
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    grpcServer.start().thenAccept(s -> {
        System.out.println("gRPC server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    // add support for standard and gRPC health checks
    HealthSupport health = HealthSupport.builder().addLiveness(HealthChecks.healthChecks()).addLiveness(grpcServer.healthChecks()).build();
    // start web server with health endpoint
    Routing routing = Routing.builder().register(health).build();
    WebServer.create(routing, config.get("webserver")).start().thenAccept(s -> {
        System.out.println("HTTP server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("HTTP server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
}
Also used : StringService(io.helidon.grpc.examples.common.StringService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) GreetServiceJava(io.helidon.grpc.examples.common.GreetServiceJava) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcServer(io.helidon.grpc.server.GrpcServer)

Example 28 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class BasicExampleBuilderMain method startServer.

static WebServer startServer() {
    LogConfig.initClass();
    Routing routing = Routing.builder().register(buildWebSecurity().securityDefaults(WebSecurity.authenticate())).any("/static[/{*}]", WebSecurity.rolesAllowed("user")).register("/static", StaticContentSupport.create("/WEB")).get("/noRoles", WebSecurity.enforce()).get("/user[/{*}]", WebSecurity.rolesAllowed("user")).get("/admin", WebSecurity.rolesAllowed("admin")).get("/deny", WebSecurity.rolesAllowed("deny").audit()).any("/noAuthn", WebSecurity.rolesAllowed("admin").authenticationOptional().audit()).get("/{*}", (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Hello, you are: \n" + securityContext.map(ctx -> ctx.user().orElse(SecurityContext.ANONYMOUS).toString()).orElse("Security context is null"));
    }).build();
    return WebServer.builder().routing(routing).build().start().await(10, TimeUnit.SECONDS);
}
Also used : Arrays(java.util.Arrays) Security(io.helidon.security.Security) Set(java.util.Set) HashMap(java.util.HashMap) SecurityContext(io.helidon.security.SecurityContext) MediaType(io.helidon.common.http.MediaType) TimeUnit(java.util.concurrent.TimeUnit) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Map(java.util.Map) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) SecureUserStore(io.helidon.security.providers.httpauth.SecureUserStore) Optional(java.util.Optional) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing)

Example 29 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class BasicExampleConfigMain method startServer.

static WebServer startServer() {
    LogConfig.initClass();
    Config config = Config.create();
    Routing routing = Routing.builder().register(WebSecurity.create(config.get("security"))).register("/static", StaticContentSupport.create("/WEB")).get("/{*}", (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Hello, you are: \n" + securityContext.map(ctx -> ctx.user().orElse(SecurityContext.ANONYMOUS).toString()).orElse("Security context is null"));
    }).build();
    return WebServer.builder().config(config.get("server")).routing(routing).build().start().await(10, TimeUnit.SECONDS);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) SecurityContext(io.helidon.security.SecurityContext) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) MediaType(io.helidon.common.http.MediaType) Optional(java.util.Optional) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing)

Example 30 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class GoogleBuilderMain method start.

static int start(int port) {
    Security security = Security.builder().addProvider(GoogleTokenProvider.builder().clientId("your-client-id.apps.googleusercontent.com")).build();
    WebSecurity ws = WebSecurity.create(security);
    Routing.Builder routing = Routing.builder().register(ws).get("/rest/profile", WebSecurity.authenticate(), (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Response from builder based service, you are: \n" + securityContext.flatMap(SecurityContext::user).map(Subject::toString).orElse("Security context is null"));
        req.next();
    }).register(StaticContentSupport.create("/WEB"));
    theServer = GoogleUtil.startIt(port, routing);
    return theServer.port();
}
Also used : Security(io.helidon.security.Security) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) SecurityContext(io.helidon.security.SecurityContext) Subject(io.helidon.security.Subject) GoogleTokenProvider(io.helidon.security.providers.google.login.GoogleTokenProvider) Routing(io.helidon.webserver.Routing) MediaType(io.helidon.common.http.MediaType) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing) Security(io.helidon.security.Security) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Subject(io.helidon.security.Subject)

Aggregations

Routing (io.helidon.webserver.Routing)86 WebServer (io.helidon.webserver.WebServer)38 Config (io.helidon.config.Config)33 Test (org.junit.jupiter.api.Test)32 Http (io.helidon.common.http.Http)23 TimeUnit (java.util.concurrent.TimeUnit)21 LogConfig (io.helidon.common.LogConfig)19 MediaType (io.helidon.common.http.MediaType)19 CoreMatchers.is (org.hamcrest.CoreMatchers.is)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 SecurityContext (io.helidon.security.SecurityContext)16 HttpException (io.helidon.webserver.HttpException)15 Optional (java.util.Optional)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)13 SERVICE_UNAVAILABLE_503 (io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503)11 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)11 Security (io.helidon.security.Security)11 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)10 TestResponse (io.helidon.webserver.testsupport.TestResponse)10