]> source.dussan.org Git - sonarqube.git/blob
62d05308fad3cce6549f8b11f8dff4cf2613f796
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.notification;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import java.io.IOException;
24 import java.io.InvalidClassException;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30 import javax.annotation.Nullable;
31 import org.sonar.api.notifications.Notification;
32 import org.sonar.api.notifications.NotificationChannel;
33 import org.sonar.api.utils.SonarException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbSession;
38 import org.sonar.db.EmailSubscriberDto;
39 import org.sonar.db.notification.NotificationQueueDto;
40 import org.sonar.server.notification.email.EmailNotificationChannel;
41
42 import static java.util.Collections.emptySet;
43 import static java.util.Collections.singletonList;
44 import static java.util.Objects.requireNonNull;
45
46 public class DefaultNotificationManager implements NotificationManager {
47
48   private static final Logger LOG = LoggerFactory.getLogger(DefaultNotificationManager.class);
49
50   private static final String UNABLE_TO_READ_NOTIFICATION = "Unable to read notification";
51
52   private final NotificationChannel[] notificationChannels;
53   private final DbClient dbClient;
54   private boolean alreadyLoggedDeserializationIssue = false;
55
56   public DefaultNotificationManager(NotificationChannel[] channels, DbClient dbClient) {
57     this.notificationChannels = channels;
58     this.dbClient = dbClient;
59   }
60
61   /**
62    * {@inheritDoc}
63    */
64   @Override
65   public <T extends Notification> void scheduleForSending(T notification) {
66     NotificationQueueDto dto = NotificationQueueDto.toNotificationQueueDto(notification);
67     dbClient.notificationQueueDao().insert(singletonList(dto));
68   }
69
70   /**
71    * Give the notification queue so that it can be processed
72    */
73   public <T extends Notification> T getFromQueue() {
74     int batchSize = 1;
75     List<NotificationQueueDto> notificationDtos = dbClient.notificationQueueDao().selectOldest(batchSize);
76     if (notificationDtos.isEmpty()) {
77       return null;
78     }
79     dbClient.notificationQueueDao().delete(notificationDtos);
80
81     return convertToNotification(notificationDtos);
82   }
83
84   private <T extends Notification> T convertToNotification(List<NotificationQueueDto> notifications) {
85     try {
86       // If batchSize is increased then we should return a list instead of a single element
87       return notifications.get(0).toNotification();
88     } catch (InvalidClassException e) {
89       // SONAR-4739
90       if (!alreadyLoggedDeserializationIssue) {
91         logDeserializationIssue();
92         alreadyLoggedDeserializationIssue = true;
93       }
94       return null;
95     } catch (IOException | ClassNotFoundException e) {
96       throw new SonarException(UNABLE_TO_READ_NOTIFICATION, e);
97     }
98   }
99
100   @VisibleForTesting
101   void logDeserializationIssue() {
102     LOG.warn("It is impossible to send pending notifications which existed prior to the upgrade of SonarQube. They will be ignored.");
103   }
104
105   public long count() {
106     return dbClient.notificationQueueDao().count();
107   }
108
109   private static void verifyProjectKey(String projectKey) {
110     requireNonNull(projectKey, "projectKey is mandatory");
111   }
112
113   @Override
114   public Set<EmailRecipient> findSubscribedEmailRecipients(String dispatcherKey, String projectKey, SubscriberPermissionsOnProject subscriberPermissionsOnProject) {
115     verifyProjectKey(projectKey);
116
117     try (DbSession dbSession = dbClient.openSession(false)) {
118       Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification(
119         dbSession, dispatcherKey, EmailNotificationChannel.class.getSimpleName(), projectKey);
120
121       return keepAuthorizedEmailSubscribers(dbSession, projectKey, subscriberPermissionsOnProject, emailSubscribers);
122     }
123   }
124
125   @Override
126   public Set<EmailRecipient> findSubscribedEmailRecipients(String dispatcherKey, String projectKey, Set<String> logins,
127     SubscriberPermissionsOnProject subscriberPermissionsOnProject) {
128     verifyProjectKey(projectKey);
129     requireNonNull(logins, "logins can't be null");
130     if (logins.isEmpty()) {
131       return emptySet();
132     }
133
134     try (DbSession dbSession = dbClient.openSession(false)) {
135       Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification(
136         dbSession, dispatcherKey, EmailNotificationChannel.class.getSimpleName(), projectKey, logins);
137
138       return keepAuthorizedEmailSubscribers(dbSession, projectKey, subscriberPermissionsOnProject, emailSubscribers);
139     }
140   }
141
142   private Set<EmailRecipient> keepAuthorizedEmailSubscribers(DbSession dbSession, String projectKey,
143     SubscriberPermissionsOnProject subscriberPermissionsOnProject, Set<EmailSubscriberDto> emailSubscribers) {
144     if (emailSubscribers.isEmpty()) {
145       return emptySet();
146     }
147
148     return keepAuthorizedEmailSubscribers(dbSession, projectKey, emailSubscribers, subscriberPermissionsOnProject)
149       .map(emailSubscriber -> new EmailRecipient(emailSubscriber.getLogin(), emailSubscriber.getEmail()))
150       .collect(Collectors.toSet());
151   }
152
153   private Stream<EmailSubscriberDto> keepAuthorizedEmailSubscribers(DbSession dbSession, String projectKey, Set<EmailSubscriberDto> emailSubscribers,
154     SubscriberPermissionsOnProject requiredPermissions) {
155     if (requiredPermissions.getGlobalSubscribers().equals(requiredPermissions.getProjectSubscribers())) {
156       return keepAuthorizedEmailSubscribers(dbSession, projectKey, emailSubscribers, null, requiredPermissions.getGlobalSubscribers());
157     } else {
158       return Stream.concat(
159         keepAuthorizedEmailSubscribers(dbSession, projectKey, emailSubscribers, true, requiredPermissions.getGlobalSubscribers()),
160         keepAuthorizedEmailSubscribers(dbSession, projectKey, emailSubscribers, false, requiredPermissions.getProjectSubscribers()));
161     }
162   }
163
164   private Stream<EmailSubscriberDto> keepAuthorizedEmailSubscribers(DbSession dbSession, String projectKey, Set<EmailSubscriberDto> emailSubscribers,
165     @Nullable Boolean global, String permission) {
166     Set<EmailSubscriberDto> subscribers = emailSubscribers.stream()
167       .filter(s -> global == null || s.isGlobal() == global)
168       .collect(Collectors.toSet());
169     if (subscribers.isEmpty()) {
170       return Stream.empty();
171     }
172
173     Set<String> logins = subscribers.stream()
174       .map(EmailSubscriberDto::getLogin)
175       .collect(Collectors.toSet());
176     Set<String> authorizedLogins = dbClient.authorizationDao().keepAuthorizedLoginsOnEntity(dbSession, logins, projectKey, permission);
177     return subscribers.stream()
178       .filter(s -> authorizedLogins.contains(s.getLogin()));
179   }
180
181   @VisibleForTesting
182   protected List<NotificationChannel> getChannels() {
183     return Arrays.asList(notificationChannels);
184   }
185
186 }