Search in sources :

Example 1 with ZPath

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));
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Stat(org.apache.zookeeper.data.Stat) ZPath(org.apache.zookeeper.server.jersey.jaxb.ZPath) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Example 2 with ZPath

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();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) CreateMode(org.apache.zookeeper.CreateMode) ZPath(org.apache.zookeeper.server.jersey.jaxb.ZPath) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) URI(java.net.URI) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 3 with ZPath

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();
}
Also used : JSONWithPadding(com.sun.jersey.api.json.JSONWithPadding) WebApplicationException(javax.ws.rs.WebApplicationException) CreateMode(org.apache.zookeeper.CreateMode) ZPath(org.apache.zookeeper.server.jersey.jaxb.ZPath) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) URI(java.net.URI) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 4 with ZPath

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));
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Stat(org.apache.zookeeper.data.Stat) ZPath(org.apache.zookeeper.server.jersey.jaxb.ZPath) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Aggregations

ZPath (org.apache.zookeeper.server.jersey.jaxb.ZPath)4 ClientResponse (com.sun.jersey.api.client.ClientResponse)2 WebResource (com.sun.jersey.api.client.WebResource)2 Builder (com.sun.jersey.api.client.WebResource.Builder)2 URI (java.net.URI)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 CreateMode (org.apache.zookeeper.CreateMode)2 Stat (org.apache.zookeeper.data.Stat)2 ZError (org.apache.zookeeper.server.jersey.jaxb.ZError)2 Test (org.junit.Test)2 JSONWithPadding (com.sun.jersey.api.json.JSONWithPadding)1