Search in sources :

Example 26 with ServerRequest

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

the class VerifyService method getPokemonById.

// Get Pokemon by ID and return its data.
private void getPokemonById(final ServerRequest request, final ServerResponse response) {
    try {
        final String idStr = AbstractService.param(request, QUERY_ID_PARAM);
        final int id = Integer.parseInt(idStr);
        final JsonObjectBuilder pokemonBuilder = Json.createObjectBuilder();
        dbClient.execute(exec -> exec.namedGet("get-pokemon-by-id", id)).thenAccept(data -> data.ifPresentOrElse(row -> {
            final JsonArrayBuilder typesBuilder = Json.createArrayBuilder();
            pokemonBuilder.add("name", row.column("name").as(String.class));
            pokemonBuilder.add("id", row.column("id").as(Integer.class));
            dbClient.execute(exec -> exec.namedQuery("get-pokemon-types", id)).forEach(typeRow -> typesBuilder.add(typeRow.as(JsonObject.class))).onComplete(() -> {
                pokemonBuilder.add("types", typesBuilder.build());
                response.send(AppResponse.okStatus(pokemonBuilder.build()));
            }).exceptionally(t -> {
                response.send(exceptionStatus(t));
                return null;
            });
        }, () -> response.send(AppResponse.okStatus(JsonObject.EMPTY_JSON_OBJECT)))).exceptionally(t -> {
            response.send(exceptionStatus(t));
            return null;
        });
    } catch (RemoteTestException ex) {
        response.send(exceptionStatus(ex));
    }
}
Also used : Config(io.helidon.config.Config) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Logger(java.util.logging.Logger) QUERY_ID_PARAM(io.helidon.tests.integration.dbclient.appl.AbstractService.QUERY_ID_PARAM) Json(jakarta.json.Json) ServerRequest(io.helidon.webserver.ServerRequest) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException) AppResponse.exceptionStatus(io.helidon.tests.integration.tools.service.AppResponse.exceptionStatus) AppResponse(io.helidon.tests.integration.tools.service.AppResponse) JsonObject(jakarta.json.JsonObject) ServerResponse(io.helidon.webserver.ServerResponse) Service(io.helidon.webserver.Service) DbClient(io.helidon.dbclient.DbClient) Routing(io.helidon.webserver.Routing) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 27 with ServerRequest

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

the class MapperService method executeUpdateTest.

private void executeUpdateTest(final ServerRequest request, final ServerResponse response, final String testName, final TestDMLFunction test) {
    LOGGER.fine(() -> String.format("Running Mapper.%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 Mapper.%s on server", testName));
        response.send(exceptionStatus(ex));
    }
}
Also used : TYPES(io.helidon.tests.integration.dbclient.appl.model.Type.TYPES) AbstractService(io.helidon.tests.integration.dbclient.appl.AbstractService) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Logger(java.util.logging.Logger) Function(java.util.function.Function) Json(jakarta.json.Json) 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) 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) DbRow(io.helidon.dbclient.DbRow) Map(java.util.Map) JsonObject(jakarta.json.JsonObject) ServerResponse(io.helidon.webserver.ServerResponse) Optional(java.util.Optional) Single(io.helidon.common.reactive.Single) LinkedList(java.util.LinkedList) DbClient(io.helidon.dbclient.DbClient) Routing(io.helidon.webserver.Routing) Multi(io.helidon.common.reactive.Multi) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 28 with ServerRequest

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

the class MapperService method executeInsertTest.

private void executeInsertTest(final ServerRequest request, final ServerResponse response, final String testName, final String pokemonName, final List<Type> pokemonTypes, final TestDMLFunction test) {
    LOGGER.fine(() -> String.format("Running Mapper.%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 Mapper.%s on server", testName));
        response.send(exceptionStatus(ex));
    }
}
Also used : TYPES(io.helidon.tests.integration.dbclient.appl.model.Type.TYPES) AbstractService(io.helidon.tests.integration.dbclient.appl.AbstractService) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Logger(java.util.logging.Logger) Function(java.util.function.Function) Json(jakarta.json.Json) 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) 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) DbRow(io.helidon.dbclient.DbRow) Map(java.util.Map) JsonObject(jakarta.json.JsonObject) ServerResponse(io.helidon.webserver.ServerResponse) Optional(java.util.Optional) Single(io.helidon.common.reactive.Single) LinkedList(java.util.LinkedList) DbClient(io.helidon.dbclient.DbClient) Routing(io.helidon.webserver.Routing) Multi(io.helidon.common.reactive.Multi) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 29 with ServerRequest

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

the class MapperService method executeDeleteTest.

private void executeDeleteTest(final ServerRequest request, final ServerResponse response, final String testName, final TestDMLFunction test) {
    LOGGER.fine(() -> String.format("Running Mapper.%s on server", testName));
    try {
        String idStr = param(request, QUERY_ID_PARAM);
        int id = Integer.parseInt(idStr);
        Pokemon pokemon = Pokemon.POKEMONS.get(id);
        test.apply(pokemon).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 Mapper.%s on server", testName));
        response.send(exceptionStatus(ex));
    }
}
Also used : TYPES(io.helidon.tests.integration.dbclient.appl.model.Type.TYPES) AbstractService(io.helidon.tests.integration.dbclient.appl.AbstractService) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Logger(java.util.logging.Logger) Function(java.util.function.Function) Json(jakarta.json.Json) 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) 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) DbRow(io.helidon.dbclient.DbRow) Map(java.util.Map) JsonObject(jakarta.json.JsonObject) ServerResponse(io.helidon.webserver.ServerResponse) Optional(java.util.Optional) Single(io.helidon.common.reactive.Single) LinkedList(java.util.LinkedList) DbClient(io.helidon.dbclient.DbClient) Routing(io.helidon.webserver.Routing) Multi(io.helidon.common.reactive.Multi) Pokemon(io.helidon.tests.integration.dbclient.appl.model.Pokemon) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 30 with ServerRequest

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

the class TransactionInsertService method executeTest.

// Common test execution code
private JsonObject 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));
    }
    return null;
}
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) 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)

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