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
35
+ * @author Artem Bilan
33
36
*/
34
37
public class UUIDConverter implements Converter <Object , UUID > {
35
38
39
+ /**
40
+ * @deprecated since 6.0.8 as it is not used internally by the UUIDConverter.
41
+ * The internal implementation relies on {@link StandardCharsets#UTF_8} instead.
42
+ */
43
+ @ Deprecated
36
44
public static final String DEFAULT_CHARSET = "UTF-8" ;
37
45
46
+ private static final Pattern UUID_REGEX =
47
+ 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}$" );
38
48
39
49
/**
40
- * Convert the input to a UUID using the convenience method
41
- * {@link #getUUID(Object)}.
42
- *
50
+ * Convert the input to a UUID using the convenience method {@link #getUUID(Object)}.
43
51
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
44
52
*/
45
53
@ Override
@@ -52,17 +60,12 @@ public UUID convert(Object source) {
52
60
* <ul>
53
61
* <li>null: returns null</li>
54
62
* <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>
63
+ * <li>a String formatted as a UUID: returns the result of {@link UUID#fromString(String)}</li>
64
+ * <li>any other String: returns {@link UUID#nameUUIDFromBytes(byte[])} with bytes generated from the input</li>
65
+ * <li>a primitive or primitive wrapper: converts to a String ans then uses the previous conversion method</li>
66
+ * <li>Serializable: returns the {@link UUID#nameUUIDFromBytes(byte[])} with the serialized bytes of the input</li>
63
67
* </ul>
64
68
* If none of the above applies there will be an exception trying to serialize.
65
- *
66
69
* @param input an Object
67
70
* @return a UUID constructed from the input
68
71
*/
@@ -73,34 +76,25 @@ public static UUID getUUID(Object input) {
73
76
if (input instanceof UUID ) {
74
77
return (UUID ) input ;
75
78
}
76
- if (input instanceof String ) {
77
- try {
78
- return UUID .fromString (( String ) input );
79
+ if (input instanceof String inputText ) {
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 fromStringBytes (inputText );
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 fromStringBytes (input .toString ());
99
89
}
100
90
byte [] bytes = serialize (input );
101
91
return UUID .nameUUIDFromBytes (bytes );
102
92
}
103
93
94
+ private static UUID fromStringBytes (String input ) {
95
+ return UUID .nameUUIDFromBytes (input .getBytes (StandardCharsets .UTF_8 ));
96
+ }
97
+
104
98
private static byte [] serialize (Object object ) {
105
99
if (object == null ) {
106
100
return null ;
@@ -109,10 +103,14 @@ private static byte[] serialize(Object object) {
109
103
try {
110
104
new ObjectOutputStream (stream ).writeObject (object );
111
105
}
112
- catch (IOException e ) {
113
- throw new IllegalArgumentException ("Could not serialize object of type: " + object .getClass (), e );
106
+ catch (IOException ex ) {
107
+ throw new IllegalArgumentException ("Could not serialize object of type: " + object .getClass (), ex );
114
108
}
115
109
return stream .toByteArray ();
116
110
}
117
111
112
+ private static boolean isValidUuidStringRepresentation (String uuid ) {
113
+ return UUID_REGEX .matcher (uuid ).matches ();
114
+ }
115
+
118
116
}
0 commit comments