Search in sources :

Example 1 with ClientConfiguration

use of org.springframework.data.elasticsearch.client.ClientConfiguration in project spring-boot by spring-projects.

the class ReactiveElasticsearchRestClientAutoConfigurationTests method whenMultipleUrisHaveHttpsSchemeThenClientConfigurationUsesSsl.

@Test
void whenMultipleUrisHaveHttpsSchemeThenClientConfigurationUsesSsl() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=https://localhost:9876,https://localhost:8765").run((context) -> {
        ClientConfiguration clientConfiguration = context.getBean(ClientConfiguration.class);
        List<InetSocketAddress> endpoints = clientConfiguration.getEndpoints();
        assertThat(endpoints).hasSize(2);
        assertThat(endpoints.get(0).getHostString()).isEqualTo("localhost");
        assertThat(endpoints.get(0).getPort()).isEqualTo(9876);
        assertThat(endpoints.get(1).getHostString()).isEqualTo("localhost");
        assertThat(endpoints.get(1).getPort()).isEqualTo(8765);
        assertThat(clientConfiguration.useSsl()).isTrue();
    });
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ClientConfiguration(org.springframework.data.elasticsearch.client.ClientConfiguration) Test(org.junit.jupiter.api.Test)

Example 2 with ClientConfiguration

use of org.springframework.data.elasticsearch.client.ClientConfiguration in project spring-boot by spring-projects.

the class ReactiveElasticsearchRestClientAutoConfigurationTests method whenUriHasUsernameAndPasswordThenDefaultAuthorizationHeaderHasUsernameAndPassword.

@Test
void whenUriHasUsernameAndPasswordThenDefaultAuthorizationHeaderHasUsernameAndPassword() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user:secret@localhost:9200").run((context) -> {
        ClientConfiguration clientConfiguration = context.getBean(ClientConfiguration.class);
        assertThat(clientConfiguration.getDefaultHeaders().get(HttpHeaders.AUTHORIZATION)).containsExactly("Basic " + Base64.getEncoder().encodeToString("user:secret".getBytes(StandardCharsets.UTF_8)));
    });
}
Also used : ClientConfiguration(org.springframework.data.elasticsearch.client.ClientConfiguration) Test(org.junit.jupiter.api.Test)

Example 3 with ClientConfiguration

use of org.springframework.data.elasticsearch.client.ClientConfiguration in project spring-boot by spring-projects.

the class ReactiveElasticsearchRestClientAutoConfiguration method clientConfiguration.

@Bean
@ConditionalOnMissingBean
public ClientConfiguration clientConfiguration() {
    ClientConfiguration.MaybeSecureClientConfigurationBuilder builder = ClientConfiguration.builder().connectedTo(this.properties.getEndpoints().toArray(new String[0]));
    PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
    map.from(this.properties.isUseSsl()).whenTrue().toCall(builder::usingSsl);
    map.from(this.properties.getCredentials()).to((credentials) -> builder.withBasicAuth(credentials.getUsername(), credentials.getPassword()));
    map.from(this.properties.getConnectionTimeout()).to(builder::withConnectTimeout);
    map.from(this.properties.getSocketTimeout()).to(builder::withSocketTimeout);
    map.from(this.properties.getPathPrefix()).to(builder::withPathPrefix);
    configureExchangeStrategies(map, builder);
    return builder.build();
}
Also used : PropertyMapper(org.springframework.boot.context.properties.PropertyMapper) ClientConfiguration(org.springframework.data.elasticsearch.client.ClientConfiguration) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with ClientConfiguration

use of org.springframework.data.elasticsearch.client.ClientConfiguration in project spring-boot by spring-projects.

the class ReactiveElasticsearchRestClientAutoConfigurationTests method whenUriUserInfoMatchesUsernameAndPasswordPropertiesThenDefaultAuthorizationHeaderIsConfigured.

@Test
void whenUriUserInfoMatchesUsernameAndPasswordPropertiesThenDefaultAuthorizationHeaderIsConfigured() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user:secret@localhost:9876", "spring.elasticsearch.username=user", "spring.elasticsearch.password=secret").run((context) -> {
        ClientConfiguration clientConfiguration = context.getBean(ClientConfiguration.class);
        assertThat(clientConfiguration.getDefaultHeaders().get(HttpHeaders.AUTHORIZATION)).containsExactly("Basic " + Base64.getEncoder().encodeToString("user:secret".getBytes(StandardCharsets.UTF_8)));
    });
}
Also used : ClientConfiguration(org.springframework.data.elasticsearch.client.ClientConfiguration) Test(org.junit.jupiter.api.Test)

Example 5 with ClientConfiguration

use of org.springframework.data.elasticsearch.client.ClientConfiguration in project spring-boot by spring-projects.

the class ReactiveElasticsearchRestClientAutoConfigurationTests method whenUriHasHttpsSchemeThenClientConfigurationUsesSsl.

@Test
void whenUriHasHttpsSchemeThenClientConfigurationUsesSsl() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=https://localhost:9876").run((context) -> {
        ClientConfiguration clientConfiguration = context.getBean(ClientConfiguration.class);
        List<InetSocketAddress> endpoints = clientConfiguration.getEndpoints();
        assertThat(endpoints).hasSize(1);
        assertThat(endpoints.get(0).getHostString()).isEqualTo("localhost");
        assertThat(endpoints.get(0).getPort()).isEqualTo(9876);
        assertThat(clientConfiguration.useSsl()).isTrue();
    });
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ClientConfiguration(org.springframework.data.elasticsearch.client.ClientConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

ClientConfiguration (org.springframework.data.elasticsearch.client.ClientConfiguration)6 Test (org.junit.jupiter.api.Test)5 InetSocketAddress (java.net.InetSocketAddress)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 PropertyMapper (org.springframework.boot.context.properties.PropertyMapper)1 Bean (org.springframework.context.annotation.Bean)1