@RequestMapping(value = "/{templateName}/{cmsName}", method = POST) public String sendEmail( DownloadRequest downloadRequest, @PathVariable String templateName, @PathVariable String cmsName) { downloadRequest.setTemplateName(templateName); downloadRequest.setCms(cmsName); downloadRequest.setCreated(LocalDateTime.now()); logger.info("New download request: {}", downloadRequest); downloadRequest = downloadRequestRepository.save(downloadRequest); long timestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); String hash = downloadRequestParamsHasher.getHash( Template.valueOf(templateName).getDownloadName(), Cms.valueOf(cmsName).getNamePl(), timestamp); HtmlEmailMessage htmlEmailMessage = new HtmlEmailMessage( "*****@*****.**", downloadRequest.getAdministrativeEmail(), "Szablon CMS ze strony PWD", getEmailMessageTemplate(), getEmailMessageModelMap( templateName, Template.valueOf(templateName).getDownloadName(), Cms.valueOf(cmsName).getNamePl(), hash, timestamp)); if (mailgunClient.sendEmail(htmlEmailMessage)) { return "email_download"; } return "error"; }
@RequestMapping( value = "/{templateName}/{cmsName}/{hashCode}/{timestamp}", method = RequestMethod.GET) public ResponseEntity<ClassPathResource> downloadTemplate( @PathVariable String templateName, @PathVariable String cmsName, @PathVariable String hashCode, @PathVariable long timestamp) { String validHash = downloadRequestParamsHasher.getHash(templateName, cmsName, timestamp); if (validHash.equals(hashCode)) { long delay = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - timestamp; if (delay < 0 || delay > 3600) { throw new CmsTemplateHashExpiredException( "Hash code: " + hashCode + " has expired. Delay: " + delay); } return sendFile(templateName, cmsName); } else { throw new CmsTemplateHashErrorException("Hash code: " + hashCode + " is invalid."); } }