-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(email-connector): add document handling to the email connector #3645
Open
mathias-vandaele
wants to merge
7
commits into
main
Choose a base branch
from
3643-email-connector-add-document-handling-feature-for-email-connector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6c6a7ad
feat(email-connector): add document handling to the email connector 2
mathias-vandaele 4a6b55a
feat(email-connector): add document handling, bring activation result
mathias-vandaele 673d895
feat(email-connector): add document handling, correct codeQL reports
mathias-vandaele 439b9c4
feat(email-connector): add document handling, fix activation boolean
mathias-vandaele cc6f4db
feat(email-connector): add document handling, multiple attachment sen…
mathias-vandaele 17fe1e8
feat(email-connector): rebase + correcting and managing conflict result
mathias-vandaele 656b337
Merge branch 'main' into 3643-email-connector-add-document-handling-f…
mathias-vandaele File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -8,23 +8,33 @@ | |
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.camunda.connector.api.outbound.OutboundConnectorContext; | ||
import io.camunda.connector.email.authentication.Authentication; | ||
import io.camunda.connector.email.client.EmailActionExecutor; | ||
import io.camunda.connector.email.client.jakarta.models.EmailAttachment; | ||
import io.camunda.connector.email.client.jakarta.utils.JakartaUtils; | ||
import io.camunda.connector.email.outbound.model.EmailRequest; | ||
import io.camunda.connector.email.outbound.protocols.Protocol; | ||
import io.camunda.connector.email.outbound.protocols.actions.*; | ||
import io.camunda.connector.email.response.*; | ||
import io.camunda.document.Document; | ||
import io.camunda.document.store.DocumentCreationRequest; | ||
import jakarta.activation.DataHandler; | ||
import jakarta.activation.DataSource; | ||
import jakarta.mail.*; | ||
import jakarta.mail.internet.*; | ||
import jakarta.mail.search.*; | ||
import jakarta.mail.util.ByteArrayDataSource; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.*; | ||
import java.util.function.Consumer; | ||
|
||
public class JakartaEmailActionExecutor implements EmailActionExecutor { | ||
|
||
private final JakartaUtils jakartaUtils; | ||
private final ObjectMapper objectMapper; | ||
private OutboundConnectorContext connectorContext; | ||
|
||
private JakartaEmailActionExecutor(JakartaUtils jakartaUtils, ObjectMapper objectMapper) { | ||
this.jakartaUtils = jakartaUtils; | ||
|
@@ -36,7 +46,9 @@ | |
return new JakartaEmailActionExecutor(sessionFactory, objectMapper); | ||
} | ||
|
||
public Object execute(EmailRequest emailRequest) { | ||
public Object execute(OutboundConnectorContext context) { | ||
this.connectorContext = context; | ||
EmailRequest emailRequest = context.bindVariables(EmailRequest.class); | ||
Authentication authentication = emailRequest.authentication(); | ||
Protocol protocol = emailRequest.data(); | ||
Action action = protocol.getProtocolAction(); | ||
|
@@ -93,6 +105,7 @@ | |
email.size(), | ||
email.body().bodyAsPlainText(), | ||
email.body().bodyAsHtml(), | ||
this.createDocumentList(email.body().attachments(), connectorContext), | ||
email.receivedAt())) | ||
.orElseThrow(() -> new MessagingException("Could not find an email ID")); | ||
} | ||
|
@@ -196,6 +209,8 @@ | |
email.size(), | ||
email.body().bodyAsPlainText(), | ||
email.body().bodyAsHtml(), | ||
this.createDocumentList( | ||
email.body().attachments(), this.connectorContext), | ||
email.receivedAt())) | ||
.orElseThrow(() -> new MessagingException("No emails have been found with this ID")); | ||
} | ||
|
@@ -257,6 +272,9 @@ | |
headers.ifPresent(stringObjectMap -> setMessageHeaders(stringObjectMap, message)); | ||
message.setSubject(smtpSendEmail.subject()); | ||
Multipart multipart = getMultipart(smtpSendEmail); | ||
if (!Objects.isNull(smtpSendEmail.attachments())) { | ||
smtpSendEmail.attachments().forEach(getDocumentConsumer(multipart)); | ||
} | ||
message.setContent(multipart); | ||
try (Transport transport = session.getTransport()) { | ||
this.jakartaUtils.connectTransport(transport, authentication); | ||
|
@@ -362,4 +380,32 @@ | |
"Unexpected value: " + object + ". List or String was expected"); | ||
}); | ||
} | ||
|
||
private Consumer<Document> getDocumentConsumer(Multipart multipart) { | ||
return document -> { | ||
try { | ||
BodyPart attachment = new MimeBodyPart(); | ||
DataSource dataSource = | ||
new ByteArrayDataSource(document.asInputStream(), document.metadata().getContentType()); | ||
attachment.setDataHandler(new DataHandler(dataSource)); | ||
attachment.setFileName(document.metadata().getFileName()); | ||
multipart.addBodyPart(attachment); | ||
} catch (IOException | MessagingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}; | ||
} | ||
|
||
private List<Document> createDocumentList( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's a code in PollingManager that looks like this one, do we want to re this function in the PollingManager? |
||
List<EmailAttachment> attachments, OutboundConnectorContext connectorContext) { | ||
return attachments.stream() | ||
.map( | ||
document -> | ||
connectorContext.createDocument( | ||
DocumentCreationRequest.from(document.inputStream()) | ||
.fileName(document.name()) | ||
.contentType(document.contentType()) | ||
.build())) | ||
.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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Missing Override annotation Note