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.

ScannerWsClientProvider.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.scanner.bootstrap;
  21. import org.picocontainer.injectors.ProviderAdapter;
  22. import org.sonar.api.CoreProperties;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  25. import org.sonarqube.ws.client.HttpConnector;
  26. import org.sonarqube.ws.client.WsClientFactories;
  27. import static java.lang.Integer.parseInt;
  28. import static java.lang.String.valueOf;
  29. import static org.apache.commons.lang.StringUtils.defaultIfBlank;
  30. public class ScannerWsClientProvider extends ProviderAdapter {
  31. static final int CONNECT_TIMEOUT_MS = 5_000;
  32. static final String READ_TIMEOUT_SEC_PROPERTY = "sonar.ws.timeout";
  33. static final int DEFAULT_READ_TIMEOUT_SEC = 60;
  34. private DefaultScannerWsClient wsClient;
  35. public synchronized DefaultScannerWsClient provide(final RawScannerProperties scannerProps,
  36. final EnvironmentInformation env, GlobalAnalysisMode globalMode, System2 system) {
  37. if (wsClient == null) {
  38. String url = defaultIfBlank(scannerProps.property("sonar.host.url"), "http://localhost:9000");
  39. HttpConnector.Builder connectorBuilder = HttpConnector.newBuilder();
  40. String timeoutSec = defaultIfBlank(scannerProps.property(READ_TIMEOUT_SEC_PROPERTY), valueOf(DEFAULT_READ_TIMEOUT_SEC));
  41. String token = defaultIfBlank(system.envVariable("SONAR_TOKEN"), null);
  42. String login = defaultIfBlank(scannerProps.property(CoreProperties.LOGIN), token);
  43. connectorBuilder
  44. .readTimeoutMilliseconds(parseInt(timeoutSec) * 1_000)
  45. .connectTimeoutMilliseconds(CONNECT_TIMEOUT_MS)
  46. .userAgent(env.toString())
  47. .url(url)
  48. .credentials(login, scannerProps.property(CoreProperties.PASSWORD));
  49. // OkHttp detect 'http.proxyHost' java property, but credentials should be filled
  50. final String proxyUser = System.getProperty("http.proxyUser", "");
  51. if (!proxyUser.isEmpty()) {
  52. connectorBuilder.proxyCredentials(proxyUser, System.getProperty("http.proxyPassword"));
  53. }
  54. wsClient = new DefaultScannerWsClient(WsClientFactories.getDefault().newClient(connectorBuilder.build()), login != null, globalMode);
  55. }
  56. return wsClient;
  57. }
  58. }