From 900389fb721eea9d793e68de72fe9ec9cef9cfdb Mon Sep 17 00:00:00 2001 From: caalador Date: Mon, 23 Sep 2024 16:07:48 +0300 Subject: [PATCH] chore: More detailed logging for ignored handling (#20007) (#20028) Make log more detailed for ignored invocation handle. --- .../vaadin/flow/component/ComponentUtil.java | 19 ++++++++ .../rpc/AbstractRpcInvocationHandler.java | 47 +++++++++++++++---- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java b/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java index 85982102d50..1290e6d041f 100644 --- a/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java +++ b/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java @@ -41,6 +41,7 @@ import com.vaadin.flow.internal.StateNode; import com.vaadin.flow.internal.StateTree; import com.vaadin.flow.internal.nodefeature.VirtualChildrenList; +import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Router; import com.vaadin.flow.server.Attributes; import com.vaadin.flow.server.VaadinService; @@ -647,4 +648,22 @@ public static Router getRouter(HasElement component) { return router; } + /** + * Walk up from given component until a Component with a Route annotation is + * found or empty if no Route is present in parents. + * + * @param component + * Component to find current route component for + * @return Optional containing Route component if found + */ + public static Optional getRouteComponent(Component component) { + if (component.getClass().isAnnotationPresent(Route.class)) { + return Optional.of(component); + } + if (component.getParent().isPresent()) { + return getRouteComponent(component.getParent().get()); + } + return Optional.empty(); + } + } diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/rpc/AbstractRpcInvocationHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/rpc/AbstractRpcInvocationHandler.java index bd68eaf4914..265129645ed 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/rpc/AbstractRpcInvocationHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/rpc/AbstractRpcInvocationHandler.java @@ -19,16 +19,21 @@ import java.util.List; import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.vaadin.flow.component.Component; +import com.vaadin.flow.component.ComponentUtil; import com.vaadin.flow.component.PollEvent; import com.vaadin.flow.component.UI; +import com.vaadin.flow.dom.Element; import com.vaadin.flow.internal.StateNode; +import com.vaadin.flow.internal.nodefeature.ElementData; +import com.vaadin.flow.router.Route; import com.vaadin.flow.shared.JsonConstants; import elemental.json.JsonObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Abstract invocation handler implementation with common methods. *

@@ -60,21 +65,43 @@ public Optional handle(UI ui, JsonObject invocationJson) { // ignore RPC requests from the client side for the nodes that are // invisible, disabled or inert if (node.isInactive()) { - getLogger().trace("Ignored RPC for invocation handler '{}' from " - + "the client side for an inactive (disabled or invisible) node id='{}'", - getClass().getName(), node.getId()); + logHandlingIgnoredMessage(node, "inactive (disabled or invisible)"); return Optional.empty(); } else if (!allowInert(ui, invocationJson) && node.isInert()) { - getLogger().trace( - "Ignored RPC for invocation handler '{}' from " - + "the client side for an inert node id='{}'", - getClass().getName(), node.getId()); + logHandlingIgnoredMessage(node, "inert"); return Optional.empty(); } else { return handleNode(node, invocationJson); } } + private void logHandlingIgnoredMessage(StateNode node, String reason) { + StringBuilder targetInfo = new StringBuilder(); + if (node != null && node.hasFeature(ElementData.class)) { + Element element = Element.get(node); + Optional component = element.getComponent(); + targetInfo.append(" element with tag").append("'") + .append(element.getTag()).append("'"); + if (component.isPresent()) { + targetInfo.append(" Component: ").append("'") + .append(component.get().getClass().getName()) + .append("'"); + Optional routeComponent = ComponentUtil + .getRouteComponent(component.get()); + if (routeComponent.isPresent()) { + targetInfo.append(" Route: ").append("'") + .append(routeComponent.get().getClass() + .getAnnotation(Route.class).value()) + .append("'"); + } + } + } + getLogger().info( + "Ignored RPC for invocation handler '{}' from " + + "the client side for an {} node id='{}'{}", + getClass().getName(), reason, node.getId(), targetInfo); + } + /** * Checks whether a Poll RPC invocation is valid or not. *