You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TomcatContexts.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.app;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.Map;
  25. import org.apache.catalina.core.StandardContext;
  26. import org.apache.catalina.startup.Tomcat;
  27. import org.apache.commons.io.FileUtils;
  28. import org.sonar.api.utils.MessageException;
  29. import org.sonar.process.Props;
  30. import static java.lang.String.format;
  31. import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
  32. import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
  33. /**
  34. * Configures Tomcat contexts:
  35. * <ul>
  36. * <li>/deploy delivers the plugins required by analyzers. It maps directory ${sonar.path.data}/web/deploy.</li>
  37. * <li>/ is the regular webapp</li>
  38. * </ul>
  39. */
  40. public class TomcatContexts {
  41. private static final String PROPERTY_CONTEXT = "sonar.web.context";
  42. private static final String WEB_DEPLOY_PATH_RELATIVE_TO_DATA_DIR = "web/deploy";
  43. private final Fs fs;
  44. public TomcatContexts() {
  45. this.fs = new Fs();
  46. }
  47. @VisibleForTesting
  48. TomcatContexts(Fs fs) {
  49. this.fs = fs;
  50. }
  51. public StandardContext configure(Tomcat tomcat, Props props) {
  52. addStaticDir(tomcat, getContextPath(props) + "/deploy", new File(props.nonNullValueAsFile(PATH_DATA.getKey()), WEB_DEPLOY_PATH_RELATIVE_TO_DATA_DIR));
  53. StandardContext webapp = addContext(tomcat, getContextPath(props), webappDir(props));
  54. for (Map.Entry<Object, Object> entry : props.rawProperties().entrySet()) {
  55. String key = entry.getKey().toString();
  56. webapp.addParameter(key, entry.getValue().toString());
  57. }
  58. return webapp;
  59. }
  60. static String getContextPath(Props props) {
  61. String context = props.value(PROPERTY_CONTEXT, "");
  62. if ("/".equals(context)) {
  63. context = "";
  64. } else if (!"".equals(context) && context != null && !context.startsWith("/")) {
  65. throw MessageException.of(format("Value of '%s' must start with a forward slash: '%s'", PROPERTY_CONTEXT, context));
  66. }
  67. return context;
  68. }
  69. @VisibleForTesting
  70. StandardContext addStaticDir(Tomcat tomcat, String contextPath, File dir) {
  71. try {
  72. fs.createOrCleanupDir(dir);
  73. } catch (IOException e) {
  74. throw new IllegalStateException(format("Fail to create or clean-up directory %s", dir.getAbsolutePath()), e);
  75. }
  76. return addContext(tomcat, contextPath, dir);
  77. }
  78. private static StandardContext addContext(Tomcat tomcat, String contextPath, File dir) {
  79. try {
  80. StandardContext context = (StandardContext) tomcat.addWebapp(contextPath, dir.getAbsolutePath());
  81. context.setClearReferencesHttpClientKeepAliveThread(false);
  82. context.setClearReferencesStopThreads(false);
  83. context.setClearReferencesStopTimerThreads(false);
  84. context.setClearReferencesStopTimerThreads(false);
  85. context.setAntiResourceLocking(false);
  86. context.setReloadable(false);
  87. context.setUseHttpOnly(true);
  88. context.setTldValidation(false);
  89. context.setXmlValidation(false);
  90. context.setXmlNamespaceAware(false);
  91. context.setUseNaming(false);
  92. context.setDelegate(true);
  93. context.setJarScanner(new NullJarScanner());
  94. context.setAllowCasualMultipartParsing(true);
  95. context.setCookies(false);
  96. // disable JSP and WebSocket support
  97. context.setContainerSciFilter("org.apache.tomcat.websocket.server.WsSci|org.apache.jasper.servlet.JasperInitializer");
  98. return context;
  99. } catch (Exception e) {
  100. throw new IllegalStateException("Fail to configure webapp from " + dir, e);
  101. }
  102. }
  103. private static File webappDir(Props props) {
  104. return new File(props.value(PATH_HOME.getKey()), "web");
  105. }
  106. static class Fs {
  107. void createOrCleanupDir(File dir) throws IOException {
  108. FileUtils.forceMkdir(dir);
  109. org.sonar.core.util.FileUtils.cleanDirectory(dir);
  110. }
  111. }
  112. }