Skip to content

Commit

Permalink
Merge pull request #17 from romanpierson/feature/vertx5
Browse files Browse the repository at this point in the history
First version for vertx 5
  • Loading branch information
romanpierson authored Nov 10, 2023
2 parents 22ed094 + 88a94f4 commit 90e9007
Show file tree
Hide file tree
Showing 14 changed files with 374 additions and 292 deletions.
17 changes: 10 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -42,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()
Expand All @@ -54,7 +57,7 @@ publishing {

groupId 'com.mdac'
artifactId 'vertx-web-accesslog'
version '1.6.0'
version '5.0.0-SNAPSHOT'

from components.java

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ public String getFormattedValue(final JsonObject values) {

}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

});
}
}
}
Expand All @@ -95,41 +102,43 @@ public AccessLoggerHandlerImpl(final JsonObject handlerConfiguration) {

final JsonObject configuration = (JsonObject) xConfiguration;

eventBus.<JsonObject>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
.<JsonObject>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());
}
});
});

Expand All @@ -156,7 +165,6 @@ private void log(final RoutingContext context, long startTSmillis){
final HttpServerRequest request = context.request();
final HttpServerResponse response = context.response();


JsonObject jsonValues = new JsonObject()
.put(RawEvent.Request.IDENTIFIERS, this.registeredIdentifiers)
.put(Data.Type.START_TS_MILLIS.getFieldName(), startTSmillis)
Expand All @@ -166,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(), request.host().contains(":") ? request.host().substring(0, request.host().indexOf(":")): request.host())
.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()){
Expand Down Expand Up @@ -194,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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.romanpierson.vertx.web.accesslogger.util;


import io.vertx.core.json.JsonObject;

public class FormatUtility {

private FormatUtility() {}

public static String getStringOrNull(final JsonObject values, final String fieldName) {
Expand All @@ -18,4 +19,5 @@ public static String getIntegerOrNull(final JsonObject values, final String fiel

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,55 @@ public void start(Promise<Void> 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();
}

});
});

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

}));

}


Expand Down
Loading

0 comments on commit 90e9007

Please sign in to comment.