@Test public void getAttachmentAsDownload() throws IOException { when(session.getAttribute("authenticationCreationTime")).thenReturn(System.currentTimeMillis()); when(request.getDateHeader(anyString())).thenReturn(-1L); when(request.getSession()).thenReturn(session); getAttachment(true, request); }
@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)") public ResponseEntity<byte[]> getAttachment(@PathVariable String projectName, @PathVariable String branchName, @PathVariable String pagePath, @PathVariable String name, @RequestParam(required=false) boolean download, HttpServletRequest request) throws IOException { try { pagePath = Util.toRealPagePath(pagePath); PageMetadata metadata = pageStore.getAttachmentMetadata(projectName, branchName, pagePath, name); HttpHeaders headers = new HttpHeaders(); long lastEdited = metadata.getLastEdited().getTime(); long authenticationCreated = AuthenticationUtil.getAuthenticationCreationTime(request.getSession()); long lastModified = Math.max(lastEdited, authenticationCreated); if (!download) { long projectEditTime = PageUtil.getProjectEditTime(projectName); if (projectEditTime >= 0) { lastModified = Math.max(lastModified, projectEditTime); } long modifiedSince = request.getDateHeader("If-Modified-Since"); if ((modifiedSince >= 0) && (lastModified <= modifiedSince)) { return new ResponseEntity<byte[]>(headers, HttpStatus.NOT_MODIFIED); } } headers.setLastModified(lastModified); headers.setExpires(0); headers.setCacheControl("must-revalidate, private"); if (download) { headers.set("Content-Disposition", "attachment; filename=\"" + name.replace('"', '_') + "\""); } Page attachment = pageStore.getAttachment(projectName, branchName, pagePath, name); headers.setContentType(MediaType.parseMediaType(attachment.getContentType())); return new ResponseEntity<byte[]>(attachment.getData().getData(), headers, HttpStatus.OK); } catch (PageNotFoundException e) { return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND); } }
AttachmentController { @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)") public ResponseEntity<byte[]> getAttachment(@PathVariable String projectName, @PathVariable String branchName, @PathVariable String pagePath, @PathVariable String name, @RequestParam(required=false) boolean download, HttpServletRequest request) throws IOException { try { pagePath = Util.toRealPagePath(pagePath); PageMetadata metadata = pageStore.getAttachmentMetadata(projectName, branchName, pagePath, name); HttpHeaders headers = new HttpHeaders(); long lastEdited = metadata.getLastEdited().getTime(); long authenticationCreated = AuthenticationUtil.getAuthenticationCreationTime(request.getSession()); long lastModified = Math.max(lastEdited, authenticationCreated); if (!download) { long projectEditTime = PageUtil.getProjectEditTime(projectName); if (projectEditTime >= 0) { lastModified = Math.max(lastModified, projectEditTime); } long modifiedSince = request.getDateHeader("If-Modified-Since"); if ((modifiedSince >= 0) && (lastModified <= modifiedSince)) { return new ResponseEntity<byte[]>(headers, HttpStatus.NOT_MODIFIED); } } headers.setLastModified(lastModified); headers.setExpires(0); headers.setCacheControl("must-revalidate, private"); if (download) { headers.set("Content-Disposition", "attachment; filename=\"" + name.replace('"', '_') + "\""); } Page attachment = pageStore.getAttachment(projectName, branchName, pagePath, name); headers.setContentType(MediaType.parseMediaType(attachment.getContentType())); return new ResponseEntity<byte[]>(attachment.getData().getData(), headers, HttpStatus.OK); } catch (PageNotFoundException e) { return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND); } } }
AttachmentController { @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)") public ResponseEntity<byte[]> getAttachment(@PathVariable String projectName, @PathVariable String branchName, @PathVariable String pagePath, @PathVariable String name, @RequestParam(required=false) boolean download, HttpServletRequest request) throws IOException { try { pagePath = Util.toRealPagePath(pagePath); PageMetadata metadata = pageStore.getAttachmentMetadata(projectName, branchName, pagePath, name); HttpHeaders headers = new HttpHeaders(); long lastEdited = metadata.getLastEdited().getTime(); long authenticationCreated = AuthenticationUtil.getAuthenticationCreationTime(request.getSession()); long lastModified = Math.max(lastEdited, authenticationCreated); if (!download) { long projectEditTime = PageUtil.getProjectEditTime(projectName); if (projectEditTime >= 0) { lastModified = Math.max(lastModified, projectEditTime); } long modifiedSince = request.getDateHeader("If-Modified-Since"); if ((modifiedSince >= 0) && (lastModified <= modifiedSince)) { return new ResponseEntity<byte[]>(headers, HttpStatus.NOT_MODIFIED); } } headers.setLastModified(lastModified); headers.setExpires(0); headers.setCacheControl("must-revalidate, private"); if (download) { headers.set("Content-Disposition", "attachment; filename=\"" + name.replace('"', '_') + "\""); } Page attachment = pageStore.getAttachment(projectName, branchName, pagePath, name); headers.setContentType(MediaType.parseMediaType(attachment.getContentType())); return new ResponseEntity<byte[]>(attachment.getData().getData(), headers, HttpStatus.OK); } catch (PageNotFoundException e) { return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND); } }  }
AttachmentController { @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)") public ResponseEntity<byte[]> getAttachment(@PathVariable String projectName, @PathVariable String branchName, @PathVariable String pagePath, @PathVariable String name, @RequestParam(required=false) boolean download, HttpServletRequest request) throws IOException { try { pagePath = Util.toRealPagePath(pagePath); PageMetadata metadata = pageStore.getAttachmentMetadata(projectName, branchName, pagePath, name); HttpHeaders headers = new HttpHeaders(); long lastEdited = metadata.getLastEdited().getTime(); long authenticationCreated = AuthenticationUtil.getAuthenticationCreationTime(request.getSession()); long lastModified = Math.max(lastEdited, authenticationCreated); if (!download) { long projectEditTime = PageUtil.getProjectEditTime(projectName); if (projectEditTime >= 0) { lastModified = Math.max(lastModified, projectEditTime); } long modifiedSince = request.getDateHeader("If-Modified-Since"); if ((modifiedSince >= 0) && (lastModified <= modifiedSince)) { return new ResponseEntity<byte[]>(headers, HttpStatus.NOT_MODIFIED); } } headers.setLastModified(lastModified); headers.setExpires(0); headers.setCacheControl("must-revalidate, private"); if (download) { headers.set("Content-Disposition", "attachment; filename=\"" + name.replace('"', '_') + "\""); } Page attachment = pageStore.getAttachment(projectName, branchName, pagePath, name); headers.setContentType(MediaType.parseMediaType(attachment.getContentType())); return new ResponseEntity<byte[]>(attachment.getData().getData(), headers, HttpStatus.OK); } catch (PageNotFoundException e) { return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND); } }  @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); }
AttachmentController { @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)") public ResponseEntity<byte[]> getAttachment(@PathVariable String projectName, @PathVariable String branchName, @PathVariable String pagePath, @PathVariable String name, @RequestParam(required=false) boolean download, HttpServletRequest request) throws IOException { try { pagePath = Util.toRealPagePath(pagePath); PageMetadata metadata = pageStore.getAttachmentMetadata(projectName, branchName, pagePath, name); HttpHeaders headers = new HttpHeaders(); long lastEdited = metadata.getLastEdited().getTime(); long authenticationCreated = AuthenticationUtil.getAuthenticationCreationTime(request.getSession()); long lastModified = Math.max(lastEdited, authenticationCreated); if (!download) { long projectEditTime = PageUtil.getProjectEditTime(projectName); if (projectEditTime >= 0) { lastModified = Math.max(lastModified, projectEditTime); } long modifiedSince = request.getDateHeader("If-Modified-Since"); if ((modifiedSince >= 0) && (lastModified <= modifiedSince)) { return new ResponseEntity<byte[]>(headers, HttpStatus.NOT_MODIFIED); } } headers.setLastModified(lastModified); headers.setExpires(0); headers.setCacheControl("must-revalidate, private"); if (download) { headers.set("Content-Disposition", "attachment; filename=\"" + name.replace('"', '_') + "\""); } Page attachment = pageStore.getAttachment(projectName, branchName, pagePath, name); headers.setContentType(MediaType.parseMediaType(attachment.getContentType())); return new ResponseEntity<byte[]>(attachment.getData().getData(), headers, HttpStatus.OK); } catch (PageNotFoundException e) { return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND); } }  @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);  }