r2253 - in branches/wao-4.0.x: . wao-services wao-services/src/main/java/fr/ifremer/wao/services/service/mail wao-services/src/test/java/fr/ifremer/wao/services/service/mail
Author: bleny Date: 2014-09-11 18:18:42 +0200 (Thu, 11 Sep 2014) New Revision: 2253 Url: http://forge.codelutin.com/projects/wao/repository/revisions/2253 Log: Remplacement de la biblioth?\195?\168que utilis?\195?\169e pour le traitement des mod?\195?\168les mustache lors de l'envoi de mail : la biblioth?\195?\168que utilis?\195?\169e ne permettaient pas de d?\195?\169sactiver l'?\195?\169chappement HTML (fixes #5774) Modified: branches/wao-4.0.x/pom.xml branches/wao-4.0.x/wao-services/pom.xml branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/mail/EmailService.java branches/wao-4.0.x/wao-services/src/test/java/fr/ifremer/wao/services/service/mail/EmailServiceTest.java Modified: branches/wao-4.0.x/pom.xml =================================================================== --- branches/wao-4.0.x/pom.xml 2014-09-11 16:03:08 UTC (rev 2252) +++ branches/wao-4.0.x/pom.xml 2014-09-11 16:18:42 UTC (rev 2253) @@ -109,7 +109,7 @@ <mockitoVersion>1.9.5</mockitoVersion> <postgresqlDriverVersion>9.3-1101-jdbc41</postgresqlDriverVersion> <commonsEmailVersion>1.3.2</commonsEmailVersion> - <mustacheVersion>0.8.15</mustacheVersion> + <mustacheVersion>1.9</mustacheVersion> <commonsCodecVersion>1.9</commonsCodecVersion> <ehCacheVersion>2.6.9</ehCacheVersion> <jFreeChartVersion>1.0.13</jFreeChartVersion> @@ -259,8 +259,8 @@ <!-- services module --> <dependency> - <groupId>com.github.spullara.mustache.java</groupId> - <artifactId>compiler</artifactId> + <groupId>com.samskivert</groupId> + <artifactId>jmustache</artifactId> <version>${mustacheVersion}</version> </dependency> Modified: branches/wao-4.0.x/wao-services/pom.xml =================================================================== --- branches/wao-4.0.x/wao-services/pom.xml 2014-09-11 16:03:08 UTC (rev 2252) +++ branches/wao-4.0.x/wao-services/pom.xml 2014-09-11 16:18:42 UTC (rev 2253) @@ -41,8 +41,8 @@ </dependency> <dependency> - <groupId>com.github.spullara.mustache.java</groupId> - <artifactId>compiler</artifactId> + <groupId>com.samskivert</groupId> + <artifactId>jmustache</artifactId> </dependency> <dependency> Modified: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/mail/EmailService.java =================================================================== --- branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/mail/EmailService.java 2014-09-11 16:03:08 UTC (rev 2252) +++ branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/mail/EmailService.java 2014-09-11 16:18:42 UTC (rev 2253) @@ -21,11 +21,10 @@ * #L% */ -import com.github.mustachejava.DefaultMustacheFactory; -import com.github.mustachejava.Mustache; -import com.github.mustachejava.MustacheFactory; import com.google.common.base.Charsets; import com.google.common.base.Objects; +import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Template; import fr.ifremer.wao.WaoApplicationConfig; import fr.ifremer.wao.WaoTechnicalException; import fr.ifremer.wao.services.service.WaoServiceSupport; @@ -35,7 +34,8 @@ import org.apache.commons.mail.EmailException; import org.apache.commons.mail.SimpleEmail; -import java.io.StringWriter; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.Locale; public class EmailService extends WaoServiceSupport { @@ -131,17 +131,19 @@ Locale locale = mail.getLocale(); - MustacheFactory mustacheFactory = new DefaultMustacheFactory(); + String templateName = "/email/" + mail.getClass().getSimpleName() + "_" + locale.getLanguage() + ".mustache"; - String templateName = "email/" + mail.getClass().getSimpleName() + "_" + locale.getLanguage() + ".mustache"; + try (InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream(templateName), Charsets.UTF_8.name())) { - Mustache mustache = mustacheFactory.compile(templateName); + Template template = Mustache.compiler().escapeHTML(false).compile(reader); - StringWriter stringWriter = new StringWriter(); + String body = template.execute(mail); - mustache.execute(stringWriter, mail); + return body; - return stringWriter.toString(); + } catch (IOException e) { + throw new WaoTechnicalException("Unable to read " + templateName, e); + } } Modified: branches/wao-4.0.x/wao-services/src/test/java/fr/ifremer/wao/services/service/mail/EmailServiceTest.java =================================================================== --- branches/wao-4.0.x/wao-services/src/test/java/fr/ifremer/wao/services/service/mail/EmailServiceTest.java 2014-09-11 16:03:08 UTC (rev 2252) +++ branches/wao-4.0.x/wao-services/src/test/java/fr/ifremer/wao/services/service/mail/EmailServiceTest.java 2014-09-11 16:18:42 UTC (rev 2253) @@ -23,6 +23,8 @@ import fr.ifremer.wao.entity.WaoUserImpl; import fr.ifremer.wao.services.AbstractWaoServiceTest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -31,6 +33,8 @@ public class EmailServiceTest extends AbstractWaoServiceTest { + private static final Log log = LogFactory.getLog(EmailServiceTest.class); + protected EmailService service; @Before @@ -38,19 +42,28 @@ service = newService(EmailService.class); + newMail(); + } + protected UserCredentialsEmail newMail() { + UserCredentialsEmail mail = service.newUserCredentialsEmail(); + WaoUserImpl waoUser = new WaoUserImpl(); + waoUser.setFirstName("F"); + waoUser.setLogin("Login"); + mail.setWaoUser(waoUser); + mail.setNewPassword("taiste'"); + return mail; + } + @Test public void testGetBody() { { serviceContext.setLocale(Locale.FRANCE); - UserCredentialsEmail mail = service.newUserCredentialsEmail(); - mail.setWaoUser(new WaoUserImpl()); - mail.setNewPassword("taiste"); - String body = service.getBody(mail); + String body = service.getBody(newMail()); Assert.assertTrue(body.contains("taiste")); Assert.assertTrue(body.contains("Bonjour")); @@ -59,11 +72,8 @@ { serviceContext.setLocale(Locale.FRENCH); - UserCredentialsEmail mail = service.newUserCredentialsEmail(); - mail.setWaoUser(new WaoUserImpl()); - mail.setNewPassword("taiste"); - String body = service.getBody(mail); + String body = service.getBody(newMail()); Assert.assertTrue(body.contains("taiste")); Assert.assertTrue(body.contains("Bonjour")); @@ -72,11 +82,8 @@ { serviceContext.setLocale(Locale.CANADA_FRENCH); - UserCredentialsEmail mail = service.newUserCredentialsEmail(); - mail.setWaoUser(new WaoUserImpl()); - mail.setNewPassword("taiste"); - String body = service.getBody(mail); + String body = service.getBody(newMail()); Assert.assertTrue(body.contains("taiste")); Assert.assertTrue(body.contains("Bonjour")); @@ -85,11 +92,8 @@ { serviceContext.setLocale(Locale.ENGLISH); - UserCredentialsEmail mail = service.newUserCredentialsEmail(); - mail.setWaoUser(new WaoUserImpl()); - mail.setNewPassword("taiste"); - String body = service.getBody(mail); + String body = service.getBody(newMail()); Assert.assertTrue(body.contains("taiste")); Assert.assertTrue(body.contains("Hi")); @@ -98,11 +102,8 @@ { serviceContext.setLocale(Locale.US); - UserCredentialsEmail mail = service.newUserCredentialsEmail(); - mail.setWaoUser(new WaoUserImpl()); - mail.setNewPassword("taiste"); - String body = service.getBody(mail); + String body = service.getBody(newMail()); Assert.assertTrue(body.contains("taiste")); Assert.assertTrue(body.contains("Hi")); @@ -111,11 +112,8 @@ { serviceContext.setLocale(Locale.CANADA); - UserCredentialsEmail mail = service.newUserCredentialsEmail(); - mail.setWaoUser(new WaoUserImpl()); - mail.setNewPassword("taiste"); - String body = service.getBody(mail); + String body = service.getBody(newMail()); Assert.assertTrue(body.contains("taiste")); Assert.assertTrue(body.contains("Hi")); @@ -123,4 +121,19 @@ } } + @Test + public void mustacheShouldNotEscapeContent() { + + serviceContext.setLocale(Locale.FRANCE); + + String body = service.getBody(newMail()); + + if (log.isDebugEnabled()) { + log.debug("body = \n" + body); + } + + Assert.assertFalse(body.contains("taiste'")); + Assert.assertTrue(body.contains("taiste'")); + + } }
participants (1)
-
bleny@users.forge.codelutin.com