sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From o...@apache.org
Subject svn commit: r1780651 - in /sling/trunk/samples/fling: pom.xml src/main/java/org/apache/sling/samples/fling/internal/FormServlet.java
Date Sat, 28 Jan 2017 09:57:31 GMT
Author: olli
Date: Sat Jan 28 09:57:31 2017
New Revision: 1780651

URL: http://svn.apache.org/viewvc?rev=1780651&view=rev
Log:
SLING-4465 Use new Scripting Thymeleaf features in Fling sample

use service resource resolver

Modified:
    sling/trunk/samples/fling/pom.xml
    sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/FormServlet.java

Modified: sling/trunk/samples/fling/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/pom.xml?rev=1780651&r1=1780650&r2=1780651&view=diff
==============================================================================
--- sling/trunk/samples/fling/pom.xml (original)
+++ sling/trunk/samples/fling/pom.xml Sat Jan 28 09:57:31 2017
@@ -112,6 +112,12 @@
     </dependency>
     <dependency>
       <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.scripting.thymeleaf</artifactId>
+      <version>1.0.1-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
       <artifactId>org.apache.sling.validation.api</artifactId>
       <version>1.0.0-SNAPSHOT</version>
       <scope>provided</scope>

Modified: sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/FormServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/FormServlet.java?rev=1780651&r1=1780650&r2=1780651&view=diff
==============================================================================
--- sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/FormServlet.java
(original)
+++ sling/trunk/samples/fling/src/main/java/org/apache/sling/samples/fling/internal/FormServlet.java
Sat Jan 28 09:57:31 2017
@@ -19,7 +19,7 @@
 package org.apache.sling.samples.fling.internal;
 
 import java.io.IOException;
-import java.util.HashMap;
+import java.util.Collections;
 import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
@@ -33,8 +33,9 @@ import javax.servlet.ServletException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.api.resource.ValueMap;
-import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
 import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
@@ -42,6 +43,7 @@ import org.apache.sling.commons.messagin
 import org.apache.sling.commons.messaging.Result;
 import org.apache.sling.samples.fling.form.Form;
 import org.apache.sling.samples.fling.form.FormFactory;
+import org.apache.sling.scripting.thymeleaf.DefaultSlingContext;
 import org.apache.sling.validation.ValidationResult;
 import org.apache.sling.validation.ValidationService;
 import org.apache.sling.validation.model.ValidationModel;
@@ -53,7 +55,6 @@ import org.osgi.service.component.annota
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.thymeleaf.ITemplateEngine;
-import org.thymeleaf.context.Context;
 import org.thymeleaf.context.IContext;
 
 @Component(
@@ -77,6 +78,12 @@ public class FormServlet extends SlingAl
         policy = ReferencePolicy.DYNAMIC,
         policyOption = ReferencePolicyOption.GREEDY
     )
+    private volatile ResourceResolverFactory resourceResolverFactory;
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
     private volatile ITemplateEngine templateEngine;
 
     @Reference(
@@ -129,13 +136,19 @@ public class FormServlet extends SlingAl
         }
 
         // render form with message template
-        final String template = "/etc/messaging/form/comment.txt"; // TODO
-        final Map<String, Object> variables = new HashMap<>();
-        variables.put(SlingBindings.RESOLVER, request.getResourceResolver()); // TODO service
resource resolver?
-        variables.put("form", form);
-        final IContext context = new Context(Locale.ENGLISH, variables);
-        logger.debug("rendering message template '{}' with variables: {}", template, variables);
-        final String message = templateEngine.process(template, context);
+        final String template = "/apps/fling/messaging/form/comment.txt"; // TODO
+        final Map<String, Object> variables = Collections.singletonMap("form", form);
+
+        final String message;
+        try (final ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null))
{
+            final IContext context = new DefaultSlingContext(resourceResolver, Locale.ENGLISH,
variables);
+            logger.debug("rendering message template '{}' with variables: {}", template,
variables);
+            message = templateEngine.process(template, context);
+        } catch (Exception e) { // TODO
+            logger.error("sending message failed: {}", e.getMessage(), e); // TODO
+            fail(form, 500, request, response);
+            return;
+        }
         logger.debug("message: '{}'", message);
 
         try {



Mime
View raw message