Search in sources :

Example 1 with ZError

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

the class ZNodeResource method deleteZNode.

@DELETE
@Produces({ MediaType.APPLICATION_JSON, "application/javascript", MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM })
public void deleteZNode(@PathParam("path") String path, @DefaultValue("-1") @QueryParam("version") String versionParam, @Context UriInfo ui) 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());
    }
    zk.delete(path, version);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 2 with ZError

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

the class ZNodeResource method setZNodeAsOctet.

@PUT
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public void setZNodeAsOctet(@PathParam("path") String path, @DefaultValue("-1") @QueryParam("version") String versionParam, @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;
    }
    zk.setData(path, data, version);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 3 with ZError

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

the class SessionsResource method createSession.

@POST
@Produces({ MediaType.APPLICATION_JSON, "application/javascript", MediaType.APPLICATION_XML })
public Response createSession(@QueryParam("op") String op, @DefaultValue("5") @QueryParam("expire") String expire, @Context UriInfo ui) {
    if (!op.equals("create")) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), "")).build());
    }
    int expireInSeconds;
    try {
        expireInSeconds = Integer.parseInt(expire);
    } catch (NumberFormatException e) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build());
    }
    String uuid = UUID.randomUUID().toString();
    while (ZooKeeperService.isConnected(contextPath, uuid)) {
        uuid = UUID.randomUUID().toString();
    }
    // establish the connection to the ZooKeeper cluster
    try {
        ZooKeeperService.getClient(contextPath, uuid, expireInSeconds);
    } catch (IOException e) {
        LOG.error("Failed while trying to create a new session", e);
        throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
    }
    URI uri = ui.getAbsolutePathBuilder().path(uuid).build();
    return Response.created(uri).entity(new JSONWithPadding(new ZSession(uuid, uri.toString()))).build();
}
Also used : JSONWithPadding(com.sun.jersey.api.json.JSONWithPadding) WebApplicationException(javax.ws.rs.WebApplicationException) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) IOException(java.io.IOException) URI(java.net.URI) ZSession(org.apache.zookeeper.server.jersey.jaxb.ZSession) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 4 with ZError

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

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

Aggregations

Produces (javax.ws.rs.Produces)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 ZError (org.apache.zookeeper.server.jersey.jaxb.ZError)6 Consumes (javax.ws.rs.Consumes)4 JSONWithPadding (com.sun.jersey.api.json.JSONWithPadding)3 URI (java.net.URI)3 POST (javax.ws.rs.POST)3 PUT (javax.ws.rs.PUT)2 CreateMode (org.apache.zookeeper.CreateMode)2 ZPath (org.apache.zookeeper.server.jersey.jaxb.ZPath)2 IOException (java.io.IOException)1 DELETE (javax.ws.rs.DELETE)1 Stat (org.apache.zookeeper.data.Stat)1 ZSession (org.apache.zookeeper.server.jersey.jaxb.ZSession)1 ZStat (org.apache.zookeeper.server.jersey.jaxb.ZStat)1