1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal.email

File EmailTemplateRenderer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

4
25
3
1
159
85
6
0.24
8.33
3
2

Classes

Class Line # Actions
EmailTemplateRenderer 56 25 0% 6 3
0.9062590.6%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.notifications.notifiers.internal.email;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Provider;
25    import javax.inject.Singleton;
26    import javax.script.ScriptContext;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.notifications.CompositeEvent;
31    import org.xwiki.notifications.NotificationException;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
34    import org.xwiki.rendering.renderer.BlockRenderer;
35    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
36    import org.xwiki.rendering.renderer.printer.WikiPrinter;
37    import org.xwiki.rendering.syntax.Syntax;
38    import org.xwiki.rendering.transformation.RenderingContext;
39    import org.xwiki.script.ScriptContextManager;
40    import org.xwiki.template.Template;
41    import org.xwiki.template.TemplateManager;
42   
43    import com.xpn.xwiki.XWikiContext;
44    import com.xpn.xwiki.web.ExternalServletURLFactory;
45    import com.xpn.xwiki.web.XWikiURLFactory;
46   
47    /**
48    * Helper to render email notifications templates.
49    *
50    * @version $Id: 29e187751a70255ddce3493e9ea58af51587dcce $
51    * @since 9.11.1
52    * @since 10.0
53    */
54    @Component(roles = EmailTemplateRenderer.class)
55    @Singleton
 
56    public class EmailTemplateRenderer
57    {
58    private static final String EVENT_BINDING_NAME = "event";
59   
60    private static final String USER_BINDING_NAME = "emailUser";
61   
62    @Inject
63    // In 2017, it's safer to use XHTML 1.0 for emails because the emails clients have a very unequal HTML support
64    @Named("xhtml/1.0")
65    private BlockRenderer htmlBlockRenderer;
66   
67    @Inject
68    @Named("plain/1.0")
69    private BlockRenderer plainTextBlockRenderer;
70   
71    @Inject
72    private TemplateManager templateManager;
73   
74    @Inject
75    private ScriptContextManager scriptContextManager;
76   
77    @Inject
78    private Provider<XWikiContext> contextProvider;
79   
80    @Inject
81    private RenderingContext renderingContext;
82   
83    /**
84    * Execute a template.
85    *
86    * @param event composite event to render
87    * @param userId id of the user who will receive the email
88    * @param template the template to use
89    * @param syntax syntax of the template and of the output
90    * @return the rendered template
91    * @throws NotificationException if something wrong happens
92    */
 
93  4 toggle public Block executeTemplate(CompositeEvent event, String userId, Template template, Syntax syntax)
94    throws NotificationException
95    {
96  4 XWikiContext context = contextProvider.get();
97  4 DocumentReference currentUser = context.getUserReference();
98  4 XWikiURLFactory originalURLFactory = context.getURLFactory();
99  4 ScriptContext scriptContext = scriptContextManager.getScriptContext();
100  4 try {
101    // Use the author of the template as current user so we can safely rely on the security system and we can
102    // make sure a wiki template written by a malicious user cannot access to more information than she should.
103    // Actually, templates should be using xwiki.getDocumentAsAuthor(), but many notifications templates does
104    // not and I don't want to break them.
105  4 context.setUserReference(template.getContent().getAuthorReference());
106    // Bind the event to some variable in the velocity context
107  4 scriptContext.setAttribute(EVENT_BINDING_NAME, event, ScriptContext.ENGINE_SCOPE);
108  4 scriptContext.setAttribute(USER_BINDING_NAME, userId, ScriptContext.ENGINE_SCOPE);
109    // Use the external URL factory to generate full URLs
110  4 context.setURLFactory(new ExternalServletURLFactory(context));
111    // Set the given syntax in the rendering context
112  4 if (renderingContext instanceof MutableRenderingContext) {
113  4 ((MutableRenderingContext) renderingContext).push(null, null, syntax, null,
114    false, syntax);
115    }
116    // Render the template or fallback to the default one
117  4 return templateManager.execute(template);
118    } catch (Exception e) {
119  0 throw new NotificationException("Failed to render the notification.", e);
120    } finally {
121    // Cleaning the rendering context
122  4 if (renderingContext instanceof MutableRenderingContext) {
123  4 ((MutableRenderingContext) renderingContext).pop();
124    }
125    // Cleaning the URL factory
126  4 context.setURLFactory(originalURLFactory);
127    // Cleaning the velocity context
128  4 scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
129  4 scriptContext.removeAttribute(USER_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
130    // Cleaning the current user
131  4 context.setUserReference(currentUser);
132    }
133    }
134   
135    /**
136    * Render a block to HTML syntax.
137    * @param block block to render
138    * @return the HTML rendered version of the block
139    */
 
140  2 toggle public String renderHTML(Block block)
141    {
142  2 WikiPrinter printer = new DefaultWikiPrinter();
143  2 htmlBlockRenderer.render(block, printer);
144  2 return printer.toString();
145    }
146   
147    /**
148    * Render a block to plain text syntax.
149    * @param block block to render
150    * @return the plain text rendered version of the block
151    */
 
152  2 toggle public String renderPlainText(Block block)
153    {
154    // TODO: this does not work at all (templates enforce HTML syntax I guess)
155  2 WikiPrinter printer = new DefaultWikiPrinter();
156  2 plainTextBlockRenderer.render(block, printer);
157  2 return printer.toString();
158    }
159    }