FileEntryApiController { @GetMapping("/download/**") public void download(User user, @RemainedPath String path, HttpServletResponse response) { FileEntry fileEntry = fileEntryService.getOne(user, path); if (fileEntry == null) { LOG.error("{} requested to download not existing file entity {}", user.getUserId(), path); throw new NGrinderRuntimeException("download requested file not exists in file entity " + path); } response.reset(); response.setContentType("application/octet-stream; charset=UTF-8"); ContentDisposition contentDisposition = ContentDisposition.builder("attachment") .filename(fileEntry.getFileName(), StandardCharsets.UTF_8) .build(); response.setHeader("Content-Disposition", contentDisposition.toString()); response.addHeader("Content-Length", "" + fileEntry.getFileSize()); try (ByteArrayInputStream fis = new ByteArrayInputStream(fileEntry.getContentBytes()); OutputStream os = new BufferedOutputStream(response.getOutputStream())) { IOUtils.copy(fis, os); } catch (IOException e) { throw processException("error while download file", e); } }  @GetMapping("/handlers") List<ScriptHandler> getHandlers(); @GetMapping("/search") List<FileEntry> search(User user, @RequestParam String query); @PostMapping("/save/**") void save(User user, @RequestBody FileSaveParams fileSaveParams); @PostMapping(value = "/new/**", params = "type=script") Map<String, Object> createScript(User user,
											@RemainedPath String path,
											@RequestBody ScriptCreationParams scriptCreationParams); @PostMapping(value = "/new/**", params = "type=folder") void addFolder(User user,
							@RemainedPath String path,
							@RequestBody String folderName); @GetMapping("/detail/**") Map<String, Object> getOne(User user,
									  @RemainedPath String path,
									  @RequestParam(value = "r", required = false) Long revision); @DeleteMapping("/delete") void delete(User user, @RequestBody List<String> paths); @PostMapping("/upload/**") void uploadFile(User user,
							 @RemainedPath String path,
							 @RequestParam String description,
							 @RequestParam("uploadFile") MultipartFile file); @PostMapping({"/", ""}) void create(User user, FileEntry fileEntry); @PostMapping(value = "/**", params = "action=upload") void uploadAPI(User user,
							   @RemainedPath String path,
							   @RequestParam String description,
							   @RequestParam("uploadFile") MultipartFile file); @GetMapping(value = "/**", params = "action=view") FileEntry viewOne(User user, @RemainedPath String path); @GetMapping(value = {"/**", "/", ""}, params = "action=all") List<FileEntry> getAll(User user); @GetMapping({"/**", "/", ""}) List<FileEntry> getAll(User user, @RemainedPath String path); @DeleteMapping("/**") void deleteOne(User user, @RemainedPath String path); @PostMapping("/validate") String validate(User user, @RequestBody ScriptValidationParams scriptValidationParams); @GetMapping("/download/**") void download(User user, @RemainedPath String path, HttpServletResponse response); @GetMapping("/github-config") Set<GitHubConfig> getGitHubConfig(User user); @PostMapping("/github-config") void createGitHubConfig(User user); @GetMapping("/github") Map<String, List<GHTreeEntry>> getGitHubScripts(User user, boolean refresh); @PostMapping("/github/validate") void validateGithubConfig(@RequestBody FileEntry fileEntry);  }
@Test public void testDownload() { String path = "download-path"; String fileName = "download_file.py"; fileEntryController.addFolder(getTestUser(), "", path); Map<String, Object> responseMap = fileEntryController.createScript(getTestUser(), path, new ScriptCreationParams(fileName, "test.com", null, "jython", false)); FileEntry script = (FileEntry) responseMap.get("file"); script.setContent(script.getContent() + "#test comment"); fileEntryController.save(getTestUser(), new FileSaveParams(script, null, "", false)); fileEntryController.createScript(getTestUser(), path, new ScriptCreationParams(fileName, "", null, "", false)); MockHttpServletResponse response = new MockHttpServletResponse(); path = path + "/" + fileName; fileEntryController.download(getTestUser(), path, response); }