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