8000 Use pattern for validating extension names, and correct url to spec. · cloudevents/sdk-java@4e83137 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e83137

Browse files
author
Gerard Klijs
committed
Use pattern for validating extension names, and correct url to spec.
1 parent b9eaa2f commit 4e83137

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import java.time.OffsetDateTime;
3131
import java.util.HashMap;
3232
import java.util.Map;
33+
import java.util.regex.Pattern;
3334

3435
import static io.cloudevents.core.v03.CloudEventV03.SPECVERSION;
3536

3637
public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<SELF, T>, T extends CloudEvent> implements CloudEventBuilder {
3738

3839
// This is a little trick for enabling fluency
3940
private final SELF self;
41+
private static final Pattern EXTENSION_NAME_PATTERN = Pattern.compile("^([\\da-z])+$");
4042

4143
protected CloudEventData data;
4244
protected Map<String, Object> extensions = new HashMap<>();
@@ -213,19 +215,10 @@ protected static IllegalStateException createMissingAttributeException(String at
213215
*
214216
* @param name the extension name
215217
* @return true if extension name is valid, false otherwise
216-
* @see <a href="https://github.com/cloudevents/spec/blob/master/spec.md#attribute-naming-convention">attribute-naming-convention</a>
218+
* @see <a href="https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#naming-conventions">attribute-naming-conventions</a>
217219
*/
218220
private static boolean isValidExtensionName(String name) {
219-
for (int i = 0; i < name.length(); i++) {
220-
if (!isValidChar(name.charAt(i))) {
221-
return false;
222-
}
223-
}
224-
return true;
225-
}
226-
227-
private static boolean isValidChar(char c) {
228-
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
221+
return EXTENSION_NAME_PATTERN.matcher(name).matches();
229222
}
230223

231224
protected void requireValidAttributeWrite(String name) {

0 commit comments

Comments
 (0)
0