Search in sources :

Example 41 with Executor

use of org.apache.http.client.fluent.Executor in project redisson by redisson.

the class RedissonSessionManagerTest method testInvalidate.

@Test
public void testInvalidate() throws Exception {
    File f = Paths.get("").toAbsolutePath().resolve("src/test/webapp/WEB-INF/redisson.yaml").toFile();
    Config config = Config.fromYAML(f);
    RedissonClient r = Redisson.create(config);
    r.getKeys().flushall();
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(8080, executor, "test", "1234");
    Cookie cookie = cookieStore.getCookies().get(0);
    invalidate(executor);
    Executor.closeIdleConnections();
    executor = Executor.newInstance();
    cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    executor.use(cookieStore);
    read(8080, executor, "test", "null");
    invalidate(executor);
    Executor.closeIdleConnections();
    server.stop();
    TimeUnit.SECONDS.sleep(60);
    Assert.assertEquals(0, r.getKeys().count());
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RedissonClient(org.redisson.api.RedissonClient) Executor(org.apache.http.client.fluent.Executor) Config(org.redisson.config.Config) File(java.io.File) Test(org.junit.Test)

Example 42 with Executor

use of org.apache.http.client.fluent.Executor in project redisson by redisson.

the class RedissonSessionManagerTest method testUpdateTwoServers.

@Test
public void testUpdateTwoServers() throws Exception {
    TomcatServer server1 = new TomcatServer("myapp", 8080, "src/test/");
    server1.start();
    TomcatServer server2 = new TomcatServer("myapp", 8081, "src/test/");
    server2.start();
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(8080, executor, "test", "1234");
    read(8081, executor, "test", "1234");
    read(8080, executor, "test", "1234");
    write(8080, executor, "test", "324");
    read(8081, executor, "test", "324");
    Executor.closeIdleConnections();
    server1.stop();
    server2.stop();
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Executor(org.apache.http.client.fluent.Executor) Test(org.junit.Test)

Example 43 with Executor

use of org.apache.http.client.fluent.Executor in project redisson by redisson.

the class RedissonSessionManagerTest method testWriteReadRemove.

@Test
public void testWriteReadRemove() throws Exception {
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    Executor executor = Executor.newInstance();
    write(8080, executor, "test", "1234");
    read(8080, executor, "test", "1234");
    remove(executor, "test", "null");
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Executor(org.apache.http.client.fluent.Executor) Test(org.junit.Test)

Example 44 with Executor

use of org.apache.http.client.fluent.Executor in project redisson by redisson.

the class RedissonSessionManagerTest method testRecreate.

@Test
public void testRecreate() throws Exception {
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    Executor executor = Executor.newInstance();
    write(executor, "test", "1");
    recreate(executor, "test", "2");
    read(executor, "test", "2");
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Executor(org.apache.http.client.fluent.Executor) Test(org.junit.jupiter.api.Test)

Example 45 with Executor

use of org.apache.http.client.fluent.Executor in project streamsx.topology by IBMStreams.

the class ICP4DAuthenticator method config.

public JsonObject config(boolean verify, boolean forceRecreate) throws IOException {
    if (cfg != null && !forceRecreate)
        return cfg;
    this.verify = verify;
    Executor executor = RestUtils.createExecutor(!verify);
    JsonObject namepwd = new JsonObject();
    String[] userPwd = Util.getDefaultUserPassword(user, password);
    namepwd.addProperty("username", userPwd[0]);
    namepwd.addProperty("password", userPwd[1]);
    Request post = Request.Post(authorizeUrl.toExternalForm()).bodyString(namepwd.toString(), ContentType.APPLICATION_JSON);
    JsonObject resp = RestUtils.requestGsonResponse(executor, post);
    String icp4dToken = GsonUtilities.jstring(resp, "token");
    // these tokens have ~ 12 hours lifetime, with 11 hours from now as expected expiry time we are comfortable
    expire = System.currentTimeMillis() + 11l * 3600l * MS;
    String icpdAuth = RestUtils.createBearerAuth(icp4dToken);
    String serviceId = null;
    JsonObject sci = null;
    JsonObject sca = null;
    // Occasionally see null for connection-info.
    for (int i = 0; i < 5; i++) {
        resp = RestUtils.getGsonResponse(executor, icpdAuth, detailsUrl);
        JsonObject sro = object(resp, "requestObj");
        serviceId = jstring(sro, "ID");
        sca = object(sro, "CreateArguments");
        sci = object(sca, "connection-info");
        if (sci != null && !sci.entrySet().isEmpty())
            break;
        sci = null;
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            break;
        }
    }
    if (sci == null)
        throw new IllegalStateException("Unable to retrieve connection details for Streams instance: " + instanceName);
    this.serviceAuth = RestUtils.createBearerAuth(icp4dToken);
    JsonObject connInfo = new JsonObject();
    final String externalBuildPoolsEndpoint = jstring(sci, "externalBuildPoolsEndpoint");
    if (externalBuildPoolsEndpoint != null) {
        URL buildPoolsUrl = urlFromEndPoint(externalBuildPoolsEndpoint);
        connInfo.addProperty("serviceBuildPoolsEndpoint", buildPoolsUrl.toExternalForm());
    }
    final String externalBuildEndpoint = jstring(sci, "externalBuildEndpoint");
    URL buildUrl = urlFromEndPoint(externalBuildEndpoint);
    final String externalRestResourceEndpoint = jstring(sci, "externalRestResourceEndpoint");
    if (externalRestResourceEndpoint != null) {
        URL restResourceUrl = urlFromEndPoint(externalRestResourceEndpoint);
        connInfo.addProperty(StreamsKeys.STREAMS_REST_RESOURCES_ENDPOINT, restResourceUrl.toExternalForm());
    }
    final String externalRestEndpoint = jstring(sci, "externalRestEndpoint");
    URL streamsUrl = urlFromEndPoint(externalRestEndpoint);
    JsonObject instance = object(sca, "metadata", "instance");
    String serviceName = jstring(instance, "id");
    // Return object matches one created in Python.
    connInfo.addProperty("serviceRestEndpoint", streamsUrl.toExternalForm());
    connInfo.addProperty("serviceBuildEndpoint", buildUrl.toExternalForm());
    JsonObject cfg = new JsonObject();
    cfg.addProperty("type", "streams");
    cfg.addProperty("externalClient", true);
    cfg.add("connection_info", connInfo);
    cfg.addProperty("service_token", icp4dToken);
    cfg.addProperty("service_token_expire", expire);
    cfg.addProperty("user_token", icp4dToken);
    cfg.addProperty("service_name", serviceName);
    cfg.addProperty("cluster_ip", icpdUrl.getHost());
    cfg.addProperty("cluster_port", icpdUrl.getPort());
    cfg.addProperty("service_id", serviceId);
    this.cfg = cfg;
    return cfg;
}
Also used : Executor(org.apache.http.client.fluent.Executor) Request(org.apache.http.client.fluent.Request) JsonObject(com.google.gson.JsonObject) URL(java.net.URL)

Aggregations

Executor (org.apache.http.client.fluent.Executor)46 Test (org.junit.Test)26 Request (org.apache.http.client.fluent.Request)12 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)11 HttpResponse (org.apache.http.HttpResponse)6 Response (org.apache.http.client.fluent.Response)6 URI (java.net.URI)5 Cookie (org.apache.http.cookie.Cookie)5 Test (org.junit.jupiter.api.Test)5 JsonObject (com.google.gson.JsonObject)4 InputStream (java.io.InputStream)4 DistributionTransportSecret (org.apache.sling.distribution.transport.DistributionTransportSecret)4 StreamsConnection (com.ibm.streamsx.rest.StreamsConnection)3 HashMap (java.util.HashMap)3 DistributionRequest (org.apache.sling.distribution.DistributionRequest)3 JsonParser (com.google.gson.JsonParser)2 Instance (com.ibm.streamsx.rest.Instance)2 ICP4DAuthenticator (com.ibm.streamsx.rest.internal.ICP4DAuthenticator)2 StandaloneAuthenticator (com.ibm.streamsx.rest.internal.StandaloneAuthenticator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2