florentos17 commented on code in PR #2405: URL: https://github.com/apache/james-project/pull/2405#discussion_r1758984076
########## server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SubAddressing.java: ########## @@ -19,33 +19,86 @@ package org.apache.james.transport.mailets; -import com.github.fge.lambdas.Throwing; -import com.google.common.collect.ImmutableList; +import static org.apache.james.mailbox.MessageManager.MailboxMetaData.RecentMode.IGNORE; + +import java.util.Optional; + import jakarta.inject.Inject; +import jakarta.inject.Named; import jakarta.mail.MessagingException; + import org.apache.james.core.MailAddress; +import org.apache.james.core.Username; +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageManager; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.exception.MailboxNotFoundException; +import org.apache.james.mailbox.model.MailboxACL; +import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.user.api.UsersRepository; +import org.apache.james.user.api.UsersRepositoryException; import org.apache.mailet.Mail; import org.apache.mailet.StorageDirective; import org.apache.mailet.base.GenericMailet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.fge.lambdas.Throwing; +import com.google.common.collect.ImmutableList; + + public class SubAddressing extends GenericMailet { private final UsersRepository usersRepository; + private final MailboxManager mailboxManager; + + private static final Logger LOG = LoggerFactory.getLogger(SubAddressing.class); @Inject - public SubAddressing(UsersRepository usersRepository) { + public SubAddressing(UsersRepository usersRepository, @Named("mailboxmanager") MailboxManager mailboxManager) { this.usersRepository = usersRepository; + this.mailboxManager = mailboxManager; } @Override public void service(Mail mail) throws MessagingException { mail.getRecipients().forEach(recipient -> - recipient.getLocalPartDetails("+") - .ifPresent( - Throwing.consumer(details -> - StorageDirective.builder().targetFolders(ImmutableList.of(details)).build() - .encodeAsAttributes(usersRepository.getUsername(recipient)) - .forEach(mail::setAttribute)) - )); + recipient.getLocalPartDetails(UsersRepository.LOCALPART_DETAIL_DELIMITER) + .ifPresent(Throwing.consumer(targetFolder -> postIfHasRight(mail, recipient, targetFolder)))); Review Comment: it's not as simple: 1. for the `LOG.info`, `targetFolder` cannot be resolved as the `Else` part is a `Runnable` and not a `Consumer`. We could maybe log a message without specifying the targetFolder, or even remove the log message at all. 2. `service()` cannot throw a MailboxException, so it cannot call `hasPostRight()` which does. I tried to do something like `.filter(ThrowingPredicate(targetFolder -> hasPostRight(mail, recipient, targetFolder)))` but it doesn't work, as targetFolder is no longer considered a String... to fix these issues, I've come up with: ``` recipient.getLocalPartDetails(UsersRepository.LOCALPART_DETAIL_DELIMITER) .ifPresent(Throwing.consumer(targetFolder -> { if (hasPostRight(mail, recipient, targetFolder)) { post(mail, recipient, targetFolder); } else { LOG.info("{} tried to address {}'s subfolder `{}` but did not have the right to", mail.getMaybeSender().toString(), recipient, targetFolder); } }))); ``` but it's a mix of Streams and if/else... which makes me want to do a method extraction, and that would bring us right back at the start... what do you suggest ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org