-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT : CDE-392-envoyer-aussi-un-mail-derreur-quand-pas-de-chargement-…
…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
Showing
7 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/fr/abes/logskbart/service/EmailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters