Skip to content

Commit

Permalink
feat(email connector): first checkpoint on email connector 7
Browse files Browse the repository at this point in the history
  • Loading branch information
mathias-vandaele committed Aug 26, 2024
1 parent 857d2d3 commit baa9aa6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,59 @@ public Object execute(EmailRequest emailRequest) {
case ImapListEmails imapListEmails -> null;
case ImapDeleteEmail imapDeleteEmail -> null;
case ImapReadEmail imapReadEmail -> null;
case Pop3DeleteEmail pop3DeleteEmail -> null;
case Pop3DeleteEmail pop3DeleteEmail ->
pop3DeleteEmail(pop3DeleteEmail, authentication, session);
case Pop3ListEmails pop3ListEmails -> pop3ListEmails(pop3ListEmails, authentication, session);
case Pop3ReadEmail pop3ReadEmail -> pop3ReadEmail(pop3ReadEmail, authentication, session);
};
}

private Object pop3DeleteEmail(
Pop3DeleteEmail pop3DeleteEmail, Authentication authentication, Session session) {
try {
try (Store store = session.getStore()) {
connectStore(store, authentication);
try (POP3Folder folder = (POP3Folder) store.getFolder("INBOX")) {
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
for (Message message : messages) {
String uid = folder.getUID(message);
if (uid.equals(pop3DeleteEmail.getUidlDelete())) {
message.setFlag(Flags.Flag.DELETED, true);
}
}
}
}
throw new RuntimeException(
"No corresponding POP3 email found for uidl %s"
.formatted(pop3DeleteEmail.getUidlDelete()));
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

private Object pop3ReadEmail(
Pop3ReadEmail pop3ReadEmail, Authentication authentication, Session session) {
try {
Store store = session.getStore();
connectStore(store, authentication);
POP3Folder folder = (POP3Folder) store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
for (Message message : messages) {
String uid = folder.getUID(message);
if (uid.equals(pop3ReadEmail.getUidlRead())) {
Email email = Email.createEmail(message);
if (pop3ReadEmail.isDeleteOnRead()) message.setFlag(Flags.Flag.DELETED, true);
return new Pop3ReadEmailResponse(
folder.getUID(message),
email.getFrom(),
email.getSubject(),
email.getSize(),
email.getBody().getBodyAsPlainText(),
email.getBody().getBodyAsHtml());
try (Store store = session.getStore()) {
connectStore(store, authentication);
try (POP3Folder folder = (POP3Folder) store.getFolder("INBOX")) {
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
for (Message message : messages) {
String uid = folder.getUID(message);
if (uid.equals(pop3ReadEmail.getUidlRead())) {
Email email = Email.createEmail(message);
if (pop3ReadEmail.isDeleteOnRead()) message.setFlag(Flags.Flag.DELETED, true);
return new Pop3ReadEmailResponse(
folder.getUID(message),
email.getFrom(),
email.getSubject(),
email.getSize(),
email.getBody().getBodyAsPlainText(),
email.getBody().getBodyAsHtml());
}
}
}
}
throw new RuntimeException(
Expand All @@ -77,20 +104,22 @@ private Object pop3ReadEmail(
private Object pop3ListEmails(
Pop3ListEmails pop3ListEmails, Authentication authentication, Session session) {
try {
Store store = session.getStore();
connectStore(store, authentication);
POP3Folder folder = (POP3Folder) store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
List<Pop3ListEmailsResponse> response = new ArrayList<>();
for (Message message : messages) {
Email email = Email.createBodylessEmail(message);
Pop3ListEmailsResponse pop3ListEmailsResponse =
new Pop3ListEmailsResponse(
folder.getUID(message), email.getFrom(), email.getSubject(), email.getSize());
response.add(pop3ListEmailsResponse);
try (Store store = session.getStore()) {
connectStore(store, authentication);
try (POP3Folder folder = (POP3Folder) store.getFolder("INBOX")) {
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
List<Pop3ListEmailsResponse> response = new ArrayList<>();
for (Message message : messages) {
Email email = Email.createBodylessEmail(message);
Pop3ListEmailsResponse pop3ListEmailsResponse =
new Pop3ListEmailsResponse(
folder.getUID(message), email.getFrom(), email.getSubject(), email.getSize());
response.add(pop3ListEmailsResponse);
}
return response;
}
}
return response;
} catch (MessagingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.camunda.connector.email.protocols.actions.Action;
import io.camunda.connector.email.protocols.actions.Pop3Action;
import io.camunda.connector.email.protocols.actions.Pop3ListEmails;
import io.camunda.connector.email.protocols.actions.Pop3ReadEmail;
import io.camunda.connector.email.protocols.actions.*;
import io.camunda.connector.email.protocols.config.Pop3Config;
import io.camunda.connector.generator.java.annotation.NestedProperties;
import io.camunda.connector.generator.java.annotation.TemplateSubType;
Expand All @@ -27,7 +24,8 @@ public final class Pop3 implements Protocol {
@JsonSubTypes(
value = {
@JsonSubTypes.Type(value = Pop3ListEmails.class, name = "listEmailsPop3"),
@JsonSubTypes.Type(value = Pop3ReadEmail.class, name = "readEmailPop3")
@JsonSubTypes.Type(value = Pop3ReadEmail.class, name = "readEmailPop3"),
@JsonSubTypes.Type(value = Pop3DeleteEmail.class, name = "deleteEmailPop3")
})
@Valid
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ public final class Pop3DeleteEmail implements Pop3Action {
binding = @TemplateProperty.PropertyBinding(name = "data.pop3Action.uidlDelete"))
@NotNull
String uidlDelete;

public @NotNull String getUidlDelete() {
return uidlDelete;
}

public void setUidlDelete(@NotNull String uidlDelete) {
this.uidlDelete = uidlDelete;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package io.camunda.connector.email.core;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class JakartaExecutorTest {
Expand Down

0 comments on commit baa9aa6

Please sign in to comment.