chibenwa commented on code in PR #2216: URL: https://github.com/apache/james-project/pull/2216#discussion_r1580890525
########## server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/IsInDropList.java: ########## @@ -0,0 +1,70 @@ +/**************************************************************** + * 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.matchers; + +import java.util.Collection; + +import jakarta.inject.Inject; +import jakarta.mail.MessagingException; + +import org.apache.james.core.MailAddress; +import org.apache.james.droplists.api.DropList; +import org.apache.james.droplists.api.OwnerScope; +import org.apache.mailet.Mail; +import org.apache.mailet.base.GenericMatcher; + +import com.google.common.collect.ImmutableList; + +/** + * This matcher that checks if a mail sender is permitted based on their status in the DropList. + * + * <p>Implements the match method to check if the sender of the incoming mail is not listed in the DropList. + * If the sender is not found in the DropList, the matcher will return all recipients of the mail for which the sender is allowed.</p> + * </p>Note:</p> + * managing DropLists can be accomplished through <a href="http://james.apache.org/server/manage-webadmin.html">WebAdmin</a>. + **/ +public class IsInDropList extends GenericMatcher { + + private final DropList dropList; + + @Inject + public IsInDropList(DropList dropList) { + this.dropList = dropList; + } + + @Override + public Collection<MailAddress> match(Mail mail) throws MessagingException { + return mail.getRecipients() + .stream() + .filter(recipient -> isRecipientAllowed(mail, recipient)) + .collect(ImmutableList.toImmutableList()); + } + + private boolean isRecipientAllowed(Mail mail, MailAddress recipient) { + MailAddress sender = mail.getMaybeSender().get(); + return isAllowed(OwnerScope.GLOBAL, recipient.asString(), sender) && + isAllowed(OwnerScope.DOMAIN, recipient.getDomain().asString(), sender) && + isAllowed(OwnerScope.USER, recipient.asString(), sender); + } + + private boolean isAllowed(OwnerScope ownerScope, String owner, MailAddress sender) { + DropList.Status status = dropList.query(ownerScope, owner, sender).block(); Review Comment: Rather than calling 3 time .block we could combine those monos in order to call it olny once and paralelize db access? ########## server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/IsInDropListTest.java: ########## @@ -0,0 +1,134 @@ +package org.apache.james.transport.matchers; Review Comment: You are missing the Apache V2 license -- 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]
