Fix CloudEvent prefix and fix the name of CloudEventAttributesProvider.java
This commit is contained in:
@@ -26,7 +26,7 @@ import org.springframework.messaging.MessageHeaders;
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 3.1
|
||||
*/
|
||||
public interface CloudEventAtttributesProvider {
|
||||
public interface CloudEventAttributesProvider {
|
||||
|
||||
/**
|
||||
* Will construct instance of {@link CloudEventAttributes} setting its required attributes.
|
||||
@@ -90,7 +90,11 @@ public class CloudEventDataContentTypeMessagePreProcessor implements Function<Me
|
||||
}
|
||||
|
||||
private Message<?> buildCeMessageFromStructured(Map<String, Object> structuredCloudEvent) {
|
||||
MessageBuilder<?> builder = MessageBuilder.withPayload(structuredCloudEvent.get(CloudEventMessageUtils.DATA));
|
||||
MessageBuilder<?> builder = MessageBuilder.withPayload(
|
||||
structuredCloudEvent.containsKey(CloudEventMessageUtils.CE_DATA)
|
||||
? structuredCloudEvent.get(CloudEventMessageUtils.CE_DATA)
|
||||
: structuredCloudEvent.get(CloudEventMessageUtils.DATA));
|
||||
structuredCloudEvent.remove(CloudEventMessageUtils.CE_DATA);
|
||||
structuredCloudEvent.remove(CloudEventMessageUtils.DATA);
|
||||
builder.copyHeaders(structuredCloudEvent);
|
||||
return builder.build();
|
||||
|
||||
@@ -49,7 +49,7 @@ public final class CloudEventMessageUtils {
|
||||
/**
|
||||
* Prefix for attributes.
|
||||
*/
|
||||
public static String ATTR_PREFIX = "ce_";
|
||||
public static String ATTR_PREFIX = "ce-";
|
||||
|
||||
/**
|
||||
* Value for 'data' attribute.
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.springframework.util.StringUtils;
|
||||
* @since 3.1
|
||||
*
|
||||
*/
|
||||
public class DefaultCloudEventAttributesProvider implements CloudEventAtttributesProvider, ApplicationContextAware {
|
||||
public class DefaultCloudEventAttributesProvider implements CloudEventAttributesProvider, ApplicationContextAware {
|
||||
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAtttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.context.FunctionProperties;
|
||||
import org.springframework.cloud.function.context.FunctionRegistration;
|
||||
import org.springframework.cloud.function.context.FunctionRegistry;
|
||||
@@ -57,7 +57,7 @@ public class BeanFactoryAwareFunctionRegistry extends SimpleFunctionRegistry imp
|
||||
private GenericApplicationContext applicationContext;
|
||||
|
||||
@Autowired(required = false)
|
||||
private CloudEventAtttributesProvider cloudEventAtttributesProvider;
|
||||
private CloudEventAttributesProvider cloudEventAtttributesProvider;
|
||||
|
||||
|
||||
public BeanFactoryAwareFunctionRegistry(ConversionService conversionService, CompositeMessageConverter messageConverter, JsonMapper jsonMapper) {
|
||||
|
||||
@@ -866,7 +866,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
output = enhancer.apply(output);
|
||||
}
|
||||
|
||||
if (ObjectUtils.isEmpty(contentType)) {
|
||||
if (ObjectUtils.isEmpty(contentType) && !(output instanceof Publisher)) {
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ import com.google.gson.Gson;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAtttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventDataContentTypeMessagePreProcessor;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventJsonMessageConverter;
|
||||
import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider;
|
||||
@@ -76,7 +76,7 @@ public class ContextFunctionCatalogAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CloudEventAtttributesProvider cloudEventAttributesProvider() {
|
||||
public CloudEventAttributesProvider cloudEventAttributesProvider() {
|
||||
return new DefaultCloudEventAttributesProvider();
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public class CloudEventTypeConversionTests {
|
||||
@Test
|
||||
public void testFromMessageBinaryPayloadMatchesType() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributes ceAttributes = ceAttrProvider
|
||||
.get(UUID.randomUUID().toString(), "1.0", "https://spring.io/", "org.springframework");
|
||||
ceAttributes.setDataContentType("text/plain");
|
||||
@@ -58,7 +58,7 @@ public class CloudEventTypeConversionTests {
|
||||
@Test
|
||||
public void testFromMessageBinaryPayloadDoesNotMatchType() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributes ceAttributes = ceAttrProvider
|
||||
.get(UUID.randomUUID().toString(), "1.0", "https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
@@ -74,7 +74,7 @@ public class CloudEventTypeConversionTests {
|
||||
// this works
|
||||
public void testFromMessageBinaryPayloadNoDataContentTypeToString() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributes ceAttributes = ceAttrProvider
|
||||
.get(UUID.randomUUID().toString(), "1.0", "https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
@@ -89,7 +89,7 @@ public class CloudEventTypeConversionTests {
|
||||
@Test // Unlike the previous test the type here is POJO so no special treatement
|
||||
public void testFromMessageBinaryPayloadNoDataContentTypeToPOJO() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributes ceAttributes = ceAttrProvider.get("https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
.copyHeaders(ceAttributes)
|
||||
@@ -103,7 +103,7 @@ public class CloudEventTypeConversionTests {
|
||||
@Test // will fall on default CT which is json
|
||||
public void testFromMessageBinaryPayloadNoDataContentTypeToPOJOThatWorks() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributes ceAttributes = ceAttrProvider.get("https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("{\"name\":\"Ricky\"}".getBytes())
|
||||
.copyHeaders(ceAttributes)
|
||||
|
||||
@@ -16,13 +16,18 @@
|
||||
|
||||
package io.spring.cloudevent;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributes;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAtttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
|
||||
import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
@@ -83,7 +88,7 @@ public class CloudeventDemoApplication {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Message<SpringReleaseEvent>, Message<SpringReleaseEvent>> consumeAndProduceCloudEvent(CloudEventAtttributesProvider ceAttrProvider) {
|
||||
public Function<Message<SpringReleaseEvent>, Message<SpringReleaseEvent>> consumeAndProduceCloudEvent(CloudEventAttributesProvider ceAttrProvider) {
|
||||
return ceMessage -> {
|
||||
SpringReleaseEvent data = ceMessage.getPayload();
|
||||
data.setVersion("2.0");
|
||||
@@ -107,4 +112,24 @@ public class CloudeventDemoApplication {
|
||||
return data;
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CloudEventAttributesProvider cloudEventAttributesProvider() {
|
||||
return new CustomCloudEventAtttributesProvider();
|
||||
}
|
||||
|
||||
public static class CustomCloudEventAtttributesProvider extends DefaultCloudEventAttributesProvider {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> generateDefaultCloudEventHeaders(Message<?> inputMessage, Object result) {
|
||||
if (inputMessage.getHeaders().containsKey(CloudEventMessageUtils.CE_ID)) { // input is a cloud event
|
||||
String applicationName = "http://spring.io/fooBar";
|
||||
return this.get(inputMessage.getHeaders())
|
||||
.setId(UUID.randomUUID().toString())
|
||||
.setType(result.getClass().getName())
|
||||
.setSource(applicationName);
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
spring.cloud.function.definition=asPOJOMessage
|
||||
#spring.cloud.function.definition=asPOJOMessage
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAtttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -39,7 +39,7 @@ public class CloudeventDemoApplicationFunctionTests {
|
||||
|
||||
try(ConfigurableApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class)) {
|
||||
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
Message<String> binaryCloudEventMessage = MessageBuilder
|
||||
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
|
||||
.copyHeaders(ceAttrProvider.get("spring.io/spring-event", "com.example.springevent"))
|
||||
@@ -68,7 +68,7 @@ public class CloudeventDemoApplicationFunctionTests {
|
||||
public void demoPureFunctionProduceConsumeCloudEvent() {
|
||||
try(ConfigurableApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class)) {
|
||||
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
Message<String> binaryCloudEventMessage = MessageBuilder
|
||||
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
|
||||
.copyHeaders(ceAttrProvider.get("spring.io/spring-event", "com.example.springevent"))
|
||||
@@ -88,7 +88,7 @@ public class CloudeventDemoApplicationFunctionTests {
|
||||
public void demoPureFunctionProduceConsumeCloudEventAsPojo() {
|
||||
try(ConfigurableApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class)) {
|
||||
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
|
||||
CloudEventAtttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
Message<String> binaryCloudEventMessage = MessageBuilder
|
||||
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
|
||||
.copyHeaders(ceAttrProvider.get("spring.io/spring-event", "com.example.springevent"))
|
||||
|
||||
@@ -174,12 +174,12 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
SpringApplication.run(CloudeventDemoApplication.class);
|
||||
|
||||
String payload = "{\n" +
|
||||
" \"specversion\" : \"1.0\",\n" +
|
||||
" \"type\" : \"org.springframework\",\n" +
|
||||
" \"source\" : \"https://spring.io/\",\n" +
|
||||
" \"id\" : \"A234-1234-1234\",\n" +
|
||||
" \"datacontenttype\" : \"application/json\",\n" +
|
||||
" \"data\" : {\n" +
|
||||
" \"ce-specversion\" : \"1.0\",\n" +
|
||||
" \"ce-type\" : \"org.springframework\",\n" +
|
||||
" \"ce-source\" : \"https://spring.io/\",\n" +
|
||||
" \"ce-id\" : \"A234-1234-1234\",\n" +
|
||||
" \"ce-datacontenttype\" : \"application/json\",\n" +
|
||||
" \"ce-data\" : {\n" +
|
||||
" \"version\" : \"1.0\",\n" +
|
||||
" \"releaseName\" : \"Spring Framework\",\n" +
|
||||
" \"releaseDate\" : \"24-03-2004\"\n" +
|
||||
|
||||
@@ -79,7 +79,9 @@ public class FunctionController {
|
||||
Publisher<?> result = (Publisher<?>) function.apply(Flux.fromIterable(files));
|
||||
BodyBuilder builder = ResponseEntity.ok();
|
||||
if (result instanceof Flux) {
|
||||
result = Flux.from(result).map(message -> ((Message<?>) message).getPayload()).collectList();
|
||||
result = Flux.from(result).map(message -> {
|
||||
return message instanceof Message ? ((Message<?>) message).getPayload() : message;
|
||||
}).collectList();
|
||||
}
|
||||
return Mono.from(result).flatMap(body -> Mono.just(builder.body(body)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user