LogViewEndpoint implements MvcEndpoint { @RequestMapping("/") @ResponseBody public String list(Model model, @RequestParam(required = false, defaultValue = "FILENAME") SortBy sortBy, @RequestParam(required = false, defaultValue = "false") boolean desc, @RequestParam(required = false) String base) throws IOException, TemplateException { securityCheck(base); Path currentFolder = loggingPath(base); List<FileEntry> files = getFileProvider(currentFolder).getFileEntries(currentFolder); List<FileEntry> sortedFiles = sortFiles(files, sortBy, desc); model.addAttribute("sortBy", sortBy); model.addAttribute("desc", desc); model.addAttribute("files", sortedFiles); model.addAttribute("currentFolder", currentFolder.toAbsolutePath().toString()); model.addAttribute("base", base != null ? URLEncoder.encode(base, "UTF-8") : ""); model.addAttribute("parent", getParent(currentFolder)); model.addAttribute("stylesheets", stylesheets); return FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfig.getTemplate("logview.ftl"), model); } 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 shouldContainEmptyParentLinkInBaseFolder() throws Exception { logViewEndpoint.list(model, SortBy.FILENAME, false, null); assertThat(model.asMap().get("parent"), is("")); }
@Test public void shouldContainEmptyParentLinkInSubfolder() throws Exception { temporaryFolder.newFolder("subfolder"); logViewEndpoint.list(model, SortBy.FILENAME, false, "subfolder"); assertThat(model.asMap().get("parent"), is("")); }
@Test public void shouldContainEmptyParentLinkInNestedSubfolder() throws Exception { temporaryFolder.newFolder("subfolder"); temporaryFolder.newFolder("subfolder", "nested"); logViewEndpoint.list(model, SortBy.FILENAME, false, "subfolder/nested"); assertThat(model.asMap().get("parent"), is("/subfolder")); }
@Test public void shouldIncludeSubfolderEntry() throws Exception { temporaryFolder.newFolder("subfolder"); logViewEndpoint.list(model, SortBy.FILENAME, false, null); List<FileEntry> fileEntries = getFileEntries(); assertThat(fileEntries, hasSize(1)); FileEntry fileEntry = fileEntries.get(0); assertThat(fileEntry.getFileType(), is(FileType.DIRECTORY)); assertThat(fileEntry.getFilename(), is("subfolder")); }
@Test public void shouldListZipContent() throws Exception { createZipArchive("file.zip", "A.log", "content"); logViewEndpoint.list(model, SortBy.FILENAME, false, "file.zip"); List<FileEntry> fileEntries = getFileEntries(); assertThat(fileEntries, hasSize(1)); FileEntry fileEntry = fileEntries.get(0); assertThat(fileEntry.getFilename(), is("A.log")); }
@Test public void shouldListTarGzContent() throws Exception { createTarGzArchive("file.tar.gz", "A.log", "content"); logViewEndpoint.list(model, SortBy.FILENAME, false, "file.tar.gz"); List<FileEntry> fileEntries = getFileEntries(); assertThat(fileEntries, hasSize(1)); FileEntry fileEntry = fileEntries.get(0); assertThat(fileEntry.getFilename(), is("A.log")); }
@Test public void shouldReturnEmptyFileListForEmptyDirectory() throws Exception { logViewEndpoint.list(model, SortBy.FILENAME, false, null); assertThat(model.containsAttribute("files"), is(true)); assertThat(getFileEntries(), hasSize(0)); }
@Test public void shouldListSortedByFilename() throws Exception { createFile("B.log", "x", now); createFile("A.log", "x", now); createFile("C.log", "x", now); logViewEndpoint.list(model, SortBy.FILENAME, false, null); assertThat(getFileNames(), contains("A.log", "B.log", "C.log")); }
@Test public void shouldListReverseSortedByFilename() throws Exception { createFile("B.log", "x", now); createFile("A.log", "x", now); createFile("C.log", "x", now); logViewEndpoint.list(model, SortBy.FILENAME, true, null); assertThat(getFileNames(), contains("C.log", "B.log", "A.log")); }
@Test public void shouldListSortedBySize() throws Exception { createFile("A.log", "xx", now); createFile("B.log", "x", now); createFile("C.log", "xxx", now); logViewEndpoint.list(model, SortBy.SIZE, false, null); assertThat(getFileNames(), contains("B.log", "A.log", "C.log")); assertThat(getFileSizes(), contains(1L, 2L, 3L)); }
@Test public void shouldListSortedByDate() throws Exception { createFile("A.log", "x", now); createFile("B.log", "x", now - 10 * 60 * 1000); createFile("C.log", "x", now - 5 * 60 * 1000); logViewEndpoint.list(model, SortBy.MODIFIED, false, null); assertThat(getFileNames(), contains("B.log", "C.log", "A.log")); assertThat(getFilePrettyTimes(), contains("10 minutes ago", "5 minutes ago", "moments ago")); }
@Test public void shouldSetFileTypeForFile() throws Exception { createFile("A.log", "x", now); logViewEndpoint.list(model, SortBy.FILENAME, false, null); assertThat(getFileEntries().get(0).getFileType(), is(FileType.FILE)); }
@Test public void shouldSetFileTypeForArchive() throws Exception { createFile("A.log.tar.gz", "x", now); logViewEndpoint.list(model, SortBy.FILENAME, false, null); assertThat(getFileEntries().get(0).getFileType(), is(FileType.ARCHIVE)); }