From 741add9f21c78acbdf4b9c4f645403d4b4e88c65 Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Fri, 10 Oct 2025 12:58:49 +0300 Subject: [PATCH] Add deprecation log utility - easy way to log usage of deprecated methods (#2741) Signed-off-by: Avgustin Marinov --- .../rest/resource/MgmtActionResource.java | 30 +++----------- .../rest/resource/MgmtTargetTagResource.java | 9 ++-- .../mgmt/rest/resource/util/LogUtility.java | 41 +++++++++++++++++++ 3 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/util/LogUtility.java diff --git a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtActionResource.java b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtActionResource.java index a4478a378..b4b896d4d 100644 --- a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtActionResource.java +++ b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtActionResource.java @@ -11,23 +11,17 @@ package org.eclipse.hawkbit.mgmt.rest.resource; import static org.eclipse.hawkbit.mgmt.rest.resource.util.PagingUtility.sanitizeActionSortParam; -import java.util.Optional; - -import jakarta.servlet.http.HttpServletRequest; - import lombok.extern.slf4j.Slf4j; import org.eclipse.hawkbit.mgmt.json.model.PagedList; import org.eclipse.hawkbit.mgmt.json.model.action.MgmtAction; import org.eclipse.hawkbit.mgmt.rest.api.MgmtActionRestApi; import org.eclipse.hawkbit.mgmt.rest.api.MgmtRepresentationMode; import org.eclipse.hawkbit.mgmt.rest.resource.mapper.MgmtActionMapper; +import org.eclipse.hawkbit.mgmt.rest.resource.util.LogUtility; import org.eclipse.hawkbit.mgmt.rest.resource.util.PagingUtility; import org.eclipse.hawkbit.repository.DeploymentManagement; import org.eclipse.hawkbit.repository.exception.EntityNotFoundException; import org.eclipse.hawkbit.repository.model.Action; -import org.eclipse.hawkbit.rest.util.RequestResponseContextHolder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; @@ -45,28 +39,13 @@ public class MgmtActionResource implements MgmtActionRestApi { this.deploymentManagement = deploymentManagement; } - private static final Logger LOGGER = LoggerFactory.getLogger("DEPRECATED_USAGE"); @Override public ResponseEntity> getActions( final String rsqlParam, final int pagingOffsetParam, final int pagingLimitParam, final String sortParam, final String representationModeParam) { - try { - if (LOGGER.isDebugEnabled()) { - final HttpServletRequest httpServletRequest = RequestResponseContextHolder.getHttpServletRequest(); - final String remoteHost = httpServletRequest.getRemoteHost(); - final String refererOrOrigin = Optional.ofNullable(httpServletRequest.getHeader("Referer")) - .map(referer -> "Referer: " + referer) - .orElseGet(() -> "Origin: " + httpServletRequest.getHeader("Origin")); - if (rsqlParam != null && rsqlParam.contains("status")) { - LOGGER.debug("[DEPRECATED] [{}/{}] Usage of getActions with RSQL that is up to modification: rsql='{}'.", - remoteHost, refererOrOrigin, rsqlParam); - } else { - LOGGER.debug("[DEPRECATED] [{}/{}] Usage of getActions:result that is up to modification.", remoteHost, refererOrOrigin); - } - } - } catch (final Exception e) { - LOGGER.error("[DEPRECATED] Unexpected logging exception!", e); - } + LogUtility.logDeprecated(rsqlParam != null && rsqlParam.contains("status") + ? "Usage of getActions with RSQL that is up to modification: rsql=" + rsqlParam + : "Usage of getActions:result that is up to modification."); final Pageable pageable = PagingUtility.toPageable(pagingOffsetParam, pagingLimitParam, sanitizeActionSortParam(sortParam)); final Slice actions; @@ -85,6 +64,7 @@ public class MgmtActionResource implements MgmtActionRestApi { @Override public ResponseEntity getAction(final Long actionId) { + LogUtility.logDeprecated("Usage of getActions:result that is up to modification."); final Action action = deploymentManagement.findAction(actionId) .orElseThrow(() -> new EntityNotFoundException(Action.class, actionId)); diff --git a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java index 7c54f3cd6..a8ee677d6 100644 --- a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java +++ b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java @@ -24,6 +24,7 @@ import org.eclipse.hawkbit.mgmt.json.model.target.MgmtTarget; import org.eclipse.hawkbit.mgmt.rest.api.MgmtTargetTagRestApi; import org.eclipse.hawkbit.mgmt.rest.resource.mapper.MgmtTagMapper; import org.eclipse.hawkbit.mgmt.rest.resource.mapper.MgmtTargetMapper; +import org.eclipse.hawkbit.mgmt.rest.resource.util.LogUtility; import org.eclipse.hawkbit.mgmt.rest.resource.util.PagingUtility; import org.eclipse.hawkbit.repository.TargetManagement; import org.eclipse.hawkbit.repository.TargetTagManagement; @@ -33,8 +34,6 @@ import org.eclipse.hawkbit.repository.model.Target; import org.eclipse.hawkbit.repository.model.TargetTag; import org.eclipse.hawkbit.security.SystemSecurityContext; import org.eclipse.hawkbit.utils.TenantConfigHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; @@ -159,10 +158,10 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi { return ResponseEntity.ok().build(); } - private static final Logger LOGGER = LoggerFactory.getLogger("DEPRECATED_USAGE"); @Override - public ResponseEntity assignTargetsPut(final Long targetTagId, final List controllerIds, final OnNotFoundPolicy onNotFoundPolicy) { - LOGGER.debug("[DEPRECATED] Deprecated usage of assignTargetsPut. Use assignTargetsPut (POST) instead."); + public ResponseEntity assignTargetsPut( + final Long targetTagId, final List controllerIds, final OnNotFoundPolicy onNotFoundPolicy) { + LogUtility.logDeprecated("Deprecated usage of assignTargetsPut. Use assignTargetsPut (POST) instead."); return assignTargets(targetTagId, controllerIds, onNotFoundPolicy); } diff --git a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/util/LogUtility.java b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/util/LogUtility.java new file mode 100644 index 000000000..f315a53ad --- /dev/null +++ b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/util/LogUtility.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2025 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.hawkbit.mgmt.rest.resource.util; + +import java.util.Optional; + +import jakarta.servlet.http.HttpServletRequest; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.eclipse.hawkbit.rest.util.RequestResponseContextHolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class LogUtility { + + public static final Logger LOGGER = LoggerFactory.getLogger("DEPRECATED_USAGE"); + + public static void logDeprecated(final String message) { + try { + if (LOGGER.isDebugEnabled()) { + final HttpServletRequest httpServletRequest = RequestResponseContextHolder.getHttpServletRequest(); + final String remoteHost = httpServletRequest.getRemoteHost(); + final String refererOrOrigin = Optional.ofNullable(httpServletRequest.getHeader("Referer")) + .map(referer -> "Referer: " + referer) + .orElseGet(() -> "Origin: " + httpServletRequest.getHeader("Origin")); + LOGGER.debug("[DEPRECATED] [{}/{}] {}", remoteHost, refererOrOrigin, message); + } + } catch (final Exception e) { + LOGGER.error("[DEPRECATED] Unexpected logging exception!", e); + } + } +}