diff --git a/src/main/java/fr/abes/logskbart/kafka/LogsListener.java b/src/main/java/fr/abes/logskbart/kafka/LogsListener.java index a23c884..bd85ac5 100644 --- a/src/main/java/fr/abes/logskbart/kafka/LogsListener.java +++ b/src/main/java/fr/abes/logskbart/kafka/LogsListener.java @@ -3,15 +3,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import fr.abes.logskbart.dto.LogKbartDto; import fr.abes.logskbart.entity.LogKbart; -import fr.abes.logskbart.service.EmailService; import fr.abes.logskbart.service.LogsService; import fr.abes.logskbart.utils.UtilsMapper; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.beans.factory.annotation.Value; import org.springframework.kafka.annotation.KafkaListener; -import org.springframework.kafka.support.Acknowledgment; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.io.File; @@ -23,10 +20,10 @@ import java.nio.file.attribute.BasicFileAttributes; import java.time.LocalDateTime; import java.time.ZoneId; -import java.util.*; +import java.util.Date; +import java.util.List; +import java.util.Map; import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; -import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -42,17 +39,14 @@ public class LogsListener { private final LogsService service; - private final EmailService emailService; - private final Map workInProgressMap; private final Executor executor; - public LogsListener(ObjectMapper mapper, UtilsMapper logsMapper, LogsService service, EmailService emailService, Map workInProgressMap, Executor executor) { + public LogsListener(ObjectMapper mapper, UtilsMapper logsMapper, LogsService service, Map workInProgressMap, Executor executor) { this.mapper = mapper; this.logsMapper = logsMapper; this.service = service; - this.emailService = emailService; this.workInProgressMap = workInProgressMap; this.executor = executor; } @@ -170,12 +164,6 @@ private void createFileBad(String filename) throws IOException { Path pathOfLog = Path.of("tempLog" + File.separator + filename.replace(".tsv", ".log")); log.info("Suppression de " + pathOfLog); Files.deleteIfExists(pathOfLog); - 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 deleted file mode 100644 index 1d27db5..0000000 --- a/src/main/java/fr/abes/logskbart/service/EmailService.java +++ /dev/null @@ -1,149 +0,0 @@ -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.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; - -@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 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); - - log.info("L'email a été correctement envoyé à " + recipient); - } - - public void sendMailWithAttachment(String packageName, Path mailAttachmentPath) { - try { - // Création du mail - String requestJson = mailToJSON(this.recipient, "[KBART2BACON : erreurs]" + getTag() + " " + packageName, "/applis/bacon/toLoad/"+mailAttachmentPath.getFileName()); - - // 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 avec PJ 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 + "v2/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 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(); - 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; - } - - private String getTag(){ - if(env.equalsIgnoreCase("PROD")){ - return ""; - } else { - return "[" + env.toUpperCase() + "]"; - } - } -} diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 98a0e40..1a66eef 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -10,6 +10,3 @@ logging.level.fr.abes=DEBUG topic.groupid.source=logskbartConsumer -# Mailing -mail.ws.url= -mail.ws.recipient= diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 6457cde..0005266 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -2,7 +2,6 @@ abes.kafka.bootstrap-servers= abes.nbThread= -# Base Postgres # Properties defined from .env on server #ignore resolution error spring.elasticsearch.uris= @@ -12,6 +11,4 @@ logging.level.fr.abes=ERROR topic.groupid.source=logskbartConsumer -# Mailing -mail.ws.url= -mail.ws.recipient= + diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index d7e7bf9..b50caf4 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -10,6 +10,3 @@ logging.level.fr.abes=INFO topic.groupid.source=logskbartConsumer -# Mailing -mail.ws.url= -mail.ws.recipient=