From b4d2b9ff70e463179da0c0ca138fe44337dba204 Mon Sep 17 00:00:00 2001 From: SamuelQuetin Date: Thu, 10 Oct 2024 09:29:05 +0200 Subject: [PATCH] Fix mail si volumineux + fix objet mail --- .../fr/abes/logskbart/kafka/LogsListener.java | 9 ++-- .../abes/logskbart/service/EmailService.java | 48 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/abes/logskbart/kafka/LogsListener.java b/src/main/java/fr/abes/logskbart/kafka/LogsListener.java index 2e21a79..3aa64b3 100644 --- a/src/main/java/fr/abes/logskbart/kafka/LogsListener.java +++ b/src/main/java/fr/abes/logskbart/kafka/LogsListener.java @@ -145,12 +145,15 @@ private void createFileBad(String filename, Integer nbRun) throws IOException { Path pathOfLog = Path.of("tempLog" + File.separator + filename.replace(".tsv", ".log")); log.info("Suppression de " + pathOfLog); Files.deleteIfExists(pathOfLog); - - emailService.sendMailWithAttachment(filename, pathOfBadLocal); + long tailleDixMo = 10 * 1024 * 1024; + if( pathOfBadFinal.toFile().length() < tailleDixMo) { + emailService.sendMailWithAttachment(filename, pathOfBadLocal); + } else { + emailService.sendEmail(filename, "Le fichier est trop volumineux, retrouvez le sur le chemin : /applis/bacon/toLoad/"+filename.replace(".tsv", ".bad")); + } log.info("Suppression de " + pathOfBadLocal + " en local"); Files.deleteIfExists(pathOfBadLocal); } - } } diff --git a/src/main/java/fr/abes/logskbart/service/EmailService.java b/src/main/java/fr/abes/logskbart/service/EmailService.java index 83d36cd..80e241f 100644 --- a/src/main/java/fr/abes/logskbart/service/EmailService.java +++ b/src/main/java/fr/abes/logskbart/service/EmailService.java @@ -11,9 +11,14 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; import java.io.*; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -30,10 +35,18 @@ public class EmailService { @Value("${spring.profiles.active}") private String env; + public void sendEmail(String packageName, String message) { + // Création du mail + String requestJson = mailToJSON(this.recipient, "[KBART2BACON : erreurs]" + getTag() + " " + packageName, message); + + // Envoi du message par mail + sendMail(requestJson); + } + public void sendMailWithAttachment(String packageName, Path mailAttachmentPath) { try { // Création du mail - String requestJson = mailToJSON(this.recipient, "[CONVERGENCE]["+env.toUpperCase()+"] Log(s) d'erreur de " + packageName, ""); + String requestJson = mailToJSON(this.recipient, "[KBART2BACON : erreurs]" + getTag() + " " + packageName, "/applis/bacon/toLoad/"+mailAttachmentPath.getFileName()); // Récupération du fichier File file = mailAttachmentPath.toFile(); @@ -81,6 +94,31 @@ protected void sendMailWithFile(String requestJson, File f) { } } + protected void sendMail(String requestJson) { + RestTemplate restTemplate = new RestTemplate(); //appel ws qui envoie le mail + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + org.springframework.http.HttpEntity entity = new org.springframework.http.HttpEntity<>(requestJson, headers); + + restTemplate.getMessageConverters() + .add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); + + try { + restTemplate.postForObject(url + "htmlMail/", entity, String.class); //appel du ws avec + } catch (Exception e) { + log.warn("Erreur dans l'envoi du mail d'erreur Sudoc" + e); + } + // Création du l'adresse du ws d'envoi de mails + HttpPost mail = new HttpPost(this.url + "htmlMail/"); + + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + httpClient.execute(mail); + } catch (IOException e) { + log.warn("Erreur lors de l'envoi du mail. " + e); + } + } + protected String mailToJSON(String to, String subject, String text) { String json = ""; ObjectMapper mapper = new ObjectMapper(); @@ -98,4 +136,12 @@ protected String mailToJSON(String to, String subject, String text) { } return json; } + + private String getTag(){ + if(env.equalsIgnoreCase("PROD")){ + return ""; + } else { + return "[" + env.toUpperCase() + "]"; + } + } }