use of org.apache.zookeeper.server.jersey.jaxb.ZPath in project zookeeper by apache.
the class CreateTest method testCreate.
@Test
public void testCreate() throws Exception {
WebResource wr = znodesr.path(path).queryParam("dataformat", encoding).queryParam("name", name);
if (data == null) {
wr = wr.queryParam("null", "true");
}
if (sequence) {
wr = wr.queryParam("sequence", "true");
}
Builder builder = wr.accept(accept);
ClientResponse cr;
if (data == null) {
cr = builder.post(ClientResponse.class);
} else {
cr = builder.post(ClientResponse.class, data);
}
Assert.assertEquals(expectedStatus, cr.getClientResponseStatus());
if (expectedPath == null) {
return;
}
ZPath zpath = cr.getEntity(ZPath.class);
if (sequence) {
Assert.assertTrue(zpath.path.startsWith(expectedPath.path));
Assert.assertTrue(zpath.uri.startsWith(znodesr.path(path).toString()));
} else {
Assert.assertEquals(expectedPath, zpath);
Assert.assertEquals(znodesr.path(path).toString(), zpath.uri);
}
// use out-of-band method to verify
byte[] data = zk.getData(zpath.path, false, new Stat());
if (data == null && this.data == null) {
return;
} else if (data == null || this.data == null) {
Assert.assertEquals(data, this.data);
} else {
Assert.assertTrue(new String(data) + " == " + new String(this.data), Arrays.equals(data, this.data));
}
}
use of org.apache.zookeeper.server.jersey.jaxb.ZPath in project zookeeper by apache.
the class ZNodeResource method createZNodeAsOctet.
@POST
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response createZNodeAsOctet(@PathParam("path") String path, @DefaultValue("create") @QueryParam("op") String op, @QueryParam("name") String name, @DefaultValue("false") @QueryParam("null") String setNull, @DefaultValue("false") @QueryParam("sequence") String sequence, @Context UriInfo ui, byte[] data) throws InterruptedException, KeeperException {
ensurePathNotNull(path);
if (path.equals("/")) {
path += name;
} else {
path += "/" + name;
}
if (!op.equals("create")) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), path + " bad operaton " + op)).build());
}
if (setNull.equals("true")) {
data = null;
}
CreateMode createMode;
if (sequence.equals("true")) {
createMode = CreateMode.PERSISTENT_SEQUENTIAL;
} else {
createMode = CreateMode.PERSISTENT;
}
String newPath = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
URI uri = ui.getAbsolutePathBuilder().path(newPath).build();
return Response.created(uri).entity(new ZPath(newPath, ui.getAbsolutePath().toString())).build();
}
use of org.apache.zookeeper.server.jersey.jaxb.ZPath in project zookeeper by apache.
the class ZNodeResource method createZNode.
@POST
@Produces({ MediaType.APPLICATION_JSON, "application/javascript", MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response createZNode(@PathParam("path") String path, @QueryParam("callback") String callback, @DefaultValue("create") @QueryParam("op") String op, @QueryParam("name") String name, @DefaultValue("base64") @QueryParam("dataformat") String dataformat, @DefaultValue("false") @QueryParam("null") String setNull, @DefaultValue("false") @QueryParam("sequence") String sequence, @DefaultValue("false") @QueryParam("ephemeral") String ephemeral, @Context UriInfo ui, byte[] data) throws InterruptedException, KeeperException {
ensurePathNotNull(path);
if (path.equals("/")) {
path += name;
} else {
path += "/" + name;
}
if (!op.equals("create")) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), path + " bad operaton " + op)).build());
}
if (setNull.equals("true")) {
data = null;
}
CreateMode createMode;
if (sequence.equals("true")) {
if (ephemeral.equals("false")) {
createMode = CreateMode.PERSISTENT_SEQUENTIAL;
} else {
createMode = CreateMode.EPHEMERAL_SEQUENTIAL;
}
} else if (ephemeral.equals("false")) {
createMode = CreateMode.PERSISTENT;
} else {
createMode = CreateMode.EPHEMERAL;
}
String newPath = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
URI uri = ui.getAbsolutePathBuilder().path(newPath).build();
return Response.created(uri).entity(new JSONWithPadding(new ZPath(newPath, ui.getAbsolutePath().toString()))).build();
}
use of org.apache.zookeeper.server.jersey.jaxb.ZPath in project zookeeper by apache.
the class RootTest method testCreate.
@Test
public void testCreate() throws Exception {
String path = "/";
String name = "roottest-create";
byte[] data = "foo".getBytes();
WebResource wr = znodesr.path(path).queryParam("dataformat", "utf8").queryParam("name", name);
Builder builder = wr.accept(MediaType.APPLICATION_JSON);
ClientResponse cr;
cr = builder.post(ClientResponse.class, data);
Assert.assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus());
ZPath zpath = cr.getEntity(ZPath.class);
Assert.assertEquals(new ZPath(path + name), zpath);
Assert.assertEquals(znodesr.path(path).toString(), zpath.uri);
// use out-of-band method to verify
byte[] rdata = zk.getData(zpath.path, false, new Stat());
Assert.assertTrue(new String(rdata) + " == " + new String(data), Arrays.equals(rdata, data));
}
Aggregations