Search in sources :

Example 36 with Routing

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

the class WebSecurityProgrammaticTest method initClass.

@BeforeAll
public static void initClass() throws InterruptedException {
    WebSecurityTestUtil.auditLogFinest();
    myAuditProvider = new UnitTestAuditProvider();
    Config config = Config.create();
    Security security = Security.builder(config.get("security")).addAuditProvider(myAuditProvider).build();
    Routing routing = Routing.builder().register(WebSecurity.create(security).securityDefaults(SecurityHandler.create().queryParam("jwt", TokenHandler.builder().tokenHeader("BEARER_TOKEN").tokenPattern(Pattern.compile("bearer (.*)")).build()).queryParam("name", TokenHandler.builder().tokenHeader("NAME_FROM_REQUEST").build()))).get("/noRoles", WebSecurity.secure()).get("/user[/{*}]", WebSecurity.rolesAllowed("user")).get("/admin", WebSecurity.rolesAllowed("admin")).get("/deny", WebSecurity.rolesAllowed("deny"), (req, res) -> {
        res.status(Http.Status.INTERNAL_SERVER_ERROR_500);
        res.send("Should not get here, this role doesn't exist");
    }).get("/auditOnly", WebSecurity.audit().auditEventType("unit_test").auditMessageFormat(AUDIT_MESSAGE_FORMAT)).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();
    server = WebServer.create(routing);
    long t = System.currentTimeMillis();
    CountDownLatch cdl = new CountDownLatch(1);
    server.start().thenAccept(webServer -> {
        long time = System.currentTimeMillis() - t;
        System.out.println("Started server on localhost:" + webServer.port() + " in " + time + " millis");
        cdl.countDown();
    });
    // we must wait for server to start, so other tests are not triggered until it is ready!
    assertThat("Timeout while waiting for server to start!", cdl.await(5, TimeUnit.SECONDS), is(true));
    baseUri = "http://localhost:" + server.port();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Security(io.helidon.security.Security) Config(io.helidon.config.Config) SecurityContext(io.helidon.security.SecurityContext) TokenHandler(io.helidon.security.util.TokenHandler) MediaType(io.helidon.common.http.MediaType) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeAll(org.junit.jupiter.api.BeforeAll) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) Optional(java.util.Optional) Config(io.helidon.config.Config) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing) Security(io.helidon.security.Security) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 37 with Routing

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

the class ServerMain method startServer.

private static WebServer startServer(final String configFile) {
    final Config config = Config.create(ConfigSources.classpath(configFile));
    final DbClient dbClient = DbClient.builder(config.get("db")).build();
    final LifeCycleService lcResource = new LifeCycleService(dbClient);
    final Routing routing = Routing.builder().register("/LifeCycle", lcResource).register("/HelloWorld", new HelloWorldService(dbClient)).build();
    final WebServer server = WebServer.builder().routing(routing).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build();
    // Set server instance to exit resource.
    lcResource.setServer(server);
    // Start the server and print some info.
    server.start().thenAccept(ws -> {
        System.out.println(String.format("WEB server is up! http://localhost:%d/", ws.port()));
    });
    // Server threads are not daemon. NO need to block. Just react.
    server.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    return server;
}
Also used : DbClient(io.helidon.dbclient.DbClient) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing)

Example 38 with Routing

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

the class Gh2631 method routing.

private static Routing routing() {
    StaticContentSupport classpath = StaticContentSupport.builder("web").welcomeFileName("index.txt").build();
    StaticContentSupport file = StaticContentSupport.builder(Paths.get("src/main/resources/web")).welcomeFileName("index.txt").build();
    return Routing.builder().register("/simple", classpath).register("/fallback", classpath).register("/fallback", StaticContentSupport.builder("fallback").pathMapper(path -> "index.txt").build()).register("/simpleFile", file).register("/fallbackFile", file).register("/fallbackFile", StaticContentSupport.builder(Paths.get("src/main/resources/fallback")).pathMapper(path -> "index.txt").build()).build();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Paths(java.nio.file.Paths) WebServer(io.helidon.webserver.WebServer) Routing(io.helidon.webserver.Routing) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport)

Example 39 with Routing

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

the class ClassPathContentHandlerTest method serveFromFilesWithWelcome.

@Test
public void serveFromFilesWithWelcome() throws Exception {
    Routing routing = Routing.builder().register(StaticContentSupport.builder("content").welcomeFileName("index.txt")).build();
    // /
    TestResponse response = TestClient.create(routing).path("/").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(filterResponse(response), is("- index TXT"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE), is(Optional.of(MediaType.TEXT_PLAIN.toString())));
    // /bar/
    response = TestClient.create(routing).path("/bar/").get();
    assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
}
Also used : Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 40 with Routing

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

the class ClassPathContentHandlerTest method resourceSlashAgnostic.

@Test
public void resourceSlashAgnostic() throws Exception {
    // Without slash
    Routing routing = Routing.builder().register("/some", StaticContentSupport.create("content")).build();
    // /some/root-a.txt
    TestResponse response = TestClient.create(routing).path("/some/root-a.txt").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    // With slash
    routing = Routing.builder().register("/some", StaticContentSupport.create("/content")).build();
    // /some/root-a.txt
    response = TestClient.create(routing).path("/some/root-a.txt").get();
    assertThat(response.status(), is(Http.Status.OK_200));
}
Also used : Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

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