PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/rodogu/remotable-plugins
Java | 194 lines | 173 code | 20 blank | 1 comment | 18 complexity | 5301ef43c9cfef7151c3a45a1daf0829 MD5 | raw file
  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.UserPreferencesRetriever;
  8. import com.atlassian.plugin.remotable.plugin.license.LicenseRetriever;
  9. import com.atlassian.plugin.remotable.plugin.module.page.PageInfo;
  10. import com.atlassian.plugin.remotable.plugin.util.LocaleHelper;
  11. import com.atlassian.plugin.remotable.spi.PermissionDeniedException;
  12. import com.atlassian.plugin.remotable.spi.RemotablePluginAccessor;
  13. import com.atlassian.plugin.remotable.spi.module.IFrameContext;
  14. import com.atlassian.plugin.remotable.spi.module.IFrameRenderer;
  15. import com.atlassian.plugin.webresource.UrlMode;
  16. import com.atlassian.plugin.webresource.WebResourceManager;
  17. import com.atlassian.plugin.webresource.WebResourceUrlProvider;
  18. import com.atlassian.sal.api.message.LocaleResolver;
  19. import com.atlassian.templaterenderer.TemplateRenderer;
  20. import com.atlassian.uri.Uri;
  21. import com.atlassian.uri.UriBuilder;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.google.common.collect.Maps;
  24. import org.apache.commons.lang.ObjectUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Component;
  27. import java.io.IOException;
  28. import java.io.StringWriter;
  29. import java.io.Writer;
  30. import java.net.URI;
  31. import java.util.Arrays;
  32. import java.util.Collections;
  33. import java.util.List;
  34. import java.util.Map;
  35. import static com.atlassian.plugin.remotable.plugin.util.EncodingUtils.escapeQuotes;
  36. import static com.google.common.base.Preconditions.checkNotNull;
  37. import static com.google.common.collect.Lists.newArrayList;
  38. import static com.google.common.collect.Maps.newHashMap;
  39. @Component
  40. public final class IFrameRendererImpl implements IFrameRenderer
  41. {
  42. private final TemplateRenderer templateRenderer;
  43. private final WebResourceManager webResourceManager;
  44. private final WebResourceUrlProvider webResourceUrlProvider;
  45. private final DefaultRemotablePluginAccessorFactory remotablePluginAccessorFactory;
  46. private final IFrameHost iframeHost;
  47. private final Plugin acPlugin;
  48. private final LicenseRetriever licenseRetriever;
  49. private final LocaleHelper localeHelper;
  50. private final UserPreferencesRetriever userPreferencesRetriever;
  51. @Autowired
  52. public IFrameRendererImpl(TemplateRenderer templateRenderer,
  53. WebResourceManager webResourceManager,
  54. IFrameHost iframeHost,
  55. WebResourceUrlProvider webResourceUrlProvider,
  56. PluginRetrievalService pluginRetrievalService,
  57. DefaultRemotablePluginAccessorFactory remotablePluginAccessorFactory,
  58. UserPreferencesRetriever userPreferencesRetriever, final LicenseRetriever licenseRetriever,
  59. LocaleHelper localeHelper)
  60. {
  61. this.licenseRetriever = licenseRetriever;
  62. this.localeHelper = localeHelper;
  63. this.userPreferencesRetriever = checkNotNull(userPreferencesRetriever);
  64. this.remotablePluginAccessorFactory = checkNotNull(remotablePluginAccessorFactory);
  65. this.templateRenderer = checkNotNull(templateRenderer);
  66. this.webResourceManager = checkNotNull(webResourceManager);
  67. this.iframeHost = checkNotNull(iframeHost);
  68. this.webResourceUrlProvider = checkNotNull(webResourceUrlProvider);
  69. this.acPlugin = checkNotNull(pluginRetrievalService).getPlugin();
  70. }
  71. @Override
  72. public String render(IFrameContext iframeContext, String remoteUser) throws IOException
  73. {
  74. return render(iframeContext, "", Collections.<String, String[]>emptyMap(), remoteUser);
  75. }
  76. public void renderPage(IFrameContext iframeContext, PageInfo pageInfo, String extraPath, Map<String, String[]> queryParams, String remoteUser, Writer writer) throws IOException
  77. {
  78. try
  79. {
  80. if (!pageInfo.getCondition().shouldDisplay(Collections.<String, Object>emptyMap()))
  81. {
  82. throw new PermissionDeniedException(iframeContext.getPluginKey(), "Cannot render iframe for this page");
  83. }
  84. Map<String, Object> ctx = newHashMap(iframeContext.getIFrameParams().getAsMap());
  85. if (queryParams.get("width") != null)
  86. {
  87. iframeContext.getIFrameParams().setParam("width", queryParams.get("width")[0]);
  88. }
  89. if (queryParams.get("height") != null)
  90. {
  91. iframeContext.getIFrameParams().setParam("height", queryParams.get("height")[0]);
  92. }
  93. ctx.put("queryParams", contextQueryParameters(queryParams));
  94. ctx.put("title", pageInfo.getTitle());
  95. ctx.put("contextPath", iframeHost.getContextPath());
  96. ctx.put("iframeHtml", render(iframeContext, extraPath, queryParams, remoteUser));
  97. ctx.put("decorator", pageInfo.getDecorator());
  98. for (Map.Entry<String, String> metaTag : pageInfo.getMetaTagsContent().entrySet())
  99. {
  100. ctx.put(metaTag.getKey(), metaTag.getValue());
  101. }
  102. templateRenderer.render("velocity/iframe-page" + pageInfo.getTemplateSuffix() + ".vm", ctx, writer);
  103. }
  104. catch (PermissionDeniedException ex)
  105. {
  106. templateRenderer.render(
  107. "velocity/iframe-page-accessdenied" + pageInfo.getTemplateSuffix() + ".vm",
  108. ImmutableMap.<String, Object>of(
  109. "title", pageInfo.getTitle(),
  110. "decorator", pageInfo.getDecorator()), writer);
  111. }
  112. }
  113. @Override
  114. public String render(IFrameContext iframeContext, String extraPath, Map<String, String[]> queryParams, String remoteUser) throws IOException
  115. {
  116. webResourceManager.requireResourcesForContext("remotable-plugins-iframe");
  117. RemotablePluginAccessor remotablePluginAccessor = remotablePluginAccessorFactory.get(iframeContext.getPluginKey());
  118. final URI hostUrl = iframeHost.getUrl();
  119. final URI iframeUrl = URI.create(iframeContext.getIframePath().getPath() + ObjectUtils.toString(extraPath));
  120. String[] dialog = queryParams.get("dialog");
  121. final String timeZone = userPreferencesRetriever.getTimeZoneFor(remoteUser).getID();
  122. Map<String,String[]> allParams = newHashMap(queryParams);
  123. allParams.put("user_id", new String[]{remoteUser});
  124. allParams.put("xdm_e", new String[]{hostUrl.toString()});
  125. allParams.put("xdm_c", new String[]{"channel-" + iframeContext.getNamespace()});
  126. allParams.put("xdm_p", new String[]{"1"});
  127. allParams.put("cp", new String[]{iframeHost.getContextPath()});
  128. allParams.put("tz", new String[]{timeZone});
  129. allParams.put("loc", new String[]{localeHelper.getLocaleTag()});
  130. allParams.put("lic", new String[]{licenseRetriever.getLicenseStatus(iframeContext.getPluginKey()).value()});
  131. if (dialog != null && dialog.length == 1) allParams.put("dialog", dialog);
  132. String signedUrl = remotablePluginAccessor.signGetUrl(iframeUrl, allParams);
  133. // clear xdm params as they are added by easyxdm later
  134. signedUrl = new UriBuilder(Uri.parse(signedUrl))
  135. .removeQueryParameter("xdm_e")
  136. .removeQueryParameter("xdm_c")
  137. .removeQueryParameter("xdm_p")
  138. .toString();
  139. Map<String,Object> ctx = newHashMap(iframeContext.getIFrameParams().getAsMap());
  140. ctx.put("iframeSrcHtml", escapeQuotes(signedUrl));
  141. ctx.put("plugin", remotablePluginAccessor);
  142. ctx.put("namespace", iframeContext.getNamespace());
  143. ctx.put("scriptUrls", getJavaScriptUrls());
  144. ctx.put("contextPath", iframeHost.getContextPath());
  145. ctx.put("userId", remoteUser == null ? "" : remoteUser);
  146. ctx.put("data", ImmutableMap.of("timeZone", timeZone));
  147. if (dialog != null && dialog.length == 1) ctx.put("dialog", dialog[0]);
  148. StringWriter output = new StringWriter();
  149. templateRenderer.render("velocity/iframe-body.vm", ctx, output);
  150. return output.toString();
  151. }
  152. public List<String> getJavaScriptUrls()
  153. {
  154. List<String> scripts = newArrayList();
  155. ModuleDescriptor<?> moduleDescriptor = acPlugin.getModuleDescriptor("iframe-host-js");
  156. for (ResourceDescriptor descriptor : moduleDescriptor.getResourceDescriptors())
  157. {
  158. String src = webResourceUrlProvider.getStaticPluginResourceUrl(moduleDescriptor, descriptor.getName(), UrlMode.AUTO);
  159. if (src.endsWith(".js")) {
  160. scripts.add(src);
  161. }
  162. }
  163. return scripts;
  164. }
  165. private Map<String, List<String>> contextQueryParameters(final Map<String, String[]> queryParams)
  166. {
  167. final Map<String, List<String>> ctxQueryParams = Maps.newHashMap();
  168. for (Map.Entry<String, String[]> param : queryParams.entrySet())
  169. {
  170. ctxQueryParams.put(param.getKey(), Arrays.asList(param.getValue()));
  171. }
  172. return ctxQueryParams;
  173. }
  174. }