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();
}
}
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);
}
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();
}
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));
}
}
Aggregations