3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.repository.settings;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.HttpURLConnection;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import javax.annotation.Nullable;
32 import org.apache.commons.lang.StringEscapeUtils;
33 import org.sonar.api.impl.utils.ScannerUtils;
34 import org.sonar.api.utils.log.Logger;
35 import org.sonar.api.utils.log.Loggers;
36 import org.sonar.api.utils.log.Profiler;
37 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
38 import org.sonarqube.ws.Settings;
39 import org.sonarqube.ws.client.GetRequest;
40 import org.sonarqube.ws.client.HttpException;
42 public abstract class AbstractSettingsLoader {
44 private static final Logger LOG = Loggers.get(AbstractSettingsLoader.class);
45 private final DefaultScannerWsClient wsClient;
47 public AbstractSettingsLoader(final DefaultScannerWsClient wsClient) {
48 this.wsClient = wsClient;
51 Map<String, String> load(@Nullable String componentKey) {
52 String url = "api/settings/values.protobuf";
53 Profiler profiler = Profiler.create(LOG);
54 if (componentKey != null) {
55 url += "?component=" + ScannerUtils.encodeForUrl(componentKey);
56 profiler.startInfo(String.format("Load project settings for component key: '%s'", componentKey));
58 profiler.startInfo("Load global settings");
60 try (InputStream is = wsClient.call(new GetRequest(url)).contentStream()) {
61 Settings.ValuesWsResponse values = Settings.ValuesWsResponse.parseFrom(is);
63 return toMap(values.getSettingsList());
64 } catch (HttpException e) {
65 if (e.code() == HttpURLConnection.HTTP_NOT_FOUND) {
66 return Collections.emptyMap();
69 } catch (IOException e) {
70 throw new IllegalStateException("Unable to load settings", e);
74 static Map<String, String> toMap(List<Settings.Setting> settingsList) {
75 Map<String, String> result = new LinkedHashMap<>();
76 for (Settings.Setting s : settingsList) {
77 if (!s.getInherited()) {
78 switch (s.getValueOneOfCase()) {
80 result.put(s.getKey(), s.getValue());
83 result.put(s.getKey(), s.getValues().getValuesList().stream().map(StringEscapeUtils::escapeCsv).collect(Collectors.joining(",")));
86 convertPropertySetToProps(result, s);
89 if (!s.getKey().endsWith(".secured")) {
90 throw new IllegalStateException("Unknown property value for " + s.getKey());
98 private static void convertPropertySetToProps(Map<String, String> result, Settings.Setting s) {
99 List<String> ids = new ArrayList<>();
101 for (Settings.FieldValues.Value v : s.getFieldValues().getFieldValuesList()) {
102 for (Map.Entry<String, String> entry : v.getValueMap().entrySet()) {
103 result.put(s.getKey() + "." + id + "." + entry.getKey(), entry.getValue());
105 ids.add(String.valueOf(id));
108 result.put(s.getKey(), String.join(",", ids));