Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,25 @@ public SELF withData(String dataContentType, URI dataSchema, CloudEventData data
}

public SELF withExtension(@Nonnull String key, String value) {
if(!isValidExtensionName(key)){
throw CloudEventRWException.newInvalidExtensionName(key);
}
this.extensions.put(key, value);
return self;
}

public SELF withExtension(@Nonnull String key, Number value) {
if(!isValidExtensionName(key)){
throw CloudEventRWException.newInvalidExtensionName(key);
}
this.extensions.put(key, value);
return self;
}

public SELF withExtension(@Nonnull String key, Boolean value) {
if(!isValidExtensionName(key)){
throw CloudEventRWException.newInvalidExtensionName(key);
}
this.extensions.put(key, value);
return self;
}
Expand Down Expand Up @@ -151,4 +160,25 @@ public CloudEvent end() {
protected static IllegalStateException createMissingAttributeException(String attributeName) {
return new IllegalStateException("Attribute '" + attributeName + "' cannot be null");
}
/**
* Validates the extension name as defined in CloudEvents spec
* See <a href="https://github.com/cloudevents/spec/blob/master/spec.md#attribute-naming-convention">attribute-naming-convention</a>
* @param name the extension name
* @return true if extension name is valid, false otherwise
*/
private static boolean isValidExtensionName(String name) {
if(name.length() > 20){
return false;
}
char[] chars = name.toCharArray();
for (char c: chars)
if (!isValidChar(c)) {
return false;
}
return true;
}

private static boolean isValidChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.extensions.DistributedTracingExtension;
import io.cloudevents.core.test.Data;
import io.cloudevents.rw.CloudEventRWException;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BaseCloudEventBuilderTest {

Expand Down Expand Up @@ -45,4 +48,28 @@ public void copyAndRemoveMaterializedExtension() {
.isEqualTo(have);
}

@Test
public void testLongExtensionName() {
Exception exception = assertThrows(RuntimeException.class, () -> {
CloudEvent cloudEvent = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
.withExtension("thisextensionnameistoolong", "")
.build();
});
String expectedMessage = "Invalid extensions name: thisextensionnameistoolong";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
@Test
public void testInvalidExtensionName() {
Exception exception = assertThrows(RuntimeException.class, () -> {
CloudEvent cloudEvent = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
.withExtension("ExtensionName", "")
.build();
});
String expectedMessage = "Invalid extensions name: ExtensionName";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
}