LogViewEndpoint implements MvcEndpoint { @RequestMapping("/view") public void view(@RequestParam String filename, @RequestParam(required = false) String base, @RequestParam(required = false) Integer tailLines, HttpServletResponse response) throws IOException { securityCheck(filename); response.setContentType(MediaType.TEXT_PLAIN_VALUE); Path path = loggingPath(base); FileProvider fileProvider = getFileProvider(path); if (tailLines != null) { fileProvider.tailContent(path, filename, response.getOutputStream(), tailLines); } else { fileProvider.streamContent(path, filename, response.getOutputStream()); } } LogViewEndpoint(String loggingPath, List<String> stylesheets); @RequestMapping void redirect(HttpServletResponse response); @RequestMapping("/") @ResponseBody String list(Model model, // TODO model should no longer be injected
                       @RequestParam(required = false, defaultValue = "FILENAME") SortBy sortBy,
                       @RequestParam(required = false, defaultValue = "false") boolean desc,
                       @RequestParam(required = false) String base); @RequestMapping("/view") void view(@RequestParam String filename,
                     @RequestParam(required = false) String base,
                     @RequestParam(required = false) Integer tailLines,
                     HttpServletResponse response); @RequestMapping("/search") void search(@RequestParam String term, HttpServletResponse response); @Override String getPath(); @Override boolean isSensitive(); @Override Class<? extends Endpoint> getEndpointType();  }
@Test public void shouldViewZipFileContent() throws Exception { createZipArchive("file.zip", "A.log", "content"); ByteArrayServletOutputStream outputStream = mockResponseOutputStream(); logViewEndpoint.view("A.log", "file.zip", null, response); assertThat(new String(outputStream.toByteArray()), is("content")); }
@Test(expected = UnsupportedOperationException.class) public void shouldThrowExceptionWhenCallingTailForZip() throws Exception { createZipArchive("file.zip", "A.log", "content"); logViewEndpoint.view("A.log", "file.zip", 1, response); }
@Test public void shouldViewTarGzFileContent() throws Exception { createTarGzArchive("file.tar.gz", "A.log", "content"); ByteArrayServletOutputStream outputStream = mockResponseOutputStream(); logViewEndpoint.view("A.log", "file.tar.gz", null, response); assertThat(new String(outputStream.toByteArray()), is("content")); }
@Test(expected = UnsupportedOperationException.class) public void shouldThrowExceptionWhenCallingTailForTarGz() throws Exception { createTarGzArchive("file.tar.gz", "A.log", "content"); logViewEndpoint.view("A.log", "file.tar.gz", 1, response); }
@Test public void shouldNotAllowToListFileOutsideRoot() throws Exception { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(containsString("this String argument must not contain the substring [..]")); logViewEndpoint.view("../somefile", null, null, null); }
@Test public void shouldViewFile() throws Exception { createFile("file.log", "abc", now); ByteArrayServletOutputStream outputStream = mockResponseOutputStream(); logViewEndpoint.view("file.log", null, null, response); assertThat(new String(outputStream.toByteArray()), is("abc")); }
@Test public void shouldTailViewOnlyLastLine() throws Exception { createFile("file.log", "line1" + System.lineSeparator() + "line2" + System.lineSeparator(), now); ByteArrayServletOutputStream outputStream = mockResponseOutputStream(); logViewEndpoint.view("file.log", null, 1, response); assertThat(new String(outputStream.toByteArray()), not(containsString("line1"))); assertThat(new String(outputStream.toByteArray()), containsString("line2")); }