Search in sources :

Example 11 with ConversionService

use of org.springframework.core.convert.ConversionService in project camel by apache.

the class SpringTypeConverter method convertTo.

@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
    // do not attempt to convert Camel types
    if (type.getCanonicalName().startsWith("org.apache")) {
        return null;
    }
    // do not attempt to convert List -> Map. Ognl expression may use this converter as a fallback expecting null
    if (type.isAssignableFrom(Map.class) && (value.getClass().isArray() || value instanceof Collection)) {
        return null;
    }
    TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
    TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);
    for (ConversionService conversionService : conversionServices) {
        if (conversionService.canConvert(sourceType, targetType)) {
            try {
                return (T) conversionService.convert(value, sourceType, targetType);
            } catch (ConversionFailedException e) {
                //
                if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
                    return null;
                } else {
                    throw new TypeConversionException(value, type, e);
                }
            }
        }
    }
    return null;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) TypeConversionException(org.apache.camel.TypeConversionException) ConversionService(org.springframework.core.convert.ConversionService) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Collection(java.util.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with ConversionService

use of org.springframework.core.convert.ConversionService in project cas by apereo.

the class CasConfigurationEmbeddedValueResolver method convertValueToDurationIfPossible.

private String convertValueToDurationIfPossible(final String value) {
    try {
        final ConversionService service = applicationContext.getEnvironment().getConversionService();
        final Duration dur = service.convert(value, Duration.class);
        if (dur != null) {
            return String.valueOf(dur.toMillis());
        }
    } catch (final ConversionFailedException e) {
        LOGGER.trace(e.getMessage());
    }
    return null;
}
Also used : ConversionFailedException(org.springframework.core.convert.ConversionFailedException) ConversionService(org.springframework.core.convert.ConversionService) Duration(java.time.Duration)

Example 13 with ConversionService

use of org.springframework.core.convert.ConversionService in project grails-core by grails.

the class AbstractGrailsPluginManager method doRuntimeConfiguration.

/**
     * Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
     * @param springConfig The RuntimeSpringConfiguration instance
     */
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
    ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
    AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
    if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
        ConversionService existingConversionService = beanFactory.getConversionService();
        ConverterRegistry converterRegistry;
        if (existingConversionService == null) {
            GenericConversionService conversionService = new GenericConversionService();
            converterRegistry = conversionService;
            beanFactory.setConversionService(conversionService);
        } else {
            converterRegistry = (ConverterRegistry) existingConversionService;
        }
        converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {

            @Override
            public Object convert(NavigableMap.NullSafeNavigator source) {
                return null;
            }
        });
    }
    checkInitialised();
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment() && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
            plugin.doWithRuntimeConfiguration(springConfig);
        }
    }
}
Also used : NavigableMap(org.grails.config.NavigableMap) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) ApplicationContext(org.springframework.context.ApplicationContext) ConverterRegistry(org.springframework.core.convert.converter.ConverterRegistry) GrailsPlugin(grails.plugins.GrailsPlugin) ConversionService(org.springframework.core.convert.ConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) ConfigObject(groovy.util.ConfigObject) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) GenericConversionService(org.springframework.core.convert.support.GenericConversionService)

Example 14 with ConversionService

use of org.springframework.core.convert.ConversionService in project spring-framework by spring-projects.

the class InitBinderBindingContextTests method createBinderWithGlobalInitialization.

@Test
public void createBinderWithGlobalInitialization() throws Exception {
    ConversionService conversionService = new DefaultFormattingConversionService();
    bindingInitializer.setConversionService(conversionService);
    ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
    BindingContext context = createBindingContext("initBinder", WebDataBinder.class);
    WebDataBinder dataBinder = context.createDataBinder(exchange, null, null);
    assertSame(conversionService, dataBinder.getConversionService());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebDataBinder(org.springframework.web.bind.WebDataBinder) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) ConversionService(org.springframework.core.convert.ConversionService) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 15 with ConversionService

use of org.springframework.core.convert.ConversionService in project spring-framework by spring-projects.

the class EvalTag method createEvaluationContext.

private EvaluationContext createEvaluationContext(PageContext pageContext) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
    context.addPropertyAccessor(new MapAccessor());
    context.addPropertyAccessor(new EnvironmentAccessor());
    context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
    ConversionService conversionService = getConversionService(pageContext);
    if (conversionService != null) {
        context.setTypeConverter(new StandardTypeConverter(conversionService));
    }
    return context;
}
Also used : StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ConversionService(org.springframework.core.convert.ConversionService) MapAccessor(org.springframework.context.expression.MapAccessor) EnvironmentAccessor(org.springframework.context.expression.EnvironmentAccessor)

Aggregations

ConversionService (org.springframework.core.convert.ConversionService)21 Test (org.junit.Test)11 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)5 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)4 ApplicationContext (org.springframework.context.ApplicationContext)3 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)3 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)3 Collection (java.util.Collection)2 Map (java.util.Map)2 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)2 FormattingConversionService (org.springframework.format.support.FormattingConversionService)2 Validator (org.springframework.validation.Validator)2 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)2 RequestMappingHandlerAdapter (org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter)2 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)2 FAIL_ON_UNKNOWN_PROPERTIES (com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)1 DEFAULT_VIEW_INCLUSION (com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION)1