From a7b851778ed51f5f5f5dc52344a0d41cb8c5d460 Mon Sep 17 00:00:00 2001 From: Vasil Ilchev Date: Mon, 24 Nov 2025 17:13:13 +0200 Subject: [PATCH] Fix sonar findings (#2832) Co-authored-by: vasilchev --- .../JpaTenantConfigurationManagement.java | 77 +++++++++++-------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTenantConfigurationManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTenantConfigurationManagement.java index a8cb668e4..58715edba 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTenantConfigurationManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTenantConfigurationManagement.java @@ -184,41 +184,9 @@ public class JpaTenantConfigurationManagement implements TenantConfigurationMana configurations.forEach((keyName, value) -> { final TenantConfigurationKey configurationKey = tenantConfigurationProperties.fromKeyName(keyName); - Object convertedValue = value; final Class targetType = configurationKey.getDataType(); - if (!targetType.isAssignableFrom(value.getClass())) { - try { - // if not assignable and it is a number - try conversion - // for example tries to assign Integer to Long - if (value instanceof Number number && Number.class.isAssignableFrom(targetType)) { - log.debug("Type {} not assignable from {} . Will try conversion.", targetType, value.getClass()); - convertedValue = CONVERSION_SERVICE.convert(number, targetType); - if (convertedValue == null) { - throw new IllegalArgumentException( - String.format("Failed to convert %s. Convertor returned null as a result", value)); - } - } else { - throw new IllegalArgumentException( - String.format("Value %s is not a Number but %s and cannot perform conversion converted.", value, value.getClass())); - } - } catch (final ConversionException | IllegalArgumentException ex) { - throw new TenantConfigurationValidatorException(String.format( - "Cannot convert the value %s of type %s to the type %s defined by the configuration key.", - value, value.getClass(), targetType)); - } - } - - configurationKey.validate(convertedValue, applicationContext); - // additional validation for specific configuration keys - if (POLLING_TIME.equals(configurationKey.getKeyName())) { - final PollingTime pollingTime = new PollingTime(value.toString()); - if (!ObjectUtils.isEmpty(pollingTime.getOverrides())) { - // validate that the QL strings are valid RSQL queries, - // nevertheless always when parse them we shall be prepared to catch exceptions if the parsers - // has been changed in not backward compatible way - pollingTime.getOverrides().forEach(override -> QLSupport.getInstance().entityMatcher(override.qlStr(), TargetFields.class)); - } - } + Object convertedValue = getConvertedValue(value, targetType); + validateConfigurationValue(value, configurationKey, convertedValue); JpaTenantConfiguration tenantConfiguration = tenantConfigurationRepository.findByKey(configurationKey.getKeyName()); if (tenantConfiguration == null) { @@ -246,6 +214,47 @@ public class JpaTenantConfigurationManagement implements TenantConfigurationMana .build())); } + private void validateConfigurationValue(final T value, final TenantConfigurationKey configurationKey, final Object convertedValue) { + configurationKey.validate(convertedValue, applicationContext); + // additional validation for specific configuration keys + if (POLLING_TIME.equals(configurationKey.getKeyName())) { + final PollingTime pollingTime = new PollingTime(value.toString()); + if (!ObjectUtils.isEmpty(pollingTime.getOverrides())) { + // validate that the QL strings are valid RSQL queries, + // nevertheless always when parse them we shall be prepared to catch exceptions if the parsers + // has been changed in not backward compatible way + pollingTime.getOverrides().forEach(override -> QLSupport.getInstance().entityMatcher(override.qlStr(), TargetFields.class)); + } + } + } + + private static Object getConvertedValue(final T value, final Class targetType) { + Object convertedValue = value; + if (!targetType.isAssignableFrom(value.getClass())) { + try { + // if not assignable and it is a number - try conversion + // for example tries to assign Integer to Long + if (value instanceof Number number && Number.class.isAssignableFrom(targetType)) { + log.debug("Type {} not assignable from {} . Will try conversion.", targetType, value.getClass()); + convertedValue = CONVERSION_SERVICE.convert(number, targetType); + if (convertedValue == null) { + throw new IllegalArgumentException( + String.format("Failed to convert %s. Convertor returned null as a result", value)); + } + } else { + throw new IllegalArgumentException( + String.format("Value %s is not a Number but %s and cannot perform conversion converted.", value, value.getClass())); + } + } catch (final ConversionException | IllegalArgumentException ex) { + throw new TenantConfigurationValidatorException(String.format( + "Cannot convert the value %s of type %s to the type %s defined by the configuration key.", + value, value.getClass(), targetType)); + } + } + return convertedValue; + } + + @SuppressWarnings("unchecked") private TenantConfigurationValue getConfigurationValue0(final String keyName, final Class propertyType) { checkAccess(keyName);