-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RHCLOUD-37053] Simplify email connector internal logic
- Loading branch information
Showing
14 changed files
with
677 additions
and
135 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
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
125 changes: 125 additions & 0 deletions
125
...rc/main/java/com/redhat/cloud/notifications/connector/email/EmailManagementProcessor.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,125 @@ | ||
package com.redhat.cloud.notifications.connector.email; | ||
|
||
import com.redhat.cloud.notifications.connector.email.config.EmailConnectorConfig; | ||
import com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty; | ||
import com.redhat.cloud.notifications.connector.email.model.bop.Email; | ||
import com.redhat.cloud.notifications.connector.email.model.bop.SendEmailsRequest; | ||
import com.redhat.cloud.notifications.connector.email.model.settings.RecipientSettings; | ||
import com.redhat.cloud.notifications.connector.email.model.settings.User; | ||
import com.redhat.cloud.notifications.connector.email.processors.bop.BOPService; | ||
import com.redhat.cloud.notifications.connector.email.processors.recipientsresolver.ExternalRecipientsResolver; | ||
import io.quarkus.logging.Log; | ||
import io.vertx.core.json.JsonObject; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Processor; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.redhat.cloud.notifications.connector.ExchangeProperty.ID; | ||
import static com.redhat.cloud.notifications.connector.ExchangeProperty.ORG_ID; | ||
import static com.redhat.cloud.notifications.connector.email.CloudEventHistoryBuilder.TOTAL_RECIPIENTS_KEY; | ||
import static java.util.stream.Collectors.toSet; | ||
|
||
@ApplicationScoped | ||
public class EmailManagementProcessor implements Processor { | ||
|
||
@Inject | ||
EmailConnectorConfig emailConnectorConfig; | ||
|
||
@Inject | ||
ExternalRecipientsResolver externalRecipientsResolver; | ||
|
||
@Inject | ||
@RestClient | ||
BOPService bopService; | ||
|
||
@Override | ||
public void process(final Exchange exchange) { | ||
// fetch recipients | ||
Set<String> recipientsList = fetchRecipients(exchange); | ||
|
||
if (recipientsList.isEmpty()) { | ||
Log.infof("Skipped Email notification because the recipients list was empty [orgId=$%s, historyId=%s]", exchange.getProperty(ORG_ID, String.class), exchange.getProperty(ID, String.class)); | ||
} else { | ||
// send to bop | ||
sendToBop(exchange, recipientsList); | ||
} | ||
} | ||
|
||
protected void sendToBop(Exchange exchange, Set<String> recipientsList) { | ||
// split recipient list and send it ot BOP | ||
List<List<String>> packedRecipients = partition(recipientsList, emailConnectorConfig.getMaxRecipientsPerEmail() - 1); | ||
final String subject = exchange.getProperty(ExchangeProperty.RENDERED_SUBJECT, String.class); | ||
final String body = exchange.getProperty(ExchangeProperty.RENDERED_BODY, String.class); | ||
final String sender = exchange.getProperty(ExchangeProperty.EMAIL_SENDER, String.class); | ||
|
||
for (int i = 0; i < packedRecipients.size(); i++) { | ||
sendToBop(exchange, packedRecipients.get(i), subject, body, sender, i, packedRecipients.size()); | ||
} | ||
} | ||
|
||
protected void sendToBop(Exchange exchange, List<String> recipients, String subject, String body, String sender, int currentIteration, int totalIterations) { | ||
|
||
// Prepare the email to be sent | ||
final Email email = new Email( | ||
subject, | ||
body, | ||
Set.copyOf(recipients) | ||
); | ||
|
||
final SendEmailsRequest request = new SendEmailsRequest( | ||
Set.of(email), | ||
sender, | ||
sender | ||
); | ||
|
||
bopService.sendEmail(emailConnectorConfig.getBopApiToken(), | ||
emailConnectorConfig.getBopClientId(), | ||
emailConnectorConfig.getBopEnv(), | ||
request); | ||
Log.infof("Sent Email notification %d/%d [orgId=%s, historyId=%s]", currentIteration + 1, totalIterations, exchange.getProperty(ORG_ID, String.class), exchange.getProperty(ID, String.class)); | ||
} | ||
|
||
private Set<String> fetchRecipients(Exchange exchange) { | ||
List<RecipientSettings> recipientSettings = exchange.getProperty(ExchangeProperty.RECIPIENT_SETTINGS, List.class); | ||
Set<String> subscribers = exchange.getProperty(ExchangeProperty.SUBSCRIBERS, Set.class); | ||
Set<String> unsubscribers = exchange.getProperty(ExchangeProperty.UNSUBSCRIBERS, Set.class); | ||
JsonObject recipientsAuthorizationCriterion = exchange.getProperty(ExchangeProperty.RECIPIENTS_AUTHORIZATION_CRITERION, JsonObject.class); | ||
|
||
boolean subscribedByDefault = exchange.getProperty(ExchangeProperty.SUBSCRIBED_BY_DEFAULT, boolean.class); | ||
final String orgId = exchange.getProperty(ORG_ID, String.class); | ||
|
||
Set<String> recipientsList = externalRecipientsResolver.recipientUsers( | ||
orgId, | ||
Set.copyOf(recipientSettings), | ||
subscribers, | ||
unsubscribers, | ||
subscribedByDefault, | ||
recipientsAuthorizationCriterion) | ||
.stream().map(User::getEmail).filter(email -> email != null && !email.isBlank()).collect(toSet()); | ||
|
||
Set<String> emails = exchange.getProperty(ExchangeProperty.EMAIL_RECIPIENTS, Set.of(), Set.class); | ||
if (emailConnectorConfig.isEmailsInternalOnlyEnabled()) { | ||
Set<String> forbiddenEmail = emails.stream().filter(email -> !email.trim().toLowerCase().endsWith("@redhat.com")).collect(Collectors.toSet()); | ||
if (!forbiddenEmail.isEmpty()) { | ||
Log.warnf(" %s emails are forbidden for message historyId: %s ", forbiddenEmail, exchange.getProperty(com.redhat.cloud.notifications.connector.ExchangeProperty.ID, String.class)); | ||
} | ||
emails.removeAll(forbiddenEmail); | ||
} | ||
recipientsList.addAll(emails); | ||
exchange.setProperty(TOTAL_RECIPIENTS_KEY, recipientsList.size()); | ||
return recipientsList; | ||
} | ||
|
||
private static List<List<String>> partition(Set<String> collection, int n) { | ||
AtomicInteger counter = new AtomicInteger(); | ||
return collection.stream() | ||
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / n)) | ||
.values().stream().collect(Collectors.toList()); | ||
} | ||
} |
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
Oops, something went wrong.