Search in sources :

Example 61 with RestClient

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

the class AuthenticationImplicitIT method testRest.

public void testRest(Protocol protocol) {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    if (!mechanism.isEmpty()) {
        builder.protocol(protocol).security().authentication().mechanism(mechanism).realm("default").username("all_user").password("all");
    }
    if (mechanism.isEmpty() || "BASIC".equals(mechanism)) {
        Exceptions.expectException(SecurityException.class, () -> SERVER_TEST.rest().withClientConfiguration(builder).create());
    } else {
        RestClient client = SERVER_TEST.rest().withClientConfiguration(builder).create();
        RestResponse response = sync(client.cache(SERVER_TEST.getMethodName()).post("k1", "v1"));
        assertEquals(204, response.getStatus());
        assertEquals(protocol, response.getProtocol());
        response = sync(client.cache(SERVER_TEST.getMethodName()).get("k1"));
        assertEquals(200, response.getStatus());
        assertEquals(protocol, response.getProtocol());
        assertEquals("v1", response.getBody());
    }
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient)

Example 62 with RestClient

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

the class AuthenticationKerberosIT method testRest.

public void testRest(Protocol protocol) {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    if (!mechanism.isEmpty()) {
        builder.protocol(protocol).security().authentication().mechanism(mechanism).clientSubject(Common.createSubject("admin", "INFINISPAN.ORG", "strongPassword".toCharArray()));
    }
    if (mechanism.isEmpty()) {
        Exceptions.expectException(SecurityException.class, () -> SERVER_TEST.rest().withClientConfiguration(builder).create());
    } else {
        RestClient client = SERVER_TEST.rest().withClientConfiguration(builder).create();
        RestResponse response = sync(client.cache(SERVER_TEST.getMethodName()).post("k1", "v1"));
        assertEquals(204, response.getStatus());
        assertEquals(protocol, response.getProtocol());
        response = sync(client.cache(SERVER_TEST.getMethodName()).get("k1"));
        assertEquals(200, response.getStatus());
        assertEquals(protocol, response.getProtocol());
        assertEquals("v1", response.getBody());
    }
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient)

Example 63 with RestClient

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

the class XSiteRestMetricsOperations method testSiteStatus.

@Test
public void testSiteStatus() throws Exception {
    String lonXML = String.format(LON_CACHE_XML_CONFIG, SERVER_TEST.getMethodName());
    String nycXML = String.format(NYC_CACHE_XML_CONFIG, SERVER_TEST.getMethodName());
    RestClient client = SERVER_TEST.rest(LON).withServerConfiguration(new StringConfiguration(lonXML)).create();
    RestMetricsClient metricsClient = client.metrics();
    // create cache in NYC
    SERVER_TEST.rest(NYC).withServerConfiguration(new StringConfiguration(nycXML)).create();
    String statusMetricName = "cache_manager_default_cache_" + SERVER_TEST.getMethodName() + "_x_site_admin_nyc_status";
    try (RestResponse response = sync(metricsClient.metrics(true))) {
        assertEquals(200, response.getStatus());
        RestMetricsResource.checkIsOpenmetrics(response.contentType());
        assertTrue(response.getBody().contains("# TYPE vendor_" + statusMetricName + " gauge\n"));
    }
    assertSiteStatusMetrics(metricsClient, statusMetricName, 1);
    try (RestResponse response = sync(client.cacheManager("default").takeOffline(NYC))) {
        assertEquals(200, response.getStatus());
    }
    assertSiteStatusMetrics(metricsClient, statusMetricName, 0);
}
Also used : StringConfiguration(org.infinispan.commons.configuration.StringConfiguration) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) RestMetricsClient(org.infinispan.client.rest.RestMetricsClient) Test(org.junit.Test)

Example 64 with RestClient

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

the class RestTestClientDriver method create.

/**
 * Create a new REST client and create a cache whose name will be the test name where this method
 * is called from.
 *
 * @return new {@link RestClient} instance
 */
public RestClient create() {
    RestClient restClient = get();
    String name = testClient.getMethodName(qualifier);
    CompletionStage<RestResponse> future;
    if (serverConfiguration != null) {
        RestEntity configEntity = RestEntity.create(MediaType.APPLICATION_XML, serverConfiguration.toStringConfiguration(name));
        future = restClient.cache(name).createWithConfiguration(configEntity, flags.toArray(new CacheContainerAdmin.AdminFlag[0]));
    } else if (mode != null) {
        future = restClient.cache(name).createWithTemplate("org.infinispan." + mode.name(), flags.toArray(new CacheContainerAdmin.AdminFlag[0]));
    } else {
        future = restClient.cache(name).createWithTemplate("org.infinispan." + CacheMode.DIST_SYNC.name(), flags.toArray(new CacheContainerAdmin.AdminFlag[0]));
    }
    RestResponse response = Exceptions.unchecked(() -> future.toCompletableFuture().get(TIMEOUT, TimeUnit.SECONDS));
    response.close();
    if (response.getStatus() != 200) {
        switch(response.getStatus()) {
            case 400:
                throw new IllegalArgumentException("Bad request while attempting to obtain rest client: " + response.getStatus());
            case 401:
            case 403:
                throw new SecurityException("Authentication error while attempting to obtain rest client = " + response.getStatus());
            default:
                throw new RuntimeException("Could not obtain rest client = " + response.getStatus());
        }
    } else {
        // If the request succeeded without authn but we were expecting to authenticate, it's an error
        if (restClient.getConfiguration().security().authentication().enabled() && !response.usedAuthentication()) {
            throw new SecurityException("Authentication expected but anonymous access succeeded");
        }
        return restClient;
    }
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) CacheContainerAdmin(org.infinispan.commons.api.CacheContainerAdmin)

Example 65 with RestClient

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

the class ForkedInfinispanServerDriver method stop.

/**
 * Stop whole cluster.
 */
@Override
protected void stop() {
    try {
        // check if the server is running
        try (RestClient restClient = getRestClient(0)) {
            RestResponse response = sync(restClient.cluster().stop());
            // Ensure non-error response code from the REST endpoint.
            if (response.getStatus() >= 400) {
                throw new IllegalStateException(String.format("Failed to shutdown the cluster gracefully, got status %d.", response.getStatus()));
            } else {
                // Ensure that the server process has really quit
                // - if it has; the getPid will throw an exception
                boolean javaProcessQuit = false;
                long endTime = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(SHUTDOWN_TIMEOUT_SECONDS);
                while (!(javaProcessQuit || endTime < System.currentTimeMillis())) {
                    try {
                        forkedServers.get(0).getPid();
                        Thread.sleep(500);
                    } catch (IllegalStateException ignore) {
                        // The process has quit.
                        javaProcessQuit = true;
                    }
                }
                if (!javaProcessQuit) {
                    throw new IllegalStateException("Server Java process has not gracefully quit within " + SHUTDOWN_TIMEOUT_SECONDS + " seconds.");
                }
            }
        } catch (RuntimeException e) {
            log.warn("Server is not running", e);
        }
    } catch (Exception e) {
        // kill the servers
        log.error("Got exception while gracefully shutting down the cluster. Killing the servers.", e);
        for (int i = 0; i < configuration.numServers(); i++) {
            try {
                kill(i);
            } catch (Exception exception) {
                log.errorf("Failed to kill server #%d, exception was: %s", i, exception);
            }
        }
    } finally {
        for (int i = 0; i < configuration.numServers(); i++) {
            // Do an internal stop - e.g. stops the log monitoring process.
            if (i < forkedServers.size()) {
                forkedServers.get(i).stopInternal();
            }
        }
    }
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

RestClient (org.infinispan.client.rest.RestClient)65 Test (org.junit.Test)41 RestResponse (org.infinispan.client.rest.RestResponse)37 RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)26 Json (org.infinispan.commons.dataconversion.internal.Json)15 RestCacheClient (org.infinispan.client.rest.RestCacheClient)11 Test (org.testng.annotations.Test)10 IOException (java.io.IOException)6 AbstractMultipleSitesTest (org.infinispan.xsite.AbstractMultipleSitesTest)6 ArrayList (java.util.ArrayList)4 RestServerHelper (org.infinispan.rest.helper.RestServerHelper)4 AbstractRestResourceTest (org.infinispan.rest.resources.AbstractRestResourceTest)4 InetSocketAddress (java.net.InetSocketAddress)3 List (java.util.List)3 RestCacheManagerClient (org.infinispan.client.rest.RestCacheManagerClient)3 RestMetricsClient (org.infinispan.client.rest.RestMetricsClient)3 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 TestUser (org.infinispan.server.test.api.TestUser)3 File (java.io.File)2 UncheckedIOException (java.io.UncheckedIOException)2