Skip to content

Commit

Permalink
Fix mail si volumineux + fix objet mail
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQuetin committed Oct 10, 2024
1 parent a29c3b2 commit b4d2b9f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/main/java/fr/abes/logskbart/kafka/LogsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}
48 changes: 47 additions & 1 deletion src/main/java/fr/abes/logskbart/service/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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<String> 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();
Expand All @@ -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() + "]";
}
}
}

0 comments on commit b4d2b9f

Please sign in to comment.