Search in sources :

Example 1 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class RestURI method toConfigurationBuilder.

public RestClientConfigurationBuilder toConfigurationBuilder() {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    for (InetSocketAddress address : addresses) {
        builder.addServer().host(address.getHostString()).port(address.getPort());
    }
    if (ssl) {
        builder.security().ssl().enable();
    }
    if (username != null) {
        builder.security().authentication().username(username);
    }
    if (password != null) {
        builder.security().authentication().password(password);
    }
    builder.withProperties(properties);
    return builder;
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class RestConnectorTest method testUrlWithoutPort.

@Test
public void testUrlWithoutPort() {
    RestConnector connector = new RestConnector();
    RestConnection connection = (RestConnection) connector.getConnection("http://localhost", null);
    RestClientConfigurationBuilder builder = connection.getBuilder();
    RestClientConfiguration configuration = builder.build();
    assertEquals(80, configuration.servers().get(0).port());
    assertEquals("localhost", configuration.servers().get(0).host());
    assertFalse(configuration.security().authentication().enabled());
}
Also used : RestClientConfiguration(org.infinispan.client.rest.configuration.RestClientConfiguration) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) Test(org.junit.Test)

Example 3 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class RestConnectorTest method testUrlWithCredentials.

@Test
public void testUrlWithCredentials() {
    RestConnector connector = new RestConnector();
    RestConnection connection = (RestConnection) connector.getConnection("http://user:password@localhost:11222", null);
    RestClientConfigurationBuilder builder = connection.getBuilder();
    RestClientConfiguration configuration = builder.build();
    assertEquals(11222, configuration.servers().get(0).port());
    assertEquals("localhost", configuration.servers().get(0).host());
    assertTrue(configuration.security().authentication().enabled());
    assertEquals("user", configuration.security().authentication().username());
    assertArrayEquals("password".toCharArray(), configuration.security().authentication().password());
}
Also used : RestClientConfiguration(org.infinispan.client.rest.configuration.RestClientConfiguration) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) Test(org.junit.Test)

Example 4 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class RestConnectorTest method testEmptyUrl.

@Test
public void testEmptyUrl() {
    RestConnector connector = new RestConnector();
    RestConnection connection = (RestConnection) connector.getConnection("", null);
    RestClientConfigurationBuilder builder = connection.getBuilder();
    RestClientConfiguration configuration = builder.build();
    assertEquals(11222, configuration.servers().get(0).port());
    assertEquals("localhost", configuration.servers().get(0).host());
}
Also used : RestClientConfiguration(org.infinispan.client.rest.configuration.RestClientConfiguration) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) Test(org.junit.Test)

Example 5 with RestClientConfigurationBuilder

use of org.infinispan.client.rest.configuration.RestClientConfigurationBuilder in project infinispan by infinispan.

the class RestConnector method getConnection.

@Override
public Connection getConnection(String connectionString, SSLContextSettings sslContextSettings) {
    try {
        RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
        if (connectionString == null || connectionString.isEmpty() || "-".equals(connectionString)) {
            builder.addServer().host("localhost").port(11222);
        } else {
            Matcher matcher = HOST_PORT.matcher(connectionString);
            if (matcher.matches()) {
                String host = matcher.group(1);
                String port = matcher.group(2);
                builder.addServer().host(host).port(port != null ? Integer.parseInt(port) : 11222);
            } else {
                URL url = new URL(connectionString);
                if (!url.getProtocol().equals("http") && !url.getProtocol().equals("https")) {
                    throw new IllegalArgumentException();
                }
                int port = url.getPort();
                builder.addServer().host(url.getHost()).port(port > 0 ? port : url.getDefaultPort());
                String userInfo = url.getUserInfo();
                if (userInfo != null) {
                    String[] split = userInfo.split(":");
                    builder.security().authentication().username(URLDecoder.decode(split[0], StandardCharsets.UTF_8.name()));
                    if (split.length == 2) {
                        builder.security().authentication().password(URLDecoder.decode(split[1], StandardCharsets.UTF_8.name()));
                    }
                }
                if (url.getProtocol().equals("https")) {
                    SslConfigurationBuilder ssl = builder.security().ssl().enable();
                    if (sslContextSettings != null) {
                        ssl.sslContext(sslContextSettings.getSslContext()).trustManagers(sslContextSettings.getTrustManagers()).hostnameVerifier(sslContextSettings.getHostnameVerifier());
                    }
                }
            }
        }
        return new RestConnection(builder);
    } catch (Throwable e) {
        return null;
    }
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) SslConfigurationBuilder(org.infinispan.client.rest.configuration.SslConfigurationBuilder) Matcher(java.util.regex.Matcher) URL(java.net.URL)

Aggregations

RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)56 Test (org.junit.Test)26 RestClient (org.infinispan.client.rest.RestClient)25 RestResponse (org.infinispan.client.rest.RestResponse)22 RestClientConfiguration (org.infinispan.client.rest.configuration.RestClientConfiguration)8 RestServerHelper (org.infinispan.rest.helper.RestServerHelper)6 ConfigurationBuilder (org.infinispan.client.hotrod.configuration.ConfigurationBuilder)5 InetSocketAddress (java.net.InetSocketAddress)4 RestCacheClient (org.infinispan.client.rest.RestCacheClient)4 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)4 RestServer (org.infinispan.rest.RestServer)4 RestServerConfigurationBuilder (org.infinispan.rest.configuration.RestServerConfigurationBuilder)4 AbstractRestResourceTest (org.infinispan.rest.resources.AbstractRestResourceTest)4 DummyServerManagement (org.infinispan.server.core.DummyServerManagement)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 RestEntity (org.infinispan.client.rest.RestEntity)3 Json (org.infinispan.commons.dataconversion.internal.Json)3 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)3 Router (org.infinispan.server.router.Router)3