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.

UpdateCenterClient.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.plugins;
  21. import java.io.InputStream;
  22. import java.net.URI;
  23. import java.net.URISyntaxException;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.Date;
  26. import java.util.Optional;
  27. import org.apache.commons.io.IOUtils;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.sonar.api.Properties;
  31. import org.sonar.api.Property;
  32. import org.sonar.api.config.Configuration;
  33. import org.sonar.api.utils.UriReader;
  34. import org.sonar.process.ProcessProperties;
  35. import org.sonar.updatecenter.common.UpdateCenter;
  36. import org.sonar.updatecenter.common.UpdateCenterDeserializer;
  37. import org.sonar.updatecenter.common.UpdateCenterDeserializer.Mode;
  38. /**
  39. * HTTP client to load data from the remote update center hosted at https://downloads.sonarsource.com/?prefix=sonarqube/update
  40. *
  41. * @since 2.4
  42. */
  43. @Properties({
  44. @Property(
  45. key = UpdateCenterClient.URL_PROPERTY,
  46. defaultValue = UpdateCenterClient.URL_DEFAULT_VALUE,
  47. name = "Update Center URL",
  48. category = "Update Center",
  49. // hidden from UI
  50. global = false),
  51. @Property(
  52. key = UpdateCenterClient.CACHE_TTL_PROPERTY,
  53. defaultValue = UpdateCenterClient.CACHE_TTL_DEFAULT_VALUE,
  54. name = "Update Center cache time-to-live in milliseconds",
  55. category = "Update Center",
  56. // hidden from UI
  57. global = false)
  58. })
  59. public class UpdateCenterClient {
  60. private static final Logger LOG = LoggerFactory.getLogger(UpdateCenterClient.class);
  61. static final String URL_PROPERTY = "sonar.updatecenter.url";
  62. static final String URL_DEFAULT_VALUE = "https://downloads.sonarsource.com/sonarqube/update/update-center.properties";
  63. static final String CACHE_TTL_PROPERTY = "sonar.updatecenter.cache.ttl";
  64. static final String CACHE_TTL_DEFAULT_VALUE = "3600000";
  65. private final long periodInMilliseconds;
  66. private final URI uri;
  67. private final UriReader uriReader;
  68. private final boolean isActivated;
  69. private UpdateCenter pluginCenter = null;
  70. private long lastRefreshDate = 0;
  71. public UpdateCenterClient(UriReader uriReader, Configuration config) throws URISyntaxException {
  72. this.uriReader = uriReader;
  73. this.uri = new URI(config.get(URL_PROPERTY).get());
  74. this.isActivated = config.getBoolean(ProcessProperties.Property.SONAR_UPDATECENTER_ACTIVATE.getKey()).get();
  75. this.periodInMilliseconds = Long.parseLong(config.get(CACHE_TTL_PROPERTY).get());
  76. LOG.info("Update center: {}", uriReader.description(uri));
  77. }
  78. public Optional<UpdateCenter> getUpdateCenter() {
  79. return getUpdateCenter(false);
  80. }
  81. public Optional<UpdateCenter> getUpdateCenter(boolean forceRefresh) {
  82. if (!isActivated) {
  83. return Optional.empty();
  84. }
  85. if (pluginCenter == null || forceRefresh || needsRefresh()) {
  86. pluginCenter = init();
  87. lastRefreshDate = System.currentTimeMillis();
  88. }
  89. return Optional.ofNullable(pluginCenter);
  90. }
  91. public Date getLastRefreshDate() {
  92. return lastRefreshDate > 0 ? new Date(lastRefreshDate) : null;
  93. }
  94. private boolean needsRefresh() {
  95. return lastRefreshDate + periodInMilliseconds < System.currentTimeMillis();
  96. }
  97. private UpdateCenter init() {
  98. InputStream input = null;
  99. try {
  100. String content = uriReader.readString(uri, StandardCharsets.UTF_8);
  101. java.util.Properties properties = new java.util.Properties();
  102. input = IOUtils.toInputStream(content, StandardCharsets.UTF_8);
  103. properties.load(input);
  104. return new UpdateCenterDeserializer(Mode.PROD, true).fromProperties(properties);
  105. } catch (Exception e) {
  106. LoggerFactory.getLogger(getClass()).error("Fail to connect to update center", e);
  107. return null;
  108. } finally {
  109. IOUtils.closeQuietly(input);
  110. }
  111. }
  112. }