Skip to content

Commit

Permalink
FEAT : CDE-392-envoyer-aussi-un-mail-derreur-quand-pas-de-chargement-…
Browse files Browse the repository at this point in the history
…fichier-bad

     - ajout de la dépendance httpmime dans pom.xml
     - ajout de la classe MailDto.java
     - ajout de la classe EmailService.java
     - ajout des paramètres mail.ws.url et mail.ws.recipient dans application-dev.properties, application-test.properties et application-prod.properties
     - ajout de l'envoi du mail dans LogsListener.java
  • Loading branch information
EryneKL committed Apr 2, 2024
1 parent 0dfabef commit cbaf063
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.14</version>
</dependency>

<!-- === SpringDoc (swagger) === -->
<dependency>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/fr/abes/logskbart/dto/MailDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.abes.logskbart.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class MailDto {
private String app;
private String[] to;
private String[] cc;
private String[] cci;
private String subject;
private String text;
}
20 changes: 15 additions & 5 deletions src/main/java/fr/abes/logskbart/kafka/LogsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.abes.logskbart.dto.LogKbartDto;
import fr.abes.logskbart.entity.LogKbart;
import fr.abes.logskbart.repository.LogKbartRepository;
import fr.abes.logskbart.service.EmailService;
import fr.abes.logskbart.utils.UtilsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
Expand Down Expand Up @@ -33,11 +34,14 @@ public class LogsListener {

private final Map<String, Timestamp> lastTimeStampByFilename;

public LogsListener(ObjectMapper mapper, UtilsMapper logsMapper, LogKbartRepository repository, Map<String, Timestamp> lastTimeStampByFilename) {
private final EmailService emailService;

public LogsListener(ObjectMapper mapper, UtilsMapper logsMapper, LogKbartRepository repository, Map<String, Timestamp> lastTimeStampByFilename, EmailService emailService) {
this.mapper = mapper;
this.logsMapper = logsMapper;
this.repository = repository;
this.lastTimeStampByFilename = lastTimeStampByFilename;
this.emailService = emailService;
}


Expand All @@ -48,7 +52,7 @@ public LogsListener(ObjectMapper mapper, UtilsMapper logsMapper, LogKbartReposit
* @throws IOException exception levée
*/
@KafkaListener(topics = {"${topic.name.source.error}"}, groupId = "${topic.groupid.source}", containerFactory = "kafkaLogsListenerContainerFactory")
public void listenInfoKbart2KafkaAndErrorKbart2Kafka(ConsumerRecord<String, String> message) throws IOException {
public void listenInfoKbart2KafkaAndErrorKbart2Kafka(ConsumerRecord<String, String> message) throws IOException, InterruptedException {
LogKbartDto dto = mapper.readValue(message.value(), LogKbartDto.class);
LogKbart logKbart = logsMapper.map(dto, LogKbart.class);

Expand All @@ -71,12 +75,13 @@ public void listenInfoKbart2KafkaAndErrorKbart2Kafka(ConsumerRecord<String, Stri
}
Path of = Path.of("tempLog" + File.separator + logKbart.getPackageName().replace(".tsv", ".bad"));

// Si la ligne de log sur le topic errorkbart2kafka est de type ERROR
// Si la ligne de log sur le topic est de type ERROR
if (logKbart.getLevel().toString().equals("ERROR")) {

if (lastTimeStampByFilename.get(logKbart.getPackageName()) != null) {
Timestamp LastTimestampPlusTwoMinutes = new Timestamp(lastTimeStampByFilename.get(logKbart.getPackageName()).getTime() + TimeUnit.MINUTES.toMillis(2 ));

// Si ca fait 2min qu'on a pas recu de message pour ce fichier
// Si ça fait 2min qu'on n'a pas reçu de message pour ce fichier
if (currentTimestamp.after(LastTimestampPlusTwoMinutes)) {
log.debug("Suppression fichier " + logKbart.getPackageName() + " si existe");
Files.deleteIfExists(of);
Expand All @@ -103,9 +108,14 @@ public void listenInfoKbart2KafkaAndErrorKbart2Kafka(ConsumerRecord<String, Stri
throw new RuntimeException(e);
}
}
} else if (logKbart.getLevel().toString().equals("INFO") && logKbart.getMessage().contains("Traitement terminé pour fichier " + logKbart.getPackageName())) {
// Envoi du mail uniquement si le fichier temporaire a été créé
if (Files.exists(of)) {
Thread.sleep(20000); // pour attendre que tous les threads de best-ppn-api aient terminé leurs traitements
emailService.sendMailWithAttachment(logKbart.getPackageName(), of);
}
}
}

// Inscrit l'entity en BDD
repository.save(logKbart);
}
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/fr/abes/logskbart/service/EmailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package fr.abes.logskbart.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import fr.abes.logskbart.dto.MailDto;
import lombok.extern.log4j.Log4j2;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

@Log4j2
@Service
public class EmailService {

@Value("${mail.ws.recipient}")
private String recipient;

@Value("${mail.ws.url}")
private String url;

@Value("${spring.profiles.active}")
private String env;

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 + ".csv", "");

// Récupération du fichier
File file = mailAttachmentPath.toFile();

// Envoi du message par mail
sendMailWithFile(requestJson, file);

// Suppression du fichier temporaire
Files.deleteIfExists(mailAttachmentPath);

log.info("L'email a été correctement envoyé à " + recipient);

} catch (IOException e) {
throw new RuntimeException(e);
}
}

protected void sendMailWithFile(String requestJson, File f) {
// Création du l'adresse du ws d'envoi de mails
HttpPost uploadFile = new HttpPost(this.url + "htmlMailAttachment/");

// Création du builder
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("mail", requestJson, ContentType.APPLICATION_JSON);

try {
builder.addBinaryBody(
"attachment",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName()
);
} catch (FileNotFoundException e) {
log.warn("Le fichier n'a pas été trouvé. " + e.getMessage());
}

// Envoi du mail
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
httpClient.execute(uploadFile);
} catch (IOException e) {
log.warn("Erreur lors de l'envoi du mail. " + e.getMessage());
}
}

protected String mailToJSON(String to, String subject, String text) {
String json = "";
ObjectMapper mapper = new ObjectMapper();
MailDto mail = new MailDto();
mail.setApp("convergence");
mail.setTo(to.split(";"));
mail.setCc(new String[]{});
mail.setCci(new String[]{});
mail.setSubject(subject);
mail.setText(text);
try {
json = mapper.writeValueAsString(mail);
} catch (JsonProcessingException e) {
log.warn("Erreur lors de la création du mail. " + e);
}
return json;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ logging.level.root=INFO
logging.level.fr.abes=DEBUG

topic.groupid.source=logskbartConsumer

# Mailing
mail.ws.url=
mail.ws.recipient=
4 changes: 4 additions & 0 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ logging.level.root=ERROR
logging.level.fr.abes=ERROR

topic.groupid.source=logskbartConsumer

# Mailing
mail.ws.url=
mail.ws.recipient=
4 changes: 4 additions & 0 deletions src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ logging.level.root=INFO
logging.level.fr.abes=INFO

topic.groupid.source=logskbartConsumer

# Mailing
mail.ws.url=
mail.ws.recipient=

0 comments on commit cbaf063

Please sign in to comment.