PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/src/main/java/com/atlassian/labs/speakeasy/ui/UserProfileRenderer.java

https://github.com/mrdon/speakeasy-plugin
Java | 199 lines | 175 code | 21 blank | 3 comment | 2 complexity | beb05e44460e551f9b915c57473a3a77 MD5 | raw file
  1. package com.atlassian.labs.speakeasy.ui;
  2. import com.atlassian.labs.speakeasy.external.SpeakeasyService;
  3. import com.atlassian.labs.speakeasy.external.UnauthorizedAccessException;
  4. import com.atlassian.labs.speakeasy.commonjs.CommonJsModules;
  5. import com.atlassian.labs.speakeasy.commonjs.CommonJsModulesAccessor;
  6. import com.atlassian.labs.speakeasy.model.UserExtension;
  7. import com.atlassian.labs.speakeasy.model.UserPlugins;
  8. import com.atlassian.labs.speakeasy.product.ProductAccessor;
  9. import com.atlassian.labs.speakeasy.proxy.ApplinkPanelRenderer;
  10. import com.atlassian.plugin.Plugin;
  11. import com.atlassian.plugin.PluginAccessor;
  12. import com.atlassian.plugin.web.WebInterfaceManager;
  13. import com.atlassian.plugin.webresource.UrlMode;
  14. import com.atlassian.plugin.webresource.WebResourceManager;
  15. import com.atlassian.sal.api.ApplicationProperties;
  16. import com.atlassian.sal.api.user.UserManager;
  17. import com.atlassian.sal.api.xsrf.XsrfTokenAccessor;
  18. import com.atlassian.sal.api.xsrf.XsrfTokenValidator;
  19. import com.atlassian.templaterenderer.TemplateRenderer;
  20. import com.atlassian.templaterenderer.annotations.HtmlSafe;
  21. import com.google.common.base.Predicate;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.samskivert.mustache.Mustache;
  24. import com.samskivert.mustache.Template;
  25. import org.apache.commons.io.IOUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Component;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.io.InputStreamReader;
  33. import java.io.Writer;
  34. import java.util.Collections;
  35. import java.util.Map;
  36. import static com.google.common.collect.Collections2.filter;
  37. import static java.util.Collections.singletonMap;
  38. /**
  39. *
  40. */
  41. @Component
  42. public class UserProfileRenderer
  43. {
  44. private final TemplateRenderer templateRenderer;
  45. private final SpeakeasyService speakeasyService;
  46. private final UserManager userManager;
  47. private final WebResourceManager webResourceManager;
  48. private final CommonJsModulesAccessor commonJsModulesAccessor;
  49. private final ProductAccessor productAccessor;
  50. private final ApplicationProperties applicationProperties;
  51. private final Plugin plugin;
  52. private final WebInterfaceManager webInterfaceManager;
  53. private final XsrfTokenAccessor xsrfTokenAccessor;
  54. private final XsrfTokenValidator xsrfTokenValidator;
  55. private final ApplinkPanelRenderer applinkPanelRenderer;
  56. private final FirefoxXpi firefoxXpi;
  57. @Autowired
  58. public UserProfileRenderer(PluginAccessor pluginAccessor, TemplateRenderer templateRenderer, SpeakeasyService speakeasyService, UserManager userManager, WebResourceManager webResourceManager, ProductAccessor productAccessor, CommonJsModulesAccessor commonJsModulesAccessor, WebInterfaceManager webInterfaceManager, XsrfTokenAccessor xsrfTokenAccessor, XsrfTokenValidator xsrfTokenValidator, ApplicationProperties applicationProperties, FirefoxXpi firefoxXpi, ApplinkPanelRenderer applinkPanelRenderer)
  59. {
  60. this.templateRenderer = templateRenderer;
  61. this.commonJsModulesAccessor = commonJsModulesAccessor;
  62. this.webInterfaceManager = webInterfaceManager;
  63. this.xsrfTokenAccessor = xsrfTokenAccessor;
  64. this.xsrfTokenValidator = xsrfTokenValidator;
  65. this.applicationProperties = applicationProperties;
  66. this.firefoxXpi = firefoxXpi;
  67. this.applinkPanelRenderer = applinkPanelRenderer;
  68. this.plugin = pluginAccessor.getPlugin("com.atlassian.labs.speakeasy-plugin");
  69. this.speakeasyService = speakeasyService;
  70. this.userManager = userManager;
  71. this.webResourceManager = webResourceManager;
  72. this.productAccessor = productAccessor;
  73. }
  74. public boolean shouldRender(String userName)
  75. {
  76. return speakeasyService.canAccessSpeakeasy(userName);
  77. }
  78. public void render(HttpServletRequest req, HttpServletResponse resp, Writer output, boolean useUserProfileDecorator) throws IOException, UnauthorizedAccessException
  79. {
  80. String user = userManager.getRemoteUsername(req);
  81. if (user == null)
  82. {
  83. throw new UnauthorizedAccessException(null, "Unauthorized - must be logged in and have access to Speakeasy");
  84. }
  85. webResourceManager.requireResource("com.atlassian.auiplugin:ajs");
  86. webResourceManager.requireResourcesForContext("speakeasy.user-profile");
  87. boolean devMode = speakeasyService.canAuthorExtensions(user);
  88. final UserPlugins plugins = speakeasyService.getRemotePluginList(user);
  89. render("templates/user" + (useUserProfileDecorator ? "-with-decorator" : "") + ".vm", ImmutableMap.<String, Object>builder().
  90. put("accessList", speakeasyService.getRemotePluginList(user)).
  91. put("user", user).
  92. put("req", req).
  93. put("baseUrl", applicationProperties.getBaseUrl()).
  94. put("contextPath", req.getContextPath()).
  95. put("enabledPlugins", filter(plugins.getPlugins(), new EnabledPluginsFilter())).
  96. put("availablePlugins", filter(plugins.getPlugins(), new AvailablePluginsFilter())).
  97. put("rowRenderer", new RowRenderer(req.getContextPath(), plugin)).
  98. put("jsdocRenderer", new JsDocRenderer(plugin, commonJsModulesAccessor.getAllPublicCommonJsModules())).
  99. put("staticResourcesPrefix", webResourceManager.getStaticResourcePrefix(UrlMode.RELATIVE)).
  100. put("product", productAccessor.getSdkName()).
  101. put("devmode", devMode).
  102. put("canAuthor", speakeasyService.canAuthorExtensions(user)).
  103. put("doesAnyGroupHaveAccess", speakeasyService.doesAnyGroupHaveAccess()).
  104. put("webInterfaceManager", webInterfaceManager).
  105. put("webInterfaceContext", Collections.<String, Object>emptyMap()).
  106. put("xsrfToken", xsrfTokenAccessor.getXsrfToken(req, resp, true)).
  107. put("xsrfTokenName", xsrfTokenValidator.getXsrfParameterName()).
  108. put("applinksRenderer", applinkPanelRenderer).
  109. put("firefoxXpi", firefoxXpi).
  110. build(), output);
  111. }
  112. protected void render(final String template, final Map<String, Object> renderContext,
  113. final Writer output)
  114. throws IOException
  115. {
  116. templateRenderer.render(template, renderContext, output);
  117. }
  118. public static class RowRenderer
  119. {
  120. private final Template rowTemplate;
  121. private final String contextPath;
  122. public RowRenderer(String contextPath, Plugin plugin)
  123. {
  124. this.contextPath = contextPath;
  125. this.rowTemplate = compile(plugin, "packages/user/speakeasy/user/row.mu");
  126. }
  127. @HtmlSafe
  128. @com.atlassian.velocity.htmlsafe.HtmlSafe
  129. public String render(UserExtension plugin)
  130. {
  131. plugin.getParams().put("screenshotUrl", contextPath + "/rest/speakeasy/1/plugins/screenshot/" + plugin.getKey() + ".png");
  132. return rowTemplate.execute(plugin);
  133. }
  134. }
  135. public static class JsDocRenderer
  136. {
  137. private final Template template;
  138. private final Iterable<CommonJsModules> modules;
  139. public JsDocRenderer(Plugin plugin, Iterable<CommonJsModules> modules)
  140. {
  141. this.modules = modules;
  142. this.template = compile(plugin, "packages/user/speakeasy/user/jsdoc/tree-template.mu");
  143. }
  144. @HtmlSafe
  145. @com.atlassian.velocity.htmlsafe.HtmlSafe
  146. public String render()
  147. {
  148. return template.execute(singletonMap("pluginModules", modules));
  149. }
  150. }
  151. private static Template compile(Plugin plugin, String path)
  152. {
  153. InputStream in = null;
  154. try
  155. {
  156. in = plugin.getResourceAsStream(path);
  157. return Mustache.compiler().standardsMode(true).compile(new InputStreamReader(in));
  158. }
  159. finally
  160. {
  161. IOUtils.closeQuietly(in);
  162. }
  163. }
  164. private static class EnabledPluginsFilter implements Predicate<UserExtension>
  165. {
  166. public boolean apply(UserExtension input)
  167. {
  168. return input.isEnabled();
  169. }
  170. }
  171. private static class AvailablePluginsFilter implements Predicate<UserExtension>
  172. {
  173. public boolean apply(UserExtension input)
  174. {
  175. return !input.isEnabled();
  176. }
  177. }
  178. }