Search in sources :

Example 21 with ServerResponse

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

the class AccessLogSupportTest method testCustomFormat.

@Test
void testCustomFormat() {
    AccessLogSupport accessLog = AccessLogSupport.builder().add(TimestampLogEntry.builder().formatter(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")).build()).add(HeaderLogEntry.create("Referer")).build();
    ServerRequest request = mock(ServerRequest.class);
    Context context = Context.create();
    when(request.remoteAddress()).thenReturn(REMOTE_IP);
    when(request.context()).thenReturn(context);
    when(request.method()).thenReturn(Http.Method.PUT);
    HttpRequest.Path path = mock(HttpRequest.Path.class);
    when(path.toRawString()).thenReturn(PATH);
    when(request.path()).thenReturn(path);
    when(request.version()).thenReturn(Http.Version.V1_1);
    RequestHeaders headers = mock(RequestHeaders.class);
    when(headers.all("Referer")).thenReturn(Arrays.asList("first", "second"));
    when(request.headers()).thenReturn(headers);
    ServerResponse response = mock(ServerResponse.class);
    when(response.status()).thenReturn(Http.Status.I_AM_A_TEAPOT);
    String logRecord = accessLog.createLogRecord(request, response, BEGIN_TIME, 0L, END_TIME, TIME_TAKEN_MICROS * 1000);
    String expected = "20071203101530 \"first,second\"";
    assertThat(logRecord, is(expected));
}
Also used : Context(io.helidon.common.context.Context) HttpRequest(io.helidon.common.http.HttpRequest) ServerResponse(io.helidon.webserver.ServerResponse) ServerRequest(io.helidon.webserver.ServerRequest) RequestHeaders(io.helidon.webserver.RequestHeaders) Test(org.junit.jupiter.api.Test)

Example 22 with ServerResponse

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

the class AccessLogSupportTest method testCommonFormat.

@Test
void testCommonFormat() {
    AccessLogSupport accessLog = AccessLogSupport.builder().commonLogFormat().build();
    ServerRequest request = mock(ServerRequest.class);
    Context context = Context.create();
    when(request.remoteAddress()).thenReturn(REMOTE_IP);
    when(request.context()).thenReturn(context);
    when(request.method()).thenReturn(Http.Method.PUT);
    HttpRequest.Path path = mock(HttpRequest.Path.class);
    when(path.toRawString()).thenReturn(PATH);
    when(request.path()).thenReturn(path);
    when(request.version()).thenReturn(Http.Version.V1_1);
    ServerResponse response = mock(ServerResponse.class);
    when(response.status()).thenReturn(Http.Status.I_AM_A_TEAPOT);
    AccessLogContext accessLogContext = mock(AccessLogContext.class);
    when(accessLogContext.requestDateTime()).thenReturn(BEGIN_TIME);
    String expectedTimestamp = TimestampLogEntry.create().doApply(accessLogContext);
    String logRecord = accessLog.createLogRecord(request, response, BEGIN_TIME, 0L, END_TIME, TIME_TAKEN_MICROS * 1000);
    // 192.168.0.104 - [18/Jun/2019:23:10:44 +0200] "GET /greet/test HTTP/1.1" 200 55 2248
    String expected = REMOTE_IP + " - - " + expectedTimestamp + " \"" + METHOD + " " + PATH + " " + HTTP_VERSION + "\" " + STATUS_CODE + " " + CONTENT_LENGTH;
    assertThat(logRecord, is(expected));
}
Also used : Context(io.helidon.common.context.Context) HttpRequest(io.helidon.common.http.HttpRequest) ServerResponse(io.helidon.webserver.ServerResponse) ServerRequest(io.helidon.webserver.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 23 with ServerResponse

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

the class AccessLogSupport method handle.

private void handle(ServerRequest req, ServerResponse res) {
    // Check if this path should be excluded from access log
    if (excludePaths.size() > 0) {
        for (PathMatcher matcher : excludePaths) {
            PathMatcher.Result r = matcher.match(req.path().toString());
            if (r.matches()) {
                req.next();
                return;
            }
        }
    }
    ZonedDateTime now = ZonedDateTime.now(clock);
    long nanoNow = System.nanoTime();
    logFormat.forEach(entry -> entry.accept(req, res));
    res.whenSent().thenAccept(aResponse -> log(req, aResponse, now, nanoNow)).exceptionally(throwable -> {
        log(req, res, now, nanoNow);
        return null;
    });
    req.next();
}
Also used : Config(io.helidon.config.Config) ZonedDateTime(java.time.ZonedDateTime) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Level(java.util.logging.Level) ServerRequest(io.helidon.webserver.ServerRequest) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) PathMatcher(io.helidon.webserver.PathMatcher) Matcher(java.util.regex.Matcher) ServerResponse(io.helidon.webserver.ServerResponse) Clock(java.time.Clock) Pattern(java.util.regex.Pattern) Service(io.helidon.webserver.Service) LinkedList(java.util.LinkedList) Routing(io.helidon.webserver.Routing) Collections(java.util.Collections) PathMatcher(io.helidon.webserver.PathMatcher) ZonedDateTime(java.time.ZonedDateTime)

Example 24 with ServerResponse

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

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

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