@Test public void testDownloadLogfile() throws Exception { final Resource resource = new ByteArrayResource(LOGFILE.getBytes(Charset.forName("UTF-8"))); given(botLogfileService.getLogfileAsResource(MAX_LOGFILE_DOWNLOAD_SIZE)).willReturn(resource); mockMvc .perform( get(LOGFILE_DOWNLOAD_URI) .header( "Authorization", "Bearer " + getJwt(VALID_USER_NAME, VALID_USER_PASSWORD))) .andDo(print()) .andExpect(status().isOk()) .andExpect(header().string("Content-Type", "application/octet-stream")) .andExpect(jsonPath("$").value(LOGFILE)); verify(botLogfileService, times(1)).getLogfileAsResource(MAX_LOGFILE_DOWNLOAD_SIZE); }
@PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_DOWNLOAD_RESOURCE_PATH) public ResponseEntity<Resource> downloadLogfile( @ApiIgnore Principal principal, HttpServletRequest request) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - downloadLogfile() - caller: " + principal.getName()); Resource logfile; try { logfile = botLogfileService.getLogfileAsResource(restApiConfig.getLogfileDownloadSize()); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } String contentType = null; try { contentType = request.getServletContext().getMimeType(logfile.getFile().getAbsolutePath()); } catch (IOException ex) { LOG.info(() -> "Could not determine file type."); } if (contentType == null) { contentType = "application/octet-stream"; } return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + logfile.getFilename() + "\"") .body(logfile); }
BotLogfileController { @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_DOWNLOAD_RESOURCE_PATH) public ResponseEntity<Resource> downloadLogfile( @ApiIgnore Principal principal, HttpServletRequest request) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - downloadLogfile() - caller: " + principal.getName()); Resource logfile; try { logfile = botLogfileService.getLogfileAsResource(restApiConfig.getLogfileDownloadSize()); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } String contentType = null; try { contentType = request.getServletContext().getMimeType(logfile.getFile().getAbsolutePath()); } catch (IOException ex) { LOG.info(() -> "Could not determine file type."); } if (contentType == null) { contentType = "application/octet-stream"; } return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + logfile.getFilename() + "\"") .body(logfile); } }
BotLogfileController { @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_DOWNLOAD_RESOURCE_PATH) public ResponseEntity<Resource> downloadLogfile( @ApiIgnore Principal principal, HttpServletRequest request) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - downloadLogfile() - caller: " + principal.getName()); Resource logfile; try { logfile = botLogfileService.getLogfileAsResource(restApiConfig.getLogfileDownloadSize()); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } String contentType = null; try { contentType = request.getServletContext().getMimeType(logfile.getFile().getAbsolutePath()); } catch (IOException ex) { LOG.info(() -> "Could not determine file type."); } if (contentType == null) { contentType = "application/octet-stream"; } return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + logfile.getFilename() + "\"") .body(logfile); } @Autowired  BotLogfileController(RestApiConfig restApiConfig, BotLogfileService botLogfileService); }
BotLogfileController { @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_DOWNLOAD_RESOURCE_PATH) public ResponseEntity<Resource> downloadLogfile( @ApiIgnore Principal principal, HttpServletRequest request) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - downloadLogfile() - caller: " + principal.getName()); Resource logfile; try { logfile = botLogfileService.getLogfileAsResource(restApiConfig.getLogfileDownloadSize()); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } String contentType = null; try { contentType = request.getServletContext().getMimeType(logfile.getFile().getAbsolutePath()); } catch (IOException ex) { LOG.info(() -> "Could not determine file type."); } if (contentType == null) { contentType = "application/octet-stream"; } return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + logfile.getFilename() + "\"") .body(logfile); } @Autowired  BotLogfileController(RestApiConfig restApiConfig, BotLogfileService botLogfileService); @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_DOWNLOAD_RESOURCE_PATH) ResponseEntity<Resource> downloadLogfile(
      @ApiIgnore Principal principal, HttpServletRequest request); @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_RESOURCE_PATH) ResponseEntity<String> getLogfile(
      @ApiIgnore Principal principal,
      @ApiParam(value = "Number of lines to fetch from head of file.", example = "100")
          @RequestParam(required = false)
          Integer head,
      @ApiParam(value = "Number of lines to fetch from tail of file.", example = "100")
          @RequestParam(required = false)
          Integer tail); }
BotLogfileController { @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_DOWNLOAD_RESOURCE_PATH) public ResponseEntity<Resource> downloadLogfile( @ApiIgnore Principal principal, HttpServletRequest request) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - downloadLogfile() - caller: " + principal.getName()); Resource logfile; try { logfile = botLogfileService.getLogfileAsResource(restApiConfig.getLogfileDownloadSize()); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } String contentType = null; try { contentType = request.getServletContext().getMimeType(logfile.getFile().getAbsolutePath()); } catch (IOException ex) { LOG.info(() -> "Could not determine file type."); } if (contentType == null) { contentType = "application/octet-stream"; } return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + logfile.getFilename() + "\"") .body(logfile); } @Autowired  BotLogfileController(RestApiConfig restApiConfig, BotLogfileService botLogfileService); @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_DOWNLOAD_RESOURCE_PATH) ResponseEntity<Resource> downloadLogfile(
      @ApiIgnore Principal principal, HttpServletRequest request); @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_RESOURCE_PATH) ResponseEntity<String> getLogfile(
      @ApiIgnore Principal principal,
      @ApiParam(value = "Number of lines to fetch from head of file.", example = "100")
          @RequestParam(required = false)
          Integer head,
      @ApiParam(value = "Number of lines to fetch from tail of file.", example = "100")
          @RequestParam(required = false)
          Integer tail);  }