Search in sources :

Example 6 with ServerRequest

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

the class WebSecurityQueryParamTest method testQueryParams.

@Test
public void testQueryParams() {
    SecurityHandler securityHandler = SecurityHandler.create().queryParam("jwt", TokenHandler.builder().tokenHeader("BEARER_TOKEN").tokenPattern(Pattern.compile("bearer (.*)")).build()).queryParam("name", TokenHandler.builder().tokenHeader("NAME_FROM_REQUEST").build());
    ServerRequest req = Mockito.mock(ServerRequest.class);
    Parameters params = Mockito.mock(Parameters.class);
    when(params.all("jwt")).thenReturn(List.of("bearer jwt_content"));
    when(params.all("name")).thenReturn(List.of("name_content"));
    when(req.queryParams()).thenReturn(params);
    SecurityContext context = Mockito.mock(SecurityContext.class);
    SecurityEnvironment env = SecurityEnvironment.create();
    when(context.env()).thenReturn(env);
    // context is a stub
    securityHandler.extractQueryParams(context, req);
    // captor captures the argument
    ArgumentCaptor<SecurityEnvironment> newHeaders = ArgumentCaptor.forClass(SecurityEnvironment.class);
    verify(context).env(newHeaders.capture());
    // now validate the value we were called with
    env = newHeaders.getValue();
    assertThat(env.headers().get("BEARER_TOKEN"), is(List.of("jwt_content")));
    assertThat(env.headers().get("NAME_FROM_REQUEST"), is(List.of("name_content")));
}
Also used : Parameters(io.helidon.common.http.Parameters) SecurityEnvironment(io.helidon.security.SecurityEnvironment) SecurityContext(io.helidon.security.SecurityContext) ServerRequest(io.helidon.webserver.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 7 with ServerRequest

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

the class TransactionUpdateService method executeTest.

// Common test execution code
private JsonObject executeTest(final ServerRequest request, final ServerResponse response, final String testName, final TestFunction test) {
    LOGGER.fine(() -> String.format("Running SimpleUpdateService.%s on server", testName));
    try {
        String name = param(request, QUERY_NAME_PARAM);
        String idStr = param(request, QUERY_ID_PARAM);
        int id = Integer.parseInt(idStr);
        Pokemon srcPokemon = Pokemon.POKEMONS.get(id);
        Pokemon updatedPokemon = new Pokemon(id, name, srcPokemon.getTypesArray());
        test.apply(updatedPokemon).thenAccept(result -> response.send(AppResponse.okStatus(Json.createValue(result)))).exceptionally(t -> {
            response.send(exceptionStatus(t));
            return null;
        });
    } catch (RemoteTestException | NumberFormatException ex) {
        LOGGER.fine(() -> String.format("Error in SimpleUpdateService.%s on server", testName));
        response.send(exceptionStatus(ex));
    }
    return null;
}
Also used : AbstractService(io.helidon.tests.integration.dbclient.appl.AbstractService) Logger(java.util.logging.Logger) Function(java.util.function.Function) Json(jakarta.json.Json) ServerRequest(io.helidon.webserver.ServerRequest) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException) AppResponse.exceptionStatus(io.helidon.tests.integration.tools.service.AppResponse.exceptionStatus) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) AppResponse(io.helidon.tests.integration.tools.service.AppResponse) Map(java.util.Map) JsonObject(jakarta.json.JsonObject) ServerResponse(io.helidon.webserver.ServerResponse) Single(io.helidon.common.reactive.Single) DbClient(io.helidon.dbclient.DbClient) Routing(io.helidon.webserver.Routing) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 8 with ServerRequest

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

the class InitService method testInitPokemonTypes.

// Initialize pokemon types relation
private void testInitPokemonTypes(final ServerRequest request, final ServerResponse response) {
    LOGGER.fine(() -> "Running InitResource.testInitPokemonTypes on server");
    sendDmlResponse(response, () -> dbClient.inTransaction(tx -> {
        Single<Long> stage = null;
        for (Map.Entry<Integer, Pokemon> entry : Pokemon.POKEMONS.entrySet()) {
            Pokemon pokemon = entry.getValue();
            for (Type type : pokemon.getTypes()) {
                if (stage == null) {
                    stage = tx.namedDml("insert-poketype", pokemon.getId(), type.getId());
                } else {
                    stage = stage.flatMapSingle(result -> tx.namedDml("insert-poketype", pokemon.getId(), type.getId()));
                }
            }
        }
        return stage;
    }).toCompletableFuture());
}
Also used : DbClientHealthCheck(io.helidon.dbclient.health.DbClientHealthCheck) Config(io.helidon.config.Config) CompletableFuture(java.util.concurrent.CompletableFuture) Logger(java.util.logging.Logger) Supplier(java.util.function.Supplier) Json(jakarta.json.Json) ServerRequest(io.helidon.webserver.ServerRequest) Type(io.helidon.tests.integration.dbclient.appl.model.Type) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) AppResponse.exceptionStatus(io.helidon.tests.integration.tools.service.AppResponse.exceptionStatus) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) Map(java.util.Map) ServerResponse(io.helidon.webserver.ServerResponse) Single(io.helidon.common.reactive.Single) AppResponse.okStatus(io.helidon.tests.integration.tools.service.AppResponse.okStatus) Service(io.helidon.webserver.Service) DbClient(io.helidon.dbclient.DbClient) Routing(io.helidon.webserver.Routing) HealthCheck(org.eclipse.microprofile.health.HealthCheck) Type(io.helidon.tests.integration.dbclient.appl.model.Type) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) Map(java.util.Map)

Example 9 with ServerRequest

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

the class SimpleInsertService method executeTest.

// Common test execution code
private void executeTest(final ServerRequest request, final ServerResponse response, final String testName, final String pokemonName, final List<Type> pokemonTypes, final TestFunction test) {
    LOGGER.fine(() -> String.format("Running SimpleInsertService.%s on server", testName));
    try {
        String idStr = param(request, QUERY_ID_PARAM);
        int id = Integer.parseInt(idStr);
        Pokemon pokemon = new Pokemon(id, pokemonName, pokemonTypes);
        test.apply(pokemon).thenAccept(result -> response.send(AppResponse.okStatus(pokemon.toJsonObject()))).exceptionally(t -> {
            response.send(AppResponse.exceptionStatus(t));
            return null;
        });
    } catch (RemoteTestException | NumberFormatException ex) {
        LOGGER.fine(() -> String.format("Error in SimpleInsertService.%s on server", testName));
        response.send(AppResponse.exceptionStatus(ex));
    }
}
Also used : TYPES(io.helidon.tests.integration.dbclient.appl.model.Type.TYPES) AbstractService(io.helidon.tests.integration.dbclient.appl.AbstractService) Logger(java.util.logging.Logger) Function(java.util.function.Function) ServerRequest(io.helidon.webserver.ServerRequest) Type(io.helidon.tests.integration.dbclient.appl.model.Type) List(java.util.List) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) AppResponse(io.helidon.tests.integration.tools.service.AppResponse) Map(java.util.Map) ServerResponse(io.helidon.webserver.ServerResponse) Single(io.helidon.common.reactive.Single) DbClient(io.helidon.dbclient.DbClient) Routing(io.helidon.webserver.Routing) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 10 with ServerRequest

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

the class GreetService method basicAuthOutbound.

private void basicAuthOutbound(ServerRequest serverRequest, ServerResponse response) {
    WebClient webClient = WebClient.builder().baseUri("http://localhost:" + Main.serverPort + "/greet/secure/basic").addService(WebClientSecurity.create()).build();
    webClient.get().request().thenAccept(clientResponse -> {
        response.status(clientResponse.status());
        response.send(clientResponse.content());
    }).exceptionally(throwable -> {
        response.status(Http.Status.INTERNAL_SERVER_ERROR_500);
        response.send();
        return null;
    });
}
Also used : WebClient(io.helidon.webclient.WebClient) DataChunk(io.helidon.common.http.DataChunk) Context(io.helidon.common.context.Context) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Level(java.util.logging.Level) FormParams(io.helidon.common.http.FormParams) ServerResponse(io.helidon.webserver.ServerResponse) JsonObject(jakarta.json.JsonObject) Service(io.helidon.webserver.Service) JsonException(jakarta.json.JsonException) Http(io.helidon.common.http.Http) Multi(io.helidon.common.reactive.Multi) Config(io.helidon.config.Config) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) Logger(java.util.logging.Logger) Contexts(io.helidon.common.context.Contexts) Executors(java.util.concurrent.Executors) ServerRequest(io.helidon.webserver.ServerRequest) Json(jakarta.json.Json) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Principal(java.security.Principal) Optional(java.util.Optional) Routing(io.helidon.webserver.Routing) Collections(java.util.Collections) WebClient(io.helidon.webclient.WebClient)

Aggregations

ServerRequest (io.helidon.webserver.ServerRequest)38 ServerResponse (io.helidon.webserver.ServerResponse)35 Routing (io.helidon.webserver.Routing)23 Logger (java.util.logging.Logger)17 JsonObject (jakarta.json.JsonObject)13 Config (io.helidon.config.Config)12 Map (java.util.Map)12 Service (io.helidon.webserver.Service)11 Json (jakarta.json.Json)11 Optional (java.util.Optional)11 Test (org.junit.jupiter.api.Test)11 Single (io.helidon.common.reactive.Single)10 DbClient (io.helidon.dbclient.DbClient)10 SecurityContext (io.helidon.security.SecurityContext)9 Pokemon (io.helidon.tests.integration.dbclient.appl.model.Pokemon)9 AppResponse (io.helidon.tests.integration.tools.service.AppResponse)9 RemoteTestException (io.helidon.tests.integration.tools.service.RemoteTestException)9 List (java.util.List)9 Http (io.helidon.common.http.Http)8 AbstractService (io.helidon.tests.integration.dbclient.appl.AbstractService)8