Polishing cleanup and javadoc

This commit is contained in:
Oleg Zhurakousky
2020-11-17 08:01:21 +01:00
parent f1a32bf5be
commit 97ba22da76
5 changed files with 54 additions and 47 deletions

View File

@@ -202,6 +202,17 @@ public final class CloudEventMessageUtils {
return get(UUID.randomUUID().toString(), "1.0", ce_source, ce_type); return get(UUID.randomUUID().toString(), "1.0", ce_source, ce_type);
} }
/**
* Will attempt to convert 'inputMessage' to a binary-mode Cloud Event {@link Message}.
* This typically happens when 'inputMessage' represents Cloud Event in structured-mode.
* <br>
* In the event the message already represents Cloud Event in binary-mode, or this
* message does not represent Cloud Event at all, it will be returned unchanged.
*
* @param inputMessage instance of incoming {@link Message}
* @param messageConverter instance of {@link MessageConverter} to assist with type conversion.
* @return instance of {@link Message} representing Cloud Event in binary-mode or unchanged 'inputMessage'.
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Message<?> toBinary(Message<?> inputMessage, MessageConverter messageConverter) { public static Message<?> toBinary(Message<?> inputMessage, MessageConverter messageConverter) {
@@ -219,6 +230,7 @@ public final class CloudEventMessageUtils {
: MimeTypeUtils.APPLICATION_JSON_VALUE; : MimeTypeUtils.APPLICATION_JSON_VALUE;
String suffix = contentType.getSubtypeSuffix(); String suffix = contentType.getSubtypeSuffix();
Assert.hasText(suffix, "Content-type 'suffix' can not be determined from " + contentType);
MimeType cloudEventDeserializationContentType = MimeTypeUtils MimeType cloudEventDeserializationContentType = MimeTypeUtils
.parseMimeType(contentType.getType() + "/" + suffix); .parseMimeType(contentType.getType() + "/" + suffix);
Message<?> cloudEventMessage = MessageBuilder.fromMessage(inputMessage) Message<?> cloudEventMessage = MessageBuilder.fromMessage(inputMessage)
@@ -238,6 +250,13 @@ public final class CloudEventMessageUtils {
return inputMessage; return inputMessage;
} }
/**
* Will attempt to determine based on the headers the origin of Message (e.g., HTTP, Kafka etc)
* and based on this designate prefix to be used for Cloud Events attributes (i.e., `ce-` or `ce_` etc).
*
* @param messageHeaders instance of {@link MessageHeaders}
* @return prefix to be used for Cloud Events attributes
*/
public static String determinePrefixToUse(MessageHeaders messageHeaders) { public static String determinePrefixToUse(MessageHeaders messageHeaders) {
Set<String> keys = messageHeaders.keySet(); Set<String> keys = messageHeaders.keySet();
if (keys.contains("user-agent")) { if (keys.contains("user-agent")) {

View File

@@ -47,6 +47,7 @@ import reactor.util.function.Tuples;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.function.cloudevent.CloudEventAttributes;
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils; import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionProperties; import org.springframework.cloud.function.context.FunctionProperties;
@@ -910,13 +911,18 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
* case that requires it since it may contain forwarding url * case that requires it since it may contain forwarding url
*/ */
private boolean containsRetainMessageSignalInHeaders(Message message) { private boolean containsRetainMessageSignalInHeaders(Message message) {
for (String headerName : message.getHeaders().keySet()) { if (new CloudEventAttributes(message.getHeaders()).isValidCloudEvent()) {
if (headerName.startsWith("lambda") || return true;
headerName.startsWith("scf-func-name")) { }
return true; else {
} for (String headerName : message.getHeaders().keySet()) {
if (headerName.startsWith("lambda") ||
headerName.startsWith("scf-func-name")) {
return true;
}
}
return false;
} }
return false;
} }
/* /*

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2017-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -34,7 +34,6 @@ import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils; import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.message.MessageUtils; import org.springframework.cloud.function.context.message.MessageUtils;
@@ -59,16 +58,11 @@ public class RequestProcessor {
private static Log logger = LogFactory.getLog(RequestProcessor.class); private static Log logger = LogFactory.getLog(RequestProcessor.class);
private final FunctionCatalog functionCatalog;
private final JsonMapper mapper; private final JsonMapper mapper;
public RequestProcessor(FunctionCatalog functionCatalog, public RequestProcessor(ObjectProvider<JsonMapper> mapper,
ObjectProvider<JsonMapper> mapper,
ObjectProvider<ServerCodecConfigurer> codecs) { ObjectProvider<ServerCodecConfigurer> codecs) {
this.mapper = mapper.getIfAvailable(); this.mapper = mapper.getIfAvailable();
this.functionCatalog = functionCatalog;
} }
public static FunctionWrapper wrapper(FunctionInvocationWrapper function) { public static FunctionWrapper wrapper(FunctionInvocationWrapper function) {
@@ -186,35 +180,27 @@ public class RequestProcessor {
private Mono<ResponseEntity<?>> response(FunctionWrapper request, Object handler, private Mono<ResponseEntity<?>> response(FunctionWrapper request, Object handler,
Publisher<?> result, Boolean single, boolean getter) { Publisher<?> result, Boolean single, boolean getter) {
BodyBuilder builder = ResponseEntity.ok(); BodyBuilder builder = ResponseEntity.ok();
if (((FunctionInvocationWrapper) handler).isInputTypeMessage()) { if (result instanceof Mono) {
result = Flux.from(result) result = Mono.from(result)
.map(message -> MessageUtils.unpack(handler, message)) .map(message -> MessageUtils.unpack(handler, message))
.doOnNext(value -> addHeaders(builder, value)) .doOnNext(value -> {
.map(message -> message.getPayload()); addHeaders(builder, value);
if (!isValidCloudEvent(value.getHeaders().keySet())) {
builder.headers(HeaderUtils.sanitize(request.headers()));
}
})
.map(message -> message.getPayload());
} }
else { else {
if (result instanceof Mono) { result = Flux.from(result)
result = Mono.from(result) .map(message -> MessageUtils.unpack(handler, message))
.map(message -> MessageUtils.unpack(handler, message)) .doOnNext(value -> {
.doOnNext(value -> { addHeaders(builder, value);
addHeaders(builder, value); if (!isValidCloudEvent(value.getHeaders().keySet())) {
if (!isValidCloudEvent(value.getHeaders().keySet())) { builder.headers(HeaderUtils.sanitize(request.headers()));
builder.headers(HeaderUtils.sanitize(request.headers())); }
} })
}) .map(message -> message.getPayload());
.map(message -> message.getPayload());
}
else {
result = Flux.from(result)
.map(message -> MessageUtils.unpack(handler, message))
.doOnNext(value -> {
addHeaders(builder, value);
if (!isValidCloudEvent(value.getHeaders().keySet())) {
builder.headers(HeaderUtils.sanitize(request.headers()));
}
})
.map(message -> message.getPayload());
}
} }
if (isOutputSingle(handler) if (isOutputSingle(handler)
@@ -231,7 +217,7 @@ public class RequestProcessor {
return Mono.from(result).flatMap(body -> Mono.just(builder.body(body))); return Mono.from(result).flatMap(body -> Mono.just(builder.body(body)));
} }
public boolean isValidCloudEvent(Set<String> headerKeys) { private boolean isValidCloudEvent(Set<String> headerKeys) {
return headerKeys.contains(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID) return headerKeys.contains(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID)
&& headerKeys.contains(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE) && headerKeys.contains(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE)
&& headerKeys.contains(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE) && headerKeys.contains(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE)

View File

@@ -102,7 +102,6 @@ class FunctionEndpointInitializer implements ApplicationContextInitializer<Gener
private void registerEndpoint(GenericApplicationContext context) { private void registerEndpoint(GenericApplicationContext context) {
context.registerBean(RequestProcessor.class, context.registerBean(RequestProcessor.class,
() -> new RequestProcessor( () -> new RequestProcessor(
context.getBean(FunctionCatalog.class),
context.getBeanProvider(JsonMapper.class), context.getBeanProvider(JsonMapper.class),
context.getBeanProvider(ServerCodecConfigurer.class))); context.getBeanProvider(ServerCodecConfigurer.class)));
context.registerBean(FunctionEndpointFactory.class, context.registerBean(FunctionEndpointFactory.class,

View File

@@ -114,10 +114,7 @@ public final class FunctionWebUtils {
result = ((Mono) result).map(v -> postProcessResult(v, isMessage)); result = ((Mono) result).map(v -> postProcessResult(v, isMessage));
} }
else if (result instanceof Message) { else if (result instanceof Message) {
if (!isMessage) { if (((Message) result).getPayload() instanceof byte[]) {
// result = ((Message) result).getPayload();
}
else if (((Message) result).getPayload() instanceof byte[]) {
String str = new String((byte[]) ((Message) result).getPayload()); String str = new String((byte[]) ((Message) result).getPayload());
result = MessageBuilder.withPayload(str).copyHeaders(((Message) result).getHeaders()).build(); result = MessageBuilder.withPayload(str).copyHeaders(((Message) result).getHeaders()).build();
} }