SLING-5655 Use Commons Messaging in Fling sample
add some messaging and SMTP (wip)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1738384 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fling/pom.xml b/fling/pom.xml
index 71716a2..3948057 100644
--- a/fling/pom.xml
+++ b/fling/pom.xml
@@ -63,6 +63,24 @@
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.annotation</artifactId>
+ <version>6.0.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.component.annotations</artifactId>
+ <version>1.3.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.metatype.annotations</artifactId>
+ <version>1.3.0</version>
+ <scope>provided</scope>
+ </dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
@@ -79,6 +97,12 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.messaging</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
<version>1.2.2</version>
<scope>provided</scope>
@@ -102,6 +126,13 @@
<version>2.12.1</version>
<scope>provided</scope>
</dependency>
+ <!-- SubEtha SMTP -->
+ <dependency>
+ <groupId>org.subethamail</groupId>
+ <artifactId>subethasmtp</artifactId>
+ <version>3.1.7</version>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
<build>
@@ -131,6 +162,7 @@
<configuration>
<instructions>
<Bundle-Activator>org.apache.sling.samples.fling.internal.Activator</Bundle-Activator>
+ <Embed-Dependency>*;scope=compile;inline=true</Embed-Dependency>
<Sling-Bundle-Resources>
/fling/assets;path:=/assets
</Sling-Bundle-Resources>
diff --git a/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java b/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java
new file mode 100644
index 0000000..d7d30cd
--- /dev/null
+++ b/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java
@@ -0,0 +1,29 @@
+/*
+ * 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.sling.samples.fling;
+
+import java.util.List;
+
+import javax.mail.internet.MimeMessage;
+
+public interface SmtpService {
+
+ List<MimeMessage> getMessages();
+
+}
diff --git a/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java b/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java
new file mode 100644
index 0000000..a01dac6
--- /dev/null
+++ b/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java
@@ -0,0 +1,74 @@
+/*
+ * 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.sling.samples.fling.internal;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.sling.commons.messaging.MessageService;
+import org.apache.sling.commons.messaging.Result;
+import org.apache.sling.samples.fling.SmtpService;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(
+ property = {
+ Constants.SERVICE_DESCRIPTION + "=Apache Sling Fling Sample “Message Sender”",
+ Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+ },
+ immediate = true
+)
+public class MessageSender {
+
+ // depend on SmtpService so MessageService can deliver the messages
+ @Reference
+ private SmtpService smtpService;
+
+ @Reference
+ private MessageService messageService;
+
+ private final Logger logger = LoggerFactory.getLogger(MessageSender.class);
+
+ public MessageSender() {
+ }
+
+ @Activate
+ public void activate() throws ExecutionException, InterruptedException {
+ logger.info("activate()");
+ for (int i = 0; i < 10; i++) {
+ final String message = "a simple text message";
+ final String recipient = "form@fling";
+ final String subject = String.format("message number %s", i);
+ final Map configuration = Collections.singletonMap("mail.subject", subject);
+ final CompletableFuture<Result> future = messageService.send(message, recipient, Collections.singletonMap("mail", configuration));
+ future.thenAccept(result -> {
+ final byte[] bytes = (byte[]) result.getMessage();
+ logger.debug("message sent: {}", new String(bytes, StandardCharsets.UTF_8));
+ });
+ }
+ }
+
+}
diff --git a/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.java b/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.java
new file mode 100644
index 0000000..03b91a7
--- /dev/null
+++ b/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.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.sling.samples.fling.internal;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.sling.samples.fling.SmtpService;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.metatype.annotations.Designate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.subethamail.wiser.Wiser;
+import org.subethamail.wiser.WiserMessage;
+
+@Component(
+ service = SmtpService.class,
+ property = {
+ Constants.SERVICE_DESCRIPTION + "=Apache Sling Fling Sample “Wiser SMTP Service”",
+ Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+ },
+ immediate = true
+)
+@Designate(
+ ocd = WiserSmtpServiceConfiguration.class
+)
+public class WiserSmtpService implements SmtpService {
+
+ private Wiser wiser;
+
+ private final Logger logger = LoggerFactory.getLogger(WiserSmtpService.class);
+
+ @Activate
+ public void activate(final WiserSmtpServiceConfiguration configuration) throws Exception {
+ wiser = new Wiser(configuration.smtpPort());
+ wiser.start();
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ wiser.stop();
+ wiser = null;
+ }
+
+ @Override
+ public List<MimeMessage> getMessages() {
+ final List<MimeMessage> messages = new ArrayList<>();
+ if (wiser != null) {
+ for (final WiserMessage message : wiser.getMessages()) {
+ try {
+ messages.add(message.getMimeMessage());
+ } catch (MessagingException e) {
+ logger.error("error getting message from server: {}", e.getMessage(), e);
+ }
+ }
+ }
+ return messages;
+ }
+
+}
diff --git a/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java b/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java
new file mode 100644
index 0000000..958b910
--- /dev/null
+++ b/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java
@@ -0,0 +1,36 @@
+/*
+ * 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.sling.samples.fling.internal;
+
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@ObjectClassDefinition(
+ name = "Apache Sling Fling Sample “Wiser SMTP Service”",
+ description = "SMTP server receiving mails from Message Service"
+)
+@interface WiserSmtpServiceConfiguration {
+
+ @AttributeDefinition(
+ name = "SMTP port",
+ description = "port of SMTP server"
+ )
+ int smtpPort() default 8025;
+
+}
diff --git a/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java b/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java
new file mode 100644
index 0000000..0abe860
--- /dev/null
+++ b/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java
@@ -0,0 +1,43 @@
+/*
+ * 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.sling.samples.fling.page;
+
+import java.util.List;
+
+import javax.mail.internet.MimeMessage;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.injectorspecific.OSGiService;
+import org.apache.sling.samples.fling.SmtpService;
+
+@Model(adaptables = Resource.class)
+public class MessagesPage extends Page {
+
+ @OSGiService
+ private SmtpService smtpService;
+
+ public MessagesPage() {
+ }
+
+ public List<MimeMessage> getMessages() {
+ return smtpService.getMessages();
+ }
+
+}
diff --git a/fling/src/main/resources/apps/fling/page/messages/html.html b/fling/src/main/resources/apps/fling/page/messages/html.html
new file mode 100644
index 0000000..f50a72c
--- /dev/null
+++ b/fling/src/main/resources/apps/fling/page/messages/html.html
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<!--
+ 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.
+-->
+<html data-th-with="page=${resource.adaptTo(@org.apache.sling.samples.fling.page.MessagesPage@class)}">
+<head data-th-replace="/apps/fling/page/fragments/head.html::default"></head>
+<body>
+<div class="container">
+ <nav class="nav-main">
+ <div class="pull-left">
+ <ul class="breadcrumb" style="direction: rtl" data-th-replace="/apps/fling/page/fragments/navigation.html::breadcrumb"></ul>
+ </div>
+ <ul class="nav nav-pills" role="tablist" data-th-replace="/apps/fling/page/fragments/navigation.html::children"></ul>
+ </nav>
+ <div class="panel panel-primary">
+ <div class="panel-heading">
+ <span data-th-text="${page.title}">MESSAGES PAGE</span>
+ </div>
+ <div class="panel-body">
+ <table data-th-with="messages=${page.messages}" class="table">
+ <thead>
+ <tr>
+ <td></td>
+ <td>Subject</td>
+ <td>From</td>
+ <td>Message ID</td>
+ </tr>
+ </thead>
+ <tbody>
+ <tr data-th-each="message,status : ${messages}">
+ <td data-th-text="${status.count}">count</td>
+ <td data-th-text="${message.subject}">subject</td>
+ <td data-th-text="${message.from[0].address}">from</td>
+ <td data-th-text="${message.messageID}">message id</td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ <div class="panel-footer" data-th-replace="/apps/fling/page/fragments/panel.html::footer"></div>
+ </div>
+</div>
+</body>
+</html>
diff --git a/fling/src/main/resources/content/fling.json b/fling/src/main/resources/content/fling.json
index 3464884..71db514 100644
--- a/fling/src/main/resources/content/fling.json
+++ b/fling/src/main/resources/content/fling.json
@@ -25,6 +25,12 @@
"title": "Sling Scripting Thymeleaf",
"content": "<p><em>Sling Scripting Thymeleaf</em> with <em>Sling Models</em> and <code>adaptTo()</code> to render resources:</p><pre><html data-th-with=\"page=${resource.adaptTo(@org.apache.sling.samples.fling.Page@class)}\">\n<head>\n <meta charset=\"UTF-8\"/>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\n <title data-th-text=\"${page.title}\">Page Title</title>\n[...]</pre><p><em>Sling Scripting Thymeleaf</em> with <em>Sling i18n</em> for localized messages:</p><pre><a href=\"/system/sling/logout\" data-th-text=\"#{action.logout}\">logout</a></pre><pre>{\n \"en\": {\n \"jcr:mixinTypes\": [\n \"mix:language\"\n ],\n \"jcr:language\": \"en\",\n \"sling:basename\": \"org.apache.sling.samples.fling\",\n \"action.logout\": {\n \"jcr:primaryType\": \"sling:MessageEntry\",\n \"sling:key\": \"action.logout\",\n \"sling:message\": \"logout\"\n },\n [...]\n }\n}\n</pre>"
},
+ "messaging": {
+ "jcr:primaryType": "nt:unstructured",
+ "sling:resourceType": "fling/page/messages",
+ "sling:resourceSuperType": "fling/page",
+ "title": "Sling Commons Messaging"
+ },
"auth": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "fling/page/simple",