A getAtttribute(String name) {
- return (A) this.get(name);
- }
-
- public boolean isValidCloudEvent() {
- return StringUtils.hasText(this.getId())
- && StringUtils.hasText(this.getSource())
- && StringUtils.hasText(this.getSpecversion())
- && StringUtils.hasText(this.getType());
+ return this;
}
}
diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventAttributesProvider.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventAttributesProvider.java
index 2833e38bc..721dd91d2 100644
--- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventAttributesProvider.java
+++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventAttributesProvider.java
@@ -18,6 +18,20 @@ package org.springframework.cloud.function.cloudevent;
/**
+ * Strategy that should be implemented by the user to help with outgoing Cloud Event attributes.
+ *
+ * The provided `attributes` are already initialized with default values, so you can only set the ones that you need.
+ *
+ * Once implemented, simply configure it as a bean and the framework will invoke it before the outbound Cloud Event Message is finalized.
+ *
+ * {@code
+ * @Bean
+ * public CloudEventAttributesProvider cloudEventAttributesProvider() {
+ * return attributes -> {
+ * attributes.setSource("https://interface21.com/").setType("com.interface21");
+ * };
+ * }}
+ *
*
* @author Oleg Zhurakousky
* @author Dave Syer
diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java
index e3a4832b4..af30a2bb8 100644
--- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java
+++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java
@@ -35,7 +35,7 @@ import org.springframework.util.StringUtils;
/**
* Miscellaneous utility methods to deal with Cloud Events - https://cloudevents.io/.
*
- * Mainly for internal use within the framework;
+ * Primarily intended for the internal use within the framework;
*
* @author Oleg Zhurakousky
* @author Dave Syer
@@ -202,20 +202,6 @@ public final class CloudEventMessageUtils {
return get(UUID.randomUUID().toString(), "1.0", ce_source, ce_type);
}
-// /**
-// * Will construct instance of {@link CloudEventAttributes} from {@link MessageHeaders}.
-// *
-// * Should copy Cloud Event related headers into an instance of {@link CloudEventAttributes}
-// * NOTE: Certain headers must not be copied.
-// *
-// * @param headers instance of {@link MessageHeaders}
-// * @return modifiable instance of {@link CloudEventAttributes}
-// */
-// public static CloudEventAttributes get(MessageHeaders headers) {
-// return new CloudEventAttributes(headers);
-// }
-
-
@SuppressWarnings("unchecked")
public static Message> toBinary(Message> inputMessage, MessageConverter messageConverter) {
@@ -252,32 +238,6 @@ public final class CloudEventMessageUtils {
return inputMessage;
}
- private static Message> buildCeMessageFromStructured(Map structuredCloudEvent, MessageHeaders originalHeaders) {
- String prefixToUse = determinePrefixToUse(originalHeaders);
- Object data = null;
- if (structuredCloudEvent.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATA)) {
- data = structuredCloudEvent.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATA);
- structuredCloudEvent.remove(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATA);
- }
- else if (structuredCloudEvent.containsKey(CloudEventMessageUtils.CANONICAL_DATA)) {
- data = structuredCloudEvent.get(CloudEventMessageUtils.CANONICAL_DATA);
- structuredCloudEvent.remove(CloudEventMessageUtils.CANONICAL_DATA);
- }
- else if (structuredCloudEvent.containsKey(CloudEventMessageUtils.DATA)) {
- data = structuredCloudEvent.get(CloudEventMessageUtils.DATA);
- structuredCloudEvent.remove(CloudEventMessageUtils.DATA);
- }
- Assert.notNull(data, "'data' must not be null");
- MessageBuilder> builder = MessageBuilder.withPayload(data);
- CloudEventAttributes attributes = new CloudEventAttributes(structuredCloudEvent);
- builder.setHeader(prefixToUse + CloudEventMessageUtils.ID, attributes.getId());
- builder.setHeader(prefixToUse + CloudEventMessageUtils.SOURCE, attributes.getSource());
- builder.setHeader(prefixToUse + CloudEventMessageUtils.TYPE, attributes.getType());
- builder.setHeader(prefixToUse + CloudEventMessageUtils.SPECVERSION, attributes.getSpecversion());
- builder.copyHeaders(originalHeaders);
- return builder.build();
- }
-
public static String determinePrefixToUse(MessageHeaders messageHeaders) {
Set keys = messageHeaders.keySet();
if (keys.contains("user-agent")) {
@@ -304,6 +264,32 @@ public final class CloudEventMessageUtils {
return generateDefaultAttributeValues(attributes, typeName, sourceName);
}
+ private static Message> buildCeMessageFromStructured(Map structuredCloudEvent, MessageHeaders originalHeaders) {
+ String prefixToUse = determinePrefixToUse(originalHeaders);
+ Object data = null;
+ if (structuredCloudEvent.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATA)) {
+ data = structuredCloudEvent.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATA);
+ structuredCloudEvent.remove(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATA);
+ }
+ else if (structuredCloudEvent.containsKey(CloudEventMessageUtils.CANONICAL_DATA)) {
+ data = structuredCloudEvent.get(CloudEventMessageUtils.CANONICAL_DATA);
+ structuredCloudEvent.remove(CloudEventMessageUtils.CANONICAL_DATA);
+ }
+ else if (structuredCloudEvent.containsKey(CloudEventMessageUtils.DATA)) {
+ data = structuredCloudEvent.get(CloudEventMessageUtils.DATA);
+ structuredCloudEvent.remove(CloudEventMessageUtils.DATA);
+ }
+ Assert.notNull(data, "'data' must not be null");
+ MessageBuilder> builder = MessageBuilder.withPayload(data);
+ CloudEventAttributes attributes = new CloudEventAttributes(structuredCloudEvent);
+ builder.setHeader(prefixToUse + CloudEventMessageUtils.ID, attributes.getId());
+ builder.setHeader(prefixToUse + CloudEventMessageUtils.SOURCE, attributes.getSource());
+ builder.setHeader(prefixToUse + CloudEventMessageUtils.TYPE, attributes.getType());
+ builder.setHeader(prefixToUse + CloudEventMessageUtils.SPECVERSION, attributes.getSpecversion());
+ builder.copyHeaders(originalHeaders);
+ return builder.build();
+ }
+
private static CloudEventAttributes generateDefaultAttributeValues(CloudEventAttributes attributes, String source, String type) {
if (attributes.isValidCloudEvent()) {
return attributes
diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java
index a365e08b4..f03158e50 100644
--- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java
+++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java
@@ -70,12 +70,6 @@ public class ContextFunctionCatalogAutoConfiguration {
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
-// @Bean
-// @ConditionalOnMissingBean
-// public CloudEventAttributesProvider cloudEventAttributesProvider() {
-// return new DefaultCloudEventAttributesProvider();
-// }
-
@Bean
public FunctionRegistry functionCatalog(List messageConverters, JsonMapper jsonMapper, ConfigurableApplicationContext context) {
ConfigurableConversionService conversionService = (ConfigurableConversionService) context.getBeanFactory().getConversionService();