use of org.springframework.core.convert.converter.Converter in project com.revolsys.open by revolsys.
the class SetCodeTableId method process.
@Override
public void process(final Record source, final Record target) {
final Map<String, Object> codeTableValues = new HashMap<>();
for (final Entry<String, Converter<Record, Object>> entry : this.codeTableValueConverters.entrySet()) {
String codeTableFieldName = entry.getKey();
final Converter<Record, Object> sourceAttributeConverter = entry.getValue();
Object sourceValue = sourceAttributeConverter.convert(source);
if (sourceValue != null) {
final RecordDefinition targetRecordDefinition = target.getRecordDefinition();
String codeTableValueName = null;
final int dotIndex = codeTableFieldName.indexOf(".");
if (dotIndex != -1) {
codeTableValueName = codeTableFieldName.substring(dotIndex + 1);
codeTableFieldName = codeTableFieldName.substring(0, dotIndex);
}
final CodeTable targetCodeTable = targetRecordDefinition.getCodeTableByFieldName(codeTableFieldName);
if (targetCodeTable != null) {
if (codeTableValueName == null) {
sourceValue = targetCodeTable.getIdentifier(sourceValue);
} else {
sourceValue = targetCodeTable.getIdentifier(Collections.singletonMap(codeTableValueName, sourceValue));
}
}
}
codeTableValues.put(codeTableFieldName, sourceValue);
}
final Object codeId = this.codeTable.getIdentifier(codeTableValues);
target.setValue(this.targetFieldName, codeId);
}
use of org.springframework.core.convert.converter.Converter in project spring-integration by spring-projects.
the class DatatypeChannelTests method conversionServiceReferenceOverridesDefault.
@Test
public void conversionServiceReferenceOverridesDefault() {
GenericApplicationContext context = new GenericApplicationContext();
Converter<Boolean, Integer> defaultConverter = new Converter<Boolean, Integer>() {
@Override
public Integer convert(Boolean source) {
return source ? 1 : 0;
}
};
GenericConversionService customConversionService = new DefaultConversionService();
customConversionService.addConverter(new Converter<Boolean, Integer>() {
@Override
public Integer convert(Boolean source) {
return source ? 99 : -99;
}
});
BeanDefinitionBuilder conversionServiceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ConversionServiceFactoryBean.class);
conversionServiceBuilder.addPropertyValue("converters", Collections.singleton(defaultConverter));
context.registerBeanDefinition("conversionService", conversionServiceBuilder.getBeanDefinition());
BeanDefinitionBuilder messageConverterBuilder = BeanDefinitionBuilder.genericBeanDefinition(DefaultDatatypeChannelMessageConverter.class);
messageConverterBuilder.addPropertyValue("conversionService", customConversionService);
context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_DATATYPE_CHANNEL_MESSAGE_CONVERTER_BEAN_NAME, messageConverterBuilder.getBeanDefinition());
BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(QueueChannel.class);
channelBuilder.addPropertyValue("datatypes", "java.lang.Integer, java.util.Date");
context.registerBeanDefinition("testChannel", channelBuilder.getBeanDefinition());
context.refresh();
QueueChannel channel = context.getBean("testChannel", QueueChannel.class);
assertTrue(channel.send(new GenericMessage<Boolean>(Boolean.TRUE)));
assertEquals(99, channel.receive().getPayload());
context.close();
}
use of org.springframework.core.convert.converter.Converter in project spring-cloud-stream by spring-cloud.
the class BindingServiceProperties method setApplicationContext.
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
GenericConversionService cs = (GenericConversionService) IntegrationUtils.getConversionService(this.applicationContext.getBeanFactory());
if (this.applicationContext.containsBean("spelConverter")) {
Converter<?, ?> converter = (Converter<?, ?>) this.applicationContext.getBean("spelConverter");
cs.addConverter(converter);
}
if (this.applicationContext.getEnvironment() instanceof ConfigurableEnvironment) {
// override the bindings store with the environment-initializing version if in
// a Spring context
Map<String, BindingProperties> delegate = new TreeMap<String, BindingProperties>(String.CASE_INSENSITIVE_ORDER);
delegate.putAll(this.bindings);
this.bindings = new EnvironmentEntryInitializingTreeMap<>(this.applicationContext.getEnvironment(), BindingProperties.class, "spring.cloud.stream.default", delegate, IntegrationUtils.getConversionService(this.applicationContext.getBeanFactory()));
}
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class OAuth2ResourceServerBeanDefinitionParserTests method requestWhenJwtAuthenticationConverterThenUsed.
@Test
public void requestWhenJwtAuthenticationConverterThenUsed() throws Exception {
this.spring.configLocations(xml("MockJwtDecoder"), xml("MockJwtAuthenticationConverter"), xml("JwtAuthenticationConverter")).autowire();
Converter<Jwt, JwtAuthenticationToken> jwtAuthenticationConverter = (Converter<Jwt, JwtAuthenticationToken>) this.spring.getContext().getBean("jwtAuthenticationConverter");
given(jwtAuthenticationConverter.convert(any(Jwt.class))).willReturn(new JwtAuthenticationToken(TestJwts.jwt().build(), Collections.emptyList()));
JwtDecoder jwtDecoder = this.spring.getContext().getBean(JwtDecoder.class);
given(jwtDecoder.decode(anyString())).willReturn(TestJwts.jwt().build());
// @formatter:off
this.mvc.perform(get("/").header("Authorization", "Bearer token")).andExpect(status().isNotFound());
// @formatter:on
verify(jwtAuthenticationConverter).convert(any(Jwt.class));
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class OAuth2AccessTokenResponseHttpMessageConverterTests method readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException.
@Test
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
Converter tokenResponseConverter = mock(Converter.class);
given(tokenResponseConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setTokenResponseConverter(tokenResponseConverter);
String tokenResponse = "{}";
MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK);
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.messageConverter.readInternal(OAuth2AccessTokenResponse.class, response)).withMessageContaining("An error occurred reading the OAuth 2.0 Access Token Response");
}
Aggregations