View Javadoc

1   package com.atlassian.plugin.web.model;
2   
3   import com.atlassian.plugin.PluginAccessor;
4   import com.atlassian.plugin.web.renderer.RendererException;
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import java.util.Map;
9   
10  /**
11   * This class is used for web panel declaration that do not have a custom
12   * <code>class</code> attribute in their descriptor, nor a <code>location</code>
13   * attribute in their resource child element.
14   * <p>
15   * This class reads the web panel's content from the resource element's
16   * content.
17   *
18   * @see com.atlassian.plugin.web.descriptors.DefaultWebPanelModuleDescriptor
19   * @since   2.5.0
20   */
21  public class EmbeddedTemplateWebPanel extends AbstractWebPanel
22  {
23      private String templateBody;
24      private static final Logger logger = LoggerFactory.getLogger(EmbeddedTemplateWebPanel.class.getName());
25  
26      public EmbeddedTemplateWebPanel(PluginAccessor pluginAccessor)
27      {
28          super(pluginAccessor);
29      }
30  
31      /**
32       * @param templateBody  the body of the web panel (may contain any content type such as velocity or just static
33       *  HTML) that was inlined in <code>atlassian-plugin.xml</code>
34       */
35      public void setTemplateBody(String templateBody)
36      {
37          this.templateBody = templateBody;
38      }
39  
40      public String getHtml(final Map<String, Object> context)
41      {
42          try
43          {
44              return getRenderer().renderFragment(templateBody, plugin, context);
45          }
46          catch (RendererException e)
47          {
48              final String message = String.format("Error rendering WebPanel: %s\n" +
49                      "Template contents: %s", e.getMessage(), templateBody);
50              logger.warn(message, e);
51              return message;
52          }
53      }
54  }