View Javadoc
1   package com.atlassian.plugin.web.renderer;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.google.common.io.CharStreams;
5   
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.io.InputStreamReader;
9   import java.io.Writer;
10  import java.util.Map;
11  
12  /**
13   * Static {@link WebPanelRenderer}, just returns the supplied text.
14   */
15  public class StaticWebPanelRenderer implements WebPanelRenderer {
16      public static final StaticWebPanelRenderer RENDERER = new StaticWebPanelRenderer();
17      public static final String RESOURCE_TYPE = "static";
18  
19      public String getResourceType() {
20          return RESOURCE_TYPE;
21      }
22  
23      public void render(String templateName, Plugin plugin, Map<String, Object> context, Writer writer) throws RendererException, IOException {
24          try (InputStreamReader in = new InputStreamReader(loadTemplate(plugin, templateName))) {
25              CharStreams.copy(in, writer);
26          }
27      }
28  
29      public String renderFragment(String fragment, Plugin plugin, Map<String, Object> context) throws RendererException {
30          return fragment;
31      }
32  
33      public void renderFragment(Writer writer, String fragment, Plugin plugin, Map<String, Object> context) throws RendererException, IOException {
34          writer.write(fragment);
35      }
36  
37      private InputStream loadTemplate(Plugin plugin, String templateName) throws IOException {
38          InputStream in = plugin.getClassLoader().getResourceAsStream(templateName);
39          if (in == null) {
40              // template not found in the plugin, try the host application:
41              if ((in = getClass().getResourceAsStream(templateName)) == null) {
42                  throw new RendererException(String.format("Static web panel template %s not found.", templateName));
43              }
44          }
45          return in;
46      }
47  }