Search in sources :

Example 1 with ZChildrenJSON

use of org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON 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 ZChildrenJSON

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

the class GetChildrenTest method testGetChildren.

@Test
public void testGetChildren() throws Exception {
    if (expectedChildren != null) {
        for (String child : expectedChildren) {
            zk.create(expectedPath + "/" + child, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    }
    ClientResponse cr = znodesr.path(path).queryParam("view", "children").accept(accept).get(ClientResponse.class);
    Assert.assertEquals(expectedStatus, cr.getClientResponseStatus());
    if (expectedChildren == null) {
        return;
    }
    if (accept.equals(MediaType.APPLICATION_JSON)) {
        ZChildrenJSON zchildren = cr.getEntity(ZChildrenJSON.class);
        Collections.sort(expectedChildren);
        Collections.sort(zchildren.children);
        Assert.assertEquals(expectedChildren, zchildren.children);
        Assert.assertEquals(znodesr.path(path).toString(), zchildren.uri);
        Assert.assertEquals(znodesr.path(path).toString() + "/{child}", zchildren.child_uri_template);
    } else if (accept.equals(MediaType.APPLICATION_XML)) {
        ZChildren zchildren = cr.getEntity(ZChildren.class);
        Collections.sort(expectedChildren);
        Collections.sort(zchildren.children);
        Assert.assertEquals(expectedChildren, zchildren.children);
        Assert.assertEquals(znodesr.path(path).toString(), zchildren.uri);
        Assert.assertEquals(znodesr.path(path).toString() + "/{child}", zchildren.child_uri_template);
    } else {
        Assert.fail("unknown accept type");
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ZChildrenJSON(org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON) ZChildren(org.apache.zookeeper.server.jersey.jaxb.ZChildren) Test(org.junit.Test)

Aggregations

ZChildren (org.apache.zookeeper.server.jersey.jaxb.ZChildren)2 ZChildrenJSON (org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON)2 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 JSONWithPadding (com.sun.jersey.api.json.JSONWithPadding)1 ArrayList (java.util.ArrayList)1 Stat (org.apache.zookeeper.data.Stat)1 ZStat (org.apache.zookeeper.server.jersey.jaxb.ZStat)1 Test (org.junit.Test)1