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.server.pushapi.sonarlint;
22 import java.io.IOException;
23 import java.util.List;
25 import java.util.stream.Collectors;
26 import javax.servlet.AsyncContext;
27 import javax.servlet.http.HttpServletResponse;
28 import org.sonar.api.server.ws.Request;
29 import org.sonar.api.server.ws.Response;
30 import org.sonar.api.server.ws.WebService;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.project.ProjectDto;
34 import org.sonar.server.pushapi.ServerPushAction;
35 import org.sonar.server.user.UserSession;
36 import org.sonar.server.ws.ServletRequest;
37 import org.sonar.server.ws.ServletResponse;
39 public class SonarLintPushAction extends ServerPushAction {
41 private static final String PROJECT_PARAM_KEY = "projectKeys";
42 private static final String LANGUAGE_PARAM_KEY = "languages";
43 private final SonarLintClientsRegistry clientsRegistry;
44 private final SonarLintClientPermissionsValidator permissionsValidator;
45 private final UserSession userSession;
46 private final DbClient dbClient;
47 private final SonarLintPushEventExecutorService sonarLintPushEventExecutorService;
49 public SonarLintPushAction(SonarLintClientsRegistry sonarLintClientRegistry, UserSession userSession, DbClient dbClient,
50 SonarLintClientPermissionsValidator permissionsValidator, SonarLintPushEventExecutorService sonarLintPushEventExecutorService) {
51 this.clientsRegistry = sonarLintClientRegistry;
52 this.userSession = userSession;
53 this.dbClient = dbClient;
54 this.permissionsValidator = permissionsValidator;
55 this.sonarLintPushEventExecutorService = sonarLintPushEventExecutorService;
59 public void define(WebService.NewController controller) {
60 WebService.NewAction action = controller
61 .createAction("sonarlint_events")
63 .setDescription("Endpoint for listening to server side events. Currently it notifies listener about change to activation of a rule")
68 .createParam(PROJECT_PARAM_KEY)
69 .setDescription("Comma-separated list of projects keys for which events will be delivered")
71 .setExampleValue("example-project-key,example-project-key2");
74 .createParam(LANGUAGE_PARAM_KEY)
75 .setDescription("Comma-separated list of languages for which events will be delivered")
77 .setExampleValue("java,cobol");
81 public void handle(Request request, Response response) throws IOException {
82 userSession.checkLoggedIn();
84 ServletRequest servletRequest = (ServletRequest) request;
85 ServletResponse servletResponse = (ServletResponse) response;
87 var params = new SonarLintPushActionParamsValidator(request);
88 params.validateParams();
90 List<ProjectDto> projectDtos = permissionsValidator.validateUserCanReceivePushEventForProjects(userSession, params.projectKeys);
92 if (!isServerSideEventsRequest(servletRequest)) {
93 servletResponse.stream().setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
97 setHeadersForResponse(servletResponse);
99 AsyncContext asyncContext = servletRequest.startAsync();
100 asyncContext.setTimeout(0);
102 Set<String> projectUuids = projectDtos.stream().map(ProjectDto::getUuid).collect(Collectors.toSet());
104 SonarLintClient sonarLintClient = new SonarLintClient(sonarLintPushEventExecutorService, asyncContext, projectUuids, params.getLanguages(), userSession.getUuid());
106 clientsRegistry.registerClient(sonarLintClient);
109 class SonarLintPushActionParamsValidator {
111 private final Request request;
112 private final Set<String> projectKeys;
113 private final Set<String> languages;
115 SonarLintPushActionParamsValidator(Request request) {
116 this.request = request;
117 this.projectKeys = parseParam(PROJECT_PARAM_KEY);
118 this.languages = parseParam(LANGUAGE_PARAM_KEY);
121 Set<String> getLanguages() {
125 private Set<String> parseParam(String paramKey) {
126 String paramProjectKeys = request.getParam(paramKey).getValue();
127 if (paramProjectKeys == null) {
128 throw new IllegalArgumentException("Param " + paramKey + " was not provided.");
130 return Set.of(paramProjectKeys.trim().split(","));
133 private void validateParams() {
134 List<ProjectDto> projectDtos;
135 try (DbSession dbSession = dbClient.openSession(false)) {
136 projectDtos = dbClient.projectDao().selectProjectsByKeys(dbSession, projectKeys);
138 if (projectDtos.size() < projectKeys.size() || projectDtos.isEmpty()) {
139 throw new IllegalArgumentException("Param " + PROJECT_PARAM_KEY + " is invalid.");