You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationDispatcher.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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. import org.apache.commons.lang.StringUtils;
  22. import org.sonar.api.ExtensionPoint;
  23. import org.sonar.api.ce.ComputeEngineSide;
  24. import org.sonar.api.notifications.Notification;
  25. import org.sonar.api.server.ServerSide;
  26. /**
  27. * <p>
  28. * Plugins should extend this class to provide logic to determine which users are interested in receiving notifications,
  29. * along with which delivery channels they selected.
  30. * </p>
  31. * For example:
  32. * <ul>
  33. * <li>notify me by email when someone comments an issue reported by me</li>
  34. * <li>notify me by X when someone comments an issue assigned to me</li>
  35. * <li>notify me by Jabber when someone mentions me in an issue comment</li>
  36. * <li>send me by SMS when there are system notifications (like password reset, account creation, ...)</li>
  37. * </ul>
  38. */
  39. @ServerSide
  40. @ComputeEngineSide
  41. @ExtensionPoint
  42. public abstract class NotificationDispatcher {
  43. private final String notificationType;
  44. /**
  45. * Additional information related to the notification, which will be used
  46. * to know who should receive the notification.
  47. */
  48. public interface Context {
  49. /**
  50. * Adds a user that will be notified through the given notification channel.
  51. *
  52. * @param userLogin the user login
  53. * @param notificationChannel the notification channel to use for this user
  54. */
  55. void addUser(String userLogin, NotificationChannel notificationChannel);
  56. }
  57. /**
  58. * Creates a new dispatcher for notifications of the given type.
  59. *
  60. * @param notificationType the type of notifications handled by this dispatcher
  61. */
  62. public NotificationDispatcher(String notificationType) {
  63. this.notificationType = notificationType;
  64. }
  65. /**
  66. * Creates a new generic dispatcher, used for any kind of notification.
  67. * <p/>
  68. * Should be avoided and replaced by the other constructor - as it is easier to understand that a
  69. * dispatcher listens for a specific type of notification.
  70. */
  71. public NotificationDispatcher() {
  72. this("");
  73. }
  74. /**
  75. * The unique key of this dispatcher. By default it's the class name without the package prefix.
  76. * <p/>
  77. * The related label in l10n bundles is 'notification.dispatcher.<key>', for example 'notification.dispatcher.NewFalsePositive'.
  78. */
  79. public String getKey() {
  80. return getClass().getSimpleName();
  81. }
  82. /**
  83. * @since 5.1
  84. */
  85. public String getType() {
  86. return notificationType;
  87. }
  88. /**
  89. * <p>
  90. * Performs the dispatch.
  91. * </p>
  92. */
  93. public final void performDispatch(Notification notification, Context context) {
  94. if (StringUtils.equals(notification.getType(), notificationType) || StringUtils.equals("", notificationType)) {
  95. dispatch(notification, context);
  96. }
  97. }
  98. /**
  99. * <p>
  100. * Implements the logic that defines which users will receive the notification.
  101. * </p>
  102. * The purpose of this method is to populate the context object with users, based on the type of notification and the content of the notification.
  103. */
  104. public abstract void dispatch(Notification notification, Context context);
  105. @Override
  106. public String toString() {
  107. return getKey();
  108. }
  109. }