Search in sources :

Example 6 with ServerResponse

use of io.helidon.webserver.ServerResponse 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 7 with ServerResponse

use of io.helidon.webserver.ServerResponse 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 8 with ServerResponse

use of io.helidon.webserver.ServerResponse 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 9 with ServerResponse

use of io.helidon.webserver.ServerResponse 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)

Example 10 with ServerResponse

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

the class StaticContentHandlerTest method redirect.

@Test
void redirect() {
    ResponseHeaders resh = mock(ResponseHeaders.class);
    ServerResponse res = mock(ServerResponse.class);
    ServerRequest req = mock(ServerRequest.class);
    Mockito.doReturn(resh).when(res).headers();
    StaticContentHandler.redirect(req, res, "/foo/");
    verify(res).status(Http.Status.MOVED_PERMANENTLY_301);
    verify(resh).put(Http.Header.LOCATION, "/foo/");
    verify(res).send();
}
Also used : ServerResponse(io.helidon.webserver.ServerResponse) ResponseHeaders(io.helidon.webserver.ResponseHeaders) ServerRequest(io.helidon.webserver.ServerRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ServerResponse (io.helidon.webserver.ServerResponse)37 ServerRequest (io.helidon.webserver.ServerRequest)36 Routing (io.helidon.webserver.Routing)23 Logger (java.util.logging.Logger)18 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 Single (io.helidon.common.reactive.Single)10 DbClient (io.helidon.dbclient.DbClient)10 Test (org.junit.jupiter.api.Test)10 Http (io.helidon.common.http.Http)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 SecurityContext (io.helidon.security.SecurityContext)8 AbstractService (io.helidon.tests.integration.dbclient.appl.AbstractService)8