Skip to content

Commit

Permalink
Use pattern for validating extension names, and correct url to spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerard Klijs committed Sep 14, 2022
1 parent b9eaa2f commit 4e83137
Showing 1 changed file with 4 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

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

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

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

protected CloudEventData data;
protected Map<String, Object> extensions = new HashMap<>();
Expand Down Expand Up @@ -213,19 +215,10 @@ protected static IllegalStateException createMissingAttributeException(String at
*
* @param name the extension name
* @return true if extension name is valid, false otherwise
* @see <a href="https://github.com/cloudevents/spec/blob/master/spec.md#attribute-naming-convention">attribute-naming-convention</a>
* @see <a href="https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#naming-conventions">attribute-naming-conventions</a>
*/
private static boolean isValidExtensionName(String name) {
for (int i = 0; i < name.length(); i++) {
if (!isValidChar(name.charAt(i))) {
return false;
}
}
return true;
}

private static boolean isValidChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
return EXTENSION_NAME_PATTERN.matcher(name).matches();
}

protected void requireValidAttributeWrite(String name) {
Expand Down

0 comments on commit 4e83137

Please sign in to comment.