syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gwim...@apache.org
Subject svn commit: r1581766 - in /syncope/branches/1_1_X: ./ core/ core/src/main/java/org/apache/syncope/core/notification/ core/src/main/resources/ core/src/main/resources/mailTemplates/ core/src/test/java/org/apache/syncope/core/notification/ core/src/test/...
Date Wed, 26 Mar 2014 09:41:22 GMT
Author: gwimmel
Date: Wed Mar 26 09:41:21 2014
New Revision: 1581766

URL: http://svn.apache.org/r1581766
Log:
[SYNCOPE-487] make Velocity tools available in templates for notifications

Modified:
    syncope/branches/1_1_X/core/pom.xml
    syncope/branches/1_1_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java
    syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.html.vm
    syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.txt.vm
    syncope/branches/1_1_X/core/src/main/resources/workflowContext.xml
    syncope/branches/1_1_X/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java
    syncope/branches/1_1_X/core/src/test/resources/noopworkflow/workflowContext.xml
    syncope/branches/1_1_X/pom.xml

Modified: syncope/branches/1_1_X/core/pom.xml
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/core/pom.xml?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/core/pom.xml (original)
+++ syncope/branches/1_1_X/core/pom.xml Wed Mar 26 09:41:21 2014
@@ -188,7 +188,12 @@ under the License.
       <groupId>org.apache.velocity</groupId>
       <artifactId>velocity</artifactId>
     </dependency>
-
+    
+    <dependency>
+      <groupId>org.apache.velocity</groupId>
+      <artifactId>velocity-tools</artifactId>
+	</dependency>
+	
     <dependency>
       <groupId>org.quartz-scheduler</groupId>
       <artifactId>quartz</artifactId>

Modified: syncope/branches/1_1_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java
(original)
+++ syncope/branches/1_1_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java
Wed Mar 26 09:41:21 2014
@@ -18,6 +18,7 @@
  */
 package org.apache.syncope.core.notification;
 
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -25,6 +26,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+
 import org.apache.syncope.common.SyncopeConstants;
 import org.apache.syncope.common.to.RoleTO;
 import org.apache.syncope.common.to.UserTO;
@@ -53,13 +55,15 @@ import org.apache.syncope.core.persisten
 import org.apache.syncope.core.rest.data.UserDataBinder;
 import org.apache.syncope.core.util.AttributableUtil;
 import org.apache.syncope.core.util.EntitlementUtil;
+import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
 import org.apache.velocity.exception.VelocityException;
+import org.apache.velocity.tools.ToolManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.ui.velocity.VelocityEngineUtils;
 
 /**
  * Create notification tasks that will be executed by NotificationJob.
@@ -121,6 +125,12 @@ public class NotificationManager {
      */
     @Autowired
     private VelocityEngine velocityEngine;
+    
+    /**
+     * Velocity tool manager.
+     */
+    @Autowired
+    private ToolManager velocityToolManager;
 
     @Autowired
     private EntitlementDAO entitlementDAO;
@@ -181,25 +191,51 @@ public class NotificationManager {
         task.setSender(notification.getSender());
         task.setSubject(notification.getSubject());
 
-        String htmlBody;
-        String textBody;
-        try {
-            htmlBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "mailTemplates/"
-                    + notification.getTemplate() + ".html.vm", SyncopeConstants.DEFAULT_ENCODING,
model);
-            textBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "mailTemplates/"
-                    + notification.getTemplate() + ".txt.vm", SyncopeConstants.DEFAULT_ENCODING,
model);
-        } catch (VelocityException e) {
-            LOG.error("Could not get mail body", e);
-
-            htmlBody = "";
-            textBody = "";
-        }
-        task.setTextBody(textBody);
+        String htmlBody = mergeTemplateIntoString("mailTemplates/" + notification.getTemplate()
+ ".html.vm", model);
+        String textBody = mergeTemplateIntoString("mailTemplates/" + notification.getTemplate()
+ ".txt.vm", model);
+        
         task.setHtmlBody(htmlBody);
-
+        task.setTextBody(textBody);
+        
         return task;
     }
 
+    
+    
+    private String mergeTemplateIntoString(String templateLocation, Map<String, Object>
model) {
+    	StringWriter result = new StringWriter();
+    	try {
+			Context velocityContext = createVelocityContext(model);
+			velocityEngine.mergeTemplate(templateLocation, SyncopeConstants.DEFAULT_ENCODING, velocityContext,
result);
+		}
+		catch (VelocityException e) {
+			LOG.error("Could not get mail body", e);
+			return "";
+		}
+    	// ensure same behaviour as by using Spring VelocityEngineUtils.mergeTemplateIntoString()
+		catch (RuntimeException e) {
+			throw e;
+		}
+		catch (Exception e) {
+			LOG.error("Could not get mail body", e);
+			return "";
+		}
+    	return result.toString();
+    	
+    }
+    
+    
+    /**
+     * Create a Velocity Context for the given model, to be passed to the template for merging.
+     * 
+     * @param model Velocity model
+     * @return Velocity context
+     */
+    protected Context createVelocityContext(Map<String, Object> model) {
+    	Context toolContext = velocityToolManager.createContext();
+    	return new VelocityContext(model, toolContext);
+    }
+
     /**
      * Create notification tasks for each notification matching the given user id and (some
of) tasks performed.
      */

Modified: syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.html.vm
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.html.vm?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.html.vm (original)
+++ syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.html.vm Wed Mar 26
09:41:21 2014
@@ -24,6 +24,7 @@ under the License.
 <p>
    Your username is $user.getUsername().<br/>
    Your email address is $user.getAttributeMap().get("email").getValues().get(0).
+   Your email address inside a <a href="http://localhost/?email=$esc.url($user.getUsername())">link</a>.
 </p>
 
 <p>

Modified: syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.txt.vm
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.txt.vm?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.txt.vm (original)
+++ syncope/branches/1_1_X/core/src/main/resources/mailTemplates/optin.txt.vm Wed Mar 26 09:41:21
2014
@@ -13,6 +13,7 @@ Hi $user.getAttributeMap().get("firstnam
 
 Your username is $user.getUsername().
 Your email address is $user.getAttributeMap().get("email").getValues().get(0).
+Your email address inside a link: http://localhost/?email=$esc.url($user.getUsername()) .
 
 This message was sent to the following recipients:
 #foreach($recipient in $recipients)

Modified: syncope/branches/1_1_X/core/src/main/resources/workflowContext.xml
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/core/src/main/resources/workflowContext.xml?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/core/src/main/resources/workflowContext.xml (original)
+++ syncope/branches/1_1_X/core/src/main/resources/workflowContext.xml Wed Mar 26 09:41:21
2014
@@ -77,4 +77,11 @@ under the License.
       </value>
     </property>
   </bean>
+  
+  <bean id="velocityToolManager" class="org.apache.velocity.tools.ToolManager">
+    <!-- autoConfigure -->
+    <constructor-arg index="0" value="true"/>
+    <!-- include default velocity tools -->
+    <constructor-arg index="1" value="true"/>
+  </bean>
 </beans>

Modified: syncope/branches/1_1_X/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java
(original)
+++ syncope/branches/1_1_X/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java
Wed Mar 26 09:41:21 2014
@@ -24,15 +24,18 @@ import static org.junit.Assert.assertNot
 
 import com.icegreen.greenmail.util.GreenMail;
 import com.icegreen.greenmail.util.ServerSetup;
+
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
+
 import javax.annotation.Resource;
 import javax.mail.Flags.Flag;
 import javax.mail.Folder;
 import javax.mail.Message;
 import javax.mail.Session;
 import javax.mail.Store;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.syncope.common.search.MembershipCond;
 import org.apache.syncope.common.search.NodeCond;
@@ -241,14 +244,21 @@ public class NotificationTest {
         notificationJob.execute(null);
         assertTrue(verifyMail(sender, subject));
 
-        // 4. get NotificationTask id
+        // 4. get NotificationTask id and text body
         Long taskId = null;
+        String textBody = null;
         for (NotificationTask task : taskDAO.findAll(NotificationTask.class)) {
             if (sender.equals(task.getSender())) {
                 taskId = task.getId();
+                textBody = task.getTextBody();
             }
         }
         assertNotNull(taskId);
+        assertNotNull(textBody);
+        assertTrue("Notification mail text doesn't contain expected content.",
+        		textBody.contains("Your email address is notificationtest@syncope.apache.org."));
+        assertTrue("Notification mail text doesn't contain expected content.",
+        		textBody.contains("Your email address inside a link: http://localhost/?email=notificationtest%40syncope.apache.org
."));
 
         // 5. execute Notification task and verify e-mail
         taskController.execute(taskId, false);

Modified: syncope/branches/1_1_X/core/src/test/resources/noopworkflow/workflowContext.xml
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/core/src/test/resources/noopworkflow/workflowContext.xml?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/core/src/test/resources/noopworkflow/workflowContext.xml (original)
+++ syncope/branches/1_1_X/core/src/test/resources/noopworkflow/workflowContext.xml Wed Mar
26 09:41:21 2014
@@ -37,4 +37,11 @@ under the License.
       </value>
     </property>
   </bean>
+  
+  <bean id="velocityToolManager" class="org.apache.velocity.tools.ToolManager">
+    <!-- autoConfigure -->
+    <constructor-arg index="0" value="true"/>
+    <!-- include default velocity tools -->
+    <constructor-arg index="1" value="true"/>
+  </bean>
 </beans>

Modified: syncope/branches/1_1_X/pom.xml
URL: http://svn.apache.org/viewvc/syncope/branches/1_1_X/pom.xml?rev=1581766&r1=1581765&r2=1581766&view=diff
==============================================================================
--- syncope/branches/1_1_X/pom.xml (original)
+++ syncope/branches/1_1_X/pom.xml Wed Mar 26 09:41:21 2014
@@ -327,6 +327,7 @@ under the License.
     <jackson.version>1.9.13</jackson.version>
     <xstream.version>1.4.7</xstream.version>
     <velocity.version>1.7</velocity.version>
+    <velocitytools.version>2.0</velocitytools.version>
     <quartz.version>2.1.7</quartz.version>
 
     <openjpa.version>2.2.2</openjpa.version>
@@ -524,6 +525,34 @@ under the License.
         <artifactId>velocity</artifactId>
         <version>${velocity.version}</version>
       </dependency>
+      
+      <dependency>
+        <groupId>org.apache.velocity</groupId>
+        <artifactId>velocity-tools</artifactId>
+        <version>${velocitytools.version}</version>
+        <exclusions>
+  			<exclusion>
+  				<artifactId>struts-core</artifactId>
+  				<groupId>org.apache.struts</groupId>
+  			</exclusion>
+  			<exclusion>
+  				<artifactId>struts-taglib</artifactId>
+  				<groupId>org.apache.struts</groupId>
+  			</exclusion>
+  			<exclusion>
+  				<artifactId>struts-tiles</artifactId>
+  				<groupId>org.apache.struts</groupId>
+  			</exclusion>
+  			<exclusion>
+  				<artifactId>sslext</artifactId>
+  				<groupId>sslext</groupId>
+  			</exclusion>
+  			<exclusion>
+  				<artifactId>commons-beanutils</artifactId>
+  				<groupId>commons-beanutils</groupId>
+  			</exclusion>
+  		</exclusions>
+      </dependency>
 
       <!-- Spring -->
       <dependency>



Mime
View raw message