diff --git a/pom.xml b/pom.xml
index bdcebfd..6ee58b6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -140,6 +140,11 @@
lombok
true
+
+ org.apache.httpcomponents
+ httpmime
+ 4.5.14
+
diff --git a/src/main/java/fr/abes/logskbart/dto/MailDto.java b/src/main/java/fr/abes/logskbart/dto/MailDto.java
new file mode 100644
index 0000000..edea5df
--- /dev/null
+++ b/src/main/java/fr/abes/logskbart/dto/MailDto.java
@@ -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;
+}
diff --git a/src/main/java/fr/abes/logskbart/kafka/LogsListener.java b/src/main/java/fr/abes/logskbart/kafka/LogsListener.java
index 79e50f2..00fc9ad 100644
--- a/src/main/java/fr/abes/logskbart/kafka/LogsListener.java
+++ b/src/main/java/fr/abes/logskbart/kafka/LogsListener.java
@@ -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;
@@ -12,12 +13,13 @@
import java.io.File;
import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardOpenOption;
-import java.sql.Date;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
import java.util.Arrays;
+import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@@ -33,16 +35,19 @@ public class LogsListener {
private final Map lastTimeStampByFilename;
- public LogsListener(ObjectMapper mapper, UtilsMapper logsMapper, LogKbartRepository repository, Map lastTimeStampByFilename) {
+ private final EmailService emailService;
+
+ public LogsListener(ObjectMapper mapper, UtilsMapper logsMapper, LogKbartRepository repository, Map lastTimeStampByFilename, EmailService emailService) {
this.mapper = mapper;
this.logsMapper = logsMapper;
this.repository = repository;
this.lastTimeStampByFilename = lastTimeStampByFilename;
+ this.emailService = emailService;
}
/**
- * Ecoute les topic de log d'erreurs et de fin de traitement bestPpn et génère un fichier err pour chaque fichier kbart
+ * Ecoute le topic de log d'erreurs et génère un fichier bad pour chaque fichier kbart
*
* @param message le message kafka
* @throws IOException exception levée
@@ -65,18 +70,23 @@ public void listenInfoKbart2KafkaAndErrorKbart2Kafka(ConsumerRecord 600000) {
+ fileToCheck.delete();
+ log.debug("Fichier obsolète supprimé : " + nameFile);
+ }
+ }
+ }
+ }
+ }
}
diff --git a/src/main/java/fr/abes/logskbart/service/EmailService.java b/src/main/java/fr/abes/logskbart/service/EmailService.java
new file mode 100644
index 0000000..83d36cd
--- /dev/null
+++ b/src/main/java/fr/abes/logskbart/service/EmailService.java
@@ -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, "");
+
+ // 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;
+ }
+}
diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties
index a82649f..1fa18bb 100644
--- a/src/main/resources/application-dev.properties
+++ b/src/main/resources/application-dev.properties
@@ -1,7 +1,7 @@
# Consumer properties
spring.kafka.consumer.bootstrap-servers=
-# Base Postgres
+# Properties defined from .env on server #ignore resolution error
spring.datasource.logsdb.driver-class-name=org.postgresql.Driver
spring.datasource.logsdb.jdbcurl=
spring.datasource.logsdb.username=
@@ -19,3 +19,7 @@ logging.level.root=INFO
logging.level.fr.abes=DEBUG
topic.groupid.source=logskbartConsumer
+
+# Mailing
+mail.ws.url=
+mail.ws.recipient=
\ No newline at end of file
diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties
index 595ff7f..550ff67 100644
--- a/src/main/resources/application-prod.properties
+++ b/src/main/resources/application-prod.properties
@@ -2,6 +2,7 @@
spring.kafka.consumer.bootstrap-servers=
# Base Postgres
+# Properties defined from .env on server #ignore resolution error
spring.datasource.logsdb.driver-class-name=org.postgresql.Driver
spring.datasource.logsdb.jdbcurl=
spring.datasource.logsdb.username=
@@ -20,3 +21,7 @@ logging.level.root=ERROR
logging.level.fr.abes=ERROR
topic.groupid.source=logskbartConsumer
+
+# Mailing
+mail.ws.url=
+mail.ws.recipient=
\ No newline at end of file
diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties
index 0e74d28..f285439 100644
--- a/src/main/resources/application-test.properties
+++ b/src/main/resources/application-test.properties
@@ -1,7 +1,7 @@
# Consumer properties
spring.kafka.consumer.bootstrap-servers=
-# Base Postgres
+# Properties defined from .env on server #ignore resolution error
spring.datasource.logsdb.driver-class-name=org.postgresql.Driver
spring.datasource.logsdb.jdbcurl=
spring.datasource.logsdb.username=
@@ -19,3 +19,7 @@ logging.level.root=INFO
logging.level.fr.abes=INFO
topic.groupid.source=logskbartConsumer
+
+# Mailing
+mail.ws.url=
+mail.ws.recipient=
\ No newline at end of file