PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ringojs-kit/src/main/java/com/atlassian/labs/remoteapps/kit/js/HttpUtils.java

https://bitbucket.org/mrdon/remoteapps-plugin
Java | 114 lines | 99 code | 11 blank | 4 comment | 9 complexity | 8d6ea9426afaba1efe3234ca9e6d334b MD5 | raw file
  1. package com.atlassian.labs.remoteapps.kit.js;
  2. import com.atlassian.plugin.Plugin;
  3. import com.atlassian.plugin.osgi.bridge.external.PluginRetrievalService;
  4. import com.samskivert.mustache.Mustache;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.StringWriter;
  9. import java.net.URL;
  10. import java.util.Map;
  11. /**
  12. * Used by atlassian/renderer
  13. */
  14. public class HttpUtils
  15. {
  16. private enum TemplateExtension
  17. {
  18. mustache, mu
  19. }
  20. private final Plugin plugin;
  21. public HttpUtils(PluginRetrievalService pluginRetrievalService)
  22. {
  23. plugin = pluginRetrievalService.getPlugin();
  24. }
  25. // @todo this is all pretty much bogus -- replace with Atlassian Template Renderer service
  26. public String render(String path, Map<String,Object> context)
  27. {
  28. String extension = null;
  29. URL resource = null;
  30. int exti = path.lastIndexOf('.') + 1;
  31. if (exti > 1 && exti < path.length())
  32. {
  33. extension = path.substring(exti);
  34. resource = plugin.getResource(path);
  35. }
  36. else
  37. {
  38. for (TemplateExtension ext : TemplateExtension.values())
  39. {
  40. extension = ext.name();
  41. String tryPath = path + "." + extension;
  42. resource = plugin.getResource(tryPath);
  43. if (resource != null)
  44. {
  45. path = tryPath;
  46. break;
  47. }
  48. }
  49. }
  50. if (resource != null)
  51. {
  52. return getRenderer(path, extension).render(path, context);
  53. }
  54. else
  55. {
  56. throw new IllegalArgumentException("No acceptable template file found for path '" + path + "'");
  57. }
  58. }
  59. private Renderer getRenderer(String path, String extension)
  60. {
  61. Renderer renderer;
  62. if (TemplateExtension.mustache.name().equalsIgnoreCase(extension)
  63. || TemplateExtension.mu.name().equalsIgnoreCase(extension))
  64. {
  65. renderer = new MustacheRenderer();
  66. }
  67. else
  68. {
  69. throw new IllegalArgumentException("Unrecognized template file extension for path '" + path + "'");
  70. }
  71. return renderer;
  72. }
  73. private static interface Renderer
  74. {
  75. String render(String path, Map<String, Object> context);
  76. }
  77. private class MustacheRenderer implements Renderer
  78. {
  79. @Override
  80. public String render(String path, Map<String, Object> context)
  81. {
  82. InputStream stream = plugin.getResourceAsStream(path);
  83. InputStreamReader reader = new InputStreamReader(stream);
  84. StringWriter writer = new StringWriter();
  85. try
  86. {
  87. Mustache.compiler().compile(reader).execute(context, writer);
  88. }
  89. finally
  90. {
  91. try
  92. {
  93. stream.close();
  94. }
  95. catch (IOException ioe)
  96. {
  97. throw new RuntimeException(ioe);
  98. }
  99. }
  100. return writer.toString();
  101. }
  102. }
  103. }