chibenwa commented on code in PR #2405: URL: https://github.com/apache/james-project/pull/2405#discussion_r1757154760
########## server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SubAddressing.java: ########## @@ -0,0 +1,106 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.mailets; + +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.MailboxConstants; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.metrics.api.MetricFactory; +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 com.github.fge.lambdas.Throwing; +import com.google.common.collect.ImmutableList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; + +import static org.apache.james.mailbox.MessageManager.MailboxMetaData.RecentMode.IGNORE; + +public class SubAddressing extends GenericMailet { + private final UsersRepository usersRepository; + private final MailboxManager mailboxManager; + private final MetricFactory metricFactory; + + private static final Logger LOG = LoggerFactory.getLogger(SubAddressing.class); + + @Inject + public SubAddressing(UsersRepository usersRepository, @Named("mailboxmanager") MailboxManager mailboxManager, MetricFactory metricFactory) { + this.usersRepository = usersRepository; + this.mailboxManager = mailboxManager; + this.metricFactory = metricFactory; + } + + @Override + public void service(Mail mail) throws MessagingException { + mail.getRecipients().forEach(recipient -> + recipient.getLocalPartDetails(UsersRepository.LOCALPART_DETAIL_DELIMITER) + .ifPresent( + Throwing.consumer(targetFolder -> postIfHasRight(mail, recipient, targetFolder)) + ) + ); + } + + private void postIfHasRight(Mail mail, MailAddress recipient, String targetFolder) throws UsersRepositoryException { + if(hasPostRight(recipient, targetFolder)) { + StorageDirective.builder().targetFolders(ImmutableList.of(targetFolder)).build() + .encodeAsAttributes(usersRepository.getUsername(recipient)) + .forEach(mail::setAttribute); + } else { + LOG.info("{} tried to address {}'s subfolder `{}` but did not have the right to", mail.getMaybeSender().toString(), recipient, targetFolder); + } + } + + private boolean hasPostRight(MailAddress recipient, String targetFolder) { + + try { + Username recipientUsername = Username.fromLocalPartWithoutDomain(recipient.getLocalPartWithoutDetails(UsersRepository.LOCALPART_DETAIL_DELIMITER)); + MailboxSession session = mailboxManager.createSystemSession(recipientUsername); + + MailboxACL aclCommand = mailboxManager + .getMailbox(MailboxPath.forUser(recipientUsername, targetFolder), session) + .getMetaData(IGNORE, session, MessageManager.MailboxMetaData.FetchGroup.NO_COUNT) + .getACL(); + + return aclCommand.getEntries().containsKey(MailboxACL.ANYBODY_KEY) + && aclCommand.getEntries() + .get(MailboxACL.ANYBODY_KEY) + .equals(MailboxACL.Rfc4314Rights.of(Collections.singletonList(MailboxACL.Right.Post))); + + } catch (MailboxException e) { + return false; Review Comment: Error handling is hard... Problem 1: we are silenting potential problems here. It's bad! We likely shall log something when recovering from an exception. Problem 2: recovering makes sense when facing MailboxNotFoundException -> OK But mailbox exception is wider than this and includes other class of issue. Would it make sense to also recover from those? Or would ot be better to forward issues for problem we do not understand so that they get later retried? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
