Skip to content

Commit

Permalink
Merge pull request #45 from CaritasDeutschland/diakonie-master
Browse files Browse the repository at this point in the history
Diakonie theming integration
  • Loading branch information
mebo4b authored Dec 3, 2021
2 parents 1b3b6dc + 2704a8c commit ad2866d
Show file tree
Hide file tree
Showing 24 changed files with 787 additions and 65 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/dockerImage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ jobs:
uses: actions/download-artifact@v2
with:
name: targetfiles
- name: Prepare Docker variables
run: |
echo "DOCKER_REGISTRY=$(echo "docker.pkg.github.com/${{ github.repository }}" | awk '{print tolower($0)}')" >> $GITHUB_ENV
echo "DOCKER_IMAGE=$(echo "${{ github.repository }}" | awk -F / '{print tolower($2)}')" >> $GITHUB_ENV
shell: bash
- name: Push to GitHub Packages
uses: docker/build-push-action@v1.1.1
with:
username: ${{ secrets.GH_PACKAGE_RELEASE_USER }}
password: ${{ secrets.GH_PACKAGE_RELEASE_TOKEN }}
registry: docker.pkg.github.com
repository: caritasdeutschland/caritas-onlineberatung-mailservice/mailservice-image
registry: ${{ env.DOCKER_REGISTRY }}
repository: ${{ env.DOCKER_IMAGE }}
tag_with_ref: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@
/.nb-gradle/
wrapper
log

### Mac ###
*.DS_Store
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.1.1</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mailapi</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
Expand Down Expand Up @@ -105,6 +111,11 @@
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

<!-- Jsoup -->
<dependency>
Expand Down Expand Up @@ -180,6 +191,9 @@
<configOptions>
<interfaceOnly>true</interfaceOnly>
<sourceFolder>/</sourceFolder>
<ignoredDependencies>
<ignoredDependency>javax.mail:mailapi:1.4.3</ignoredDependency>
</ignoredDependencies>
</configOptions>
<inputSpec>${project.basedir}/api/mailservice.yaml</inputSpec>
<generatorName>spring</generatorName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import de.caritas.cob.mailservice.api.exception.ExchangeMailServiceException;
import de.caritas.cob.mailservice.api.mailtemplate.TemplateImage;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
Expand All @@ -26,6 +25,8 @@
public class ExchangeMailService {

private static final String TEMPLATE_IMAGE_DIR = "/templates/images/";
private static final String CUSTOM_TEMPLATE_IMAGE_DIR = "images/";


@Value("${mail.sender}")
private String mailSender;
Expand All @@ -45,11 +46,17 @@ public class ExchangeMailService {
@Value("${mail.exchange.version}")
String exchangeVersion;

@Value("${template.custom.resources.path}")
private String customResourcePath;

@Value("${template.use.custom.resources.path}")
private boolean useCustomResourcesPath;

/**
* Preparing and sending an html mail via Exchange.
*
* @param recipient The mail address of the recipient
* @param subject The subject of the mail
* @param recipient The mail address of the recipient
* @param subject The subject of the mail
* @param htmlTemplate The name of the html template
*/
public void prepareAndSendHtmlMail(String recipient, String subject, String htmlTemplate,
Expand All @@ -61,8 +68,8 @@ public void prepareAndSendHtmlMail(String recipient, String subject, String html
* Preparing and sending an text mail via Exchange.
*
* @param recipients The mail address of the recipients
* @param subject The subject of the mail
* @param body The text to send
* @param subject The subject of the mail
* @param body The text to send
*/
public void prepareAndSendTextMail(String recipients, String subject, String body)
throws ExchangeMailServiceException {
Expand All @@ -76,7 +83,7 @@ private void prepareAndSendMail(String recipients, String subject, String bodyTe
throw new ExchangeMailServiceException("No sender mail address set");
}

ExchangeService exchangeService = new ExchangeService(ExchangeVersion.valueOf(exchangeVersion));
var exchangeService = new ExchangeService(ExchangeVersion.valueOf(exchangeVersion));
setupExchangeService(exchangeService);
EmailMessage msg = buildEmailMessage(subject, bodyText, bodyType, exchangeService);
addEmailAttachmentsIfNecessary(templateImages, msg);
Expand Down Expand Up @@ -110,10 +117,10 @@ private void setupExchangeService(ExchangeService exchangeService)
private EmailMessage buildEmailMessage(String subject, String bodyText, BodyType bodyType,
ExchangeService exchangeService) throws ExchangeMailServiceException {
try {
EmailMessage msg = new EmailMessage(exchangeService);
var msg = new EmailMessage(exchangeService);
msg.setSubject(subject);

MessageBody messageBody = new MessageBody();
var messageBody = new MessageBody();
messageBody.setBodyType(bodyType);
messageBody.setText(bodyText);
msg.setBody(messageBody);
Expand All @@ -126,22 +133,27 @@ private EmailMessage buildEmailMessage(String subject, String bodyText, BodyType
private void addEmailAttachmentsIfNecessary(List<TemplateImage> templateImages, EmailMessage msg)
throws ExchangeMailServiceException {
if (!CollectionUtils.isEmpty(templateImages)) {
try {
int attachmentIndex = 0;
for (TemplateImage templateImage : templateImages) {
InputStream inputStream =
getClass().getResourceAsStream(TEMPLATE_IMAGE_DIR + templateImage.getFilename());

int attachmentIndex = 0;
for (TemplateImage templateImage : templateImages) {
try {
var inputStream =
useCustomResourcesPath ? getClass().getResourceAsStream(
customResourcePath + CUSTOM_TEMPLATE_IMAGE_DIR + templateImage.getFilename())
: getClass()
.getResourceAsStream(TEMPLATE_IMAGE_DIR + templateImage.getFilename());
msg.getAttachments().addFileAttachment(templateImage.getFilename(), inputStream);
msg.getAttachments().getItems().get(attachmentIndex).setIsInline(true);
msg.getAttachments().getItems().get(attachmentIndex)
.setContentId(templateImage.getFilename());
msg.getAttachments().getItems().get(attachmentIndex).setName(templateImage.getFilename());
msg.getAttachments().getItems().get(attachmentIndex)
.setName(templateImage.getFilename());
msg.getAttachments().getItems().get(attachmentIndex)
.setContentType(templateImage.getFiletype());
attachmentIndex++;
} catch (Exception e) {
throw new ExchangeMailServiceException("Error while processing attachments", e);
}
} catch (Exception e) {
throw new ExchangeMailServiceException("Error while processing attachments", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.caritas.cob.mailservice.api.service;

import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;

import de.caritas.cob.mailservice.api.exception.ExchangeMailServiceException;
import de.caritas.cob.mailservice.api.exception.InternalServerErrorException;
Expand Down Expand Up @@ -181,4 +180,4 @@ private void loadUnescapedMailDataAndSendMail(MailDTO mail,

}

}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package de.caritas.cob.mailservice.api.service;

import static java.util.Objects.isNull;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.mailservice.api.exception.SmtpMailServiceException;
import de.caritas.cob.mailservice.api.mailtemplate.TemplateImage;
import java.io.FileInputStream;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
Expand All @@ -22,15 +27,22 @@
public class SmtpMailService {

private static final String TEMPLATE_IMAGE_DIR = "/templates/images/";
private static final String CUSTOM_TEMPLATE_IMAGE_DIR = "images/";

private JavaMailSender javaMailSender;
private final JavaMailSender javaMailSender;

@Value("${mail.sender}")
private String mailSender;

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

@Value("${template.custom.resources.path}")
private String customResourcePath;

@Value("${template.use.custom.resources.path}")
private boolean useCustomResourcesPath;

/**
* Standard constructor for mail service
*/
Expand All @@ -42,14 +54,14 @@ public SmtpMailService(JavaMailSender javaMailSender) {
/**
* Preparing and sending an html mail via smtp.
*
* @param recipient The mail address of the recipient
* @param subject The subject of the mail
* @param recipient The mail address of the recipient
* @param subject The subject of the mail
* @param htmlTemplate The name of the html template
*/
public void prepareAndSendHtmlMail(String recipient, String subject, String htmlTemplate,
List<TemplateImage> templateImages) throws SmtpMailServiceException {

if (mailSender == null) {
if (isNull(mailSender)) {
throw new SmtpMailServiceException("No sender mail address set");
}

Expand All @@ -65,21 +77,27 @@ public void prepareAndSendHtmlMail(String recipient, String subject, String html
private MimeMessagePreparator buildHtmlMessagePreparator(String recipient, String subject,
String htmlTemplate, List<TemplateImage> templateImages) {
return mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,
var messageHelper = new MimeMessageHelper(mimeMessage,
(!CollectionUtils.isEmpty(templateImages)), "UTF-8");
messageHelper.setFrom(this.mailSender);
if (isNotBlank(fixMailRecipient)) {
messageHelper.setTo(fixMailRecipient);
} else {
messageHelper.setTo(recipient);
}
messageHelper.setTo(getRecipients(recipient));
messageHelper.setSubject(subject);
messageHelper.setText(htmlTemplate, true);

if (!CollectionUtils.isEmpty(templateImages)) {
for (TemplateImage templateImage : templateImages) {
messageHelper.addInline(templateImage.getFilename(),
new ClassPathResource(TEMPLATE_IMAGE_DIR + templateImage.getFilename()),
InputStreamSource inputStreamSource;
if (useCustomResourcesPath) {
inputStreamSource = new ByteArrayResource(
IOUtils.toByteArray(new FileInputStream(
customResourcePath + CUSTOM_TEMPLATE_IMAGE_DIR + templateImage
.getFilename())));
} else {
inputStreamSource = new ClassPathResource(
TEMPLATE_IMAGE_DIR + templateImage
.getFilename());
}
messageHelper.addInline(templateImage.getFilename(), inputStreamSource,
templateImage.getFiletype());
}
}
Expand All @@ -90,8 +108,8 @@ private MimeMessagePreparator buildHtmlMessagePreparator(String recipient, Strin
* Preparing and sending an simple text mail.
*
* @param recipient The mail address of the recipient
* @param subject The subject of the mail
* @param body The body of the mail
* @param subject The subject of the mail
* @param body The body of the mail
*/
public void prepareAndSendTextMail(String recipient, String subject, String body)
throws SmtpMailServiceException {
Expand All @@ -111,17 +129,19 @@ private MimeMessagePreparator buildTextMessagePreparator(String recipient, Strin
String body) {
return mimeMessage -> {

MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
var messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom(this.mailSender);
if (isNotBlank(fixMailRecipient)) {
messageHelper.setTo(fixMailRecipient);
} else {
String[] recipients = recipient.split(",");
messageHelper.setTo(recipients);
}
messageHelper.setTo(getRecipients(recipient));
messageHelper.setSubject(subject);
messageHelper.setText(body, false);
};
}

private String[] getRecipients(String recipient) {
if (isNotBlank(fixMailRecipient)) {
return new String[] { fixMailRecipient };
} else {
return recipient.split(",");
}
}
}
Loading

0 comments on commit ad2866d

Please sign in to comment.