@CrossOrigin(origins = "*") @RequestMapping(value = "/sendEmailFromTo") @ResponseStatus(HttpStatus.OK) public String sendEmail(String fromAddress, String toAddress, String uuid) throws IOException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Message message = new Message( fromAddress, toAddress, "Your mazda routes exported at " + sdf.format(new Date()), "These are your exported cached routes from Mazda"); byte[] data = createZip(uuid); Attachment attachment = new Attachment("routes.zip", data); message.setAttachments(attachment); try { mailService.send(message); log.info("Routes sent to \"{}\"", toAddress); } catch (CallNotFoundException ex) { log.error("Cannot send email: {}", ExceptionUtils.getRootCauseMessage(ex)); File file = new File("routes_" + toAddress + "_" + System.currentTimeMillis() + ".zip"); FileUtils.writeByteArrayToFile(file, data); log.info("Routes save to file: {}", file.getAbsolutePath()); } finally { routeRepository.clearRoutes(uuid); } return "{}"; }
@RequestMapping( value = "/saveAsZip/filename/{uuid}/{filename}", method = RequestMethod.GET, produces = "application/zip") public ResponseEntity<InputStreamResource> saveAsZip( @PathVariable String uuid, @PathVariable String filename) throws IOException { HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); headers.add("Content-Disposition", "attachment; filename=" + filename + ".zip"); headers.add("Set-Cookie", "fileDownload=true; path=/"); byte[] data = createZip(uuid); routeRepository.clearRoutes(uuid); return ResponseEntity.ok() .headers(headers) .contentLength(data.length) .contentType(MediaType.parseMediaType("application/zip")) .body(new InputStreamResource(new ByteArrayInputStream(data))); }
private byte[] createZip(String uuid) throws IOException { return zipUtils.doZip( routeCacheFileUtils.createFileContent(routeRepository.getAllRoutes(uuid))); }