Clean up and simplifications around CloudEvent processing

This commit effectively a merge of work with #607 and simplifies the following
- CloudEventAttributesProvider now provides CloudEventAttributes initialized with defaults to be set by the user
- In HTTP RequestProcessor the logic of sanitizing headers was improved to ensure that correct prefix is applied

Resolves #607
This commit is contained in:
Oleg Zhurakousky
2020-11-16 13:40:53 +01:00
parent a0208ceadd
commit ac54a83e09
11 changed files with 152 additions and 206 deletions

View File

@@ -21,7 +21,7 @@ import java.util.function.Function;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.function.cloudevent.CloudEventAttributesHelper;
import org.springframework.cloud.function.cloudevent.CloudEventAttributes;
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
import org.springframework.context.annotation.Bean;
@@ -84,20 +84,21 @@ public class CloudeventDemoApplication {
}
@Bean
public Function<Message<SpringReleaseEvent>, Message<SpringReleaseEvent>> consumeAndProduceCloudEvent(CloudEventAttributesProvider ceAttrProvider) {
public Function<Message<SpringReleaseEvent>, Message<SpringReleaseEvent>> consumeAndProduceCloudEvent() {
return ceMessage -> {
SpringReleaseEvent data = ceMessage.getPayload();
data.setVersion("2.0");
data.setReleaseDateAsString("01-10-2006");
CloudEventAttributesHelper ceAttributes = CloudEventMessageUtils.get(ceMessage.getHeaders())
.setSource("https://interface21.com/")
.setType("com.interface21");
return MessageBuilder.withPayload(data).copyHeaders(ceAttributes).build();
return MessageBuilder.withPayload(data).build();
};
}
@Bean
public CloudEventAttributesProvider cloudEventAttributesProvider() {
return attributes -> attributes.setSource("https://interface21.com/").setType("com.interface21");
}
@Bean
public Function<Map<String, Object>, Map<String, Object>> consumeAndProduceCloudEventAsMapToMap() {

View File

@@ -262,6 +262,10 @@ public class CloudeventDemoApplicationRESTTests {
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE))
.isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName()));
assertThat(response.getHeaders().get(CloudEventMessageUtils.ATTR_PREFIX + CloudEventMessageUtils.TYPE)).isNull();
assertThat(response.getHeaders().get(CloudEventMessageUtils.ATTR_PREFIX + CloudEventMessageUtils.SOURCE)).isNull();
assertThat(response.getHeaders().get(CloudEventMessageUtils.ATTR_PREFIX + CloudEventMessageUtils.ID)).isNull();
assertThat(response.getHeaders().get(CloudEventMessageUtils.ATTR_PREFIX + CloudEventMessageUtils.SPECVERSION)).isNull();
}