PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-plugins-osgi-testrunner-bundle/src/main/java/com/atlassian/plugins/osgi/test/servlet/TestConsoleServlet.java

https://bitbucket.org/Adaptavist/atlassian-plugins-osgi-testrunner-parent
Java | 246 lines | 209 code | 37 blank | 0 comment | 26 complexity | 721fcf6a38f05bc73a9331ac25b73fc8 MD5 | raw file
  1. package com.atlassian.plugins.osgi.test.servlet;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.util.*;
  5. import javax.annotation.Nullable;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import com.atlassian.plugin.Plugin;
  11. import com.atlassian.plugin.PluginAccessor;
  12. import com.atlassian.plugin.osgi.util.OsgiHeaderUtil;
  13. import com.atlassian.plugins.osgi.test.BundleTestClassesManager;
  14. import com.atlassian.plugins.osgi.test.OsgiTestClassLoader;
  15. import com.atlassian.sal.api.ApplicationProperties;
  16. import com.atlassian.sal.api.auth.LoginUriProvider;
  17. import com.atlassian.sal.api.user.UserManager;
  18. import com.atlassian.sal.api.websudo.WebSudoManager;
  19. import com.atlassian.sal.api.websudo.WebSudoSessionException;
  20. import com.atlassian.templaterenderer.TemplateRenderer;
  21. import com.google.common.base.Function;
  22. import com.google.common.collect.Collections2;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.osgi.framework.Bundle;
  25. import org.springframework.osgi.io.internal.OsgiHeaderUtils;
  26. public class TestConsoleServlet extends HttpServlet
  27. {
  28. static final String JIRA_SERAPH_SECURITY_ORIGINAL_URL = "os_security_originalurl";
  29. static final String CONF_SERAPH_SECURITY_ORIGINAL_URL = "seraph_originalurl";
  30. public static final String CONSOLE_TEMPLATE = "/templates/it-test-console.vm";
  31. public static final String BUNDLE_TEMPLATE = "/templates/it-test-bundles.vm";
  32. public static final String ERROR_TEMPLATE = "/templates/it-test-error.vm";
  33. public static final String BUNDLE_PARAM = "bundle";
  34. private final TemplateRenderer renderer;
  35. private final ApplicationProperties applicationProperties;
  36. private final PluginAccessor pluginAccessor;
  37. private final OsgiTestClassLoader testLoader;
  38. private final LoginUriProvider loginUriProvider;
  39. private final WebSudoManager webSudoManager;
  40. private final UserManager userManager;
  41. public TestConsoleServlet(TemplateRenderer renderer, ApplicationProperties applicationProperties, PluginAccessor pluginAccessor, OsgiTestClassLoader testLoader, LoginUriProvider loginUriProvider, WebSudoManager webSudoManager, UserManager userManager)
  42. {
  43. this.renderer = renderer;
  44. this.applicationProperties = applicationProperties;
  45. this.pluginAccessor = pluginAccessor;
  46. this.testLoader = testLoader;
  47. this.loginUriProvider = loginUriProvider;
  48. this.webSudoManager = webSudoManager;
  49. this.userManager = userManager;
  50. }
  51. @Override
  52. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  53. {
  54. try
  55. {
  56. webSudoManager.willExecuteWebSudoRequest(request);
  57. if(!isAdmin())
  58. {
  59. redirectToLogin(request,response);
  60. return;
  61. }
  62. }
  63. catch (WebSudoSessionException wse)
  64. {
  65. webSudoManager.enforceWebSudoProtection(request, response);
  66. return;
  67. }
  68. response.setContentType("text/html;charset=utf-8");
  69. final Map<String, Object> context = new HashMap<String, Object>();
  70. context.put("contextPath",applicationProperties.getBaseUrl());
  71. context.put("productName",applicationProperties.getDisplayName().toLowerCase());
  72. BundleTestClassesManager manager = BundleTestClassesManager.instance();
  73. String bundle = request.getParameter(BUNDLE_PARAM);
  74. Set<Bundle> osgiBundles = new HashSet<Bundle>(testLoader.findAllBundles());
  75. Set<Bundle> testBundles = new HashSet<Bundle>(osgiBundles);
  76. testBundles.addAll(manager.getAllBundles());
  77. if(null == bundle)
  78. {
  79. if(testBundles.size() > 1)
  80. {
  81. List<BundleNameAndPlugin> bundleNamesAndPlugins = new ArrayList<BundleNameAndPlugin>();
  82. for(Bundle testBundle : testBundles)
  83. {
  84. Plugin plugin = null;
  85. String pluginKey = OsgiHeaderUtil.getPluginKey(testBundle);
  86. if (StringUtils.isNotBlank(pluginKey))
  87. {
  88. plugin = pluginAccessor.getPlugin(pluginKey);
  89. }
  90. if(null != plugin)
  91. {
  92. bundleNamesAndPlugins.add(new BundleNameAndPlugin(testBundle.getSymbolicName(),plugin));
  93. }
  94. }
  95. context.put("bundleNamesAndPlugins",bundleNamesAndPlugins);
  96. renderer.render(BUNDLE_TEMPLATE,context,response.getWriter());
  97. return;
  98. }
  99. else if(testBundles.size() > 0)
  100. {
  101. bundle = testBundles.iterator().next().getSymbolicName();
  102. }
  103. }
  104. if(null != bundle && (hasBundleKey(bundle,testBundles)))
  105. {
  106. Bundle ourBundle = getBundleForKey(bundle,testBundles);
  107. String pluginKey = OsgiHeaderUtil.getPluginKey(ourBundle);
  108. Plugin plugin = null;
  109. if (StringUtils.isNotBlank(pluginKey))
  110. {
  111. plugin = pluginAccessor.getPlugin(pluginKey);
  112. }
  113. if(null != plugin)
  114. {
  115. context.put("testPlugin",plugin);
  116. }
  117. if(testBundles.size() > 1)
  118. {
  119. context.put("hasMultipleTestPlugins",true);
  120. }
  121. else
  122. {
  123. context.put("hasMultipleTestPlugins",false);
  124. }
  125. context.put("bundleSymbolicName",bundle);
  126. renderer.render(CONSOLE_TEMPLATE,context,response.getWriter());
  127. }
  128. else
  129. {
  130. context.put("bundleSymbolicName",bundle);
  131. renderer.render(ERROR_TEMPLATE,context,response.getWriter());
  132. }
  133. }
  134. private boolean hasBundleKey(String bundleKey, Set<Bundle> bundles)
  135. {
  136. boolean hasKey = false;
  137. for(Bundle bundle : bundles)
  138. {
  139. if(bundle.getSymbolicName().equals(bundleKey))
  140. {
  141. hasKey = true;
  142. break;
  143. }
  144. }
  145. return hasKey;
  146. }
  147. private Bundle getBundleForKey(String bundleKey, Set<Bundle> bundles)
  148. {
  149. Bundle foundBundle = null;
  150. for(Bundle bundle : bundles)
  151. {
  152. if(bundle.getSymbolicName().equals(bundleKey))
  153. {
  154. foundBundle = bundle;
  155. break;
  156. }
  157. }
  158. return foundBundle;
  159. }
  160. protected boolean isAdmin()
  161. {
  162. if (userManager.getRemoteUsername() == null)
  163. {
  164. return false;
  165. }
  166. return userManager.isSystemAdmin(userManager.getRemoteUsername()) || userManager.isAdmin(userManager.getRemoteUsername());
  167. }
  168. protected void redirectToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException
  169. {
  170. final URI uri = getUri(request);
  171. String uriString = uri.toASCIIString();
  172. request.getSession().setAttribute(JIRA_SERAPH_SECURITY_ORIGINAL_URL, uriString);
  173. request.getSession().setAttribute(CONF_SERAPH_SECURITY_ORIGINAL_URL, uriString);
  174. response.sendRedirect(loginUriProvider.getLoginUri(uri).toASCIIString());
  175. }
  176. protected URI getUri(HttpServletRequest request)
  177. {
  178. StringBuffer builder = request.getRequestURL();
  179. if (request.getQueryString() != null)
  180. {
  181. builder.append("?");
  182. builder.append(request.getQueryString());
  183. }
  184. return URI.create(builder.toString());
  185. }
  186. public class BundleNameAndPlugin {
  187. private final String bundleSymbolicName;
  188. private final String bundleDivId;
  189. private final Plugin plugin;
  190. public BundleNameAndPlugin(String bundleSymbolicName, Plugin plugin)
  191. {
  192. this.bundleSymbolicName = bundleSymbolicName;
  193. this.bundleDivId = bundleSymbolicName.replaceAll("\\.","_");
  194. this.plugin = plugin;
  195. }
  196. public String getBundleSymbolicName()
  197. {
  198. return bundleSymbolicName;
  199. }
  200. public String getBundleDivId()
  201. {
  202. return bundleDivId;
  203. }
  204. public Plugin getPlugin()
  205. {
  206. return plugin;
  207. }
  208. }
  209. }