Search in sources :

Example 1 with ZSession

use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.

the class SessionTest method testDeleteSession.

@Test
public void testDeleteSession() {
    ZSession session = createSession("30");
    WebResource wr = sessionsr.path(session.id);
    Builder b = wr.accept(MediaType.APPLICATION_JSON);
    Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
    ClientResponse cr = b.delete(ClientResponse.class, null);
    Assert.assertEquals(ClientResponse.Status.NO_CONTENT, cr.getClientResponseStatus());
    Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) ZSession(org.apache.zookeeper.server.jersey.jaxb.ZSession) Test(org.junit.Test)

Example 2 with ZSession

use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.

the class SessionTest method testCreateNewSession.

@Test
public void testCreateNewSession() throws JSONException {
    ZSession session = createSession();
    Assert.assertEquals(session.id.length(), 36);
    // use out-of-band method to verify
    Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
Also used : ZSession(org.apache.zookeeper.server.jersey.jaxb.ZSession) Test(org.junit.Test)

Example 3 with ZSession

use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.

the class SessionTest method testSendHeartbeat.

@Test
public void testSendHeartbeat() throws InterruptedException {
    ZSession session = createSession("2");
    Thread.sleep(1000);
    WebResource wr = sessionsr.path(session.id);
    Builder b = wr.accept(MediaType.APPLICATION_JSON);
    ClientResponse cr = b.put(ClientResponse.class, null);
    Assert.assertEquals(ClientResponse.Status.OK, cr.getClientResponseStatus());
    Thread.sleep(1500);
    Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
    Thread.sleep(1000);
    Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) ZSession(org.apache.zookeeper.server.jersey.jaxb.ZSession) Test(org.junit.Test)

Example 4 with ZSession

use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.

the class SessionTest method testSessionExpires.

@Test
public void testSessionExpires() throws InterruptedException {
    ZSession session = createSession("1");
    // use out-of-band method to verify
    Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
    // wait for the session to be closed
    Thread.sleep(1500);
    Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
Also used : ZSession(org.apache.zookeeper.server.jersey.jaxb.ZSession) Test(org.junit.Test)

Example 5 with ZSession

use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.

the class SessionsResource method createSession.

@POST
@Produces({ MediaType.APPLICATION_JSON, "application/javascript", MediaType.APPLICATION_XML })
public Response createSession(@QueryParam("op") String op, @DefaultValue("5") @QueryParam("expire") String expire, @Context UriInfo ui) {
    if (!op.equals("create")) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), "")).build());
    }
    int expireInSeconds;
    try {
        expireInSeconds = Integer.parseInt(expire);
    } catch (NumberFormatException e) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build());
    }
    String uuid = UUID.randomUUID().toString();
    while (ZooKeeperService.isConnected(contextPath, uuid)) {
        uuid = UUID.randomUUID().toString();
    }
    // establish the connection to the ZooKeeper cluster
    try {
        ZooKeeperService.getClient(contextPath, uuid, expireInSeconds);
    } catch (IOException e) {
        LOG.error("Failed while trying to create a new session", e);
        throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
    }
    URI uri = ui.getAbsolutePathBuilder().path(uuid).build();
    return Response.created(uri).entity(new JSONWithPadding(new ZSession(uuid, uri.toString()))).build();
}
Also used : JSONWithPadding(com.sun.jersey.api.json.JSONWithPadding) WebApplicationException(javax.ws.rs.WebApplicationException) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) IOException(java.io.IOException) URI(java.net.URI) ZSession(org.apache.zookeeper.server.jersey.jaxb.ZSession) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

ZSession (org.apache.zookeeper.server.jersey.jaxb.ZSession)6 Test (org.junit.Test)5 ClientResponse (com.sun.jersey.api.client.ClientResponse)3 WebResource (com.sun.jersey.api.client.WebResource)3 Builder (com.sun.jersey.api.client.WebResource.Builder)3 JSONWithPadding (com.sun.jersey.api.json.JSONWithPadding)1 IOException (java.io.IOException)1 URI (java.net.URI)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 Stat (org.apache.zookeeper.data.Stat)1 ZError (org.apache.zookeeper.server.jersey.jaxb.ZError)1