Search in sources :

Example 1 with JSONWithPadding

use of com.sun.jersey.api.json.JSONWithPadding 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 JSONWithPadding

use of com.sun.jersey.api.json.JSONWithPadding 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 3 with JSONWithPadding

use of com.sun.jersey.api.json.JSONWithPadding 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();
}
Also used : JSONWithPadding(com.sun.jersey.api.json.JSONWithPadding) Stat(org.apache.zookeeper.data.Stat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) WebApplicationException(javax.ws.rs.WebApplicationException) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 4 with JSONWithPadding

use of com.sun.jersey.api.json.JSONWithPadding 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

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