GH-422, GH-606 Improve generation of default attributes for Consumer

This commit is contained in:
Oleg Zhurakousky
2020-11-16 16:49:59 +01:00
parent 8993a9751f
commit 8a3cbed877
2 changed files with 21 additions and 9 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.function.cloudevent; package org.springframework.cloud.function.cloudevent;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -286,21 +285,30 @@ public final class CloudEventMessageUtils {
} }
} }
public static CloudEventAttributes generateAttributesWithProvider(MessageHeaders headers, CloudEventAttributesProvider provider) { /**
CloudEventAttributes attributes = new CloudEventAttributes(headers); * Typically called by Consumer.
*/
public static CloudEventAttributes generateAttributes(Message<?> message, CloudEventAttributesProvider provider) {
CloudEventAttributes attributes = generateDefaultAttributeValues(new CloudEventAttributes(message.getHeaders()),
message.getPayload().getClass().getName().getClass().getName(), message.getPayload().getClass().getName().getClass().getName());
provider.generateDefaultCloudEventHeaders(attributes); provider.generateDefaultCloudEventHeaders(attributes);
return attributes; return attributes;
} }
public static CloudEventAttributes generateAttributes(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)); CloudEventAttributes attributes = new CloudEventAttributes(inputMessage.getHeaders(), CloudEventMessageUtils.determinePrefixToUse(inputMessage));
return generateDefaultAttributeValues(attributes, result.getClass().getName(), applicationName);
}
private static CloudEventAttributes generateDefaultAttributeValues(CloudEventAttributes attributes, String source, String type) {
if (attributes.isValidCloudEvent()) { if (attributes.isValidCloudEvent()) {
return attributes return attributes
.setSpecversion("1.0") .setSpecversion("1.0")
.setId(UUID.randomUUID().toString()) .setId(UUID.randomUUID().toString())
.setType(result.getClass().getName()) .setType(type)
.setSource(applicationName); .setSource(source);
} }
return new CloudEventAttributes(Collections.emptyMap()); return attributes;
} }
} }

View File

@@ -135,12 +135,16 @@ public class CloudeventDemoApplication {
return eventMessage -> { return eventMessage -> {
RequestEntity<SpringReleaseEvent> entity = RequestEntity.post(URI.create("http://foo.com")) RequestEntity<SpringReleaseEvent> entity = RequestEntity.post(URI.create("http://foo.com"))
.headers(HeaderUtils.fromMessage( .headers(HeaderUtils.fromMessage(
new MessageHeaders(CloudEventMessageUtils.generateAttributesWithProvider(eventMessage.getHeaders(), provider)))) new MessageHeaders(CloudEventMessageUtils.generateAttributes(eventMessage, provider))))
.body(eventMessage.getPayload()); .body(eventMessage.getPayload());
List<String> sourceHeader = entity.getHeaders().get("ce-source"); List<String> sourceHeader = entity.getHeaders().get("ce-source");
List<String> typeHeader = entity.getHeaders().get("ce-type");
Assert.isTrue(sourceHeader.get(0).equals("https://interface21.com/"), "'source' must be https://interface21.com/"); Assert.isTrue(sourceHeader.get(0).equals("https://interface21.com/"), "'source' must be https://interface21.com/");
List<String> typeHeader = entity.getHeaders().get("ce-type");
Assert.isTrue(typeHeader.get(0).equals("com.interface21"), "'source' must be com.interface21"); Assert.isTrue(typeHeader.get(0).equals("com.interface21"), "'source' must be com.interface21");
List<String> idHeader = entity.getHeaders().get("ce-id");
Assert.notEmpty(idHeader, "'id' must not be null");
List<String> specversionHeader = entity.getHeaders().get("ce-specversion");
Assert.notEmpty(specversionHeader, "'specversion' must not be null");
}; };
} }
@@ -153,7 +157,7 @@ public class CloudeventDemoApplication {
return eventMessage -> { return eventMessage -> {
RequestEntity<Map<String, Object>> entity = RequestEntity.post(URI.create("http://foo.com")) RequestEntity<Map<String, Object>> entity = RequestEntity.post(URI.create("http://foo.com"))
.headers(HeaderUtils.fromMessage( .headers(HeaderUtils.fromMessage(
new MessageHeaders(CloudEventMessageUtils.generateAttributesWithProvider(eventMessage.getHeaders(), provider)))) new MessageHeaders(CloudEventMessageUtils.generateAttributes(eventMessage, provider, "io.spring"))))
.body(eventMessage.getPayload()); .body(eventMessage.getPayload());
client.exchange(entity, byte[].class); client.exchange(entity, byte[].class);
}; };