From 4d7b274a1e2e615020989aa96869201e2091d7d4 Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Fri, 20 Oct 2023 20:48:38 +0200 Subject: [PATCH 1/9] First version for vertx 5 --- build.gradle | 13 ++- .../impl/AccessLoggerHandlerImpl.java | 57 +++++----- .../verticle/SimpleJsonResponseVerticle.java | 86 +++++++-------- .../web/accesslogger/AcessLogServerTest.java | 13 ++- .../web/accesslogger/ConsoleAppenderTest.java | 68 ++++++------ .../ElasticSearchAppenderTest.java | 68 +++++++----- .../accesslogger/EventBusAppenderTest.java | 102 ++++++++++-------- .../web/accesslogger/LoggingAppenderTest.java | 100 +++++++++-------- .../web/accesslogger/element/ElementTest.java | 102 ++++++++++-------- 9 files changed, 342 insertions(+), 267 deletions(-) diff --git a/build.gradle b/build.gradle index 46d1f89..3ded621 100755 --- a/build.gradle +++ b/build.gradle @@ -7,14 +7,17 @@ plugins { repositories { mavenCentral() + maven { + url "https://s01.oss.sonatype.org/content/repositories/snapshots" + } } ext{ - vertxVersion = '4.4.1' - jupiterVersion = '5.9.1' - jupiterLauncherVersion = '1.9.1' - slfApiVersion = '2.0.5' - logbackVersion = '1.4.5' + vertxVersion = '5.0.0-SNAPSHOT' + jupiterVersion = '5.10.0' + jupiterLauncherVersion = '1.10.0' + slfApiVersion = '2.0.9' + logbackVersion = '1.4.11' } dependencies { diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java index c4b466c..055ede2 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java @@ -95,41 +95,43 @@ public AccessLoggerHandlerImpl(final JsonObject handlerConfiguration) { final JsonObject configuration = (JsonObject) xConfiguration; - eventBus.request(AccessLoggerConstants.EVENTBUS_REGISTER_EVENT_NAME, configuration, ar -> { - - final String configurationIdentifier = configuration.getString(Registration.Request.IDENTIFIER); - - if(ar.succeeded()) { - JsonObject response = ar.result().body(); - if(Registration.Response.RESULT_OK.equals(response.getString(Registration.Response.RESULT, null))){ - - this.requiresCookies = response.getBoolean(Registration.Response.REQUIRES_COOKIES, false) ? true : this.requiresCookies; - this.requiresIncomingHeaders = response.getBoolean(Registration.Response.REQUIRES_INCOMING_HEADERS, false) ? true : this.requiresIncomingHeaders; - this.requiresOutgoingHeaders = response.getBoolean(Registration.Response.REQUIRES_OUTGOING_HEADERS, false) ? true : this.requiresOutgoingHeaders; - - this.registeredIdentifiers.add(configurationIdentifier); - - if(this.requiredConfigurationsCounter == this.registeredIdentifiers.size()) { - this.allConfigurationsSuccessfullyRegistered = true; + eventBus + .request(AccessLoggerConstants.EVENTBUS_REGISTER_EVENT_NAME, configuration) + .onComplete(ar -> { + + final String configurationIdentifier = configuration.getString(Registration.Request.IDENTIFIER); + + if(ar.succeeded()) { + JsonObject response = ar.result().body(); + if(Registration.Response.RESULT_OK.equals(response.getString(Registration.Response.RESULT, null))){ - logger.debug("Successfully registered all [" + this.requiredConfigurationsCounter + "] configurations with identifiers " + this.registeredIdentifiers); + this.requiresCookies = response.getBoolean(Registration.Response.REQUIRES_COOKIES, false) ? true : this.requiresCookies; + this.requiresIncomingHeaders = response.getBoolean(Registration.Response.REQUIRES_INCOMING_HEADERS, false) ? true : this.requiresIncomingHeaders; + this.requiresOutgoingHeaders = response.getBoolean(Registration.Response.REQUIRES_OUTGOING_HEADERS, false) ? true : this.requiresOutgoingHeaders; - if(this.requiresCookies || this.requiresIncomingHeaders || this.requiresOutgoingHeaders) { + this.registeredIdentifiers.add(configurationIdentifier); + + if(this.requiredConfigurationsCounter == this.registeredIdentifiers.size()) { + this.allConfigurationsSuccessfullyRegistered = true; - logger.debug("Specific data required for cookies [" + this.requiresCookies + "], incoming headers [" + this.requiresIncomingHeaders + "], outgoing headers [" + this.requiresOutgoingHeaders + "]"); + logger.debug("Successfully registered all [" + this.requiredConfigurationsCounter + "] configurations with identifiers " + this.registeredIdentifiers); - } else { - logger.debug("No specific data required"); + if(this.requiresCookies || this.requiresIncomingHeaders || this.requiresOutgoingHeaders) { + + logger.debug("Specific data required for cookies [" + this.requiresCookies + "], incoming headers [" + this.requiresIncomingHeaders + "], outgoing headers [" + this.requiresOutgoingHeaders + "]"); + + } else { + logger.debug("No specific data required"); + } } + + } else { + throw new AccessLoggerException("Unable to register access log configuration for identifier [" + configurationIdentifier + "]"); } } else { - throw new AccessLoggerException("Unable to register access log configuration for identifier [" + configurationIdentifier + "]"); + throw new AccessLoggerException("Unable to register access log configuration [" + configurationIdentifier + "]", ar.cause()); } - - } else { - throw new AccessLoggerException("Unable to register access log configuration [" + configurationIdentifier + "]", ar.cause()); - } }); }); @@ -166,7 +168,8 @@ private void log(final RoutingContext context, long startTSmillis){ .put(Data.Type.URI.getFieldName(), request.path()) .put(Data.Type.VERSION.getFieldName(), request.version()) .put(Data.Type.REMOTE_HOST.getFieldName(), request.remoteAddress().host()) - .put(Data.Type.LOCAL_HOST.getFieldName(), request.host().contains(":") ? request.host().substring(0, request.host().indexOf(":")): request.host()) + // TODO + //.put(Data.Type.LOCAL_HOST.getFieldName(), request.host().contains(":") ? request.host().substring(0, request.host().indexOf(":")): request.host()) .put(Data.Type.LOCAL_PORT.getFieldName(), request.localAddress().port()); if(request.query() != null && !request.query().trim().isEmpty()){ diff --git a/src/test/java/com/romanpierson/vertx/test/verticle/SimpleJsonResponseVerticle.java b/src/test/java/com/romanpierson/vertx/test/verticle/SimpleJsonResponseVerticle.java index 81f0061..23f2222 100644 --- a/src/test/java/com/romanpierson/vertx/test/verticle/SimpleJsonResponseVerticle.java +++ b/src/test/java/com/romanpierson/vertx/test/verticle/SimpleJsonResponseVerticle.java @@ -29,53 +29,55 @@ public void start(Promise startPromise) throws Exception { .setConfig(new JsonObject().put("path", configFile)); ConfigRetriever retriever = ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(store)); - - retriever.getConfig(result -> { - - if(result.succeeded()) { - router - .route() - .handler(BodyHandler.create()) - .handler(AccessLoggerHandler.create(result.result()) - ); + + retriever + .getConfig() + .onComplete(result -> { - router - .post("/posttest") - .handler(ctx -> { - - //System.out.println(ctx.body()); + if(result.succeeded()) { + router + .route() + .handler(BodyHandler.create()) + .handler(AccessLoggerHandler.create(result.result()) + ); + + router + .post("/posttest") + .handler(ctx -> { + + //System.out.println(ctx.body()); - final JsonObject resultJson = new JsonObject(); + final JsonObject resultJson = new JsonObject(); + + resultJson.put("uri", ctx.request().uri()); - resultJson.put("uri", ctx.request().uri()); - - ctx.response().putHeader("Content-Type", "application/json; charset=utf-8").end(resultJson.encodePrettily()); - }); - - router - .get("/empty") - .handler(ctx -> { - - ctx.response().putHeader("Content-Type", "application/json; charset=utf-8").end(); - }); - - router - .get() - .handler(ctx -> { - - final JsonObject resultJson = new JsonObject(); + ctx.response().putHeader("Content-Type", "application/json; charset=utf-8").end(resultJson.encodePrettily()); + }); + + router + .get("/empty") + .handler(ctx -> { - resultJson.put("uri", ctx.request().uri()); - - ctx.response().putHeader("Content-Type", "application/json; charset=utf-8").end(resultJson.encodePrettily()); - }); - - vertx.createHttpServer().requestHandler(router).listen(8080); + ctx.response().putHeader("Content-Type", "application/json; charset=utf-8").end(); + }); + + router + .get() + .handler(ctx -> { + + final JsonObject resultJson = new JsonObject(); + + resultJson.put("uri", ctx.request().uri()); + + ctx.response().putHeader("Content-Type", "application/json; charset=utf-8").end(resultJson.encodePrettily()); + }); + + vertx.createHttpServer().requestHandler(router).listen(8080); + + startPromise.complete(); + } - startPromise.complete(); - } - - }); + }); } diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/AcessLogServerTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/AcessLogServerTest.java index 8640598..9a5bb59 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/AcessLogServerTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/AcessLogServerTest.java @@ -30,16 +30,21 @@ class AcessLogServerTest { void testInvalidConfig(Vertx vertx, VertxTestContext testContext) { vertx.exceptionHandler(throwable -> { + throwable.printStackTrace(); assertTrue(throwable instanceof IllegalArgumentException); assertEquals("must specify at least one valid configuration", throwable.getMessage()); testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-invalid.yaml")) + .onComplete(testContext.succeedingThenComplete()); + })); - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-invalid.yaml")); - - })); + } diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/ConsoleAppenderTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/ConsoleAppenderTest.java index 5f4872c..010a8b6 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/ConsoleAppenderTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/ConsoleAppenderTest.java @@ -33,7 +33,6 @@ class ConsoleAppenderTest { void testInvalidConsoleAppenderWithMissingResolvedPatttern(Vertx vertx, VertxTestContext testContext) { vertx.exceptionHandler(throwable -> { - throwable.printStackTrace(); assertTrue(throwable instanceof AccessLoggerException); assertEquals("Failed to create appender with [com.romanpierson.vertx.web.accesslogger.appender.console.impl.ConsoleAppender]", throwable.getMessage()); Throwable internalCause = throwable.getCause().getCause(); @@ -42,11 +41,15 @@ void testInvalidConsoleAppenderWithMissingResolvedPatttern(Vertx vertx, VertxTes testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-invalid-console-appender.yaml")); - - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-invalid-console-appender.yaml")) + .onComplete(testContext.succeedingThenComplete()); + })); + + } @Test @@ -60,33 +63,38 @@ void testWithValidData(Vertx vertx, VertxTestContext testContext) { testContext.failNow(throwable); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-console-appender-valid.yaml"), testContext.succeeding(id2 -> { + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-console-appender-valid.yaml")) + .onComplete(testContext.succeeding(deploymentId2 -> { - System.setOut(new PrintStream(catchingStream)); - - HttpClient client = vertx.createHttpClient(); - - // Just to fix github actions issue - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // we dont care - } - - client - .request(HttpMethod.GET, 8080, "localhost", "/test") - .compose(req -> req.send().compose(HttpClientResponse::body)) - .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + System.setOut(new PrintStream(catchingStream)); - assertEquals("/test\n", catchingStream.toString()); // Has a newline at the end.... - - System.setOut(originalStream); - - testContext.completeNow(); + HttpClient client = vertx.createHttpClient(); + + // Just to fix github actions issue + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // we dont care + } + + client + .request(HttpMethod.GET, 8080, "localhost", "/test") + .compose(req -> req.send().compose(HttpClientResponse::body)) + .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { - }))); - })); + assertEquals("/test\n", catchingStream.toString()); // Has a newline at the end.... + + System.setOut(originalStream); + + testContext.completeNow(); + + }))); + })); })); + } } diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/ElasticSearchAppenderTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/ElasticSearchAppenderTest.java index 77128b0..51cc1b6 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/ElasticSearchAppenderTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/ElasticSearchAppenderTest.java @@ -39,11 +39,13 @@ void testInvalidElasticSearchAppenderWithMissingInstanceIdentifier(Vertx vertx, testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elasticsearch-appender-invalid-instance.yaml")); - - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-invalid-console-appender.yaml")) + .onComplete(testContext.succeedingThenComplete()); + })); } @Test @@ -58,11 +60,13 @@ void testInvalidElasticSearchAppenderWithMissingFieldNames(Vertx vertx, VertxTes testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elasticsearch-appender-invalid-fieldnames.yaml")); - - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elasticsearch-appender-invalid-fieldnames.yaml")) + .onComplete(testContext.succeedingThenComplete()); + })); } @Test @@ -80,25 +84,31 @@ void testValid(Vertx vertx, VertxTestContext testContext) { testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elasticsearch-appender-valid.yaml"), testContext.succeeding(id2 -> { - - // Just to fix github actions issue - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // we dont care - } - - HttpClient client = vertx.createHttpClient(); - - client - .request(HttpMethod.GET, 8080, "localhost", "/test") - .compose(req -> req.send().compose(HttpClientResponse::body)) - .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elasticsearch-appender-valid.yaml")) + .onComplete(testContext.succeeding(deploymentId2 -> { - }))); - })); - })); + // Just to fix github actions issue + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // we dont care + } + + HttpClient client = vertx.createHttpClient(); + + client + .request(HttpMethod.GET, 8080, "localhost", "/test") + .compose(req -> req.send().compose(HttpClientResponse::body)) + .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + + }))); + + })); + })); + } } diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/EventBusAppenderTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/EventBusAppenderTest.java index 5b11d33..bf45d3c 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/EventBusAppenderTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/EventBusAppenderTest.java @@ -38,11 +38,14 @@ void testInvalidConfig(Vertx vertx, VertxTestContext testContext) { testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-eventbus-appender-invalid-config.yaml")); - - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-eventbus-appender-invalid-config.yaml")) + .onComplete(testContext.succeedingThenComplete()); + })); + } @Test @@ -53,25 +56,31 @@ void testWithNotExistingAdress(Vertx vertx, VertxTestContext testContext) { testContext.failNow(throwable); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-eventbus-appender-inexisting-address.yaml"), testContext.succeeding(id2 -> { - - HttpClient client = vertx.createHttpClient(); - - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - client - .request(HttpMethod.GET, 8080, "localhost", "/test") - .compose(req -> req.send().compose(HttpClientResponse::body)) - .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { - testContext.completeNow(); - }))); - })); - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-eventbus-appender-inexisting-address.yaml")) + .onComplete(testContext.succeeding(deploymentId2 -> { + + HttpClient client = vertx.createHttpClient(); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + client + .request(HttpMethod.GET, 8080, "localhost", "/test") + .compose(req -> req.send().compose(HttpClientResponse::body)) + .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + testContext.completeNow(); + }))); + + })); + })); + } @Test @@ -88,25 +97,32 @@ void testWithExistingAdress(Vertx vertx, VertxTestContext testContext) { testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-eventbus-appender-existing-address.yaml"), testContext.succeeding(id2 -> { - - HttpClient client = vertx.createHttpClient(); - - // Just to fix github actions issue - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // we dont care - } - - client - .request(HttpMethod.GET, 8080, "localhost", "/test") - .compose(req -> req.send().compose(HttpClientResponse::body)) - .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-eventbus-appender-existing-address.yaml")) + .onComplete(testContext.succeeding(deploymentId2 -> { - }))); - })); - })); + HttpClient client = vertx.createHttpClient(); + + // Just to fix github actions issue + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // we dont care + } + + client + .request(HttpMethod.GET, 8080, "localhost", "/test") + .compose(req -> req.send().compose(HttpClientResponse::body)) + .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + + }))); + + })); + })); + + } } diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/LoggingAppenderTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/LoggingAppenderTest.java index 0dc15c6..6adf63d 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/LoggingAppenderTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/LoggingAppenderTest.java @@ -44,11 +44,13 @@ void testInvalidConfig(Vertx vertx, VertxTestContext testContext) { testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-logging-appender-invalid-loggername.yaml")); - - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-logging-appender-invalid-loggername.yaml")) + .onComplete(testContext.succeedingThenComplete()); + })); } @Test @@ -68,11 +70,13 @@ void testInvalidLoggingAppenderWithMissingResolvedPatttern(Vertx vertx, VertxTes testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-logging-appender-invalid-logpattern.yaml")); - - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-logging-appender-invalid-logpattern.yaml")) + .onComplete(testContext.succeedingThenComplete()); + })); } // TODO this test needs to be fixed - need to check how the message written by logback can be read and verified @@ -87,40 +91,46 @@ void testWithValidData(Vertx vertx, VertxTestContext testContext) { testContext.failNow(throwable); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-logging-appender-valid.yaml"), testContext.succeeding(id2 -> { - - // Just to fix github actions issue - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // we dont care - } - - System.setOut(new PrintStream(catchingStream)); - - HttpClient client = vertx.createHttpClient(); - - client - .request(HttpMethod.GET, 8080, "localhost", "/test") - .compose(req -> req.send().compose(HttpClientResponse::body)) - .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { - - // Ensure it got logged by slf4j/logback - Thread.sleep(1000); - - //assertEquals("/test\n", catchingStream.toString()); - - System.setOut(originalStream); - - - //System.out.println(catchingStream); - //assertEquals("/test", catchingStream.toString()); - - testContext.completeNow(); - - }))); - })); - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-logging-appender-valid.yaml")) + .onComplete(testContext.succeeding(deploymentId2 -> { + + // Just to fix github actions issue + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // we dont care + } + + System.setOut(new PrintStream(catchingStream)); + + HttpClient client = vertx.createHttpClient(); + + client + .request(HttpMethod.GET, 8080, "localhost", "/test") + .compose(req -> req.send().compose(HttpClientResponse::body)) + .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + + // Ensure it got logged by slf4j/logback + Thread.sleep(1000); + + //assertEquals("/test\n", catchingStream.toString()); + + System.setOut(originalStream); + + + //System.out.println(catchingStream); + //assertEquals("/test", catchingStream.toString()); + + testContext.completeNow(); + + }))); + + })); + })); + } } diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java index f2f1974..ed9156e 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java @@ -4,6 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.PrintStream; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -11,6 +13,8 @@ import com.romanpierson.vertx.web.accesslogger.verticle.AccessLoggerProducerVerticle; import io.vertx.core.Vertx; +import io.vertx.core.http.HttpClient; +import io.vertx.core.http.HttpClientResponse; import io.vertx.core.http.HttpMethod; import io.vertx.core.json.JsonArray; import io.vertx.ext.web.client.WebClient; @@ -45,26 +49,33 @@ void testWithEmptyResponse(Vertx vertx, VertxTestContext testContext) { testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elements-empty-result.yaml"), testContext.succeeding(id2 -> { - - WebClient client = WebClient.create(vertx); - - // Just to fix github actions issue - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // we dont care - } - - client - .request(HttpMethod.GET, 8080, "localhost", "/empty") - .send() - .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { - - }))); - })); - })); + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elements-empty-result.yaml")) + .onComplete(testContext.succeeding(deploymentId2 -> { + + WebClient client = WebClient.create(vertx); + + // Just to fix github actions issue + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // we dont care + } + + client + .request(HttpMethod.GET, 8080, "localhost", "/empty") + .send() + .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + + }))); + + })); + })); + + } /** @@ -97,7 +108,8 @@ void testWithNonEmptyResponse(Vertx vertx, VertxTestContext testContext) { assertEquals("200", message.body().getString(5)); assertEquals("HTTP/1.1", message.body().getString(6)); assertNotNull(message.body().getString(7)); - assertEquals("localhost", message.body().getString(8)); + // TODO + //assertEquals("localhost", message.body().getString(8)); assertEquals("8080", message.body().getString(9)); assertEquals("GET", message.body().getString(10)); assertEquals("GET", message.body().getString(11)); @@ -123,28 +135,34 @@ void testWithNonEmptyResponse(Vertx vertx, VertxTestContext testContext) { testContext.completeNow(); }); - vertx.deployVerticle(new AccessLoggerProducerVerticle(),testContext.succeeding(id -> { - vertx.deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elements-result.yaml"), testContext.succeeding(id2 -> { - - WebClient client = WebClient.create(vertx); - - // Just to fix github actions issue - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // we dont care - } - - client - .request(HttpMethod.GET, 8080, "localhost", "/nonEmpty?foo=bar") - .putHeader("header1", "header1val") - .putHeader("Cookie", "cookie1=cookie1Value; cookie2=cookie2Value") - .send() - .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + vertx + .deployVerticle(new AccessLoggerProducerVerticle()) + .onComplete(testContext.succeeding(deploymentId -> { + vertx + .deployVerticle(new SimpleJsonResponseVerticle("accesslog-config-elements-result.yaml")) + .onComplete(testContext.succeeding(deploymentId2 -> { + + WebClient client = WebClient.create(vertx); + + // Just to fix github actions issue + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // we dont care + } - }))); - })); - })); + client + .request(HttpMethod.GET, 8080, "localhost", "/nonEmpty?foo=bar") + .putHeader("header1", "header1val") + .putHeader("Cookie", "cookie1=cookie1Value; cookie2=cookie2Value") + .send() + .onComplete(testContext.succeeding(buffer -> testContext.verify(() -> { + + }))); + + })); + })); + } } From 9b8fe4216cfc231de74a6634980fb966183d7e3c Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Fri, 20 Oct 2023 20:53:38 +0200 Subject: [PATCH 2/9] Add branch to CI --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd2008a..391b8f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,6 +4,7 @@ on: push: branches: - master + - feature/vertx5 jobs: ci: From 1f38c47f5c1581777068d66bd321b4df86e89e79 Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Fri, 20 Oct 2023 20:57:04 +0200 Subject: [PATCH 3/9] Check if CI works on branch --- .../vertx/web/accesslogger/element/ElementTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java index ed9156e..f80189b 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java @@ -108,7 +108,7 @@ void testWithNonEmptyResponse(Vertx vertx, VertxTestContext testContext) { assertEquals("200", message.body().getString(5)); assertEquals("HTTP/1.1", message.body().getString(6)); assertNotNull(message.body().getString(7)); - // TODO + // TODO //assertEquals("localhost", message.body().getString(8)); assertEquals("8080", message.body().getString(9)); assertEquals("GET", message.body().getString(10)); From 2663586b064968f52a7666c836ad964f20a99bd8 Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Sun, 29 Oct 2023 20:09:38 +0100 Subject: [PATCH 4/9] Reimplementing host element with new vertx 5 API --- .../element/impl/HostElement.java | 18 +++++++++++++++++- .../impl/AccessLoggerHandlerImpl.java | 15 +++++++++++---- .../web/accesslogger/util/FormatUtility.java | 18 ++++++++++++++++++ .../pattern/PatternResolverTest.java | 12 ++++++------ .../web/accesslogger/element/ElementTest.java | 5 +---- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java index c4b31ff..5025f48 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java @@ -14,9 +14,12 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Objects; +import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants; import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants.Request.Data; +import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants.Request.Data.Type; import com.romanpierson.vertx.web.accesslogger.configuration.element.AccessLogElement; import com.romanpierson.vertx.web.accesslogger.configuration.pattern.ExtractedPosition; import com.romanpierson.vertx.web.accesslogger.util.FormatUtility; @@ -67,7 +70,7 @@ public String getFormattedValue(final JsonObject values) { switch (this.mode){ case LOCAL_HOST: - return FormatUtility.getStringOrNull(values, Data.Type.LOCAL_HOST.getFieldName()); + return FormatUtility.getHostFromHeaderOrNull(values); case LOCAL_PORT: return FormatUtility.getIntegerOrNull(values, Data.Type.LOCAL_PORT.getFieldName()); case REMOTE_HOST: @@ -77,5 +80,18 @@ public String getFormattedValue(final JsonObject values) { } } + + @Override + public Collection claimDataParts() { + + if(Mode.LOCAL_HOST.equals(this.mode)) { + // Host is in headers + return Collections.singletonList(AccessLoggerConstants.Request.Data.Type.REQUEST_HEADERS); + } + + return AccessLogElement.super.claimDataParts(); + } + + } diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java index 055ede2..1d6c02e 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java @@ -27,6 +27,7 @@ import io.vertx.core.DeploymentOptions; import io.vertx.core.MultiMap; +import io.vertx.core.ThreadingModel; import io.vertx.core.Vertx; import io.vertx.core.eventbus.EventBus; import io.vertx.core.http.Cookie; @@ -79,10 +80,16 @@ public AccessLoggerHandlerImpl(final JsonObject handlerConfiguration) { logger.info("Start creating singleton verticle"); - Vertx.currentContext().owner().deployVerticle(AccessLoggerProducerVerticle.class.getName(), new DeploymentOptions().setWorker(true)); - - isProducerVerticleCreated = true; - + Vertx.currentContext().owner().deployVerticle(AccessLoggerProducerVerticle.class.getName(), new DeploymentOptions().setThreadingModel(ThreadingModel.WORKER)) + .onComplete(ar -> { + + if(ar.succeeded()) { + isProducerVerticleCreated = true; + } else { + throw new AccessLoggerException("Unable to deploy AccessLoggerProducerVerticle", ar.cause()); + } + + }); } } } diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java index df55c45..538dcdd 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java @@ -1,9 +1,13 @@ package com.romanpierson.vertx.web.accesslogger.util; +import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants.Request.Data; + import io.vertx.core.json.JsonObject; public class FormatUtility { + private final static JsonObject EMPTY_JSON_OBJECT = new JsonObject(); + private FormatUtility() {} public static String getStringOrNull(final JsonObject values, final String fieldName) { @@ -18,4 +22,18 @@ public static String getIntegerOrNull(final JsonObject values, final String fiel } + public static String getHostFromHeaderOrNull(final JsonObject values) { + + final String hostHeaderValue = values.getJsonObject(Data.Type.REQUEST_HEADERS.getFieldName(), EMPTY_JSON_OBJECT).getString("host", null); + + int portDivider = hostHeaderValue == null ? -1 : hostHeaderValue.indexOf(':'); + + if(portDivider >= 0) { + return hostHeaderValue.substring(0, portDivider); + } else { + return hostHeaderValue; + } + + } + } diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/configuration/pattern/PatternResolverTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/configuration/pattern/PatternResolverTest.java index 8d5c2f4..3fa968d 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/configuration/pattern/PatternResolverTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/configuration/pattern/PatternResolverTest.java @@ -22,7 +22,7 @@ public class PatternResolverTest { - //@Test + @Test public void resolvePattern_off_the_shelf_pattern() { PatternResolver patternResolver = new PatternResolver(); String logPattern = "server1 %{server1}static %{Accept-Encoding}i %t %D cs-uri %{foo}C"; @@ -36,7 +36,7 @@ public void resolvePattern_off_the_shelf_pattern() { assertResolvedPatterns(expected, actual); } - //@Test + @Test public void resolvePattern_single_pattern_element() { PatternResolver patternResolver = new PatternResolver(); String logPattern = "%m"; @@ -48,7 +48,7 @@ public void resolvePattern_single_pattern_element() { assertResolvedPatterns(expected, actual); } - //@Test + @Test public void resolvePattern_two_pattern_elements() { PatternResolver patternResolver = new PatternResolver(); String logPattern = "%D %m"; @@ -60,7 +60,7 @@ public void resolvePattern_two_pattern_elements() { assertResolvedPatterns(expected, actual); } - //@Test + @Test public void resolvePattern_two_pattern_elements_reversed_order() { PatternResolver patternResolver = new PatternResolver(); String logPattern = "%m %D"; @@ -72,7 +72,7 @@ public void resolvePattern_two_pattern_elements_reversed_order() { assertResolvedPatterns(expected, actual); } - //@Test + @Test public void resolvePattern_duplicate_pattern_elements() { PatternResolver patternResolver = new PatternResolver(); String logPattern = "%s %s %s"; @@ -84,7 +84,7 @@ public void resolvePattern_duplicate_pattern_elements() { assertResolvedPatterns(expected, actual); } - //@Test + @Test public void resolvePattern_unresolvable_parts() { PatternResolver patternResolver = new PatternResolver(); String logPattern = "xx %m yy"; diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java index f80189b..7aef7a0 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java @@ -96,8 +96,6 @@ void testWithNonEmptyResponse(Vertx vertx, VertxTestContext testContext) { vertx.eventBus().consumer("target", message -> { - //System.out.println(message.body()); - assertEquals(30, message.body().size()); assertEquals("33", message.body().getString(0)); @@ -108,8 +106,7 @@ void testWithNonEmptyResponse(Vertx vertx, VertxTestContext testContext) { assertEquals("200", message.body().getString(5)); assertEquals("HTTP/1.1", message.body().getString(6)); assertNotNull(message.body().getString(7)); - // TODO - //assertEquals("localhost", message.body().getString(8)); + assertEquals("localhost", message.body().getString(8)); assertEquals("8080", message.body().getString(9)); assertEquals("GET", message.body().getString(10)); assertEquals("GET", message.body().getString(11)); From 7491ba0901c1a7fe1e714aa4113f8f1963cea64a Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Tue, 7 Nov 2023 21:59:09 +0100 Subject: [PATCH 5/9] Fix host handling --- .../configuration/element/impl/HostElement.java | 2 +- .../impl/AccessLoggerHandlerImpl.java | 5 +++-- .../web/accesslogger/util/FormatUtility.java | 16 ---------------- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java index 5025f48..04bce64 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java @@ -70,7 +70,7 @@ public String getFormattedValue(final JsonObject values) { switch (this.mode){ case LOCAL_HOST: - return FormatUtility.getHostFromHeaderOrNull(values); + return FormatUtility.getStringOrNull(values, Data.Type.LOCAL_HOST.getFieldName()); case LOCAL_PORT: return FormatUtility.getIntegerOrNull(values, Data.Type.LOCAL_PORT.getFieldName()); case REMOTE_HOST: diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java index 1d6c02e..80738d0 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java @@ -165,6 +165,8 @@ private void log(final RoutingContext context, long startTSmillis){ final HttpServerRequest request = context.request(); final HttpServerResponse response = context.response(); + // getHeader is case insensitive + final String headerHostValue = request.getHeader("Host"); JsonObject jsonValues = new JsonObject() .put(RawEvent.Request.IDENTIFIERS, this.registeredIdentifiers) @@ -175,8 +177,7 @@ private void log(final RoutingContext context, long startTSmillis){ .put(Data.Type.URI.getFieldName(), request.path()) .put(Data.Type.VERSION.getFieldName(), request.version()) .put(Data.Type.REMOTE_HOST.getFieldName(), request.remoteAddress().host()) - // TODO - //.put(Data.Type.LOCAL_HOST.getFieldName(), request.host().contains(":") ? request.host().substring(0, request.host().indexOf(":")): request.host()) + .put(Data.Type.LOCAL_HOST.getFieldName(), headerHostValue.contains(":") ? headerHostValue.substring(0, headerHostValue.indexOf(":")): headerHostValue) .put(Data.Type.LOCAL_PORT.getFieldName(), request.localAddress().port()); if(request.query() != null && !request.query().trim().isEmpty()){ diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java index 538dcdd..43e9fbf 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/util/FormatUtility.java @@ -1,12 +1,9 @@ package com.romanpierson.vertx.web.accesslogger.util; -import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants.Request.Data; import io.vertx.core.json.JsonObject; public class FormatUtility { - - private final static JsonObject EMPTY_JSON_OBJECT = new JsonObject(); private FormatUtility() {} @@ -22,18 +19,5 @@ public static String getIntegerOrNull(final JsonObject values, final String fiel } - public static String getHostFromHeaderOrNull(final JsonObject values) { - - final String hostHeaderValue = values.getJsonObject(Data.Type.REQUEST_HEADERS.getFieldName(), EMPTY_JSON_OBJECT).getString("host", null); - - int portDivider = hostHeaderValue == null ? -1 : hostHeaderValue.indexOf(':'); - - if(portDivider >= 0) { - return hostHeaderValue.substring(0, portDivider); - } else { - return hostHeaderValue; - } - - } } From 1dece1de01e4f0c0d76ef8c4d3c71bb915023a90 Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Wed, 8 Nov 2023 22:00:26 +0100 Subject: [PATCH 6/9] Changed gradle build --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 3ded621..7d949e9 100755 --- a/build.gradle +++ b/build.gradle @@ -45,7 +45,7 @@ test { sourceCompatibility='1.8' targetCompatibility='1.8' -jar.archiveFileName = "vertx-web-accesslog-1.6.0.jar" +jar.archiveFileName = "vertx-web-accesslog-5.0.0-SNAPSHOT.jar" java { withSourcesJar() @@ -57,7 +57,7 @@ publishing { groupId 'com.mdac' artifactId 'vertx-web-accesslog' - version '1.6.0' + version '5.0.0-SNAPSHOT' from components.java From 03d32e0a25f6a2228efb6d82635e143090461868 Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Fri, 10 Nov 2023 20:16:19 +0100 Subject: [PATCH 7/9] First version for vertx 5 --- .../element/impl/HeaderElement.java | 2 +- .../impl/AccessLoggerHandlerImpl.java | 7 ++----- .../web/accesslogger/element/ElementTest.java | 16 +++++++++------- .../accesslog-config-elements-result.yaml | 2 +- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HeaderElement.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HeaderElement.java index 3689561..638f724 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HeaderElement.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HeaderElement.java @@ -39,7 +39,7 @@ public static HeaderElement of(final Mode mode, final String identifier) { HeaderElement element = new HeaderElement(); element.mode = mode; - element.identifier = identifier; + element.identifier = identifier.toLowerCase(); return element; } diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java index 80738d0..853b346 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/impl/AccessLoggerHandlerImpl.java @@ -165,9 +165,6 @@ private void log(final RoutingContext context, long startTSmillis){ final HttpServerRequest request = context.request(); final HttpServerResponse response = context.response(); - // getHeader is case insensitive - final String headerHostValue = request.getHeader("Host"); - JsonObject jsonValues = new JsonObject() .put(RawEvent.Request.IDENTIFIERS, this.registeredIdentifiers) .put(Data.Type.START_TS_MILLIS.getFieldName(), startTSmillis) @@ -177,7 +174,7 @@ private void log(final RoutingContext context, long startTSmillis){ .put(Data.Type.URI.getFieldName(), request.path()) .put(Data.Type.VERSION.getFieldName(), request.version()) .put(Data.Type.REMOTE_HOST.getFieldName(), request.remoteAddress().host()) - .put(Data.Type.LOCAL_HOST.getFieldName(), headerHostValue.contains(":") ? headerHostValue.substring(0, headerHostValue.indexOf(":")): headerHostValue) + .put(Data.Type.LOCAL_HOST.getFieldName(), request.authority() == null ? null : request.authority().host()) .put(Data.Type.LOCAL_PORT.getFieldName(), request.localAddress().port()); if(request.query() != null && !request.query().trim().isEmpty()){ @@ -205,7 +202,7 @@ private void log(final RoutingContext context, long startTSmillis){ private JsonObject extractHeaders(final MultiMap headersMap){ JsonObject headers = new JsonObject(); - headersMap.forEach(entry -> headers.put(entry.getKey(), entry.getValue())); + headersMap.forEach(entry -> headers.put(entry.getKey().toLowerCase(), entry.getValue())); return headers; diff --git a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java index 7aef7a0..1e89f3c 100644 --- a/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java +++ b/src/test/java/com/romanpierson/vertx/web/accesslogger/element/ElementTest.java @@ -96,7 +96,7 @@ void testWithNonEmptyResponse(Vertx vertx, VertxTestContext testContext) { vertx.eventBus().consumer("target", message -> { - assertEquals(30, message.body().size()); + assertEquals(32, message.body().size()); assertEquals("33", message.body().getString(0)); assertEquals("33", message.body().getString(1)); @@ -122,12 +122,14 @@ void testWithNonEmptyResponse(Vertx vertx, VertxTestContext testContext) { assertEquals("xy", message.body().getString(21)); assertEquals("envVal1", message.body().getString(22)); assertEquals("header1val", message.body().getString(23)); - assertEquals("", message.body().getString(24)); - assertEquals("application/json; charset=utf-8", message.body().getString(25)); - assertEquals("", message.body().getString(26)); - assertEquals("cookie1Value", message.body().getString(27)); - assertEquals("cookie2Value", message.body().getString(28)); - assertEquals("", message.body().getString(29)); + assertEquals("header1val", message.body().getString(24)); + assertEquals("", message.body().getString(25)); + assertEquals("application/json; charset=utf-8", message.body().getString(26)); + assertEquals("application/json; charset=utf-8", message.body().getString(27)); + assertEquals("", message.body().getString(28)); + assertEquals("cookie1Value", message.body().getString(29)); + assertEquals("cookie2Value", message.body().getString(30)); + assertEquals("", message.body().getString(31)); testContext.completeNow(); }); diff --git a/src/test/resources/accesslog-config-elements-result.yaml b/src/test/resources/accesslog-config-elements-result.yaml index ecea71c..c3e2b84 100644 --- a/src/test/resources/accesslog-config-elements-result.yaml +++ b/src/test/resources/accesslog-config-elements-result.yaml @@ -1,7 +1,7 @@ isAutoDeployProducerVerticle: false configurations: - identifier: accesslog-formatted - logPattern: '%b %B %D %T sc-status %s %H %h %v %p cs-method %m %r cs-uri %U cs-uri-stem %q cs-uri-query %t %{msec}t %x %{xy}static %{envVar1}env %{header1}i %{header2}i %{Content-Type}o %{header3}o %{cookie1}C %{cookie2}C %{cookieFoo}C' + logPattern: '%b %B %D %T sc-status %s %H %h %v %p cs-method %m %r cs-uri %U cs-uri-stem %q cs-uri-query %t %{msec}t %x %{xy}static %{envVar1}env %{header1}i %{heADeR1}i %{header2}i %{Content-Type}o %{CoNTent-tyPe}o %{header3}o %{cookie1}C %{cookie2}C %{cookieFoo}C' appenders: - appenderClassName : com.romanpierson.vertx.web.accesslogger.appender.eventbus.impl.EventBusAppender config: From 3a18649ca32a8bf51555f177db6978f5f25e3155 Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Fri, 10 Nov 2023 20:23:42 +0100 Subject: [PATCH 8/9] CI configs and small fix --- .github/workflows/ci-vert.x-4.x.yml | 24 +++++++++++++++++++ .../workflows/{ci.yml => ci-vert.x-5.x.yml} | 3 +-- .../element/impl/HostElement.java | 12 ---------- 3 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/ci-vert.x-4.x.yml rename .github/workflows/{ci.yml => ci-vert.x-5.x.yml} (93%) diff --git a/.github/workflows/ci-vert.x-4.x.yml b/.github/workflows/ci-vert.x-4.x.yml new file mode 100644 index 0000000..7738103 --- /dev/null +++ b/.github/workflows/ci-vert.x-4.x.yml @@ -0,0 +1,24 @@ +name: vert.x (4.x) + +on: + push: + branches: + - vert.x-4.x + +jobs: + ci: + name: Any (supported) + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest ] + java: [ 11 ] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + java-version: ${{ matrix.java }} + distribution: temurin + - name: Build project + run: ./gradlew clean build diff --git a/.github/workflows/ci.yml b/.github/workflows/ci-vert.x-5.x.yml similarity index 93% rename from .github/workflows/ci.yml rename to .github/workflows/ci-vert.x-5.x.yml index 391b8f6..6aa3673 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci-vert.x-5.x.yml @@ -1,10 +1,9 @@ -name: CI +name: vert.x (5.x) on: push: branches: - master - - feature/vertx5 jobs: ci: diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java index 04bce64..8503a54 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java @@ -81,17 +81,5 @@ public String getFormattedValue(final JsonObject values) { } } - @Override - public Collection claimDataParts() { - - if(Mode.LOCAL_HOST.equals(this.mode)) { - // Host is in headers - return Collections.singletonList(AccessLoggerConstants.Request.Data.Type.REQUEST_HEADERS); - } - - return AccessLogElement.super.claimDataParts(); - } - - } From 88a94f48a8a836dc522b2c212e58598bd6c8e95a Mon Sep 17 00:00:00 2001 From: Roman Pierson Date: Fri, 10 Nov 2023 20:24:06 +0100 Subject: [PATCH 9/9] Fixed imports --- .../accesslogger/configuration/element/impl/HostElement.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java index 8503a54..a92f545 100644 --- a/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java +++ b/src/main/java/com/romanpierson/vertx/web/accesslogger/configuration/element/impl/HostElement.java @@ -14,12 +14,9 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Objects; -import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants; import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants.Request.Data; -import com.romanpierson.vertx.web.accesslogger.AccessLoggerConstants.Request.Data.Type; import com.romanpierson.vertx.web.accesslogger.configuration.element.AccessLogElement; import com.romanpierson.vertx.web.accesslogger.configuration.pattern.ExtractedPosition; import com.romanpierson.vertx.web.accesslogger.util.FormatUtility;