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

/plugin/src/main/java/com/atlassian/plugin/remotable/plugin/module/page/RemotePageDescriptorCreator.java

https://bitbucket.org/rodogu/remotable-plugins
Java | 176 lines | 152 code | 20 blank | 4 comment | 0 complexity | 260fea9a8f5a9a1e46b9e54600da3844 MD5 | raw file
  1. package com.atlassian.plugin.remotable.plugin.module.page;
  2. import com.atlassian.plugin.ModuleDescriptor;
  3. import com.atlassian.plugin.Plugin;
  4. import com.atlassian.plugin.PluginParseException;
  5. import com.atlassian.plugin.module.ModuleFactory;
  6. import com.atlassian.plugin.remotable.plugin.module.IFrameParamsImpl;
  7. import com.atlassian.plugin.remotable.plugin.module.IFrameRendererImpl;
  8. import com.atlassian.plugin.remotable.plugin.module.WebItemContext;
  9. import com.atlassian.plugin.remotable.plugin.module.WebItemCreator;
  10. import com.atlassian.plugin.remotable.plugin.integration.plugins.DescriptorToRegister;
  11. import com.atlassian.plugin.remotable.plugin.util.node.Node;
  12. import com.atlassian.plugin.remotable.spi.module.IFrameParams;
  13. import com.atlassian.plugin.remotable.spi.product.ProductAccessor;
  14. import com.atlassian.plugin.servlet.ServletModuleManager;
  15. import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
  16. import com.atlassian.plugin.web.Condition;
  17. import com.atlassian.plugin.web.conditions.AlwaysDisplayCondition;
  18. import com.atlassian.sal.api.user.UserManager;
  19. import com.google.common.collect.ImmutableSet;
  20. import com.google.common.collect.Maps;
  21. import org.dom4j.DocumentHelper;
  22. import org.dom4j.Element;
  23. import org.osgi.framework.BundleContext;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.stereotype.Component;
  26. import java.net.URI;
  27. import java.util.Map;
  28. import static com.atlassian.plugin.remotable.plugin.util.OsgiServiceUtils.getService;
  29. import static com.google.common.base.Preconditions.checkNotNull;
  30. /**
  31. * Creates a builder for remote page descriptor generation. Builder instances meant to be shared
  32. * across threads.
  33. */
  34. @Component
  35. public final class RemotePageDescriptorCreator
  36. {
  37. private final BundleContext bundleContext;
  38. private final UserManager userManager;
  39. private final WebItemCreator webItemCreator;
  40. private final IFrameRendererImpl iFrameRenderer;
  41. private final ProductAccessor productAccessor;
  42. @Autowired
  43. public RemotePageDescriptorCreator(
  44. BundleContext bundleContext, UserManager userManager,
  45. WebItemCreator webItemCreator, IFrameRendererImpl iFrameRenderer,
  46. ProductAccessor productAccessor)
  47. {
  48. this.bundleContext = bundleContext;
  49. this.userManager = userManager;
  50. this.webItemCreator = webItemCreator;
  51. this.iFrameRenderer = iFrameRenderer;
  52. this.productAccessor = productAccessor;
  53. }
  54. public Builder newBuilder()
  55. {
  56. return new Builder();
  57. }
  58. public static URI createLocalUrl(String pluginKey, String pageUrl)
  59. {
  60. return URI.create("/remotable-plugins/" + pluginKey + (pageUrl.startsWith("/") ? "" : "/") + pageUrl);
  61. }
  62. public class Builder
  63. {
  64. private WebItemCreator.Builder webItemCreatorBuilder;
  65. private String decorator = "";
  66. private String templateSuffix = "";
  67. private Condition condition = new AlwaysDisplayCondition();
  68. private Map<String, String> metaTagsContent = Maps.newHashMap();
  69. public Builder()
  70. {
  71. this.webItemCreatorBuilder = webItemCreator.newBuilder();
  72. this.webItemCreatorBuilder.setPreferredWeight(productAccessor.getPreferredGeneralWeight());
  73. this.webItemCreatorBuilder.setPreferredSectionKey(productAccessor.getPreferredGeneralSectionKey());
  74. this.webItemCreatorBuilder.setContextParams(productAccessor.getLinkContextParams());
  75. this.webItemCreatorBuilder.setCondition(condition.getClass());
  76. }
  77. public Iterable<DescriptorToRegister> build(Plugin plugin, Node descriptor)
  78. {
  79. checkNotNull(decorator);
  80. String key = descriptor.get("key").asString();
  81. final URI url = descriptor.get("url").asURI();
  82. URI localUrl = createLocalUrl(plugin.getKey(), key);
  83. DescriptorToRegister webItemModuleDescriptor = new DescriptorToRegister(webItemCreatorBuilder.build(plugin, key, localUrl, descriptor));
  84. return ImmutableSet.of(
  85. createServletDescriptor(plugin, descriptor, key, url, localUrl),
  86. webItemModuleDescriptor);
  87. }
  88. private DescriptorToRegister createServletDescriptor(
  89. final Plugin plugin,
  90. Node e,
  91. String key,
  92. final URI path,
  93. URI localUrl
  94. )
  95. {
  96. final String pageName = e.get("name").asString();
  97. Element config = DocumentHelper.createElement("servlet");
  98. final String moduleKey = "servlet-" + key;
  99. config.addAttribute("key", moduleKey);
  100. config.addAttribute("system", "true");
  101. config.addAttribute("class", IFramePageServlet.class.getName());
  102. config.addElement("url-pattern").setText(localUrl + "");
  103. config.addElement("url-pattern").setText(localUrl + "/*");
  104. final IFrameParams params = new IFrameParamsImpl(e);
  105. final ServletModuleDescriptor descriptor = new ServletModuleDescriptor(new ModuleFactory()
  106. {
  107. @Override
  108. public <T> T createModule(String name, ModuleDescriptor<T> moduleDescriptor) throws
  109. PluginParseException
  110. {
  111. PageInfo pageInfo = new PageInfo(decorator, templateSuffix, pageName, condition, metaTagsContent);
  112. return (T) new IFramePageServlet(
  113. pageInfo,
  114. iFrameRenderer,
  115. new IFrameContextImpl(plugin.getKey(), path, moduleKey, params), userManager
  116. );
  117. }
  118. }, getService(bundleContext, ServletModuleManager.class));
  119. descriptor.init(plugin, config);
  120. return new DescriptorToRegister(descriptor);
  121. }
  122. public Builder setDecorator(String decorator)
  123. {
  124. this.decorator = decorator;
  125. return this;
  126. }
  127. public Builder setTemplateSuffix(String templateSuffix)
  128. {
  129. this.templateSuffix = templateSuffix;
  130. return this;
  131. }
  132. public Builder setCondition(Condition condition)
  133. {
  134. this.condition = condition;
  135. webItemCreatorBuilder.setCondition(condition.getClass());
  136. return this;
  137. }
  138. public Builder setWebItemStyleClass(String webItemStyleClass)
  139. {
  140. webItemCreatorBuilder.setAdditionalStyleClass(webItemStyleClass);
  141. return this;
  142. }
  143. public Builder setWebItemContext(WebItemContext webItemContext)
  144. {
  145. webItemCreatorBuilder.setContextParams(webItemContext.getContextParams())
  146. .setPreferredSectionKey(webItemContext.getPreferredSectionKey())
  147. .setPreferredWeight(webItemContext.getPreferredWeight());
  148. return this;
  149. }
  150. public Builder setMetaTagContent(String name, String content)
  151. {
  152. metaTagsContent.put(name, content);
  153. return this;
  154. }
  155. }
  156. }