Search in sources :

Example 1 with Converter

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);
}
Also used : CodeTable(com.revolsys.record.code.CodeTable) HashMap(java.util.HashMap) Converter(org.springframework.core.convert.converter.Converter) Record(com.revolsys.record.Record) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 2 with Converter

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();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DefaultDatatypeChannelMessageConverter(org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter) GenericConverter(org.springframework.core.convert.converter.GenericConverter) Converter(org.springframework.core.convert.converter.Converter) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 3 with Converter

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()));
    }
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Converter(org.springframework.core.convert.converter.Converter) TreeMap(java.util.TreeMap) GenericConversionService(org.springframework.core.convert.support.GenericConversionService)

Example 4 with Converter

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));
}
Also used : JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Jwt(org.springframework.security.oauth2.jwt.Jwt) NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) Converter(org.springframework.core.convert.converter.Converter) Test(org.junit.jupiter.api.Test)

Example 5 with Converter

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");
}
Also used : Converter(org.springframework.core.convert.converter.Converter) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Converter (org.springframework.core.convert.converter.Converter)34 Test (org.junit.jupiter.api.Test)27 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)13 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)12 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)12 StandardCharsets (java.nio.charset.StandardCharsets)10 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 BeforeEach (org.junit.jupiter.api.BeforeEach)9 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)9 JWK (com.nimbusds.jose.jwk.JWK)8 Collections (java.util.Collections)8 Function (java.util.function.Function)8 SecretKeySpec (javax.crypto.spec.SecretKeySpec)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)8 AfterEach (org.junit.jupiter.api.AfterEach)8