Search in sources :

Example 1 with ZStat

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

the class ZNodeResource method getZNodeList.

private Response getZNodeList(boolean json, String path, String callback, String view, String dataformat, UriInfo ui) throws InterruptedException, KeeperException {
    ensurePathNotNull(path);
    if (view.equals("children")) {
        List<String> children = new ArrayList<String>();
        for (String child : zk.getChildren(path, false)) {
            children.add(child);
        }
        Object child;
        String childTemplate = ui.getAbsolutePath().toString();
        if (!childTemplate.endsWith("/")) {
            childTemplate += "/";
        }
        childTemplate += "{child}";
        if (json) {
            child = new ZChildrenJSON(path, ui.getAbsolutePath().toString(), childTemplate, children);
        } else {
            child = new ZChildren(path, ui.getAbsolutePath().toString(), childTemplate, children);
        }
        return Response.status(Response.Status.OK).entity(new JSONWithPadding(child, callback)).build();
    } else {
        Stat stat = new Stat();
        byte[] data = zk.getData(path, false, stat);
        byte[] data64;
        String dataUtf8;
        if (data == null) {
            data64 = null;
            dataUtf8 = null;
        } else if (!dataformat.equals("utf8")) {
            data64 = data;
            dataUtf8 = null;
        } else {
            data64 = null;
            dataUtf8 = new String(data);
        }
        ZStat zstat = new ZStat(path, ui.getAbsolutePath().toString(), data64, dataUtf8, stat.getCzxid(), stat.getMzxid(), stat.getCtime(), stat.getMtime(), stat.getVersion(), stat.getCversion(), stat.getAversion(), stat.getEphemeralOwner(), stat.getDataLength(), stat.getNumChildren(), stat.getPzxid());
        return Response.status(Response.Status.OK).entity(new JSONWithPadding(zstat, callback)).build();
    }
}
Also used : JSONWithPadding(com.sun.jersey.api.json.JSONWithPadding) Stat(org.apache.zookeeper.data.Stat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) ArrayList(java.util.ArrayList) ZChildrenJSON(org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON) ZChildren(org.apache.zookeeper.server.jersey.jaxb.ZChildren)

Example 2 with ZStat

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

the class GetTest method testGet.

@Test
public void testGet() throws Exception {
    if (expectedStat != null) {
        if (expectedStat.data64 != null || expectedStat.dataUtf8 == null) {
            zk.setData(expectedStat.path, expectedStat.data64, -1);
        } else {
            zk.setData(expectedStat.path, expectedStat.dataUtf8.getBytes(), -1);
        }
    }
    ClientResponse cr = znodesr.path(path).queryParam("dataformat", encoding).accept(accept).get(ClientResponse.class);
    Assert.assertEquals(expectedStatus, cr.getClientResponseStatus());
    if (expectedStat == null) {
        return;
    }
    ZStat zstat = cr.getEntity(ZStat.class);
    Assert.assertEquals(expectedStat, zstat);
    Assert.assertEquals(znodesr.path(path).toString(), zstat.uri);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) Test(org.junit.Test)

Example 3 with ZStat

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

the class ZNodeResource method setZNode.

@PUT
@Produces({ MediaType.APPLICATION_JSON, "application/javascript", MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response setZNode(@PathParam("path") String path, @QueryParam("callback") String callback, @DefaultValue("-1") @QueryParam("version") String versionParam, @DefaultValue("base64") @QueryParam("dataformat") String dataformat, @DefaultValue("false") @QueryParam("null") String setNull, @Context UriInfo ui, byte[] data) throws InterruptedException, KeeperException {
    ensurePathNotNull(path);
    int version;
    try {
        version = Integer.parseInt(versionParam);
    } catch (NumberFormatException e) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), path + " bad version " + versionParam)).build());
    }
    if (setNull.equals("true")) {
        data = null;
    }
    Stat stat = zk.setData(path, data, version);
    ZStat zstat = new ZStat(path, ui.getAbsolutePath().toString(), null, null, stat.getCzxid(), stat.getMzxid(), stat.getCtime(), stat.getMtime(), stat.getVersion(), stat.getCversion(), stat.getAversion(), stat.getEphemeralOwner(), stat.getDataLength(), stat.getNumChildren(), stat.getPzxid());
    return Response.status(Response.Status.OK).entity(new JSONWithPadding(zstat, callback)).build();
}
Also used : JSONWithPadding(com.sun.jersey.api.json.JSONWithPadding) Stat(org.apache.zookeeper.data.Stat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) WebApplicationException(javax.ws.rs.WebApplicationException) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 4 with ZStat

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

the class SetTest method testSet.

@Test
public void testSet() throws Exception {
    if (expectedStat != null) {
        zk.create(expectedStat.path, "initial".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    WebResource wr = znodesr.path(path).queryParam("dataformat", encoding);
    if (data == null) {
        wr = wr.queryParam("null", "true");
    }
    Builder builder = wr.accept(accept).type(MediaType.APPLICATION_OCTET_STREAM);
    ClientResponse cr;
    if (data == null) {
        cr = builder.put(ClientResponse.class);
    } else {
        // this shouldn't be necessary (wrapping data with string)
        // but without it there are problems on the server - ie it
        // hangs for 30 seconds and doesn't get the data.
        // TODO investigate
        cr = builder.put(ClientResponse.class, new String(data));
    }
    Assert.assertEquals(expectedStatus, cr.getClientResponseStatus());
    if (expectedStat == null) {
        return;
    }
    ZStat zstat = cr.getEntity(ZStat.class);
    Assert.assertEquals(expectedStat, zstat);
    // use out-of-band method to verify
    byte[] data = zk.getData(zstat.path, false, new Stat());
    if (data == null && this.data == null) {
        return;
    } else if (data == null || this.data == null) {
        Assert.fail((data == null ? null : new String(data)) + " == " + (this.data == null ? null : new String(this.data)));
    } else {
        Assert.assertTrue(new String(data) + " == " + new String(this.data), Arrays.equals(data, this.data));
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Stat(org.apache.zookeeper.data.Stat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Aggregations

ZStat (org.apache.zookeeper.server.jersey.jaxb.ZStat)4 Stat (org.apache.zookeeper.data.Stat)3 ClientResponse (com.sun.jersey.api.client.ClientResponse)2 JSONWithPadding (com.sun.jersey.api.json.JSONWithPadding)2 Test (org.junit.Test)2 WebResource (com.sun.jersey.api.client.WebResource)1 Builder (com.sun.jersey.api.client.WebResource.Builder)1 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ZChildren (org.apache.zookeeper.server.jersey.jaxb.ZChildren)1 ZChildrenJSON (org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON)1 ZError (org.apache.zookeeper.server.jersey.jaxb.ZError)1