You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ol...@apache.org on 2016/04/09 21:28:35 UTC

svn commit: r1738384 - in /sling/trunk/samples/fling: ./ src/main/java/org/apache/sling/samples/fling/ src/main/java/org/apache/sling/samples/fling/internal/ src/main/java/org/apache/sling/samples/fling/page/ src/main/resources/apps/fling/page/messages...

Author: olli
Date: Sat Apr  9 19:28:35 2016
New Revision: 1738384

URL: http://svn.apache.org/viewvc?rev=1738384&view=rev
Log:
SLING-5655 Use Commons Messaging in Fling sample

add some messaging and SMTP (wip)

Added:
    sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java
    sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java
    sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.java
    sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java
    sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java
    sling/trunk/samples/fling/src/main/resources/apps/fling/page/messages/
    sling/trunk/samples/fling/src/main/resources/apps/fling/page/messages/html.html
Modified:
    sling/trunk/samples/fling/pom.xml
    sling/trunk/samples/fling/src/main/resources/content/fling.json

Modified: sling/trunk/samples/fling/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/pom.xml?rev=1738384&r1=1738383&r2=1738384&view=diff
==============================================================================
--- sling/trunk/samples/fling/pom.xml (original)
+++ sling/trunk/samples/fling/pom.xml Sat Apr  9 19:28:35 2016
@@ -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>

Added: sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java?rev=1738384&view=auto
==============================================================================
--- sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java (added)
+++ sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/SmtpService.java Sat Apr  9 19:28:35 2016
@@ -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();
+
+}

Added: sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java?rev=1738384&view=auto
==============================================================================
--- sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java (added)
+++ sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/MessageSender.java Sat Apr  9 19:28:35 2016
@@ -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));
+            });
+        }
+    }
+
+}

Added: sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.java?rev=1738384&view=auto
==============================================================================
--- sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.java (added)
+++ sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpService.java Sat Apr  9 19:28:35 2016
@@ -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;
+    }
+
+}

Added: sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java?rev=1738384&view=auto
==============================================================================
--- sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java (added)
+++ sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/WiserSmtpServiceConfiguration.java Sat Apr  9 19:28:35 2016
@@ -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;
+
+}

Added: sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java?rev=1738384&view=auto
==============================================================================
--- sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java (added)
+++ sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/page/MessagesPage.java Sat Apr  9 19:28:35 2016
@@ -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();
+    }
+
+}

Added: sling/trunk/samples/fling/src/main/resources/apps/fling/page/messages/html.html
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/resources/apps/fling/page/messages/html.html?rev=1738384&view=auto
==============================================================================
--- sling/trunk/samples/fling/src/main/resources/apps/fling/page/messages/html.html (added)
+++ sling/trunk/samples/fling/src/main/resources/apps/fling/page/messages/html.html Sat Apr  9 19:28:35 2016
@@ -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>

Modified: sling/trunk/samples/fling/src/main/resources/content/fling.json
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/resources/content/fling.json?rev=1738384&r1=1738383&r2=1738384&view=diff
==============================================================================
--- sling/trunk/samples/fling/src/main/resources/content/fling.json (original)
+++ sling/trunk/samples/fling/src/main/resources/content/fling.json Sat Apr  9 19:28:35 2016
@@ -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>&lt;html data-th-with=\"page=${resource.adaptTo(@org.apache.sling.samples.fling.Page@class)}\"&gt;\n&lt;head&gt;\n  &lt;meta charset=\"UTF-8\"/&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/&gt;\n  &lt;title data-th-text=\"${page.title}\"&gt;Page Title&lt;/title&gt;\n[...]</pre><p><em>Sling Scripting Thymeleaf</em> with <em>Sling i18n</em> for localized messages:</p><pre>&lt;a href=\"/system/sling/logout\" data-th-text=\"#{action.logout}\"&gt;logout&lt;/a&gt;</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            \"slin
 g: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",