aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/config/EmailSettings.java
blob: 2a785123b6def80e09e71cf22cb0d473e9dd49c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * SonarQube
 * Copyright (C) 2009-2019 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.config;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.PropertyType;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;

import static java.util.Arrays.asList;
import static org.sonar.api.CoreProperties.CATEGORY_GENERAL;
import static org.sonar.api.CoreProperties.SERVER_BASE_URL;
import static org.sonar.api.CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE;
import static org.sonar.api.CoreProperties.SUBCATEGORY_EMAIL;
import static org.sonar.api.PropertyType.INTEGER;
import static org.sonar.api.PropertyType.SINGLE_SELECT_LIST;

/**
 * @since 3.2
 */
@ServerSide
@ComputeEngineSide
public class EmailSettings {
  public static final String SMTP_HOST = "email.smtp_host.secured";
  public static final String SMTP_HOST_DEFAULT = "";
  public static final String SMTP_PORT = "email.smtp_port.secured";
  public static final String SMTP_PORT_DEFAULT = "25";
  public static final String SMTP_SECURE_CONNECTION = "email.smtp_secure_connection.secured";
  public static final String SMTP_SECURE_CONNECTION_DEFAULT = "";
  public static final String SMTP_USERNAME = "email.smtp_username.secured";
  public static final String SMTP_USERNAME_DEFAULT = "";
  public static final String SMTP_PASSWORD = "email.smtp_password.secured";
  public static final String SMTP_PASSWORD_DEFAULT = "";
  public static final String FROM = "email.from";
  public static final String FROM_DEFAULT = "noreply@nowhere";
  public static final String FROM_NAME = "email.fromName";
  public static final String FROM_NAME_DEFAULT = "SonarQube";
  public static final String PREFIX = "email.prefix";
  public static final String PREFIX_DEFAULT = "[SONARQUBE]";

  private final Configuration config;

  public EmailSettings(Configuration config) {
    this.config = config;
  }

  public String getSmtpHost() {
    return get(SMTP_HOST, SMTP_HOST_DEFAULT);
  }

  public int getSmtpPort() {
    return Integer.parseInt(get(SMTP_PORT, SMTP_PORT_DEFAULT));
  }

  public String getSecureConnection() {
    return get(SMTP_SECURE_CONNECTION, SMTP_SECURE_CONNECTION_DEFAULT);
  }

  public String getSmtpUsername() {
    return get(SMTP_USERNAME, SMTP_USERNAME_DEFAULT);
  }

  public String getSmtpPassword() {
    return get(SMTP_PASSWORD, SMTP_PASSWORD_DEFAULT);
  }

  public String getFrom() {
    return get(FROM, FROM_DEFAULT);
  }

  public String getFromName() {
    return get(FROM_NAME, FROM_NAME_DEFAULT);
  }

  public String getPrefix() {
    return get(PREFIX, PREFIX_DEFAULT);
  }

  public String getServerBaseURL() {
    return config.get(SERVER_BASE_URL)
      .map(t -> StringUtils.removeEnd(t, "/"))
      .orElse(SERVER_BASE_URL_DEFAULT_VALUE);
  }

  private String get(String key, String defaultValue) {
    return config.get(key).orElse(defaultValue);
  }

  /**
   * @since 6.1
   */
  public static List<PropertyDefinition> definitions() {
    return asList(
      PropertyDefinition.builder(SMTP_HOST)
        .name("SMTP host")
        .description("For example \"smtp.gmail.com\". Leave blank to disable email sending.")
        .defaultValue(SMTP_HOST_DEFAULT)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .build(),
      PropertyDefinition.builder(SMTP_PORT)
        .name("SMTP port")
        .description("Port number to connect with SMTP server.")
        .defaultValue(SMTP_PORT_DEFAULT)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .type(INTEGER)
        .build(),
      PropertyDefinition.builder(SMTP_SECURE_CONNECTION)
        .name("Secure connection")
        .description("Type of secure connection. Leave empty to not use secure connection.")
        .defaultValue(SMTP_SECURE_CONNECTION_DEFAULT)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .type(SINGLE_SELECT_LIST)
        .options("ssl", "starttls")
        .build(),
      PropertyDefinition.builder(SMTP_USERNAME)
        .name("SMTP username")
        .description("Username to use with authenticated SMTP.")
        .defaultValue(SMTP_USERNAME_DEFAULT)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .build(),
      PropertyDefinition.builder(SMTP_PASSWORD)
        .name("SMTP password")
        .description("Password to use with authenticated SMTP.")
        .defaultValue(SMTP_PASSWORD_DEFAULT)
        .type(PropertyType.PASSWORD)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .build(),
      PropertyDefinition.builder(FROM)
        .name("From address")
        .description("Emails will come from this address. For example - \"noreply@sonarsource.com\". Note that the mail server may ignore this setting.")
        .defaultValue(FROM_DEFAULT)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .build(),
      PropertyDefinition.builder(FROM_NAME)
        .name("From name")
        .description("Emails will come from this address name. For example - \"SonarQube\". Note that the mail server may ignore this setting.")
        .defaultValue(FROM_NAME_DEFAULT)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .build(),
      PropertyDefinition.builder(PREFIX)
        .name("Email prefix")
        .description("Prefix will be prepended to all outgoing email subjects.")
        .defaultValue(PREFIX_DEFAULT)
        .category(CATEGORY_GENERAL)
        .subCategory(SUBCATEGORY_EMAIL)
        .build());
  }
}