GridLogCommandHandler extends GridRestCommandHandlerAdapter { @Override public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) { assert req != null; if (req.command() == LOG) { if (log.isDebugEnabled()) log.debug("Handling log REST request: " + req); GridRestLogRequest req0 = (GridRestLogRequest)req; if (req0.from() < -1 || req0.to() < -1) return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "One of the request parameters is invalid [from=" + req0.from() + ", to=" + req0.to() + ']')); int from; if (req0.from() != -1) { if (req0.to() == -1) return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'to' is not set.")); from = req0.from(); } else from = DEFAULT_FROM; int to; if (req0.to() != -1) { if (req0.from() == -1) return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'from' is not set.")); to = req0.to(); } else to = DEFAULT_TO; if (from >= to) return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'from' must be less than 'to'.")); File logFile; try { if (req0.path() != null) { if (log.fileName() != null) { if (!req0.path().equals(log.fileName())) { return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'path' must contain a path to valid log file.")); } else logFile = new File(req0.path()); } else if (req0.path().startsWith(ctx.config().getIgniteHome())) logFile = new File(req0.path()); else { return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'path' must contain a path to valid log file.")); } } else if (log.fileName() == null) logFile = new File(ctx.config().getIgniteHome() + "/work/log/ignite.log"); else logFile = new File(log.fileName()); } catch (InvalidPathException e) { return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Incorrect path to a log file [msg=" + e.getMessage() + ']')); } try { String content = readLog(from, to, logFile); return new GridFinishedFuture<>(new GridRestResponse(content)); } catch (IgniteCheckedException e) { return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage())); } } return new GridFinishedFuture<>(); } GridLogCommandHandler(GridKernalContext ctx); @Override Collection<GridRestCommand> supportedCommands(); @Override IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req);  }
@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()); }
@Test public void testHandleAsyncForNonExistingLines() throws Exception { IgniteConfiguration cfg = new IgniteConfiguration(); cfg.setIgniteHome(igniteHome); GridTestKernalContext ctx = newContext(cfg); GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx); GridRestLogRequest req = new GridRestLogRequest(); req.to(50); req.from(20); req.path(igniteHome + "/work/log/" + "test.log"); IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req); assertEquals("Request parameter 'from' and 'to' are for lines that do not exist in log file.", resp.result().getError()); assertEquals(GridRestResponse.STATUS_FAILED, resp.result().getSuccessStatus()); assertNull(resp.result().getResponse()); }
@Test public void testHandleAsyncFromAndToNotSet() throws Exception { IgniteConfiguration cfg = new IgniteConfiguration(); cfg.setIgniteHome(igniteHome); GridTestKernalContext ctx = newContext(cfg); GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx); GridRestLogRequest req = new GridRestLogRequest(); 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()); }
@Test public void testHandleAsyncPathNotSet() throws Exception { GridTestKernalContext ctx = newContext(); ctx.config().setIgniteHome(igniteHome); GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx); GridRestLogRequest req = new GridRestLogRequest(); req.to(5); req.from(2); IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req); assertNull(resp.result().getError()); assertEquals(GridRestResponse.STATUS_SUCCESS, resp.result().getSuccessStatus()); assertNotNull(resp.result().getResponse()); }
@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()); }
@Test public void testHandleAsyncFromGreaterThanTo() 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(7); 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()); }
@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()); }