james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [08/21] james-project git commit: JAMES-1902 create jmap module
Date Tue, 14 Mar 2017 11:14:49 GMT
http://git-wip-us.apache.org/repos/asf/james-project/blob/3d0e2ae7/server/container/guice/protocols/jmap/src/main/java/org/apache/james/modules/protocols/JMAPServerModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/protocols/jmap/src/main/java/org/apache/james/modules/protocols/JMAPServerModule.java
b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/modules/protocols/JMAPServerModule.java
new file mode 100644
index 0000000..a04e1db
--- /dev/null
+++ b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/modules/protocols/JMAPServerModule.java
@@ -0,0 +1,83 @@
+/****************************************************************
+ * 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.protocols;
+
+import java.security.Security;
+import java.util.List;
+
+import org.apache.james.jmap.JMAPModule;
+import org.apache.james.jmap.JMAPServer;
+import org.apache.james.jmap.crypto.JamesSignatureHandler;
+import org.apache.james.lifecycle.api.Configurable;
+import org.apache.james.utils.ConfigurationPerformer;
+import org.apache.james.utils.GuiceProbe;
+import org.apache.james.utils.JmapGuiceProbe;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+
+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.Singleton;
+import com.google.inject.multibindings.Multibinder;
+
+public class JMAPServerModule extends AbstractModule {
+
+    @Override
+    protected void configure() {
+        install(new JMAPModule());
+        Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(JMAPModuleConfigurationPerformer.class);
+        Multibinder.newSetBinder(binder(), GuiceProbe.class).addBinding().to(JmapGuiceProbe.class);
+    }
+
+    @Singleton
+    public static class JMAPModuleConfigurationPerformer implements ConfigurationPerformer
{
+
+        private final JMAPServer server;
+        private final JamesSignatureHandler signatureHandler;
+
+        @Inject
+        public JMAPModuleConfigurationPerformer(JMAPServer server, JamesSignatureHandler
signatureHandler) {
+            this.server = server;
+            this.signatureHandler = signatureHandler;
+        }
+
+        @Override
+        public void initModule() {
+            try {
+                signatureHandler.init();
+                server.configure(null);
+                registerPEMWithSecurityProvider();
+            } catch (Exception e) {
+                Throwables.propagate(e);
+            }
+        }
+
+        private void registerPEMWithSecurityProvider() {
+            Security.addProvider(new BouncyCastleProvider());
+        }
+
+        @Override
+        public List<Class<? extends Configurable>> forClasses() {
+            return ImmutableList.of(JMAPServer.class);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/3d0e2ae7/server/container/guice/protocols/jmap/src/main/java/org/apache/james/utils/JmapGuiceProbe.java
----------------------------------------------------------------------
diff --git a/server/container/guice/protocols/jmap/src/main/java/org/apache/james/utils/JmapGuiceProbe.java
b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/utils/JmapGuiceProbe.java
new file mode 100644
index 0000000..627d1c6
--- /dev/null
+++ b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/utils/JmapGuiceProbe.java
@@ -0,0 +1,78 @@
+/****************************************************************
+ * 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 java.util.Arrays;
+
+import javax.inject.Inject;
+
+import org.apache.james.jmap.JMAPServer;
+import org.apache.james.jmap.api.vacation.AccountId;
+import org.apache.james.jmap.api.vacation.Vacation;
+import org.apache.james.jmap.api.vacation.VacationPatch;
+import org.apache.james.jmap.api.vacation.VacationRepository;
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageIdManager;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxId;
+import org.apache.james.mailbox.model.MessageId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class JmapGuiceProbe implements GuiceProbe {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(JmapGuiceProbe.class);
+
+    private final VacationRepository vacationRepository;
+    private final JMAPServer jmapServer;
+    private final MessageIdManager messageIdManager;
+    private final MailboxManager mailboxManager;
+
+    @Inject
+    private JmapGuiceProbe(VacationRepository vacationRepository, JMAPServer jmapServer,
MessageIdManager messageIdManager, MailboxManager mailboxManager) {
+        this.vacationRepository = vacationRepository;
+        this.jmapServer = jmapServer;
+        this.messageIdManager = messageIdManager;
+        this.mailboxManager = mailboxManager;
+    }
+
+    public int getJmapPort() {
+        return jmapServer.getPort();
+    }
+
+    public void addMailboxListener(MailboxListener listener) throws MailboxException {
+        mailboxManager.addGlobalListener(listener, mailboxManager.createSystemSession("jmap",
LOGGER));
+    }
+
+    public void modifyVacation(AccountId accountId, VacationPatch vacationPatch) {
+        vacationRepository.modifyVacation(accountId, vacationPatch).join();
+    }
+
+    public Vacation retrieveVacation(AccountId accountId) {
+        return vacationRepository.retrieveVacation(accountId).join();
+    }
+
+    public void setInMailboxes(MessageId messageId, String username, MailboxId... mailboxIds)
throws MailboxException {
+        MailboxSession mailboxSession = mailboxManager.createSystemSession(username, LOGGER);
+        messageIdManager.setInMailboxes(messageId, Arrays.asList(mailboxIds), mailboxSession);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/3d0e2ae7/server/container/guice/protocols/jmap/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/protocols/jmap/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java
b/server/container/guice/protocols/jmap/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java
new file mode 100644
index 0000000..0e1af99
--- /dev/null
+++ b/server/container/guice/protocols/jmap/src/test/java/org/apache/james/jmap/MailetPreconditionTest.java
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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/3d0e2ae7/server/container/guice/protocols/jmap/src/test/java/org/apache/james/modules/TestJMAPServerModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/protocols/jmap/src/test/java/org/apache/james/modules/TestJMAPServerModule.java
b/server/container/guice/protocols/jmap/src/test/java/org/apache/james/modules/TestJMAPServerModule.java
new file mode 100644
index 0000000..6512fac
--- /dev/null
+++ b/server/container/guice/protocols/jmap/src/test/java/org/apache/james/modules/TestJMAPServerModule.java
@@ -0,0 +1,68 @@
+/****************************************************************
+ * 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;
+
+import java.io.FileNotFoundException;
+import java.util.Optional;
+
+import javax.inject.Singleton;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.james.jmap.JMAPConfiguration;
+import org.apache.james.jmap.methods.GetMessageListMethod;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Provides;
+import com.google.inject.name.Names;
+
+public class TestJMAPServerModule extends AbstractModule{
+
+    private static final String PUBLIC_PEM_KEY = "-----BEGIN PUBLIC KEY-----\n" +
+            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtlChO/nlVP27MpdkG0Bh\n" +
+            "16XrMRf6M4NeyGa7j5+1UKm42IKUf3lM28oe82MqIIRyvskPc11NuzSor8HmvH8H\n" +
+            "lhDs5DyJtx2qp35AT0zCqfwlaDnlDc/QDlZv1CoRZGpQk1Inyh6SbZwYpxxwh0fi\n" +
+            "+d/4RpE3LBVo8wgOaXPylOlHxsDizfkL8QwXItyakBfMO6jWQRrj7/9WDhGf4Hi+\n" +
+            "GQur1tPGZDl9mvCoRHjFrD5M/yypIPlfMGWFVEvV5jClNMLAQ9bYFuOc7H1fEWw6\n" +
+            "U1LZUUbJW9/CH45YXz82CYqkrfbnQxqRb2iVbVjs/sHopHd1NTiCfUtwvcYJiBVj\n" +
+            "kwIDAQAB\n" +
+            "-----END PUBLIC KEY-----";
+
+    private final int maximumLimit;
+
+    public TestJMAPServerModule(int maximumLimit) {
+        this.maximumLimit = maximumLimit;
+    }
+
+    @Override
+    protected void configure() {
+        bindConstant().annotatedWith(Names.named(GetMessageListMethod.MAXIMUM_LIMIT)).to(maximumLimit);
+    }
+
+    @Provides
+    @Singleton
+    JMAPConfiguration provideConfiguration() throws FileNotFoundException, ConfigurationException{
+        return JMAPConfiguration.builder()
+                .keystore("keystore")
+                .secret("james72laBalle")
+                .jwtPublicKeyPem(Optional.of(PUBLIC_PEM_KEY))
+                .randomPort()
+                .build();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/3d0e2ae7/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/pom.xml
b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/pom.xml
index 13c1463..cc96dd9 100644
--- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/pom.xml
+++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/pom.xml
@@ -185,6 +185,12 @@
                 </dependency>
                 <dependency>
                     <groupId>org.apache.james</groupId>
+                    <artifactId>james-server-guice-jmap</artifactId>
+                    <scope>test</scope>
+                    <type>test-jar</type>
+                </dependency>
+                <dependency>
+                    <groupId>org.apache.james</groupId>
                     <artifactId>james-server-jmap-integration-testing</artifactId>
                     <type>test-jar</type>
                     <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/james-project/blob/3d0e2ae7/server/protocols/webadmin-integration-test/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin-integration-test/pom.xml b/server/protocols/webadmin-integration-test/pom.xml
index 5f3d9db..0817987 100644
--- a/server/protocols/webadmin-integration-test/pom.xml
+++ b/server/protocols/webadmin-integration-test/pom.xml
@@ -166,6 +166,12 @@
                 </dependency>
                 <dependency>
                     <groupId>org.apache.james</groupId>
+                    <artifactId>james-server-guice-jmap</artifactId>
+                    <type>test-jar</type>
+                    <scope>test</scope>
+                </dependency>
+                <dependency>
+                    <groupId>org.apache.james</groupId>
                     <artifactId>james-server-webadmin</artifactId>
                     <scope>test</scope>
                 </dependency>


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message