001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.config;
021
022import java.util.List;
023import org.sonar.api.PropertyType;
024import org.sonar.api.ce.ComputeEngineSide;
025import org.sonar.api.server.ServerSide;
026
027import static java.util.Arrays.asList;
028import static org.sonar.api.CoreProperties.CATEGORY_GENERAL;
029import static org.sonar.api.CoreProperties.SERVER_BASE_URL;
030import static org.sonar.api.CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE;
031import static org.sonar.api.CoreProperties.SUBCATEGORY_EMAIL;
032import static org.sonar.api.PropertyType.INTEGER;
033import static org.sonar.api.PropertyType.SINGLE_SELECT_LIST;
034
035/**
036 * @since 3.2
037 */
038@ServerSide
039@ComputeEngineSide
040public class EmailSettings {
041  public static final String SMTP_HOST = "email.smtp_host.secured";
042  public static final String SMTP_HOST_DEFAULT = "";
043  public static final String SMTP_PORT = "email.smtp_port.secured";
044  public static final String SMTP_PORT_DEFAULT = "25";
045  public static final String SMTP_SECURE_CONNECTION = "email.smtp_secure_connection.secured";
046  public static final String SMTP_SECURE_CONNECTION_DEFAULT = "";
047  public static final String SMTP_USERNAME = "email.smtp_username.secured";
048  public static final String SMTP_USERNAME_DEFAULT = "";
049  public static final String SMTP_PASSWORD = "email.smtp_password.secured";
050  public static final String SMTP_PASSWORD_DEFAULT = "";
051  public static final String FROM = "email.from";
052  public static final String FROM_DEFAULT = "noreply@nowhere";
053  public static final String FROM_NAME = "email.fromName";
054  public static final String FROM_NAME_DEFAULT = "SonarQube";
055  public static final String PREFIX = "email.prefix";
056  public static final String PREFIX_DEFAULT = "[SONARQUBE]";
057
058  private final Configuration config;
059
060  public EmailSettings(Configuration config) {
061    this.config = config;
062  }
063
064  public String getSmtpHost() {
065    return get(SMTP_HOST, SMTP_HOST_DEFAULT);
066  }
067
068  public int getSmtpPort() {
069    return Integer.parseInt(get(SMTP_PORT, SMTP_PORT_DEFAULT));
070  }
071
072  public String getSecureConnection() {
073    return get(SMTP_SECURE_CONNECTION, SMTP_SECURE_CONNECTION_DEFAULT);
074  }
075
076  public String getSmtpUsername() {
077    return get(SMTP_USERNAME, SMTP_USERNAME_DEFAULT);
078  }
079
080  public String getSmtpPassword() {
081    return get(SMTP_PASSWORD, SMTP_PASSWORD_DEFAULT);
082  }
083
084  public String getFrom() {
085    return get(FROM, FROM_DEFAULT);
086  }
087
088  public String getFromName() {
089    return get(FROM_NAME, FROM_NAME_DEFAULT);
090  }
091
092  public String getPrefix() {
093    return get(PREFIX, PREFIX_DEFAULT);
094  }
095
096  public String getServerBaseURL() {
097    return get(SERVER_BASE_URL, SERVER_BASE_URL_DEFAULT_VALUE);
098  }
099
100  private String get(String key, String defaultValue) {
101    return config.get(key).orElse(defaultValue);
102  }
103
104  /**
105   * @since 6.1
106   */
107  public static List<PropertyDefinition> definitions() {
108    return asList(
109      PropertyDefinition.builder(SMTP_HOST)
110        .name("SMTP host")
111        .description("For example \"smtp.gmail.com\". Leave blank to disable email sending.")
112        .defaultValue(SMTP_HOST_DEFAULT)
113        .category(CATEGORY_GENERAL)
114        .subCategory(SUBCATEGORY_EMAIL)
115        .build(),
116      PropertyDefinition.builder(SMTP_PORT)
117        .name("SMTP port")
118        .description("Port number to connect with SMTP server.")
119        .defaultValue(SMTP_PORT_DEFAULT)
120        .category(CATEGORY_GENERAL)
121        .subCategory(SUBCATEGORY_EMAIL)
122        .type(INTEGER)
123        .build(),
124      PropertyDefinition.builder(SMTP_SECURE_CONNECTION)
125        .name("Secure connection")
126        .description("Type of secure connection. Leave empty to not use secure connection.")
127        .defaultValue(SMTP_SECURE_CONNECTION_DEFAULT)
128        .category(CATEGORY_GENERAL)
129        .subCategory(SUBCATEGORY_EMAIL)
130        .type(SINGLE_SELECT_LIST)
131        .options("ssl", "starttls")
132        .build(),
133      PropertyDefinition.builder(SMTP_USERNAME)
134        .name("SMTP username")
135        .description("Username to use with authenticated SMTP.")
136        .defaultValue(SMTP_USERNAME_DEFAULT)
137        .category(CATEGORY_GENERAL)
138        .subCategory(SUBCATEGORY_EMAIL)
139        .build(),
140      PropertyDefinition.builder(SMTP_PASSWORD)
141        .name("SMTP password")
142        .description("Password to use with authenticated SMTP.")
143        .defaultValue(SMTP_PASSWORD_DEFAULT)
144        .type(PropertyType.PASSWORD)
145        .category(CATEGORY_GENERAL)
146        .subCategory(SUBCATEGORY_EMAIL)
147        .build(),
148      PropertyDefinition.builder(FROM)
149        .name("From address")
150        .description("Emails will come from this address. For example - \"noreply@sonarsource.com\". Note that server may ignore this setting.")
151        .defaultValue(FROM_DEFAULT)
152        .category(CATEGORY_GENERAL)
153        .subCategory(SUBCATEGORY_EMAIL)
154        .build(),
155      PropertyDefinition.builder(FROM_NAME)
156        .name("From name")
157        .description("Emails will come from this address name. For example - \"SonarQube\". Note that server may ignore this setting.")
158        .defaultValue(FROM_NAME_DEFAULT)
159        .category(CATEGORY_GENERAL)
160        .subCategory(SUBCATEGORY_EMAIL)
161        .build(),
162      PropertyDefinition.builder(PREFIX)
163        .name("Email prefix")
164        .description("Prefix will be prepended to all outgoing email subjects.")
165        .defaultValue(PREFIX_DEFAULT)
166        .category(CATEGORY_GENERAL)
167        .subCategory(SUBCATEGORY_EMAIL)
168        .build());
169  }
170}