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 PREFIX = "email.prefix";
054  public static final String PREFIX_DEFAULT = "[SONARQUBE]";
055
056  private final Configuration config;
057
058  public EmailSettings(Configuration config) {
059    this.config = config;
060  }
061
062  public String getSmtpHost() {
063    return get(SMTP_HOST, SMTP_HOST_DEFAULT);
064  }
065
066  public int getSmtpPort() {
067    return Integer.parseInt(get(SMTP_PORT, SMTP_PORT_DEFAULT));
068  }
069
070  public String getSecureConnection() {
071    return get(SMTP_SECURE_CONNECTION, SMTP_SECURE_CONNECTION_DEFAULT);
072  }
073
074  public String getSmtpUsername() {
075    return get(SMTP_USERNAME, SMTP_USERNAME_DEFAULT);
076  }
077
078  public String getSmtpPassword() {
079    return get(SMTP_PASSWORD, SMTP_PASSWORD_DEFAULT);
080  }
081
082  public String getFrom() {
083    return get(FROM, FROM_DEFAULT);
084  }
085
086  public String getPrefix() {
087    return get(PREFIX, PREFIX_DEFAULT);
088  }
089
090  public String getServerBaseURL() {
091    return get(SERVER_BASE_URL, SERVER_BASE_URL_DEFAULT_VALUE);
092  }
093
094  private String get(String key, String defaultValue) {
095    return config.get(key).orElse(defaultValue);
096  }
097
098  /**
099   * @since 6.1
100   */
101  public static List<PropertyDefinition> definitions() {
102    return asList(
103      PropertyDefinition.builder(SMTP_HOST)
104        .name("SMTP host")
105        .description("For example \"smtp.gmail.com\". Leave blank to disable email sending.")
106        .defaultValue(SMTP_HOST_DEFAULT)
107        .category(CATEGORY_GENERAL)
108        .subCategory(SUBCATEGORY_EMAIL)
109        .build(),
110      PropertyDefinition.builder(SMTP_PORT)
111        .name("SMTP port")
112        .description("Port number to connect with SMTP server.")
113        .defaultValue(SMTP_PORT_DEFAULT)
114        .category(CATEGORY_GENERAL)
115        .subCategory(SUBCATEGORY_EMAIL)
116        .type(INTEGER)
117        .build(),
118      PropertyDefinition.builder(SMTP_SECURE_CONNECTION)
119        .name("Secure connection")
120        .description("Type of secure connection. Leave empty to not use secure connection.")
121        .defaultValue(SMTP_SECURE_CONNECTION_DEFAULT)
122        .category(CATEGORY_GENERAL)
123        .subCategory(SUBCATEGORY_EMAIL)
124        .type(SINGLE_SELECT_LIST)
125        .options("ssl", "starttls")
126        .build(),
127      PropertyDefinition.builder(SMTP_USERNAME)
128        .name("SMTP username")
129        .description("Username to use with authenticated SMTP.")
130        .defaultValue(SMTP_USERNAME_DEFAULT)
131        .category(CATEGORY_GENERAL)
132        .subCategory(SUBCATEGORY_EMAIL)
133        .build(),
134      PropertyDefinition.builder(SMTP_PASSWORD)
135        .name("SMTP password")
136        .description("Password to use with authenticated SMTP.")
137        .defaultValue(SMTP_PASSWORD_DEFAULT)
138        .type(PropertyType.PASSWORD)
139        .category(CATEGORY_GENERAL)
140        .subCategory(SUBCATEGORY_EMAIL)
141        .build(),
142      PropertyDefinition.builder(FROM)
143        .name("From address")
144        .description("Emails will come from this address. For example - \"noreply@sonarsource.com\". Note that server may ignore this setting.")
145        .defaultValue(FROM_DEFAULT)
146        .category(CATEGORY_GENERAL)
147        .subCategory(SUBCATEGORY_EMAIL)
148        .build(),
149      PropertyDefinition.builder(PREFIX)
150        .name("Email prefix")
151        .description("Prefix will be prepended to all outgoing email subjects.")
152        .defaultValue(PREFIX_DEFAULT)
153        .category(CATEGORY_GENERAL)
154        .subCategory(SUBCATEGORY_EMAIL)
155        .build());
156  }
157}