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();
}
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;
}
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();
}
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));
}
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));
}
Aggregations