From 23eba51106de9123e99a5f1bc5e7edebed9fe179 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Mon, 16 Oct 2023 17:33:07 +0200 Subject: [PATCH 1/3] Streamline UUIDConverter - Use StandardCharsets.UTF_8 as a charset configuration instead of string. Later remove the necessity of handling encoder errors. - Use regular expressions to validate the UUID string standard representation. Later obsolete the need for try/catch exceptions. --- .../integration/util/UUIDConverter.java | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java index 2c4283ecc4e..00c51e9a9f8 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,10 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.UUID; +import java.util.regex.Pattern; import org.springframework.core.convert.converter.Converter; import org.springframework.util.ClassUtils; @@ -30,15 +32,17 @@ * * @author Dave Syer * @author Gary Russell + * @author Christian Tzolov */ public class UUIDConverter implements Converter { - public static final String DEFAULT_CHARSET = "UTF-8"; + public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + private static final Pattern UUID_REGEX = Pattern + .compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); /** - * Convert the input to a UUID using the convenience method - * {@link #getUUID(Object)}. + * Convert the input to a UUID using the convenience method {@link #getUUID(Object)}. * * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object) */ @@ -52,14 +56,10 @@ public UUID convert(Object source) { * * If none of the above applies there will be an exception trying to serialize. * @@ -74,28 +74,16 @@ public static UUID getUUID(Object input) { return (UUID) input; } if (input instanceof String) { - try { - return UUID.fromString((String) input); + String inputText = (String) input; + if (isValidUuidString(inputText)) { + return UUID.fromString(inputText); } - catch (Exception e) { - try { - return UUID.nameUUIDFromBytes(((String) input).getBytes(DEFAULT_CHARSET)); - } - catch (UnsupportedEncodingException ex) { - IllegalStateException exception = - new IllegalStateException("Cannot convert String using charset=" + DEFAULT_CHARSET, ex); - exception.addSuppressed(e); - throw exception; // NOSONAR - added to suppressed exceptions - } + else { + return UUID.nameUUIDFromBytes((inputText).getBytes(DEFAULT_CHARSET)); } } if (ClassUtils.isPrimitiveOrWrapper(input.getClass())) { - try { - return UUID.nameUUIDFromBytes(input.toString().getBytes(DEFAULT_CHARSET)); - } - catch (UnsupportedEncodingException e) { - throw new IllegalStateException("Cannot convert primitive using charset=" + DEFAULT_CHARSET, e); - } + return UUID.nameUUIDFromBytes(input.toString().getBytes(DEFAULT_CHARSET)); } byte[] bytes = serialize(input); return UUID.nameUUIDFromBytes(bytes); @@ -115,4 +103,14 @@ private static byte[] serialize(Object object) { return stream.toByteArray(); } + /** + * Verifies if the provided UUID string complies with the standard representation. + * + * @param uuid UUID text to verify. + * @return Returns true for string standard representation and false otherwise. + */ + private static boolean isValidUuidString(String uuid) { + return UUID_REGEX.matcher(uuid).matches(); + } + } From e88e79a1092c803bf4e7605f1c3de8b2d33a9a9e Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Mon, 16 Oct 2023 19:43:33 +0200 Subject: [PATCH 2/3] address review comments --- .../integration/util/UUIDConverter.java | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java index 00c51e9a9f8..525e5cc3674 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java @@ -19,7 +19,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.UUID; import java.util.regex.Pattern; @@ -36,14 +35,18 @@ */ public class UUIDConverter implements Converter { - public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + /** + * @deprecated since 6.2 as it is not used internally by the UUIDConverter. The internal implementation relies, now, + * on StandardCharsets.UTF_8 instead. + */ + @Deprecated + public static final String DEFAULT_CHARSET = "UTF-8"; private static final Pattern UUID_REGEX = Pattern .compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); /** * Convert the input to a UUID using the convenience method {@link #getUUID(Object)}. - * * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object) */ @Override @@ -62,7 +65,6 @@ public UUID convert(Object source) { *
  • Serializable: returns the {@link UUID#nameUUIDFromBytes(byte[])} with the serialized bytes of the input
  • * * If none of the above applies there will be an exception trying to serialize. - * * @param input an Object * @return a UUID constructed from the input */ @@ -75,15 +77,15 @@ public static UUID getUUID(Object input) { } if (input instanceof String) { String inputText = (String) input; - if (isValidUuidString(inputText)) { + if (isValidUuidStringRepresentation(inputText)) { return UUID.fromString(inputText); } else { - return UUID.nameUUIDFromBytes((inputText).getBytes(DEFAULT_CHARSET)); + return UUID.nameUUIDFromBytes((inputText).getBytes(StandardCharsets.UTF_8)); } } if (ClassUtils.isPrimitiveOrWrapper(input.getClass())) { - return UUID.nameUUIDFromBytes(input.toString().getBytes(DEFAULT_CHARSET)); + return UUID.nameUUIDFromBytes(input.toString().getBytes(StandardCharsets.UTF_8)); } byte[] bytes = serialize(input); return UUID.nameUUIDFromBytes(bytes); @@ -103,13 +105,7 @@ private static byte[] serialize(Object object) { return stream.toByteArray(); } - /** - * Verifies if the provided UUID string complies with the standard representation. - * - * @param uuid UUID text to verify. - * @return Returns true for string standard representation and false otherwise. - */ - private static boolean isValidUuidString(String uuid) { + private static boolean isValidUuidStringRepresentation(String uuid) { return UUID_REGEX.matcher(uuid).matches(); } From f9506de4688afb95c827f9c504f7f5f07f3a3c29 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 16 Oct 2023 14:02:24 -0400 Subject: [PATCH 3/3] Update since version to `6.0.8` in the deprecation Javadoc for `DEFAULT_CHARSET` --- .../org/springframework/integration/util/UUIDConverter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java index 525e5cc3674..14ce2872fa7 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java @@ -36,7 +36,7 @@ public class UUIDConverter implements Converter { /** - * @deprecated since 6.2 as it is not used internally by the UUIDConverter. The internal implementation relies, now, + * @deprecated since 6.0.8 as it is not used internally by the UUIDConverter. The internal implementation relies, now, * on StandardCharsets.UTF_8 instead. */ @Deprecated