View Javadoc

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