use of io.helidon.webserver.Routing in project helidon by oracle.
the class IdcsBuilderMain method main.
/**
* Start the example.
*
* @param args ignored
* @throws IOException if logging configuration fails
*/
public static void main(String[] args) throws IOException {
// load logging configuration
LogConfig.configureRuntime();
Config config = buildConfig();
OidcConfig oidcConfig = OidcConfig.builder().clientId("clientId.of.your.application").clientSecret("clientSecret.of.your.application").identityUri(URI.create("https://idcs-tenant-id.identity.oracle.com")).frontendUri("http://your.host:your.port").serverType("idcs").build();
Security security = Security.builder().addProvider(OidcProvider.create(oidcConfig)).addProvider(IdcsRoleMapperProvider.builder().config(config).oidcConfig(oidcConfig)).build();
Routing.Builder routing = Routing.builder().register(WebSecurity.create(security, config.get("security"))).register(OidcSupport.create(config)).get("/rest/profile", (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"));
});
theServer = IdcsUtil.startIt(routing);
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class TestMinimalMetricsSupport method testEndpoint.
@Test
void testEndpoint() throws ExecutionException, InterruptedException {
MetricsSupport metricsSupport = MetricsSupport.create(MetricsSettings.create(), RestServiceSettings.builder().webContext("/metrics").build());
Routing routing = Routing.builder().register(metricsSupport).build();
WebServer webServer = null;
try {
webServer = WebServer.builder().host("localhost").port(0).routing(routing).build().start().toCompletableFuture().get();
WebClientResponse webClientResponse = WebClient.builder().baseUri("http://localhost:" + webServer.port()).get().get().path("/metrics").request().get();
assertThat("Response code from /metrics endpoint", webClientResponse.status().code(), is(404));
assertThat("Response text from /metrics endpoint", webClientResponse.content().as(String.class).get(), is(equalTo(MinimalMetricsSupport.DISABLED_ENDPOINT_MESSAGE)));
} finally {
if (webServer != null) {
webServer.shutdown().get();
}
}
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class WebSecurityFromConfigTest method initClass.
@BeforeAll
public static void initClass() throws InterruptedException {
WebSecurityTestUtil.auditLogFinest();
myAuditProvider = new UnitTestAuditProvider();
Config securityConfig = Config.create().get("security");
Security security = Security.builder(securityConfig).addAuditProvider(myAuditProvider).build();
Routing routing = Routing.builder().register(WebSecurity.create(security, securityConfig)).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 WebSecurityTest method testExecutorService.
@Test
public void testExecutorService() throws TimeoutException, InterruptedException {
Security security = Security.builder().build();
AtomicReference<Class> execClassHolder = new AtomicReference<>();
Routing routing = Routing.builder().register(WebSecurity.create(security)).get("/unit_test", (req, res) -> {
req.context().get(SecurityContext.class).ifPresent(context -> execClassHolder.set(context.executorService().getClass()));
req.next();
}).build();
TestClient.create(routing).path("/unit_test").get();
Class execClass = execClassHolder.get();
assertThat(execClass, notNullValue());
assertThat(execClass, is(not(ForkJoinPool.class)));
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class JsonSupportTest method explicitJsonSupportRegistrationMissingJsonProperty.
@Test
public void explicitJsonSupportRegistrationMissingJsonProperty() throws Exception {
Routing routing = Routing.builder().post("/foo", Handler.create(JsonObject.class, (req, res, json) -> res.send(json))).build();
JsonObject json = createJson();
TestResponse response = TestClient.create(routing).path("/foo").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
assertThat(response.status(), is(equalTo(Http.Status.INTERNAL_SERVER_ERROR_500)));
}
Aggregations