Search in sources :

Example 61 with Routing

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);
}
Also used : OidcConfig(io.helidon.security.providers.oidc.common.OidcConfig) Config(io.helidon.config.Config) OidcConfig(io.helidon.security.providers.oidc.common.OidcConfig) LogConfig(io.helidon.common.LogConfig) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing) Security(io.helidon.security.Security) WebSecurity(io.helidon.security.integration.webserver.WebSecurity)

Example 62 with 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();
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 63 with Routing

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();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) CoreMatchers.is(org.hamcrest.CoreMatchers.is) CountDownLatch(java.util.concurrent.CountDownLatch) Security(io.helidon.security.Security) Config(io.helidon.config.Config) BeforeAll(org.junit.jupiter.api.BeforeAll) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) SecurityContext(io.helidon.security.SecurityContext) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Routing(io.helidon.webserver.Routing) MediaType(io.helidon.common.http.MediaType) 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 64 with Routing

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)));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Security(io.helidon.security.Security) ForkJoinPool(java.util.concurrent.ForkJoinPool) TestClient(io.helidon.webserver.testsupport.TestClient) CoreMatchers.not(org.hamcrest.CoreMatchers.not) TimeoutException(java.util.concurrent.TimeoutException) SecurityContext(io.helidon.security.SecurityContext) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Routing(io.helidon.webserver.Routing) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Routing(io.helidon.webserver.Routing) AtomicReference(java.util.concurrent.atomic.AtomicReference) Security(io.helidon.security.Security) Test(org.junit.jupiter.api.Test)

Example 65 with Routing

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)));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) JsonObject(jakarta.json.JsonObject) 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