Skip to content

Commit 77d8dfd

Browse files
authored
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. * Deprecate `UUIDConverter.DEFAULT_CHARSET` since it is out of use. **Cherry-pick to `6.1.x` & `6.0.x`**
1 parent 25630f5 commit 77d8dfd

File tree

1 file changed

+26
-32
lines changed
  • spring-integration-core/src/main/java/org/springframework/integration/util

1 file changed

+26
-32
lines changed

spring-integration-core/src/main/java/org/springframework/integration/util/UUIDConverter.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,9 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
2121
import java.io.ObjectOutputStream;
22-
import java.io.UnsupportedEncodingException;
22+
import java.nio.charset.StandardCharsets;
2323
import java.util.UUID;
24+
import java.util.regex.Pattern;
2425

2526
import org.springframework.core.convert.converter.Converter;
2627
import org.springframework.util.ClassUtils;
@@ -30,16 +31,22 @@
3031
*
3132
* @author Dave Syer
3233
* @author Gary Russell
34+
* @author Christian Tzolov
3335
*/
3436
public class UUIDConverter implements Converter<Object, UUID> {
3537

38+
/**
39+
* @deprecated since 6.0.8 as it is not used internally by the UUIDConverter. The internal implementation relies, now,
40+
* on StandardCharsets.UTF_8 instead.
41+
*/
42+
@Deprecated
3643
public static final String DEFAULT_CHARSET = "UTF-8";
3744

45+
private static final Pattern UUID_REGEX = Pattern
46+
.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}$");
3847

3948
/**
40-
* Convert the input to a UUID using the convenience method
41-
* {@link #getUUID(Object)}.
42-
*
49+
* Convert the input to a UUID using the convenience method {@link #getUUID(Object)}.
4350
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
4451
*/
4552
@Override
@@ -52,17 +59,12 @@ public UUID convert(Object source) {
5259
* <ul>
5360
* <li>null: returns null</li>
5461
* <li>a UUID: returns the input unchanged</li>
55-
* <li>a String formatted as a UUID: returns the result of
56-
* {@link UUID#fromString(String)}</li>
57-
* <li>any other String: returns {@link UUID#nameUUIDFromBytes(byte[])} with
58-
* bytes generated from the input</li>
59-
* <li>a primitive or primitive wrapper: converts to a String ans then uses
60-
* the previous conversion method</li>
61-
* <li>Serializable: returns the {@link UUID#nameUUIDFromBytes(byte[])} with
62-
* the serialized bytes of the input</li>
62+
* <li>a String formatted as a UUID: returns the result of {@link UUID#fromString(String)}</li>
63+
* <li>any other String: returns {@link UUID#nameUUIDFromBytes(byte[])} with bytes generated from the input</li>
64+
* <li>a primitive or primitive wrapper: converts to a String ans then uses the previous conversion method</li>
65+
* <li>Serializable: returns the {@link UUID#nameUUIDFromBytes(byte[])} with the serialized bytes of the input</li>
6366
* </ul>
6467
* If none of the above applies there will be an exception trying to serialize.
65-
*
6668
* @param input an Object
6769
* @return a UUID constructed from the input
6870
*/
@@ -74,28 +76,16 @@ public static UUID getUUID(Object input) {
7476
return (UUID) input;
7577
}
7678
if (input instanceof String) {
77-
try {
78-
return UUID.fromString((String) input);
79+
String inputText = (String) input;
80+
if (isValidUuidStringRepresentation(inputText)) {
81+
return UUID.fromString(inputText);
7982
}
80-
catch (Exception e) {
81-
try {
82-
return UUID.nameUUIDFromBytes(((String) input).getBytes(DEFAULT_CHARSET));
83-
}
84-
catch (UnsupportedEncodingException ex) {
85-
IllegalStateException exception =
86-
new IllegalStateException("Cannot convert String using charset=" + DEFAULT_CHARSET, ex);
87-
exception.addSuppressed(e);
88-
throw exception; // NOSONAR - added to suppressed exceptions
89-
}
83+
else {
84+
return UUID.nameUUIDFromBytes((inputText).getBytes(StandardCharsets.UTF_8));
9085
}
9186
}
9287
if (ClassUtils.isPrimitiveOrWrapper(input.getClass())) {
93-
try {
94-
return UUID.nameUUIDFromBytes(input.toString().getBytes(DEFAULT_CHARSET));
95-
}
96-
catch (UnsupportedEncodingException e) {
97-
throw new IllegalStateException("Cannot convert primitive using charset=" + DEFAULT_CHARSET, e);
98-
}
88+
return UUID.nameUUIDFromBytes(input.toString().getBytes(StandardCharsets.UTF_8));
9989
}
10090
byte[] bytes = serialize(input);
10191
return UUID.nameUUIDFromBytes(bytes);
@@ -115,4 +105,8 @@ private static byte[] serialize(Object object) {
115105
return stream.toByteArray();
116106
}
117107

108+
private static boolean isValidUuidStringRepresentation(String uuid) {
109+
return UUID_REGEX.matcher(uuid).matches();
110+
}
111+
118112
}

0 commit comments

Comments
 (0)