AttachmentController { @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{branchName:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}/" + "{pagePath:" + DocumentrConstants.PAGE_PATH_URL_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasPagePermission(#projectName, #branchName, #pagePath, EDIT_PAGE)") public String saveAttachment(@PathVariable String projectName, @PathVariable String branchName, @PathVariable String pagePath, @RequestParam MultipartFile file, Authentication authentication) throws IOException { saveAttachmentInternal(projectName, branchName, pagePath, file, authentication); return "redirect:/attachment/list/" + projectName + "/" + branchName + "/" + pagePath; }  @RequestMapping(value="/list/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{branchName:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}/" + "{pagePath:" + DocumentrConstants.PAGE_PATH_URL_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("isAuthenticated() and hasPagePermission(#projectName, #branchName, #pagePath, VIEW)") String getAttachments(@PathVariable String projectName, @PathVariable String branchName,
			@PathVariable String pagePath, Model model); @RequestMapping(value="/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{branchName:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}/" + "{pagePath:" + DocumentrConstants.PAGE_PATH_URL_PATTERN + "}/" + "{name:.*}", method={ RequestMethod.GET, RequestMethod.HEAD }) @PreAuthorize("hasPagePermission(#projectName, #branchName, #pagePath, VIEW)") ResponseEntity<byte[]> getAttachment(@PathVariable String projectName, @PathVariable String branchName,
			@PathVariable String pagePath, @PathVariable String name, @RequestParam(required=false) boolean download,
			HttpServletRequest request); @RequestMapping(value="/create/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{branchName:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}/" + "{pagePath:" + DocumentrConstants.PAGE_PATH_URL_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasPagePermission(#projectName, #branchName, #pagePath, EDIT_PAGE)") String createAttachment(@PathVariable String projectName, @PathVariable String branchName,
			@PathVariable String pagePath, Model model); @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{branchName:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}/" + "{pagePath:" + DocumentrConstants.PAGE_PATH_URL_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasPagePermission(#projectName, #branchName, #pagePath, EDIT_PAGE)") String saveAttachment(@PathVariable String projectName, @PathVariable String branchName,
			@PathVariable String pagePath, @RequestParam MultipartFile file, Authentication authentication); @RequestMapping(value="/saveViaJson/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{branchName:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}/" + "{pagePath:" + DocumentrConstants.PAGE_PATH_URL_PATTERN + "}/json", method=RequestMethod.POST) @PreAuthorize("hasPagePermission(#projectName, #branchName, #pagePath, EDIT_PAGE)") @ResponseBody Map<String, Object> saveAttachmentViaJson(@PathVariable String projectName, @PathVariable String branchName,
			@PathVariable String pagePath, @RequestParam MultipartFile file, Authentication authentication); @RequestMapping(value="/delete/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{branchName:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}/" + "{pagePath:" + DocumentrConstants.PAGE_PATH_URL_PATTERN + "}/" + "{name:.*}", method=RequestMethod.GET) @PreAuthorize("hasPagePermission(#projectName, #branchName, #pagePath, EDIT_PAGE)") String deleteAttachment(@PathVariable String projectName, @PathVariable String branchName,
			@PathVariable String pagePath, @PathVariable String name, Authentication authentication);  }
@Test public void saveAttachment() throws IOException { byte[] data = { 1, 2, 3 }; InputStream inputStream = new ByteArrayInputStream(data); when(multipartFile.getInputStream()).thenReturn(inputStream); when(multipartFile.getOriginalFilename()).thenReturn("test.png"); String view = attachmentController.saveAttachment(PROJECT, BRANCH, PAGE_PATH_URL, multipartFile, authentication); assertEquals("/attachment/list/" + PROJECT + "/" + BRANCH + "/" + PAGE_PATH_URL, removeViewPrefix(view)); assertRedirect(view); Page attachment = Page.fromData(data, "image/png"); verify(pageStore).saveAttachment(PROJECT, BRANCH, PAGE_PATH, "test.png", attachment, USER); }
@Test public void saveAttachmentMustUseDefaultContentTypeIfUnknown() throws IOException { byte[] data = { 1, 2, 3 }; InputStream inputStream = new ByteArrayInputStream(data); when(multipartFile.getInputStream()).thenReturn(inputStream); when(multipartFile.getOriginalFilename()).thenReturn("test.dat"); String view = attachmentController.saveAttachment(PROJECT, BRANCH, PAGE_PATH_URL, multipartFile, authentication); assertEquals("/attachment/list/" + PROJECT + "/" + BRANCH + "/" + PAGE_PATH_URL, removeViewPrefix(view)); assertRedirect(view); Page attachment = Page.fromData(data, DocumentrConstants.DEFAULT_MIME_TYPE); verify(pageStore).saveAttachment(PROJECT, BRANCH, PAGE_PATH, "test.dat", attachment, USER); }