3 * Copyright (C) 2009-2023 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.notification;
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;
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;
42 import static java.util.Collections.emptySet;
43 import static java.util.Collections.singletonList;
44 import static java.util.Objects.requireNonNull;
46 public class DefaultNotificationManager implements NotificationManager {
48 private static final Logger LOG = LoggerFactory.getLogger(DefaultNotificationManager.class);
50 private static final String UNABLE_TO_READ_NOTIFICATION = "Unable to read notification";
52 private final NotificationChannel[] notificationChannels;
53 private final DbClient dbClient;
54 private boolean alreadyLoggedDeserializationIssue = false;
56 public DefaultNotificationManager(NotificationChannel[] channels, DbClient dbClient) {
57 this.notificationChannels = channels;
58 this.dbClient = dbClient;
65 public <T extends Notification> void scheduleForSending(T notification) {
66 NotificationQueueDto dto = NotificationQueueDto.toNotificationQueueDto(notification);
67 dbClient.notificationQueueDao().insert(singletonList(dto));
71 * Give the notification queue so that it can be processed
73 public <T extends Notification> T getFromQueue() {
75 List<NotificationQueueDto> notificationDtos = dbClient.notificationQueueDao().selectOldest(batchSize);
76 if (notificationDtos.isEmpty()) {
79 dbClient.notificationQueueDao().delete(notificationDtos);
81 return convertToNotification(notificationDtos);
84 private <T extends Notification> T convertToNotification(List<NotificationQueueDto> notifications) {
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) {
90 if (!alreadyLoggedDeserializationIssue) {
91 logDeserializationIssue();
92 alreadyLoggedDeserializationIssue = true;
95 } catch (IOException | ClassNotFoundException e) {
96 throw new SonarException(UNABLE_TO_READ_NOTIFICATION, e);
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.");
105 public long count() {
106 return dbClient.notificationQueueDao().count();
109 private static void verifyProjectKey(String projectKey) {
110 requireNonNull(projectKey, "projectKey is mandatory");
114 public Set<EmailRecipient> findSubscribedEmailRecipients(String dispatcherKey, String projectKey, SubscriberPermissionsOnProject subscriberPermissionsOnProject) {
115 verifyProjectKey(projectKey);
117 try (DbSession dbSession = dbClient.openSession(false)) {
118 Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification(
119 dbSession, dispatcherKey, EmailNotificationChannel.class.getSimpleName(), projectKey);
121 return keepAuthorizedEmailSubscribers(dbSession, projectKey, subscriberPermissionsOnProject, emailSubscribers);
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()) {
134 try (DbSession dbSession = dbClient.openSession(false)) {
135 Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification(
136 dbSession, dispatcherKey, EmailNotificationChannel.class.getSimpleName(), projectKey, logins);
138 return keepAuthorizedEmailSubscribers(dbSession, projectKey, subscriberPermissionsOnProject, emailSubscribers);
142 private Set<EmailRecipient> keepAuthorizedEmailSubscribers(DbSession dbSession, String projectKey,
143 SubscriberPermissionsOnProject subscriberPermissionsOnProject, Set<EmailSubscriberDto> emailSubscribers) {
144 if (emailSubscribers.isEmpty()) {
148 return keepAuthorizedEmailSubscribers(dbSession, projectKey, emailSubscribers, subscriberPermissionsOnProject)
149 .map(emailSubscriber -> new EmailRecipient(emailSubscriber.getLogin(), emailSubscriber.getEmail()))
150 .collect(Collectors.toSet());
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());
158 return Stream.concat(
159 keepAuthorizedEmailSubscribers(dbSession, projectKey, emailSubscribers, true, requiredPermissions.getGlobalSubscribers()),
160 keepAuthorizedEmailSubscribers(dbSession, projectKey, emailSubscribers, false, requiredPermissions.getProjectSubscribers()));
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();
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()));
182 protected List<NotificationChannel> getChannels() {
183 return Arrays.asList(notificationChannels);