JAMES-1902 create mailet module
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5e965d82 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5e965d82 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5e965d82 Branch: refs/heads/master Commit: 5e965d829fbfa4d0f0af5fc824ae9215a95a78a2 Parents: dc03251 Author: Antoine Duprat <adup...@linagora.com> Authored: Tue Mar 7 11:12:42 2017 +0100 Committer: benwa <btell...@linagora.com> Committed: Tue Mar 14 18:13:55 2017 +0700 ---------------------------------------------------------------------- server/container/guice/guice-common/pom.xml | 16 +- .../server/CamelMailetContainerModule.java | 187 ------------------ .../apache/james/utils/GuiceGenericLoader.java | 47 ----- .../apache/james/utils/GuiceMailetLoader.java | 53 ----- .../apache/james/utils/GuiceMatcherLoader.java | 53 ----- .../james/jmap/MailetPreconditionTest.java | 122 ------------ server/container/guice/mailet/pom.xml | 192 +++++++++++++++++++ .../server/CamelMailetContainerModule.java | 187 ++++++++++++++++++ .../apache/james/utils/GuiceGenericLoader.java | 47 +++++ .../apache/james/utils/GuiceMailetLoader.java | 53 +++++ .../apache/james/utils/GuiceMatcherLoader.java | 53 +++++ server/container/guice/pom.xml | 6 + 12 files changed, 542 insertions(+), 474 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/guice-common/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/pom.xml b/server/container/guice/guice-common/pom.xml index 1576d90..6071182 100644 --- a/server/container/guice/guice-common/pom.xml +++ b/server/container/guice/guice-common/pom.xml @@ -137,10 +137,6 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>apache-mailet-standard</artifactId> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> <artifactId>james-server-cli</artifactId> </dependency> <dependency> @@ -197,6 +193,10 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>james-server-guice-mailet</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>james-server-guice-managedsieve</artifactId> </dependency> <dependency> @@ -212,14 +212,6 @@ <artifactId>james-server-mailets</artifactId> </dependency> <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>james-server-mailetcontainer-api</artifactId> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>james-server-mailetcontainer-camel</artifactId> - </dependency> - <dependency> <groupId>org.apache.james</groupId> <artifactId>metrics-api</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java deleted file mode 100644 index 34a64a5..0000000 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java +++ /dev/null @@ -1,187 +0,0 @@ -/**************************************************************** - * 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.modules.server; - -import java.util.List; -import java.util.Optional; -import java.util.Set; - -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.impl.SimpleRegistry; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.james.dnsservice.api.DNSService; -import org.apache.james.domainlist.api.DomainList; -import org.apache.james.lifecycle.api.Configurable; -import org.apache.james.mailetcontainer.api.MailProcessor; -import org.apache.james.mailetcontainer.api.MailetLoader; -import org.apache.james.mailetcontainer.api.MatcherLoader; -import org.apache.james.mailetcontainer.api.jmx.MailSpoolerMBean; -import org.apache.james.mailetcontainer.impl.JamesMailSpooler; -import org.apache.james.mailetcontainer.impl.JamesMailetContext; -import org.apache.james.mailetcontainer.impl.MatcherMailetPair; -import org.apache.james.mailetcontainer.impl.camel.CamelCompositeProcessor; -import org.apache.james.mailetcontainer.impl.camel.CamelMailetProcessor; -import org.apache.james.queue.api.MailQueueFactory; -import org.apache.james.transport.mailets.RemoveMimeHeader; -import org.apache.james.transport.matchers.All; -import org.apache.james.user.api.UsersRepository; -import org.apache.james.utils.ConfigurationPerformer; -import org.apache.james.utils.ConfigurationProvider; -import org.apache.james.utils.GuiceMailetLoader; -import org.apache.james.utils.GuiceMatcherLoader; -import org.apache.mailet.MailetContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Preconditions; -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableList; -import com.google.inject.AbstractModule; -import com.google.inject.Inject; -import com.google.inject.Provides; -import com.google.inject.Scopes; -import com.google.inject.Singleton; -import com.google.inject.multibindings.Multibinder; - -public class CamelMailetContainerModule extends AbstractModule { - - private static final Logger CAMEL_LOGGER = LoggerFactory.getLogger(CamelCompositeProcessor.class); - private static final Logger SPOOLER_LOGGER = LoggerFactory.getLogger(JamesMailSpooler.class); - private static final Logger MAILET_LOGGER = LoggerFactory.getLogger(JamesMailetContext.class); - - @Override - protected void configure() { - bind(CamelCompositeProcessor.class).in(Scopes.SINGLETON); - bind(MailProcessor.class).to(CamelCompositeProcessor.class); - - bind(JamesMailSpooler.class).in(Scopes.SINGLETON); - bind(MailSpoolerMBean.class).to(JamesMailSpooler.class); - - bind(MailetLoader.class).to(GuiceMailetLoader.class); - bind(MatcherLoader.class).to(GuiceMatcherLoader.class); - - Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(MailetModuleConfigurationPerformer.class); - - Multibinder<CamelMailetContainerModule.TransportProcessorCheck> transportProcessorChecks = Multibinder.newSetBinder(binder(), CamelMailetContainerModule.TransportProcessorCheck.class); - transportProcessorChecks.addBinding().to(BccMailetCheck.class); - } - - @Provides - @Singleton - private MailetContext provideMailetContext(MailQueueFactory mailQueueFactory, - DNSService dns, - UsersRepository localusers, - DomainList domains) { - JamesMailetContext jamesMailetContext = new JamesMailetContext(); - jamesMailetContext.setLog(MAILET_LOGGER); - jamesMailetContext.setDNSService(dns); - jamesMailetContext.retrieveRootMailQueue(mailQueueFactory); - jamesMailetContext.setUsersRepository(localusers); - jamesMailetContext.setDomainList(domains); - return jamesMailetContext; - } - - @Singleton - public static class MailetModuleConfigurationPerformer implements ConfigurationPerformer { - - private final ConfigurationProvider configurationProvider; - private final CamelCompositeProcessor camelCompositeProcessor; - private final JamesMailSpooler jamesMailSpooler; - private final JamesMailetContext mailetContext; - private final MailQueueFactory mailQueueFactory; - private Set<TransportProcessorCheck> transportProcessorCheckSet; - - @Inject - public MailetModuleConfigurationPerformer(ConfigurationProvider configurationProvider, - CamelCompositeProcessor camelCompositeProcessor, - JamesMailSpooler jamesMailSpooler, - JamesMailetContext mailetContext, - MailQueueFactory mailQueueFactory, - Set<TransportProcessorCheck> transportProcessorCheckSet) { - this.configurationProvider = configurationProvider; - this.camelCompositeProcessor = camelCompositeProcessor; - this.jamesMailSpooler = jamesMailSpooler; - this.mailetContext = mailetContext; - this.mailQueueFactory = mailQueueFactory; - this.transportProcessorCheckSet = transportProcessorCheckSet; - } - - @Override - public void initModule() { - try { - DefaultCamelContext camelContext = new DefaultCamelContext(); - camelContext.disableJMX(); - camelContext.setRegistry(new SimpleRegistry()); - camelCompositeProcessor.setLog(CAMEL_LOGGER); - camelCompositeProcessor.setCamelContext(camelContext); - camelCompositeProcessor.configure(configurationProvider.getConfiguration("mailetcontainer").configurationAt("processors")); - camelCompositeProcessor.init(); - checkProcessors(); - jamesMailSpooler.setMailProcessor(camelCompositeProcessor); - jamesMailSpooler.setLog(SPOOLER_LOGGER); - jamesMailSpooler.configure(configurationProvider.getConfiguration("mailetcontainer").configurationAt("spooler")); - jamesMailSpooler.init(); - mailetContext.setLog(MAILET_LOGGER); - mailetContext.configure(configurationProvider.getConfiguration("mailetcontainer").configurationAt("context")); - mailetContext.retrieveRootMailQueue(mailQueueFactory); - } catch (Exception e) { - throw Throwables.propagate(e); - } - - } - - private void checkProcessors() throws ConfigurationException { - MailProcessor mailProcessor = Optional.ofNullable(camelCompositeProcessor.getProcessor("transport")) - .orElseThrow(() -> new RuntimeException("JMAP needs a transport processor")); - if (mailProcessor instanceof CamelMailetProcessor) { - List<MatcherMailetPair> matcherMailetPairs = ((CamelMailetProcessor) mailProcessor).getPairs(); - for (TransportProcessorCheck check : transportProcessorCheckSet) { - check.check(matcherMailetPairs); - } - } else { - throw new RuntimeException("Can not perform checks as transport processor is not an instance of " + MailProcessor.class); - } - } - - @Override - public List<Class<? extends Configurable>> forClasses() { - return ImmutableList.of(CamelCompositeProcessor.class, JamesMailSpooler.class, JamesMailetContext.class); - } - } - - @FunctionalInterface - public interface TransportProcessorCheck { - void check(List<MatcherMailetPair> pairs) throws ConfigurationException; - } - - public static class BccMailetCheck implements CamelMailetContainerModule.TransportProcessorCheck { - @Override - public void check(List<MatcherMailetPair> pairs) throws ConfigurationException { - Preconditions.checkNotNull(pairs); - pairs.stream() - .filter(pair -> pair.getMailet().getClass().equals(RemoveMimeHeader.class)) - .filter(pair -> pair.getMatcher().getClass().equals(All.class)) - .filter(pair -> pair.getMailet().getMailetConfig().getInitParameter("name").equals("bcc")) - .findAny() - .orElseThrow(() -> new ConfigurationException("Missing RemoveMimeHeader in mailets configuration (mailetcontainer -> processors -> transport). Should be configured to remove Bcc header")); - } - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceGenericLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceGenericLoader.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceGenericLoader.java deleted file mode 100644 index 645d9b0..0000000 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceGenericLoader.java +++ /dev/null @@ -1,47 +0,0 @@ -/**************************************************************** - * 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.utils; - -import com.google.inject.Injector; - -public class GuiceGenericLoader<T> { - - private final Injector injector; - private final String defaultPackageName; - - public GuiceGenericLoader(Injector injector, String defaultPackageName) { - this.injector = injector; - this.defaultPackageName = defaultPackageName; - } - - public T instanciate(String className) throws Exception { - @SuppressWarnings("unchecked") - Class<T> clazz = (Class<T>) ClassLoader.getSystemClassLoader().loadClass(constructFullName(className)); - return injector.getInstance(clazz); - } - - private String constructFullName(String name) { - if (! name.contains(".")) { - return defaultPackageName + name; - } - return name; - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMailetLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMailetLoader.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMailetLoader.java deleted file mode 100644 index f979ac2..0000000 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMailetLoader.java +++ /dev/null @@ -1,53 +0,0 @@ -/**************************************************************** - * 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.utils; - -import javax.mail.MessagingException; - -import org.apache.james.mailetcontainer.api.MailetLoader; -import org.apache.mailet.Mailet; -import org.apache.mailet.MailetConfig; - -import com.google.inject.Inject; -import com.google.inject.Injector; - -public class GuiceMailetLoader implements MailetLoader { - - private static final String STANDARD_PACKAGE = "org.apache.james.transport.mailets."; - - private final GuiceGenericLoader<Mailet> genericLoader; - - @Inject - public GuiceMailetLoader(Injector injector) { - this.genericLoader = new GuiceGenericLoader<>(injector, STANDARD_PACKAGE); - } - - @Override - public Mailet getMailet(MailetConfig config) throws MessagingException { - try { - Mailet result = genericLoader.instanciate(config.getMailetName()); - result.init(config); - return result; - } catch (Exception e) { - throw new MessagingException("Can not load mailet " + config.getMailetName(), e); - } - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java deleted file mode 100644 index 10e7cd6..0000000 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java +++ /dev/null @@ -1,53 +0,0 @@ -/**************************************************************** - * 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.utils; - -import javax.mail.MessagingException; - -import org.apache.james.mailetcontainer.api.MatcherLoader; -import org.apache.mailet.Matcher; -import org.apache.mailet.MatcherConfig; - -import com.google.inject.Inject; -import com.google.inject.Injector; - -public class GuiceMatcherLoader implements MatcherLoader { - - private static final String STANDARD_PACKAGE = "org.apache.james.transport.matchers."; - - private final GuiceGenericLoader<Matcher> genericLoader; - - @Inject - public GuiceMatcherLoader(Injector injector) { - this.genericLoader = new GuiceGenericLoader<>(injector, STANDARD_PACKAGE); - } - - @Override - public Matcher getMatcher(MatcherConfig config) throws MessagingException { - try { - Matcher result = genericLoader.instanciate(config.getMatcherName()); - result.init(config); - return result; - } catch (Exception e) { - throw new MessagingException("Can not load matcher " + config.getMatcherName(), e); - } - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/guice-common/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java b/server/container/guice/guice-common/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java deleted file mode 100644 index 0e1af99..0000000 --- a/server/container/guice/guice-common/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java +++ /dev/null @@ -1,122 +0,0 @@ -/**************************************************************** - * 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.jmap; - -import java.util.List; -import java.util.Properties; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.james.jmap.mailet.VacationMailet; -import org.apache.james.mailetcontainer.impl.MatcherMailetPair; -import org.apache.james.modules.server.CamelMailetContainerModule; -import org.apache.james.transport.mailets.Null; -import org.apache.james.transport.mailets.RemoveMimeHeader; -import org.apache.james.transport.matchers.All; -import org.apache.james.transport.matchers.RecipientIsLocal; -import org.apache.mailet.MailetContext; -import org.apache.mailet.base.test.FakeMailetConfig; -import org.junit.Test; - -import com.google.common.collect.Lists; - -public class MailetPreconditionTest { - - private static final MailetContext MAILET_CONTEXT = null; - private static final String WRONG_NAME = "wrong"; - private static final String BCC = "bcc"; - - @Test(expected = ConfigurationException.class) - public void vacationMailetCheckShouldThrowOnEmptyList() throws Exception { - new JMAPModule.VacationMailetCheck().check(Lists.newArrayList()); - } - - @Test(expected = NullPointerException.class) - public void vacationMailetCheckShouldThrowOnNullList() throws Exception { - new JMAPModule.VacationMailetCheck().check(null); - } - - @Test(expected = ConfigurationException.class) - public void vacationMailetCheckShouldThrowOnWrongMatcher() throws Exception { - List<MatcherMailetPair> pairs = Lists.newArrayList(new MatcherMailetPair(new All(), new VacationMailet(null, null, null, null, null))); - new JMAPModule.VacationMailetCheck().check(pairs); - } - - @Test(expected = ConfigurationException.class) - public void vacationMailetCheckShouldThrowOnWrongMailet() throws Exception { - List<MatcherMailetPair> pairs = Lists.newArrayList(new MatcherMailetPair(new RecipientIsLocal(), new Null())); - new JMAPModule.VacationMailetCheck().check(pairs); - } - - @Test - public void vacationMailetCheckShouldNotThrowIfValidPairPresent() throws Exception { - List<MatcherMailetPair> pairs = Lists.newArrayList(new MatcherMailetPair(new RecipientIsLocal(), new VacationMailet(null, null, null, null, null))); - new JMAPModule.VacationMailetCheck().check(pairs); - } - - @Test(expected = ConfigurationException.class) - public void bccMailetCheckShouldThrowOnEmptyList() throws Exception { - new CamelMailetContainerModule.BccMailetCheck().check(Lists.newArrayList()); - } - - @Test(expected = NullPointerException.class) - public void bccMailetCheckShouldThrowOnNullList() throws Exception { - new CamelMailetContainerModule.BccMailetCheck().check(null); - } - - @Test(expected = ConfigurationException.class) - public void bccMailetCheckShouldThrowOnWrongMatcher() throws Exception { - List<MatcherMailetPair> pairs = Lists.newArrayList(new MatcherMailetPair(new RecipientIsLocal(), new RemoveMimeHeader())); - new JMAPModule.VacationMailetCheck().check(pairs); - } - - @Test(expected = ConfigurationException.class) - public void bccMailetCheckShouldThrowOnWrongMailet() throws Exception { - List<MatcherMailetPair> pairs = Lists.newArrayList(new MatcherMailetPair(new All(), new Null())); - new JMAPModule.VacationMailetCheck().check(pairs); - } - - @Test(expected = ConfigurationException.class) - public void bccMailetCheckShouldThrowOnWrongMailetName() throws Exception { - Properties properties = new Properties(); - properties.setProperty("name", WRONG_NAME); - RemoveMimeHeader removeMimeHeader = new RemoveMimeHeader(); - removeMimeHeader.init(FakeMailetConfig.builder() - .mailetName(WRONG_NAME) - .mailetContext(MAILET_CONTEXT) - .setProperty("name", WRONG_NAME) - .build()); - - List<MatcherMailetPair> pairs = Lists.newArrayList(new MatcherMailetPair(new All(), removeMimeHeader)); - new JMAPModule.VacationMailetCheck().check(pairs); - } - - @Test(expected = ConfigurationException.class) - public void bccMailetCheckShouldNotThrowOnValidPair() throws Exception { - RemoveMimeHeader removeMimeHeader = new RemoveMimeHeader(); - removeMimeHeader.init(FakeMailetConfig.builder() - .mailetName(BCC) - .mailetContext(MAILET_CONTEXT) - .setProperty("name", BCC) - .build()); - - List<MatcherMailetPair> pairs = Lists.newArrayList(new MatcherMailetPair(new All(), removeMimeHeader)); - new JMAPModule.VacationMailetCheck().check(pairs); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/mailet/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/pom.xml b/server/container/guice/mailet/pom.xml new file mode 100644 index 0000000..80edd78 --- /dev/null +++ b/server/container/guice/mailet/pom.xml @@ -0,0 +1,192 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>james-server-guice</artifactId> + <groupId>org.apache.james</groupId> + <version>3.0.0-beta6-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>james-server-guice-mailet</artifactId> + + <name>Apache James :: Server :: Guice :: Mailet</name> + <description>Mailet modules for Guice implementation of James server</description> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + </plugin> + </plugins> + </build> + + <profiles> + <profile> + <id>disable-build-for-older-jdk</id> + <activation> + <jdk>(,1.8)</jdk> + </activation> + <build> + <plugins> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <id>default-jar</id> + <phase>none</phase> + </execution> + <execution> + <id>jar</id> + <phase>none</phase> + </execution> + <execution> + <id>test-jar</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <executions> + <execution> + <id>default-compile</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testCompile</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <executions> + <execution> + <id>default-test</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-source-plugin</artifactId> + <executions> + <execution> + <id>attach-sources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <executions> + <execution> + <id>default-install</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <executions> + <execution> + <id>default-resources</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testResources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <executions> + <execution> + <id>attach-descriptor</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>build-for-jdk-8</id> + <activation> + <jdk>[1.8,)</jdk> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>apache-mailet-standard</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>james-server-guice-configuration</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>james-server-mailetcontainer-api</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>james-server-mailetcontainer-camel</artifactId> + </dependency> + <dependency> + <groupId>com.google.inject</groupId> + <artifactId>guice</artifactId> + </dependency> + <dependency> + <groupId>com.google.inject.extensions</groupId> + <artifactId>guice-multibindings</artifactId> + </dependency> + </dependencies> + </profile> + <profile> + <id>animal-sniffer-java-8</id> + <activation> + <jdk>[1.8,)</jdk> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>animal-sniffer-maven-plugin</artifactId> + <configuration> + <signature> + <groupId>org.codehaus.mojo.signature</groupId> + <artifactId>java18</artifactId> + <version>1.0</version> + </signature> + </configuration> + <executions> + <execution> + <id>check_java_8</id> + <phase>test</phase> + <goals> + <goal>check</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project> http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java b/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java new file mode 100644 index 0000000..34a64a5 --- /dev/null +++ b/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java @@ -0,0 +1,187 @@ +/**************************************************************** + * 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.modules.server; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.SimpleRegistry; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.domainlist.api.DomainList; +import org.apache.james.lifecycle.api.Configurable; +import org.apache.james.mailetcontainer.api.MailProcessor; +import org.apache.james.mailetcontainer.api.MailetLoader; +import org.apache.james.mailetcontainer.api.MatcherLoader; +import org.apache.james.mailetcontainer.api.jmx.MailSpoolerMBean; +import org.apache.james.mailetcontainer.impl.JamesMailSpooler; +import org.apache.james.mailetcontainer.impl.JamesMailetContext; +import org.apache.james.mailetcontainer.impl.MatcherMailetPair; +import org.apache.james.mailetcontainer.impl.camel.CamelCompositeProcessor; +import org.apache.james.mailetcontainer.impl.camel.CamelMailetProcessor; +import org.apache.james.queue.api.MailQueueFactory; +import org.apache.james.transport.mailets.RemoveMimeHeader; +import org.apache.james.transport.matchers.All; +import org.apache.james.user.api.UsersRepository; +import org.apache.james.utils.ConfigurationPerformer; +import org.apache.james.utils.ConfigurationProvider; +import org.apache.james.utils.GuiceMailetLoader; +import org.apache.james.utils.GuiceMatcherLoader; +import org.apache.mailet.MailetContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Preconditions; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; +import com.google.inject.AbstractModule; +import com.google.inject.Inject; +import com.google.inject.Provides; +import com.google.inject.Scopes; +import com.google.inject.Singleton; +import com.google.inject.multibindings.Multibinder; + +public class CamelMailetContainerModule extends AbstractModule { + + private static final Logger CAMEL_LOGGER = LoggerFactory.getLogger(CamelCompositeProcessor.class); + private static final Logger SPOOLER_LOGGER = LoggerFactory.getLogger(JamesMailSpooler.class); + private static final Logger MAILET_LOGGER = LoggerFactory.getLogger(JamesMailetContext.class); + + @Override + protected void configure() { + bind(CamelCompositeProcessor.class).in(Scopes.SINGLETON); + bind(MailProcessor.class).to(CamelCompositeProcessor.class); + + bind(JamesMailSpooler.class).in(Scopes.SINGLETON); + bind(MailSpoolerMBean.class).to(JamesMailSpooler.class); + + bind(MailetLoader.class).to(GuiceMailetLoader.class); + bind(MatcherLoader.class).to(GuiceMatcherLoader.class); + + Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(MailetModuleConfigurationPerformer.class); + + Multibinder<CamelMailetContainerModule.TransportProcessorCheck> transportProcessorChecks = Multibinder.newSetBinder(binder(), CamelMailetContainerModule.TransportProcessorCheck.class); + transportProcessorChecks.addBinding().to(BccMailetCheck.class); + } + + @Provides + @Singleton + private MailetContext provideMailetContext(MailQueueFactory mailQueueFactory, + DNSService dns, + UsersRepository localusers, + DomainList domains) { + JamesMailetContext jamesMailetContext = new JamesMailetContext(); + jamesMailetContext.setLog(MAILET_LOGGER); + jamesMailetContext.setDNSService(dns); + jamesMailetContext.retrieveRootMailQueue(mailQueueFactory); + jamesMailetContext.setUsersRepository(localusers); + jamesMailetContext.setDomainList(domains); + return jamesMailetContext; + } + + @Singleton + public static class MailetModuleConfigurationPerformer implements ConfigurationPerformer { + + private final ConfigurationProvider configurationProvider; + private final CamelCompositeProcessor camelCompositeProcessor; + private final JamesMailSpooler jamesMailSpooler; + private final JamesMailetContext mailetContext; + private final MailQueueFactory mailQueueFactory; + private Set<TransportProcessorCheck> transportProcessorCheckSet; + + @Inject + public MailetModuleConfigurationPerformer(ConfigurationProvider configurationProvider, + CamelCompositeProcessor camelCompositeProcessor, + JamesMailSpooler jamesMailSpooler, + JamesMailetContext mailetContext, + MailQueueFactory mailQueueFactory, + Set<TransportProcessorCheck> transportProcessorCheckSet) { + this.configurationProvider = configurationProvider; + this.camelCompositeProcessor = camelCompositeProcessor; + this.jamesMailSpooler = jamesMailSpooler; + this.mailetContext = mailetContext; + this.mailQueueFactory = mailQueueFactory; + this.transportProcessorCheckSet = transportProcessorCheckSet; + } + + @Override + public void initModule() { + try { + DefaultCamelContext camelContext = new DefaultCamelContext(); + camelContext.disableJMX(); + camelContext.setRegistry(new SimpleRegistry()); + camelCompositeProcessor.setLog(CAMEL_LOGGER); + camelCompositeProcessor.setCamelContext(camelContext); + camelCompositeProcessor.configure(configurationProvider.getConfiguration("mailetcontainer").configurationAt("processors")); + camelCompositeProcessor.init(); + checkProcessors(); + jamesMailSpooler.setMailProcessor(camelCompositeProcessor); + jamesMailSpooler.setLog(SPOOLER_LOGGER); + jamesMailSpooler.configure(configurationProvider.getConfiguration("mailetcontainer").configurationAt("spooler")); + jamesMailSpooler.init(); + mailetContext.setLog(MAILET_LOGGER); + mailetContext.configure(configurationProvider.getConfiguration("mailetcontainer").configurationAt("context")); + mailetContext.retrieveRootMailQueue(mailQueueFactory); + } catch (Exception e) { + throw Throwables.propagate(e); + } + + } + + private void checkProcessors() throws ConfigurationException { + MailProcessor mailProcessor = Optional.ofNullable(camelCompositeProcessor.getProcessor("transport")) + .orElseThrow(() -> new RuntimeException("JMAP needs a transport processor")); + if (mailProcessor instanceof CamelMailetProcessor) { + List<MatcherMailetPair> matcherMailetPairs = ((CamelMailetProcessor) mailProcessor).getPairs(); + for (TransportProcessorCheck check : transportProcessorCheckSet) { + check.check(matcherMailetPairs); + } + } else { + throw new RuntimeException("Can not perform checks as transport processor is not an instance of " + MailProcessor.class); + } + } + + @Override + public List<Class<? extends Configurable>> forClasses() { + return ImmutableList.of(CamelCompositeProcessor.class, JamesMailSpooler.class, JamesMailetContext.class); + } + } + + @FunctionalInterface + public interface TransportProcessorCheck { + void check(List<MatcherMailetPair> pairs) throws ConfigurationException; + } + + public static class BccMailetCheck implements CamelMailetContainerModule.TransportProcessorCheck { + @Override + public void check(List<MatcherMailetPair> pairs) throws ConfigurationException { + Preconditions.checkNotNull(pairs); + pairs.stream() + .filter(pair -> pair.getMailet().getClass().equals(RemoveMimeHeader.class)) + .filter(pair -> pair.getMatcher().getClass().equals(All.class)) + .filter(pair -> pair.getMailet().getMailetConfig().getInitParameter("name").equals("bcc")) + .findAny() + .orElseThrow(() -> new ConfigurationException("Missing RemoveMimeHeader in mailets configuration (mailetcontainer -> processors -> transport). Should be configured to remove Bcc header")); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java new file mode 100644 index 0000000..645d9b0 --- /dev/null +++ b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java @@ -0,0 +1,47 @@ +/**************************************************************** + * 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.utils; + +import com.google.inject.Injector; + +public class GuiceGenericLoader<T> { + + private final Injector injector; + private final String defaultPackageName; + + public GuiceGenericLoader(Injector injector, String defaultPackageName) { + this.injector = injector; + this.defaultPackageName = defaultPackageName; + } + + public T instanciate(String className) throws Exception { + @SuppressWarnings("unchecked") + Class<T> clazz = (Class<T>) ClassLoader.getSystemClassLoader().loadClass(constructFullName(className)); + return injector.getInstance(clazz); + } + + private String constructFullName(String name) { + if (! name.contains(".")) { + return defaultPackageName + name; + } + return name; + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java new file mode 100644 index 0000000..f979ac2 --- /dev/null +++ b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java @@ -0,0 +1,53 @@ +/**************************************************************** + * 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.utils; + +import javax.mail.MessagingException; + +import org.apache.james.mailetcontainer.api.MailetLoader; +import org.apache.mailet.Mailet; +import org.apache.mailet.MailetConfig; + +import com.google.inject.Inject; +import com.google.inject.Injector; + +public class GuiceMailetLoader implements MailetLoader { + + private static final String STANDARD_PACKAGE = "org.apache.james.transport.mailets."; + + private final GuiceGenericLoader<Mailet> genericLoader; + + @Inject + public GuiceMailetLoader(Injector injector) { + this.genericLoader = new GuiceGenericLoader<>(injector, STANDARD_PACKAGE); + } + + @Override + public Mailet getMailet(MailetConfig config) throws MessagingException { + try { + Mailet result = genericLoader.instanciate(config.getMailetName()); + result.init(config); + return result; + } catch (Exception e) { + throw new MessagingException("Can not load mailet " + config.getMailetName(), e); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java new file mode 100644 index 0000000..10e7cd6 --- /dev/null +++ b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMatcherLoader.java @@ -0,0 +1,53 @@ +/**************************************************************** + * 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.utils; + +import javax.mail.MessagingException; + +import org.apache.james.mailetcontainer.api.MatcherLoader; +import org.apache.mailet.Matcher; +import org.apache.mailet.MatcherConfig; + +import com.google.inject.Inject; +import com.google.inject.Injector; + +public class GuiceMatcherLoader implements MatcherLoader { + + private static final String STANDARD_PACKAGE = "org.apache.james.transport.matchers."; + + private final GuiceGenericLoader<Matcher> genericLoader; + + @Inject + public GuiceMatcherLoader(Injector injector) { + this.genericLoader = new GuiceGenericLoader<>(injector, STANDARD_PACKAGE); + } + + @Override + public Matcher getMatcher(MatcherConfig config) throws MessagingException { + try { + Matcher result = genericLoader.instanciate(config.getMatcherName()); + result.init(config); + return result; + } catch (Exception e) { + throw new MessagingException("Can not load matcher " + config.getMatcherName(), e); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/5e965d82/server/container/guice/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/pom.xml b/server/container/guice/pom.xml index 7c712cd..f762e63 100644 --- a/server/container/guice/pom.xml +++ b/server/container/guice/pom.xml @@ -56,6 +56,11 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>james-server-guice-mailet</artifactId> + <version>3.0.0-beta6-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>james-server-guice-managedsieve</artifactId> <version>3.0.0-beta6-SNAPSHOT</version> </dependency> @@ -78,6 +83,7 @@ <module>cassandra-guice</module> <module>cassandra-ldap-guice</module> <module>mailbox</module> + <module>mailet</module> <module>memory-guice</module> <module>jpa-guice</module> <module>onami</module> --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org