3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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;
22 import com.google.common.base.Throwables;
24 import com.google.common.collect.HashBasedTable;
25 import com.google.common.collect.Table;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.HttpURLConnection;
30 import java.util.Date;
33 import org.apache.commons.io.IOUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.sonar.api.utils.MessageException;
37 import org.sonar.scanner.bootstrap.BatchWsClient;
38 import org.sonar.scanner.util.BatchUtils;
39 import org.sonarqube.ws.WsBatch.WsProjectResponse;
40 import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath;
41 import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings;
42 import org.sonarqube.ws.client.GetRequest;
43 import org.sonarqube.ws.client.HttpException;
45 public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
46 private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
47 private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
48 private BatchWsClient wsClient;
50 public DefaultProjectRepositoriesLoader(BatchWsClient wsClient) {
51 this.wsClient = wsClient;
55 public ProjectRepositories load(String projectKey, boolean issuesMode) {
57 GetRequest request = new GetRequest(getUrl(projectKey, issuesMode));
58 InputStream is = wsClient.call(request).contentStream();
59 return processStream(is, projectKey);
60 } catch (RuntimeException e) {
65 LOG.debug("Project repository not available - continuing without it", e);
66 return new ProjectRepositories();
70 private static String getUrl(String projectKey, boolean issuesMode) {
71 StringBuilder builder = new StringBuilder();
73 builder.append(BATCH_PROJECT_URL)
74 .append("?key=").append(BatchUtils.encodeForUrl(projectKey));
76 builder.append("&issues_mode=true");
78 return builder.toString();
81 private static boolean shouldThrow(Exception e) {
82 for (Throwable t : Throwables.getCausalChain(e)) {
83 if (t instanceof HttpException) {
84 HttpException http = (HttpException) t;
85 return http.code() != HttpURLConnection.HTTP_NOT_FOUND;
87 if (t instanceof MessageException) {
95 private static ProjectRepositories processStream(InputStream is, String projectKey) {
97 WsProjectResponse response = WsProjectResponse.parseFrom(is);
99 Table<String, String, FileData> fileDataTable = HashBasedTable.create();
100 Table<String, String, String> settings = HashBasedTable.create();
102 Map<String, Settings> settingsByModule = response.getSettingsByModule();
103 for (Map.Entry<String, Settings> e1 : settingsByModule.entrySet()) {
104 for (Map.Entry<String, String> e2 : e1.getValue().getSettings().entrySet()) {
105 settings.put(e1.getKey(), e2.getKey(), e2.getValue());
109 Map<String, FileDataByPath> fileDataByModuleAndPath = response.getFileDataByModuleAndPath();
110 for (Map.Entry<String, FileDataByPath> e1 : fileDataByModuleAndPath.entrySet()) {
111 for (Map.Entry<String, org.sonarqube.ws.WsBatch.WsProjectResponse.FileData> e2 : e1.getValue().getFileDataByPath().entrySet()) {
112 FileData fd = new FileData(e2.getValue().getHash(), e2.getValue().getRevision());
113 fileDataTable.put(e1.getKey(), e2.getKey(), fd);
117 return new ProjectRepositories(settings, fileDataTable, new Date(response.getLastAnalysisDate()));
118 } catch (IOException e) {
119 throw new IllegalStateException("Couldn't load project repository for " + projectKey, e);
121 IOUtils.closeQuietly(is);