LogViewEndpoint implements MvcEndpoint { @RequestMapping("/search") public void search(@RequestParam String term, HttpServletResponse response) throws IOException { Path folder = loggingPath(null); List<FileEntry> files = getFileProvider(folder).getFileEntries(folder); List<FileEntry> sortedFiles = sortFiles(files, SortBy.MODIFIED, false); response.setContentType(MediaType.TEXT_PLAIN_VALUE); ServletOutputStream outputStream = response.getOutputStream(); sortedFiles.stream() .filter(file -> file.getFileType().equals(FileType.FILE)) .forEach(file -> searchAndStreamFile(file, term, outputStream)); } 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 shouldSearchInFiles() throws Exception { String sep = System.lineSeparator(); createFile("A.log", "A-line1" + sep + "A-line2" + sep + "A-line3", now - 1); createFile("B.log", "B-line1" + sep + "B-line2" + sep + "B-line3", now); ByteArrayServletOutputStream outputStream = mockResponseOutputStream(); logViewEndpoint.search("line2", response); String output = new String(outputStream.toByteArray()); assertThat(output, containsString("[A.log] A-line2")); assertThat(output, containsString("[B.log] B-line2")); assertThat(output, not(containsString("line1"))); assertThat(output, not(containsString("line3"))); }