LoggingManagementController extends BaseController { @ApiOperation(value = "Used to download log data.") @ApiResponses(value = { @ApiResponse(code = 200, message = "The log file was retrieved successfully."), @ApiResponse(code = 400, message = "Could not download log data.") }) @GetMapping(value = "/download/{path:.+}", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity downloadLog(@PathVariable("path") String path, @RequestHeader(HttpHeaders.ACCEPT) String accept) { try { Path logsBaseDir = Paths.get(propertiesBooter.getLogsDirectory()); Path requestedLogPath = Paths.get(logsBaseDir.toString(), path); logger.debug(String.format("Requested downloading log from path: [%s] resolved to [%s]", path, requestedLogPath)); if (!Files.exists(requestedLogPath)) { return getNotFoundResponseEntity("Requested path does not exist!", accept); } if (Files.isDirectory(requestedLogPath)) { return getBadRequestResponseEntity("Requested path is a directory!", accept); } return getStreamToResponseEntity(logFileInputStream(requestedLogPath), FilenameUtils.getName(path)); } catch (IOException e) { String message = "Could not download log data."; return getExceptionResponseEntity(BAD_REQUEST, message, e, accept); } }  DirectoryListingService getDirectoryListingService(); @ApiOperation(value = "Used to download log data.") @ApiResponses(value = { @ApiResponse(code = 200, message = "The log file was retrieved successfully."), @ApiResponse(code = 400, message = "Could not download log data.") }) @GetMapping(value = "/download/{path:.+}", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE, // forces browser to actually download the file MediaType.TEXT_PLAIN_VALUE, // plain text / json upon errors MediaType.APPLICATION_JSON_VALUE }) ResponseEntity downloadLog(@PathVariable("path") String path,
                                      @RequestHeader(HttpHeaders.ACCEPT) String accept); @ApiOperation(value = "Used to get logs directory.") @ApiResponses(value = { @ApiResponse(code = 200, message = "The logs directory was retrieved successfully."), @ApiResponse(code = 500, message = "Server error.") }) @GetMapping(value = { "/browse/{path:.+}" }, produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.TEXT_HTML_VALUE, MediaType.APPLICATION_JSON_VALUE }) Object browseLogsDirectory(@PathVariable("path") Optional<String> path,
                                      ModelMap model,
                                      HttpServletRequest request,
                                      @RequestHeader(value = HttpHeaders.ACCEPT,
                                                     required = false) String acceptHeader); @ApiOperation(value = "Used to stream logging file.") @GetMapping(value = "/stream", produces = { org.carlspring.strongbox.net.MediaType.TEXT_EVENT_STREAM_UTF8_VALUE, org.carlspring.strongbox.net.MediaType.TEXT_PLAIN_UTF8_VALUE }) SseEmitter logFileStream(); final static String ROOT_CONTEXT; }
@Test @WithMockUser(authorities = { "ADMIN" }) public void testDownloadLog() throws Exception { String testLogName = "strongbox-test.log"; Path file = Paths.get(propertiesBooter.getLogsDirectory(), testLogName); Files.createFile(file) .toFile() .deleteOnExit(); List<String> lines = Arrays.asList("Strongbox", "is", "awesome!"); Files.write(file, lines, StandardCharsets.UTF_8); assertThat(file.toFile().length()).isGreaterThan(0); String url = getContextBaseUrl() + "/download/" + testLogName; ResponseBodyExtractionOptions body = mockMvc.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE) .when() .get(url) .peek() .then() .statusCode(HttpStatus.OK.value()) .extract() .body(); assertThat(body.asByteArray().length).isEqualTo(file.toFile().length()); }