GH-422, GH-606 Fix attributes generatioin
This commit is contained in:
@@ -286,7 +286,7 @@ public final class CloudEventMessageUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Object> generateDefaultCloudEventHeaders(Message<?> inputMessage, Object result, String applicationName) {
|
||||
public static CloudEventAttributes generateAttributes(Message<?> inputMessage, Object result, String applicationName) {
|
||||
CloudEventAttributes attributes = new CloudEventAttributes(inputMessage.getHeaders(), CloudEventMessageUtils.determinePrefixToUse(inputMessage));
|
||||
if (attributes.isValidCloudEvent()) {
|
||||
return attributes
|
||||
@@ -295,6 +295,6 @@ public final class CloudEventMessageUtils {
|
||||
.setType(result.getClass().getName())
|
||||
.setSource(applicationName);
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
return new CloudEventAttributes(Collections.emptyMap());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.cloud.function.context.catalog;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
@@ -168,14 +167,14 @@ public class BeanFactoryAwareFunctionRegistry extends SimpleFunctionRegistry imp
|
||||
@Override
|
||||
public Message<?> apply(Message<?> inputMessage, Object invocationResult) {
|
||||
// TODO: Factor it out! Cloud Events specific code
|
||||
Map<String, Object> generatedCeHeaders = CloudEventMessageUtils
|
||||
.generateDefaultCloudEventHeaders(inputMessage, invocationResult, getApplicationName());
|
||||
CloudEventAttributes attributes = new CloudEventAttributes(generatedCeHeaders);
|
||||
CloudEventAttributes generatedCeHeaders = CloudEventMessageUtils
|
||||
.generateAttributes(inputMessage, invocationResult, getApplicationName());
|
||||
CloudEventAttributes attributes = new CloudEventAttributes(generatedCeHeaders, CloudEventMessageUtils.determinePrefixToUse(inputMessage));
|
||||
if (cloudEventAtttributesProvider != null) {
|
||||
cloudEventAtttributesProvider.generateDefaultCloudEventHeaders(attributes);
|
||||
}
|
||||
Message message = MessageBuilder.withPayload(invocationResult)
|
||||
.copyHeaders(generatedCeHeaders)
|
||||
.copyHeaders(attributes)
|
||||
.build();
|
||||
|
||||
return message;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package io.spring.cloudevent;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -96,7 +97,9 @@ public class CloudeventDemoApplication {
|
||||
|
||||
@Bean
|
||||
public CloudEventAttributesProvider cloudEventAttributesProvider() {
|
||||
return attributes -> attributes.setSource("https://interface21.com/").setType("com.interface21");
|
||||
return attributes -> {
|
||||
attributes.setSource("https://interface21.com/").setType("com.interface21");
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -116,4 +119,12 @@ public class CloudeventDemoApplication {
|
||||
return event;
|
||||
};
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public Consumer<SpringReleaseEvent> pojoConsumer(CloudEventAttributesProvider provider) {
|
||||
// return event -> {
|
||||
//
|
||||
// provider.generateDefaultCloudEventHeaders(attributes);
|
||||
// };
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -215,9 +215,9 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
|
||||
assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2050\",\"releaseName\":\"Spring Framework\",\"version\":\"10.0\"}");
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE))
|
||||
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
|
||||
.isEqualTo(Collections.singletonList("https://interface21.com/"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE))
|
||||
.isEqualTo(Collections.singletonList(LinkedHashMap.class.getName()));
|
||||
.isEqualTo(Collections.singletonList("com.interface21"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -232,9 +232,9 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
|
||||
assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2006\",\"releaseName\":\"Spring Framework\",\"version\":\"2.0\"}");
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE))
|
||||
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
|
||||
.isEqualTo(Collections.singletonList("https://interface21.com/"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE))
|
||||
.isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName()));
|
||||
.isEqualTo(Collections.singletonList("com.interface21"));
|
||||
}
|
||||
|
||||
|
||||
@@ -259,9 +259,9 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
|
||||
assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2006\",\"releaseName\":\"Spring Framework\",\"version\":\"2.0\"}");
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE))
|
||||
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
|
||||
.isEqualTo(Collections.singletonList("https://interface21.com/"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE))
|
||||
.isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName()));
|
||||
.isEqualTo(Collections.singletonList("com.interface21"));
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user