Initial refactoring and simplificatioin of web module
This commit is contained in:
@@ -61,7 +61,6 @@ import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.converter.CompositeMessageConverter;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -762,6 +761,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
}
|
||||
else if (input instanceof Message) {
|
||||
convertedInput = this.convertInputMessageIfNecessary((Message) input, type);
|
||||
if (convertedInput == null) { // give ConversionService a chance
|
||||
convertedInput = this.convertNonMessageInputIfNecessary(type, ((Message) input).getPayload());
|
||||
}
|
||||
if (convertedInput != null && !FunctionTypeUtils.isMultipleArgumentType(this.inputType)) {
|
||||
convertedInput = !convertedInput.equals(input)
|
||||
? new OriginalMessageHolder(convertedInput, (Message<?>) input)
|
||||
@@ -792,6 +794,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
output = ((Message) output).getPayload();
|
||||
}
|
||||
}
|
||||
if (ObjectUtils.isEmpty(contentType)) {
|
||||
return output;
|
||||
}
|
||||
|
||||
if (!(output instanceof Publisher) && this.enhancer != null) {
|
||||
output = enhancer.apply(output);
|
||||
@@ -925,6 +930,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
convertedInput = MessageBuilder.withPayload(convertedInput).copyHeaders(message.getHeaders()).build();
|
||||
}
|
||||
}
|
||||
// convertedInput = convertedInput == null ? message.getPayload() : convertedInput;
|
||||
return convertedInput;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2019 the original author or authors.
|
||||
* Copyright 2016-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -37,6 +37,7 @@ import org.springframework.cloud.function.context.catalog.BeanFactoryAwareFuncti
|
||||
import org.springframework.cloud.function.json.GsonMapper;
|
||||
import org.springframework.cloud.function.json.JacksonMapper;
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import org.springframework.cloud.function.utils.PrimitiveTypesFromStringMessageConverter;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -93,17 +94,9 @@ public class ContextFunctionCatalogAutoConfiguration {
|
||||
|
||||
mcList = mcList.stream()
|
||||
.filter(c -> isConverterEligible(c))
|
||||
// .map(converter -> {
|
||||
// return converter instanceof AbstractMessageConverter
|
||||
// ? NegotiatingMessageConverterWrapper.wrap((AbstractMessageConverter) converter)
|
||||
// : converter;
|
||||
// })
|
||||
.collect(Collectors.toList());
|
||||
// mcList.add(NegotiatingMessageConverterWrapper.wrap(new JsonMessageConverter(jsonMapper)));
|
||||
// mcList.add(NegotiatingMessageConverterWrapper.wrap(new ByteArrayMessageConverter()));
|
||||
// mcList.add(NegotiatingMessageConverterWrapper.wrap(new StringMessageConverter()));
|
||||
|
||||
mcList.add(new StringMessageConverter());
|
||||
mcList.add(new PrimitiveTypesFromStringMessageConverter(conversionService));
|
||||
mcList.add(new JsonMessageConverter(jsonMapper));
|
||||
mcList.add(new ByteArrayMessageConverter());
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.springframework.cloud.function.context.FunctionRegistration;
|
||||
import org.springframework.cloud.function.context.FunctionRegistry;
|
||||
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry;
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import org.springframework.cloud.function.utils.PrimitiveTypesFromStringMessageConverter;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
@@ -52,7 +53,6 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.core.type.filter.AssignableTypeFilter;
|
||||
import org.springframework.format.support.DefaultFormattingConversionService;
|
||||
import org.springframework.messaging.converter.ByteArrayMessageConverter;
|
||||
import org.springframework.messaging.converter.CompositeMessageConverter;
|
||||
import org.springframework.messaging.converter.MessageConverter;
|
||||
import org.springframework.messaging.converter.StringMessageConverter;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -60,6 +60,7 @@ import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class ContextFunctionCatalogInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
|
||||
@@ -169,10 +170,8 @@ public class ContextFunctionCatalogInitializer implements ApplicationContextInit
|
||||
List<MessageConverter> messageConverters = new ArrayList<>();
|
||||
JsonMapper jsonMapper = this.context.getBean(JsonMapper.class);
|
||||
|
||||
// messageConverters.add(NegotiatingMessageConverterWrapper.wrap(new JsonMessageConverter(jsonMapper)));
|
||||
// messageConverters.add(NegotiatingMessageConverterWrapper.wrap(new ByteArrayMessageConverter()));
|
||||
// messageConverters.add(NegotiatingMessageConverterWrapper.wrap(new StringMessageConverter()));
|
||||
messageConverters.add(new StringMessageConverter());
|
||||
messageConverters.add(new PrimitiveTypesFromStringMessageConverter(new DefaultConversionService()));
|
||||
messageConverters.add(new JsonMessageConverter(jsonMapper));
|
||||
messageConverters.add(new ByteArrayMessageConverter());
|
||||
|
||||
|
||||
@@ -110,4 +110,16 @@ public abstract class JsonMapper {
|
||||
|
||||
return isJson;
|
||||
}
|
||||
|
||||
public static boolean isJsonStringRepresentsCollection(Object value) {
|
||||
boolean isJson = false;
|
||||
if (value instanceof byte[]) {
|
||||
value = new String((byte[]) value, StandardCharsets.UTF_8);
|
||||
}
|
||||
if (value instanceof String) {
|
||||
String str = ((String) value).trim();
|
||||
isJson = isJsonString(value) && str.startsWith("[") && str.endsWith("]");
|
||||
}
|
||||
return isJson;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.function.utils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.converter.AbstractMessageConverter;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 3.1
|
||||
*/
|
||||
public class PrimitiveTypesFromStringMessageConverter extends AbstractMessageConverter {
|
||||
|
||||
|
||||
private final ConversionService conversionService;
|
||||
|
||||
public PrimitiveTypesFromStringMessageConverter(ConversionService conversionService) {
|
||||
super(new MimeType("text", "plain"));
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected boolean supports(Class<?> clazz) {
|
||||
return (Integer.class == clazz || Long.class == clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
|
||||
return conversionService.convert(message.getPayload(), targetClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
|
||||
return payload.toString().getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user