use of org.apache.ignite.internal.processors.rest.GridRestResponse in project ignite by apache.
the class GridTopologyCommandHandler method handleAsync.
/**
* {@inheritDoc}
*/
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
assert req instanceof GridRestTopologyRequest : "Invalid command for topology handler: " + req;
assert SUPPORTED_COMMANDS.contains(req.command());
if (log.isDebugEnabled())
log.debug("Handling topology REST request: " + req);
GridRestTopologyRequest req0 = (GridRestTopologyRequest) req;
GridRestResponse res = new GridRestResponse();
boolean mtr = req0.includeMetrics();
boolean attr = req0.includeAttributes();
boolean caches = req0.includeCaches();
switch(req.command()) {
case TOPOLOGY:
{
Collection<ClusterNode> allNodes = F.concat(false, ctx.discovery().allNodes(), ctx.discovery().daemonNodes());
Collection<GridClientNodeBean> top = new ArrayList<>(allNodes.size());
for (ClusterNode node : allNodes) top.add(createNodeBean(node, mtr, attr, caches));
res.setResponse(top);
break;
}
case NODE:
{
UUID id = req0.nodeId();
final String ip = req0.nodeIp();
if (id == null && ip == null)
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request (either id or ip should be specified)."));
ClusterNode node;
if (id != null) {
// Always refresh topology so client see most up-to-date view.
ctx.discovery().alive(id);
node = ctx.grid().cluster().node(id);
if (ip != null && node != null && !containsIp(node.addresses(), ip))
node = null;
} else
node = F.find(ctx.discovery().allNodes(), null, new P1<ClusterNode>() {
@Override
public boolean apply(ClusterNode n) {
return containsIp(n.addresses(), ip);
}
});
if (node != null)
res.setResponse(createNodeBean(node, mtr, attr, caches));
else
res.setResponse(null);
break;
}
default:
assert false : "Invalid command for topology handler: " + req;
}
if (log.isDebugEnabled())
log.debug("Handled topology REST request [res=" + res + ", req=" + req + ']');
return new GridFinishedFuture<>(res);
}
use of org.apache.ignite.internal.processors.rest.GridRestResponse in project ignite by apache.
the class GridTcpRestNioListener method onMessage.
/**
* {@inheritDoc}
*/
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
if (msg instanceof GridMemcachedMessage)
memcachedLsnr.onMessage(ses, (GridMemcachedMessage) msg);
else if (msg instanceof GridRedisMessage)
redisLsnr.onMessage(ses, (GridRedisMessage) msg);
else if (msg instanceof GridClientPingPacket)
ses.send(msg);
else if (msg instanceof GridClientHandshakeRequest) {
GridClientHandshakeRequest hs = (GridClientHandshakeRequest) msg;
short ver = hs.version();
if (!SUPP_VERS.contains(ver)) {
U.error(log, "Client protocol version is not supported [ses=" + ses + ", ver=" + ver + ", supported=" + SUPP_VERS + ']');
onSessionClosed(ses);
} else {
byte marshId = hs.marshallerId();
if (marshMapLatch.getCount() > 0) {
try {
U.await(marshMapLatch);
} catch (IgniteInterruptedCheckedException e) {
U.error(log, "Marshaller is not initialized.", e);
onSessionClosed(ses);
return;
}
}
GridClientMarshaller marsh = marshMap.get(marshId);
if (marsh == null) {
U.error(log, "Client marshaller ID is invalid. Note that .NET and C++ clients " + "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']');
onSessionClosed(ses);
} else {
ses.addMeta(MARSHALLER.ordinal(), marsh);
ses.send(GridClientHandshakeResponse.OK);
}
}
} else {
final GridRestRequest req = createRestRequest(ses, msg);
if (req != null) {
IgniteInternalFuture<GridRestResponse> taskFut = hnd.handleAsync(req);
if (isInterruptible(msg))
addFutureToSession(ses, taskFut);
taskFut.listen(new CI1<IgniteInternalFuture<GridRestResponse>>() {
@Override
public void apply(IgniteInternalFuture<GridRestResponse> fut) {
removeFutureFromSession(ses, taskFut);
GridClientResponse res = new GridClientResponse();
res.requestId(msg.requestId());
res.clientId(msg.clientId());
try {
GridRestResponse restRes = fut.get();
res.sessionToken(restRes.sessionTokenBytes());
res.successStatus(restRes.getSuccessStatus());
res.errorMessage(restRes.getError());
Object o = restRes.getResponse();
// In case of metrics a little adjustment is needed.
if (o instanceof GridCacheRestMetrics)
o = ((GridCacheRestMetrics) o).map();
res.result(o);
} catch (IgniteCheckedException e) {
U.error(log, "Failed to process client request: " + msg, e);
res.successStatus(GridClientResponse.STATUS_FAILED);
res.errorMessage("Failed to process client request: " + e.getMessage());
}
GridNioFuture<?> sf = ses.send(res);
// Check if send failed.
sf.listen(new CI1<IgniteInternalFuture<?>>() {
@Override
public void apply(IgniteInternalFuture<?> fut) {
try {
fut.get();
} catch (IgniteCheckedException e) {
U.error(log, "Failed to process client request [ses=" + ses + ", msg=" + msg + ']', e);
}
}
});
}
});
} else
U.error(log, "Failed to process client request (unknown packet type) [ses=" + ses + ", msg=" + msg + ']');
}
}
use of org.apache.ignite.internal.processors.rest.GridRestResponse in project ignite by apache.
the class GridLogCommandHandlerTest method testHandleAsyncPathIsOutsideIgniteHome.
/**
* @throws Exception If failed.
*/
@Test
public void testHandleAsyncPathIsOutsideIgniteHome() throws Exception {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setIgniteHome(igniteHome);
GridTestKernalContext ctx = newContext(cfg);
GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
GridRestLogRequest req = new GridRestLogRequest();
req.to(5);
req.from(2);
req.path("/home/users/mytest.log");
IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
assertEquals("Request parameter 'path' must contain a path to valid log file.", resp.result().getError());
assertEquals(GridRestResponse.STATUS_FAILED, resp.result().getSuccessStatus());
assertNull(resp.result().getResponse());
}
use of org.apache.ignite.internal.processors.rest.GridRestResponse in project ignite by apache.
the class GridLogCommandHandlerTest method testHandleAsync.
/**
* @throws Exception If failed.
*/
@Test
public void testHandleAsync() throws Exception {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setIgniteHome(igniteHome);
GridTestKernalContext ctx = newContext(cfg);
GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
GridRestLogRequest req = new GridRestLogRequest();
req.to(5);
req.from(2);
req.path(igniteHome + "/work/log/" + "test.log");
IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
assertNull(resp.result().getError());
assertEquals(GridRestResponse.STATUS_SUCCESS, resp.result().getSuccessStatus());
assertNotNull(resp.result().getResponse());
}
use of org.apache.ignite.internal.processors.rest.GridRestResponse in project ignite by apache.
the class GridLogCommandHandlerTest method testHandleAsyncFromEqualTo.
/**
* @throws Exception If failed.
*/
@Test
public void testHandleAsyncFromEqualTo() throws Exception {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setIgniteHome(igniteHome);
GridTestKernalContext ctx = newContext(cfg);
GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
GridRestLogRequest req = new GridRestLogRequest();
req.to(5);
req.from(5);
req.path(igniteHome + "/work/log/" + "test.log");
IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
assertEquals("Request parameter 'from' must be less than 'to'.", resp.result().getError());
assertEquals(GridRestResponse.STATUS_FAILED, resp.result().getSuccessStatus());
assertNull(resp.result().getResponse());
}
Aggregations