Search in sources :

Example 6 with RestClientConfigurationBuilder

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

the class RestEndpointRouterTest method shouldRouteToProperRestServerBasedOnPath.

/**
 * In this scenario we create 2 REST servers, each one with different REST Path: <ul> <li>REST1 -
 * http://127.0.0.1:8080/rest/rest1</li> <li>REST2 - http://127.0.0.1:8080/rest/rest2</li> </ul>
 * <p>
 * The router should match requests based on path and redirect them to proper server.
 */
@Test
public void shouldRouteToProperRestServerBasedOnPath() {
    // given
    restServer1 = RestTestingUtil.createDefaultRestServer("rest1", "default");
    restServer2 = RestTestingUtil.createDefaultRestServer("rest2", "default");
    RestServerRouteDestination rest1Destination = new RestServerRouteDestination("rest1", restServer1);
    RestRouteSource rest1Source = new RestRouteSource("rest1");
    Route<RestRouteSource, RestServerRouteDestination> routeToRest1 = new Route<>(rest1Source, rest1Destination);
    RestServerRouteDestination rest2Destination = new RestServerRouteDestination("rest2", restServer2);
    RestRouteSource rest2Source = new RestRouteSource("rest2");
    Route<RestRouteSource, RestServerRouteDestination> routeToRest2 = new Route<>(rest2Source, rest2Destination);
    RouterConfigurationBuilder routerConfigurationBuilder = new RouterConfigurationBuilder();
    routerConfigurationBuilder.rest().port(8080).ip(InetAddress.getLoopbackAddress()).routing().add(routeToRest1).add(routeToRest2);
    router = new Router(routerConfigurationBuilder.build());
    router.start();
    int port = router.getRouter(EndpointRouter.Protocol.REST).get().getPort();
    // when
    ServerConfigurationBuilder builder = new RestClientConfigurationBuilder().addServer().host("127.0.0.1").port(port);
    restClient = RestClient.forConfiguration(builder.build());
    RestRawClient rawClient = restClient.raw();
    String path1 = "/rest/rest1/v2/caches/default/test";
    String path2 = "/rest/rest2/v2/caches/default/test";
    join(rawClient.putValue(path1, emptyMap(), "rest1", TEXT_PLAIN_TYPE));
    join(rawClient.putValue(path2, emptyMap(), "rest2", TEXT_PLAIN_TYPE));
    String valueReturnedFromRest1 = join(rawClient.get(path1)).getBody();
    String valueReturnedFromRest2 = join(rawClient.get(path2)).getBody();
    // then
    assertThat(valueReturnedFromRest1).isEqualTo("rest1");
    assertThat(valueReturnedFromRest2).isEqualTo("rest2");
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestRawClient(org.infinispan.client.rest.RestRawClient) RestRouteSource(org.infinispan.server.router.routes.rest.RestRouteSource) Router(org.infinispan.server.router.Router) EndpointRouter(org.infinispan.server.router.router.EndpointRouter) RestServerRouteDestination(org.infinispan.server.router.routes.rest.RestServerRouteDestination) RouterConfigurationBuilder(org.infinispan.server.router.configuration.builder.RouterConfigurationBuilder) Route(org.infinispan.server.router.routes.Route) ServerConfigurationBuilder(org.infinispan.client.rest.configuration.ServerConfigurationBuilder) Test(org.junit.Test)

Example 7 with RestClientConfigurationBuilder

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

the class SinglePortTest method shouldUpgradeThroughALPN.

@Test
public void shouldUpgradeThroughALPN() throws Exception {
    checkForOpenSSL();
    // given
    restServer = RestTestingUtil.createDefaultRestServer("rest", "default");
    RestServerRouteDestination restDestination = new RestServerRouteDestination("rest", restServer);
    SinglePortRouteSource singlePortSource = new SinglePortRouteSource();
    Route<SinglePortRouteSource, RestServerRouteDestination> routeToRest = new Route<>(singlePortSource, restDestination);
    SslContextFactory sslContextFactory = new SslContextFactory();
    RouterConfigurationBuilder routerConfigurationBuilder = new RouterConfigurationBuilder();
    routerConfigurationBuilder.singlePort().sslContext(sslContextFactory.keyStoreFileName(KEY_STORE_PATH).keyStorePassword(KEY_STORE_PASSWORD.toCharArray()).getContext()).port(0).ip(InetAddress.getLoopbackAddress()).routing().add(routeToRest);
    router = new Router(routerConfigurationBuilder.build());
    router.start();
    EndpointRouter singlePortRouter = router.getRouter(EndpointRouter.Protocol.SINGLE_PORT).get();
    // when
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    builder.addServer().host(singlePortRouter.getHost()).port(singlePortRouter.getPort()).protocol(Protocol.HTTP_20).security().ssl().trustStoreFileName(TRUST_STORE_PATH).trustStorePassword("secret".toCharArray()).hostnameVerifier((hostname, session) -> true);
    httpClient = RestClient.forConfiguration(builder.build());
    CompletionStage<RestResponse> response = httpClient.cache("default").post("test", VALUE);
    // then
    ResponseAssertion.assertThat(response).hasNoContent();
}
Also used : SslContextFactory(org.infinispan.commons.util.SslContextFactory) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) SinglePortRouteSource(org.infinispan.server.router.routes.singleport.SinglePortRouteSource) RestResponse(org.infinispan.client.rest.RestResponse) Router(org.infinispan.server.router.Router) EndpointRouter(org.infinispan.server.router.router.EndpointRouter) RestServerRouteDestination(org.infinispan.server.router.routes.rest.RestServerRouteDestination) EndpointRouter(org.infinispan.server.router.router.EndpointRouter) RouterConfigurationBuilder(org.infinispan.server.router.configuration.builder.RouterConfigurationBuilder) Route(org.infinispan.server.router.routes.Route) Test(org.junit.Test)

Example 8 with RestClientConfigurationBuilder

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

the class SinglePortTest method shouldUpgradeThroughHTTP11UpgradeHeaders.

@Test
public void shouldUpgradeThroughHTTP11UpgradeHeaders() {
    // given
    restServer = RestTestingUtil.createDefaultRestServer("rest", "default");
    RestServerRouteDestination restDestination = new RestServerRouteDestination("rest1", restServer);
    SinglePortRouteSource singlePortSource = new SinglePortRouteSource();
    Route<SinglePortRouteSource, RestServerRouteDestination> routeToRest = new Route<>(singlePortSource, restDestination);
    RouterConfigurationBuilder routerConfigurationBuilder = new RouterConfigurationBuilder();
    routerConfigurationBuilder.singlePort().port(0).ip(InetAddress.getLoopbackAddress()).routing().add(routeToRest);
    router = new Router(routerConfigurationBuilder.build());
    router.start();
    int port = router.getRouter(EndpointRouter.Protocol.SINGLE_PORT).get().getPort();
    // when
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    builder.addServer().host("localhost").port(port).protocol(Protocol.HTTP_20);
    httpClient = RestClient.forConfiguration(builder.build());
    CompletionStage<RestResponse> response = httpClient.cache("default").post("test", VALUE);
    // then
    ResponseAssertion.assertThat(response).hasNoContent();
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) SinglePortRouteSource(org.infinispan.server.router.routes.singleport.SinglePortRouteSource) RestResponse(org.infinispan.client.rest.RestResponse) Router(org.infinispan.server.router.Router) EndpointRouter(org.infinispan.server.router.router.EndpointRouter) RestServerRouteDestination(org.infinispan.server.router.routes.rest.RestServerRouteDestination) RouterConfigurationBuilder(org.infinispan.server.router.configuration.builder.RouterConfigurationBuilder) Route(org.infinispan.server.router.routes.Route) Test(org.junit.Test)

Example 9 with RestClientConfigurationBuilder

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

the class IndexedCacheNonIndexedEntityTest method createCacheManager.

@Override
protected EmbeddedCacheManager createCacheManager() {
    GlobalConfigurationBuilder gcb = new GlobalConfigurationBuilder().nonClusteredDefault();
    EmbeddedCacheManager cm = TestCacheManagerFactory.createCacheManager(gcb, getDefaultStandaloneCacheConfig(false));
    restServer = new RestServerHelper(cm);
    restServer.start(TestResourceTracker.getCurrentTestShortName() + "-" + cm.getAddress());
    RestClientConfigurationBuilder clientConfigurationBuilder = new RestClientConfigurationBuilder();
    clientConfigurationBuilder.addServer().host(restServer.getHost()).port(restServer.getPort());
    client = RestClient.forConfiguration(clientConfigurationBuilder.build());
    return cm;
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) RestServerHelper(org.infinispan.rest.helper.RestServerHelper)

Example 10 with RestClientConfigurationBuilder

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

the class ForkedInfinispanServerDriver method getRestClient.

private RestClient getRestClient(int server) {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    // Filter driver properties for REST client configuration properties, e.g. security configuration
    // and apply them before applying rest of the dynamically created configuration, e.g. port.
    Properties securityConfigurationProperties = new Properties();
    configuration.properties().entrySet().stream().filter(entry -> entry.getKey().toString().startsWith("infinispan.client.rest.")).forEach(entry -> securityConfigurationProperties.put(entry.getKey(), entry.getValue()));
    builder.withProperties(securityConfigurationProperties);
    // Ensure to not print out the *values*!!!
    log.debugf("Configured client with the following properties: %s", securityConfigurationProperties.keySet().toString());
    builder.addServer().host("localhost").port(getServerPort(server, ForkedServer.DEFAULT_SINGLE_PORT));
    return RestClient.forConfiguration(builder.build());
}
Also used : RestClient(org.infinispan.client.rest.RestClient) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) Server(org.infinispan.server.Server) ConnectException(java.net.ConnectException) Path(java.nio.file.Path) Properties(java.util.Properties) MBeanServerConnection(javax.management.MBeanServerConnection) Log(org.infinispan.commons.logging.Log) RestResponse(org.infinispan.client.rest.RestResponse) Files(java.nio.file.Files) Util(org.infinispan.commons.util.Util) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Paths(java.nio.file.Paths) Exceptions(org.infinispan.commons.test.Exceptions) CommonsTestingUtil(org.infinispan.commons.test.CommonsTestingUtil) OS(org.infinispan.commons.util.OS) LogFactory(org.infinispan.commons.logging.LogFactory) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) Properties(java.util.Properties)

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