@Test public void testGetLogfileTailWithTailParamSetToZero() throws Exception { final int tailLineCount = 0; given(botLogfileService.getLogfile(MAX_LOGFILE_LINES)) .willReturn(LOGFILE_LINE_2 + System.lineSeparator() + LOGFILE_LINE_3); mockMvc .perform( get(LOGFILE_ENDPOINT_URI + "?tail=" + tailLineCount) .header( "Authorization", "Bearer " + getJwt(VALID_USER_NAME, VALID_USER_PASSWORD))) .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$").value(LOGFILE_LINE_2 + System.lineSeparator() + LOGFILE_LINE_3)); verify(botLogfileService, times(1)).getLogfile(MAX_LOGFILE_LINES); }
@PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_RESOURCE_PATH) public 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) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - getLogfile() - caller: " + principal.getName() + ", head=" + head + ", tail=" + tail); String logfile; final int maxLogfileLineCount = restApiConfig.getMaxLogfileLines(); try { if (head != null && head > 0) { if (head > maxLogfileLineCount) { LOG.warn( () -> "Requested head line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileHead(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileHead(head); } } else if (tail != null && tail > 0) { if (tail > maxLogfileLineCount) { LOG.warn( () -> "Requested tail line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileTail(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileTail(tail); } } else { logfile = botLogfileService.getLogfile(restApiConfig.getMaxLogfileLines()); } return new ResponseEntity<>(logfile, null, HttpStatus.OK); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }
BotLogfileController { @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_RESOURCE_PATH) public 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) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - getLogfile() - caller: " + principal.getName() + ", head=" + head + ", tail=" + tail); String logfile; final int maxLogfileLineCount = restApiConfig.getMaxLogfileLines(); try { if (head != null && head > 0) { if (head > maxLogfileLineCount) { LOG.warn( () -> "Requested head line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileHead(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileHead(head); } } else if (tail != null && tail > 0) { if (tail > maxLogfileLineCount) { LOG.warn( () -> "Requested tail line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileTail(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileTail(tail); } } else { logfile = botLogfileService.getLogfile(restApiConfig.getMaxLogfileLines()); } return new ResponseEntity<>(logfile, null, HttpStatus.OK); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } }
BotLogfileController { @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_RESOURCE_PATH) public 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) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - getLogfile() - caller: " + principal.getName() + ", head=" + head + ", tail=" + tail); String logfile; final int maxLogfileLineCount = restApiConfig.getMaxLogfileLines(); try { if (head != null && head > 0) { if (head > maxLogfileLineCount) { LOG.warn( () -> "Requested head line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileHead(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileHead(head); } } else if (tail != null && tail > 0) { if (tail > maxLogfileLineCount) { LOG.warn( () -> "Requested tail line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileTail(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileTail(tail); } } else { logfile = botLogfileService.getLogfile(restApiConfig.getMaxLogfileLines()); } return new ResponseEntity<>(logfile, null, HttpStatus.OK); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @Autowired  BotLogfileController(RestApiConfig restApiConfig, BotLogfileService botLogfileService); }
BotLogfileController { @PreAuthorize("hasRole('USER')") @GetMapping(value = LOGFILE_RESOURCE_PATH) public 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) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - getLogfile() - caller: " + principal.getName() + ", head=" + head + ", tail=" + tail); String logfile; final int maxLogfileLineCount = restApiConfig.getMaxLogfileLines(); try { if (head != null && head > 0) { if (head > maxLogfileLineCount) { LOG.warn( () -> "Requested head line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileHead(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileHead(head); } } else if (tail != null && tail > 0) { if (tail > maxLogfileLineCount) { LOG.warn( () -> "Requested tail line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileTail(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileTail(tail); } } else { logfile = botLogfileService.getLogfile(restApiConfig.getMaxLogfileLines()); } return new ResponseEntity<>(logfile, null, HttpStatus.OK); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @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_RESOURCE_PATH) public 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) { LOG.info( () -> "GET " + LOGFILE_RESOURCE_PATH + " - getLogfile() - caller: " + principal.getName() + ", head=" + head + ", tail=" + tail); String logfile; final int maxLogfileLineCount = restApiConfig.getMaxLogfileLines(); try { if (head != null && head > 0) { if (head > maxLogfileLineCount) { LOG.warn( () -> "Requested head line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileHead(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileHead(head); } } else if (tail != null && tail > 0) { if (tail > maxLogfileLineCount) { LOG.warn( () -> "Requested tail line count exceeds max line count. Using max line count: " + maxLogfileLineCount); logfile = botLogfileService.getLogfileTail(maxLogfileLineCount); } else { logfile = botLogfileService.getLogfileTail(tail); } } else { logfile = botLogfileService.getLogfile(restApiConfig.getMaxLogfileLines()); } return new ResponseEntity<>(logfile, null, HttpStatus.OK); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @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);  }