Search in sources :

Example 1 with RestRawClient

use of org.infinispan.client.rest.RestRawClient 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 2 with RestRawClient

use of org.infinispan.client.rest.RestRawClient in project infinispan by infinispan.

the class StaticResourceTest method call.

private RestResponse call(String path, String ifModifiedSince) {
    Map<String, String> allHeaders = new HashMap<>(NO_COMPRESSION);
    allHeaders.put(IF_MODIFIED_SINCE.getValue(), ifModifiedSince);
    allHeaders.putAll(NO_COMPRESSION);
    RestRawClient rawClient = client.raw();
    return join(rawClient.get(path, allHeaders));
}
Also used : RestRawClient(org.infinispan.client.rest.RestRawClient) HashMap(java.util.HashMap)

Example 3 with RestRawClient

use of org.infinispan.client.rest.RestRawClient in project infinispan by infinispan.

the class CacheResourceTest method testCORSPreflight.

@Test
public void testCORSPreflight() {
    String url = String.format("/rest/v2/caches/%s/%s", "default", "key");
    RestRawClient rawClient = client.raw();
    join(client.cache("default").put("key", "value"));
    Map<String, String> headers = new HashMap<>();
    headers.put(HOST.toString(), "localhost");
    headers.put(ORIGIN.toString(), "http://localhost:" + restServer().getPort());
    headers.put(ACCESS_CONTROL_REQUEST_METHOD.toString(), "GET");
    CompletionStage<RestResponse> preFlight = rawClient.options(url, headers);
    assertThat(preFlight).isOk();
    assertThat(preFlight).hasNoContent();
    assertThat(preFlight).containsAllHeaders(ACCESS_CONTROL_ALLOW_ORIGIN.toString(), ACCESS_CONTROL_ALLOW_METHODS.toString(), ACCESS_CONTROL_ALLOW_HEADERS.toString());
    assertThat(preFlight).hasHeaderWithValues(ACCESS_CONTROL_ALLOW_HEADERS.toString(), (String[]) RequestHeader.toArray());
}
Also used : RestRawClient(org.infinispan.client.rest.RestRawClient) HashMap(java.util.HashMap) RestResponse(org.infinispan.client.rest.RestResponse) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Test(org.testng.annotations.Test)

Example 4 with RestRawClient

use of org.infinispan.client.rest.RestRawClient in project infinispan by infinispan.

the class CacheResourceV2Test method testConversionFromXML.

@Test
public void testConversionFromXML() {
    RestRawClient rawClient = client.raw();
    String xml = "<infinispan>\n" + "    <cache-container>\n" + "        <distributed-cache name=\"cacheName\" mode=\"SYNC\">\n" + "            <memory>\n" + "                <object size=\"20\"/>\n" + "            </memory>\n" + "        </distributed-cache>\n" + "    </cache-container>\n" + "</infinispan>";
    CompletionStage<RestResponse> response = rawClient.post("/rest/v2/caches?action=convert", Collections.singletonMap("Accept", APPLICATION_JSON_TYPE), xml, APPLICATION_XML_TYPE);
    assertThat(response).isOk();
    checkJSON(response);
    response = rawClient.post("/rest/v2/caches?action=convert", Collections.singletonMap("Accept", APPLICATION_YAML_TYPE), xml, APPLICATION_XML_TYPE);
    assertThat(response).isOk();
    checkYaml(response);
}
Also used : RestRawClient(org.infinispan.client.rest.RestRawClient) RestResponse(org.infinispan.client.rest.RestResponse) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Test(org.testng.annotations.Test)

Example 5 with RestRawClient

use of org.infinispan.client.rest.RestRawClient in project infinispan by infinispan.

the class CacheResourceV2Test method testConversionFromJSON.

@Test
public void testConversionFromJSON() throws Exception {
    RestRawClient rawClient = client.raw();
    String json = "{\"distributed-cache\":{\"mode\":\"SYNC\",\"memory\":{\"storage\":\"OBJECT\",\"max-count\":\"20\"}}}";
    CompletionStage<RestResponse> response = rawClient.post("/rest/v2/caches?action=convert", Collections.singletonMap("Accept", APPLICATION_XML_TYPE), json, APPLICATION_JSON_TYPE);
    assertThat(response).isOk();
    checkXML(response);
    response = rawClient.post("/rest/v2/caches?action=convert", Collections.singletonMap("Accept", APPLICATION_YAML_TYPE), json, APPLICATION_JSON_TYPE);
    assertThat(response).isOk();
    checkYaml(response);
}
Also used : RestRawClient(org.infinispan.client.rest.RestRawClient) RestResponse(org.infinispan.client.rest.RestResponse) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Test(org.testng.annotations.Test)

Aggregations

RestRawClient (org.infinispan.client.rest.RestRawClient)7 RestResponse (org.infinispan.client.rest.RestResponse)5 Test (org.testng.annotations.Test)5 Util.getResourceAsString (org.infinispan.commons.util.Util.getResourceAsString)4 HashMap (java.util.HashMap)2 RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)1 ServerConfigurationBuilder (org.infinispan.client.rest.configuration.ServerConfigurationBuilder)1 Router (org.infinispan.server.router.Router)1 RouterConfigurationBuilder (org.infinispan.server.router.configuration.builder.RouterConfigurationBuilder)1 EndpointRouter (org.infinispan.server.router.router.EndpointRouter)1 Route (org.infinispan.server.router.routes.Route)1 RestRouteSource (org.infinispan.server.router.routes.rest.RestRouteSource)1 RestServerRouteDestination (org.infinispan.server.router.routes.rest.RestServerRouteDestination)1 Test (org.junit.Test)1