1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .io .ByteArrayOutputStream ;
20
20
import java .io .IOException ;
21
21
import java .io .ObjectOutputStream ;
22
- import java .io . UnsupportedEncodingException ;
22
+ import java .nio . charset . StandardCharsets ;
23
23
import java .util .UUID ;
24
+ import java .util .regex .Pattern ;
24
25
25
26
import org .springframework .core .convert .converter .Converter ;
26
27
import org .springframework .util .ClassUtils ;
30
31
*
31
32
* @author Dave Syer
32
33
* @author Gary Russell
34
+ * @author Christian Tzolov
33
35
*/
34
36
public class UUIDConverter implements Converter <Object , UUID > {
35
37
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
36
43
public static final String DEFAULT_CHARSET = "UTF-8" ;
37
44
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}$" );
38
47
39
48
/**
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)}.
43
50
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
44
51
*/
45
52
@ Override
@@ -52,17 +59,12 @@ public UUID convert(Object source) {
52
59
* <ul>
53
60
* <li>null: returns null</li>
54
61
* <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>
63
66
* </ul>
64
67
* If none of the above applies there will be an exception trying to serialize.
65
- *
66
68
* @param input an Object
67
69
* @return a UUID constructed from the input
68
70
*/
@@ -74,28 +76,16 @@ public static UUID getUUID(Object input) {
74
76
return (UUID ) input ;
75
77
}
76
78
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 );
79
82
}
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 ));
90
85
}
91
86
}
92
87
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 ));
99
89
}
100
90
byte [] bytes = serialize (input );
101
91
return UUID .nameUUIDFromBytes (bytes );
@@ -115,4 +105,8 @@ private static byte[] serialize(Object object) {
115
105
return stream .toByteArray ();
116
106
}
117
107
108
+ private static boolean isValidUuidStringRepresentation (String uuid ) {
109
+ return UUID_REGEX .matcher (uuid ).matches ();
110
+ }
111
+
118
112
}
0 commit comments