PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/src/main/java/com/atlassian/plugin/remotable/plugin/module/IFrameRendererImpl.java

https://bitbucket.org/morzechowski/remotable-plugins-jradev-16952
Java | 160 lines | 142 code | 17 blank | 1 comment | 16 complexity | 7da7c785c3fc01c40e7a58a8c9b79c2e MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.plugin.remotable.plugin.module;
  2. import com.atlassian.plugin.ModuleDescriptor;
  3. import com.atlassian.plugin.Plugin;
  4. import com.atlassian.plugin.elements.ResourceDescriptor;
  5. import com.atlassian.plugin.osgi.bridge.external.PluginRetrievalService;
  6. import com.atlassian.plugin.remotable.plugin.DefaultRemotablePluginAccessorFactory;
  7. import com.atlassian.plugin.remotable.plugin.module.page.PageInfo;
  8. import com.atlassian.plugin.remotable.spi.PermissionDeniedException;
  9. import com.atlassian.plugin.remotable.spi.RemotablePluginAccessor;
  10. import com.atlassian.plugin.remotable.spi.module.IFrameContext;
  11. import com.atlassian.plugin.remotable.spi.module.IFrameRenderer;
  12. import com.atlassian.plugin.webresource.UrlMode;
  13. import com.atlassian.plugin.webresource.WebResourceManager;
  14. import com.atlassian.plugin.webresource.WebResourceUrlProvider;
  15. import com.atlassian.templaterenderer.TemplateRenderer;
  16. import com.atlassian.uri.Uri;
  17. import com.atlassian.uri.UriBuilder;
  18. import com.google.common.collect.ImmutableMap;
  19. import org.apache.commons.lang.ObjectUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Component;
  22. import java.io.IOException;
  23. import java.io.StringWriter;
  24. import java.io.Writer;
  25. import java.net.URI;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.Map;
  29. import static com.atlassian.plugin.remotable.plugin.util.EncodingUtils.escapeQuotes;
  30. import static com.google.common.base.Preconditions.checkNotNull;
  31. import static com.google.common.collect.Lists.newArrayList;
  32. import static com.google.common.collect.Maps.newHashMap;
  33. @Component
  34. public final class IFrameRendererImpl implements IFrameRenderer
  35. {
  36. private final TemplateRenderer templateRenderer;
  37. private final WebResourceManager webResourceManager;
  38. private final WebResourceUrlProvider webResourceUrlProvider;
  39. private final DefaultRemotablePluginAccessorFactory remotablePluginAccessorFactory;
  40. private final IFrameHost iframeHost;
  41. private final Plugin plugin;
  42. @Autowired
  43. public IFrameRendererImpl(TemplateRenderer templateRenderer,
  44. WebResourceManager webResourceManager,
  45. IFrameHost iframeHost,
  46. WebResourceUrlProvider webResourceUrlProvider,
  47. PluginRetrievalService pluginRetrievalService,
  48. DefaultRemotablePluginAccessorFactory remotablePluginAccessorFactory
  49. )
  50. {
  51. this.remotablePluginAccessorFactory = checkNotNull(remotablePluginAccessorFactory);
  52. this.templateRenderer = checkNotNull(templateRenderer);
  53. this.webResourceManager = checkNotNull(webResourceManager);
  54. this.iframeHost = checkNotNull(iframeHost);
  55. this.webResourceUrlProvider = checkNotNull(webResourceUrlProvider);
  56. this.plugin = checkNotNull(pluginRetrievalService).getPlugin();
  57. }
  58. @Override
  59. public String render(IFrameContext iframeContext, String remoteUser) throws IOException
  60. {
  61. return render(iframeContext, "", Collections.<String, String[]>emptyMap(), remoteUser);
  62. }
  63. public void renderPage(IFrameContext iframeContext, PageInfo pageInfo, String extraPath, Map<String, String[]> queryParams, String remoteUser, Writer writer) throws IOException
  64. {
  65. try
  66. {
  67. if (!pageInfo.getCondition().shouldDisplay(Collections.<String, Object>emptyMap()))
  68. {
  69. throw new PermissionDeniedException(iframeContext.getPluginKey(), "Cannot render iframe for this page");
  70. }
  71. Map<String, Object> ctx = newHashMap(iframeContext.getIFrameParams().getAsMap());
  72. if (queryParams.get("width") != null)
  73. {
  74. iframeContext.getIFrameParams().setParam("width", queryParams.get("width")[0]);
  75. }
  76. if (queryParams.get("height") != null)
  77. {
  78. iframeContext.getIFrameParams().setParam("height", queryParams.get("height")[0]);
  79. }
  80. ctx.put("title", pageInfo.getTitle());
  81. ctx.put("contextPath", iframeHost.getContextPath());
  82. ctx.put("iframeHtml", render(iframeContext, extraPath, queryParams, remoteUser));
  83. ctx.put("decorator", pageInfo.getDecorator());
  84. templateRenderer.render("velocity/iframe-page" + pageInfo.getTemplateSuffix() + ".vm", ctx, writer);
  85. }
  86. catch (PermissionDeniedException ex)
  87. {
  88. templateRenderer.render(
  89. "velocity/iframe-page-accessdenied" + pageInfo.getTemplateSuffix() + ".vm",
  90. ImmutableMap.<String, Object>of(
  91. "title", pageInfo.getTitle(),
  92. "decorator", pageInfo.getDecorator()), writer);
  93. }
  94. }
  95. @Override
  96. public String render(IFrameContext iframeContext, String extraPath, Map<String, String[]> queryParams, String remoteUser) throws IOException
  97. {
  98. webResourceManager.requireResourcesForContext("remotable-plugins-iframe");
  99. RemotablePluginAccessor remotablePluginAccessor = remotablePluginAccessorFactory.get(
  100. iframeContext.getPluginKey());
  101. final URI hostUrl = iframeHost.getUrl();
  102. final URI iframeUrl = URI.create(iframeContext.getIframePath().getPath() + ObjectUtils.toString(extraPath));
  103. String[] dialog = queryParams.get("dialog");
  104. Map<String,String[]> allParams = newHashMap(queryParams);
  105. allParams.put("user_id", new String[]{remoteUser});
  106. allParams.put("xdm_e", new String[]{hostUrl.toString()});
  107. allParams.put("xdm_c", new String[]{"channel-" + iframeContext.getNamespace()});
  108. allParams.put("xdm_p", new String[]{"1"});
  109. allParams.put("cp", new String[]{iframeHost.getContextPath()});
  110. if (dialog != null && dialog.length == 1) allParams.put("dialog", dialog);
  111. String signedUrl = remotablePluginAccessor.signGetUrl(iframeUrl, allParams);
  112. // clear xdm params as they are added by easyxdm later
  113. signedUrl = new UriBuilder(Uri.parse(signedUrl))
  114. .removeQueryParameter("xdm_e")
  115. .removeQueryParameter("xdm_c")
  116. .removeQueryParameter("xdm_p")
  117. .toString();
  118. Map<String,Object> ctx = newHashMap(iframeContext.getIFrameParams().getAsMap());
  119. ctx.put("iframeSrcHtml", escapeQuotes(signedUrl));
  120. ctx.put("plugin", remotablePluginAccessor);
  121. ctx.put("namespace", iframeContext.getNamespace());
  122. ctx.put("scriptUrls", getJavaScriptUrls());
  123. ctx.put("contextPath", iframeHost.getContextPath());
  124. ctx.put("userId", remoteUser == null ? "" : remoteUser);
  125. if (dialog != null && dialog.length == 1) ctx.put("dialog", dialog[0]);
  126. StringWriter output = new StringWriter();
  127. templateRenderer.render("velocity/iframe-body.vm", ctx, output);
  128. return output.toString();
  129. }
  130. public List<String> getJavaScriptUrls()
  131. {
  132. List<String> scripts = newArrayList();
  133. ModuleDescriptor<?> moduleDescriptor = plugin.getModuleDescriptor("iframe-host-js");
  134. for (ResourceDescriptor descriptor : moduleDescriptor.getResourceDescriptors())
  135. {
  136. String src = webResourceUrlProvider.getStaticPluginResourceUrl(moduleDescriptor, descriptor.getName(), UrlMode.AUTO);
  137. if (src.endsWith(".js")) {
  138. scripts.add(src);
  139. }
  140. }
  141. return scripts;
  142. }
  143. }