From ad65cea140f9f58aa05db0e975c9635353910b66 Mon Sep 17 00:00:00 2001 From: Andreas Fankhauser <23085769+hiddenalpha@users.noreply.github.com> Date: Fri, 5 Jan 2024 15:55:34 +0100 Subject: [PATCH] [SDCISA-13736, SDCISA-10974] Cleanup. --- .../ConfigurationResourceConsumer.java | 2 +- .../ConfigurationResourceValidator.java | 17 ++++++----- .../gateleen/core/event/EventBusHandler.java | 29 ++++++++++--------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceConsumer.java b/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceConsumer.java index f9ff93ee0..670176d84 100644 --- a/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceConsumer.java +++ b/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceConsumer.java @@ -12,7 +12,7 @@ */ public abstract class ConfigurationResourceConsumer implements ConfigurationResourceObserver { - private final Logger log = LoggerFactory.getLogger(ConfigurationResourceConsumer.class); + private static final Logger log = LoggerFactory.getLogger(ConfigurationResourceConsumer.class); private final ConfigurationResourceManager configurationResourceManager; private final String configResourceUri; diff --git a/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceValidator.java b/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceValidator.java index d4b3735c7..9710fffd7 100644 --- a/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceValidator.java +++ b/gateleen-core/src/main/java/org/swisspush/gateleen/core/configuration/ConfigurationResourceValidator.java @@ -13,7 +13,6 @@ import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.swisspush.gateleen.core.json.JsonUtil; import org.swisspush.gateleen.core.util.StringUtils; import org.swisspush.gateleen.core.validation.ValidationResult; @@ -22,6 +21,8 @@ import java.io.IOException; import java.util.Set; +import static org.slf4j.LoggerFactory.getLogger; + /** * Validates the configuration resources * @@ -30,9 +31,9 @@ class ConfigurationResourceValidator { private static final String SCHEMA_DECLARATION = "http://json-schema.org/draft-04/schema#"; - private Logger log = LoggerFactory.getLogger(ConfigurationResourceValidator.class); + private static final Logger log = getLogger(ConfigurationResourceValidator.class); - private Vertx vertx; + private final Vertx vertx; public ConfigurationResourceValidator(Vertx vertx) { this.vertx = vertx; @@ -86,12 +87,12 @@ public void validateConfigurationResource(Buffer configurationResource, String r } } catch (IOException e) { String message = "Cannot read JSON"; - log.warn(message, e.getMessage()); + log.warn("{}", message, e); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, message)); } } else { String message = "Invalid schema: Expected property '$schema' with content '"+SCHEMA_DECLARATION+"'"; - log.warn(message); + log.warn("{}", message); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, message)); } }, resultHandler); @@ -99,10 +100,10 @@ public void validateConfigurationResource(Buffer configurationResource, String r private JsonArray extractMessagesAsJson(Set valMsgs){ JsonArray resultArray = new JsonArray(); - valMsgs.forEach(msg -> { - log.warn(msg.toString()); + for (ValidationMessage msg : valMsgs) { + log.warn("{}", msg); resultArray.add(JsonObject.mapFrom(msg)); - }); + } return resultArray; } } diff --git a/gateleen-core/src/main/java/org/swisspush/gateleen/core/event/EventBusHandler.java b/gateleen-core/src/main/java/org/swisspush/gateleen/core/event/EventBusHandler.java index f1802a194..b81504254 100644 --- a/gateleen-core/src/main/java/org/swisspush/gateleen/core/event/EventBusHandler.java +++ b/gateleen-core/src/main/java/org/swisspush/gateleen/core/event/EventBusHandler.java @@ -9,6 +9,7 @@ import io.vertx.core.eventbus.Message; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.http.HttpServerResponse; import io.vertx.core.json.DecodeException; import io.vertx.core.json.JsonObject; import io.vertx.ext.bridge.BridgeEventType; @@ -138,6 +139,7 @@ public boolean handle(final HttpServerRequest request) { try { message.put(PAYLOAD, new JsonObject(buffer.toString())); } catch (DecodeException e) { + requestLog.info("{}", request.uri(), e); request.response().setStatusCode(BAD_REQUEST); request.response().end(e.getMessage()); return; @@ -152,6 +154,7 @@ public boolean handle(final HttpServerRequest request) { if (HttpMethod.GET == request.method() || Boolean.TRUE.toString().equals(request.headers().get(SYNC))) { requestLog.debug("This is a synchronous request"); vertx.eventBus().request(address, message, new DeliveryOptions().setSendTimeout(TIMEOUT), (Handler>>) reply -> { + HttpServerResponse rsp = request.response(); if (reply.succeeded()) { requestLog.debug("Got response"); JsonObject response = reply.result().body(); @@ -159,7 +162,7 @@ public boolean handle(final HttpServerRequest request) { try { if (response.fieldNames().contains(HEADERS)) { headers = JsonMultiMap.fromJson(response.getJsonArray(HEADERS)); - request.response().headers().setAll(headers); + rsp.headers().setAll(headers); } } catch (DecodeException e) { requestLog.warn("Wrong headers in reply", e); @@ -173,28 +176,28 @@ public boolean handle(final HttpServerRequest request) { } requestLog.debug("Response content type is {}", responseContentType); try { - request.response().setChunked(true); + rsp.setChunked(true); if (responseContentType != null && responseContentType.contains(APPLICATION_JSON)) { - request.response().end(response.getJsonObject(PAYLOAD).encode()); + rsp.end(response.getJsonObject(PAYLOAD).encode()); } else if (responseContentType != null && responseContentType.contains(TEXT)) { - request.response().end(response.getString(PAYLOAD)); + rsp.end(response.getString(PAYLOAD)); } else { - request.response().end(Buffer.buffer(response.getBinary(PAYLOAD))); + rsp.end(Buffer.buffer(response.getBinary(PAYLOAD))); } } catch (DecodeException e) { - requestLog.warn("Wrong payload in reply for content-type " + responseContentType, e); - request.response().setStatusCode(500); - request.response().end("Wrong payload in reply for content-type " + responseContentType + ": ", e.getMessage()); + requestLog.warn("Wrong payload in reply for content-type {}", responseContentType, e); + rsp.setStatusCode(500); + rsp.end("Wrong payload in reply for content-type " + responseContentType + ": "+ e.getMessage()); } } else { requestLog.debug("No payload in response"); - request.response().end(); + rsp.end(); } } else { - requestLog.debug("Timeout"); - request.response().setStatusCode(GATEWAY_TIMEOUT); - request.response().setChunked(true); - request.response().end("Gateway Timeout"); + requestLog.debug("Timeout", reply.cause()); + rsp.setStatusCode(GATEWAY_TIMEOUT); + rsp.setChunked(true); + rsp.end("Gateway Timeout"); } }); } else {