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 com.google.common.base.MoreObjects;
023import com.google.common.collect.ImmutableList;
024import java.util.List;
025import org.sonar.api.PropertyType;
026import org.sonar.api.batch.ScannerSide;
027import org.sonar.api.ce.ComputeEngineSide;
028import org.sonar.api.server.ServerSide;
029
030import static org.sonar.api.CoreProperties.CATEGORY_GENERAL;
031import static org.sonar.api.CoreProperties.SERVER_BASE_URL;
032import static org.sonar.api.CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE;
033import static org.sonar.api.CoreProperties.SUBCATEGORY_EMAIL;
034import static org.sonar.api.PropertyType.INTEGER;
035import static org.sonar.api.PropertyType.SINGLE_SELECT_LIST;
036
037/**
038 * If batch extensions use this component, then batch must be executed with administrator rights (see properties sonar.login and sonar.password)
039 *
040 * @since 3.2
041 */
042@ScannerSide
043@ServerSide
044@ComputeEngineSide
045public class EmailSettings {
046  public static final String SMTP_HOST = "email.smtp_host.secured";
047  public static final String SMTP_HOST_DEFAULT = "";
048  public static final String SMTP_PORT = "email.smtp_port.secured";
049  public static final String SMTP_PORT_DEFAULT = "25";
050  public static final String SMTP_SECURE_CONNECTION = "email.smtp_secure_connection.secured";
051  public static final String SMTP_SECURE_CONNECTION_DEFAULT = "";
052  public static final String SMTP_USERNAME = "email.smtp_username.secured";
053  public static final String SMTP_USERNAME_DEFAULT = "";
054  public static final String SMTP_PASSWORD = "email.smtp_password.secured";
055  public static final String SMTP_PASSWORD_DEFAULT = "";
056  public static final String FROM = "email.from";
057  public static final String FROM_DEFAULT = "noreply@nowhere";
058  public static final String PREFIX = "email.prefix";
059  public static final String PREFIX_DEFAULT = "[SONARQUBE]";
060
061  private final Settings settings;
062
063  public EmailSettings(Settings settings) {
064    this.settings = settings;
065  }
066
067  public String getSmtpHost() {
068    return get(SMTP_HOST, SMTP_HOST_DEFAULT);
069  }
070
071  public int getSmtpPort() {
072    return Integer.parseInt(get(SMTP_PORT, SMTP_PORT_DEFAULT));
073  }
074
075  public String getSecureConnection() {
076    return get(SMTP_SECURE_CONNECTION, SMTP_SECURE_CONNECTION_DEFAULT);
077  }
078
079  public String getSmtpUsername() {
080    return get(SMTP_USERNAME, SMTP_USERNAME_DEFAULT);
081  }
082
083  public String getSmtpPassword() {
084    return get(SMTP_PASSWORD, SMTP_PASSWORD_DEFAULT);
085  }
086
087  public String getFrom() {
088    return get(FROM, FROM_DEFAULT);
089  }
090
091  public String getPrefix() {
092    return get(PREFIX, PREFIX_DEFAULT);
093  }
094
095  public String getServerBaseURL() {
096    return get(SERVER_BASE_URL, SERVER_BASE_URL_DEFAULT_VALUE);
097  }
098
099  private String get(String key, String defaultValue) {
100    return MoreObjects.firstNonNull(settings.getString(key), defaultValue);
101  }
102
103  /**
104   * @since 6.1
105   */
106  public static List<PropertyDefinition> definitions() {
107    return ImmutableList.of(
108      PropertyDefinition.builder(SMTP_HOST)
109        .name("SMTP host")
110        .description("For example \"smtp.gmail.com\". Leave blank to disable email sending.")
111        .defaultValue(SMTP_HOST_DEFAULT)
112        .category(CATEGORY_GENERAL)
113        .subCategory(SUBCATEGORY_EMAIL)
114        .build(),
115      PropertyDefinition.builder(SMTP_PORT)
116        .name("SMTP port")
117        .description("Port number to connect with SMTP server.")
118        .defaultValue(SMTP_PORT_DEFAULT)
119        .category(CATEGORY_GENERAL)
120        .subCategory(SUBCATEGORY_EMAIL)
121        .type(INTEGER)
122        .build(),
123      PropertyDefinition.builder(SMTP_SECURE_CONNECTION)
124        .name("Secure connection")
125        .description("Type of secure connection. Leave empty to not use secure connection.")
126        .defaultValue(SMTP_SECURE_CONNECTION_DEFAULT)
127        .category(CATEGORY_GENERAL)
128        .subCategory(SUBCATEGORY_EMAIL)
129        .type(SINGLE_SELECT_LIST)
130        .options("ssl", "starttls")
131        .build(),
132      PropertyDefinition.builder(SMTP_USERNAME)
133        .name("SMTP username")
134        .description("Username to use with authenticated SMTP.")
135        .defaultValue(SMTP_USERNAME_DEFAULT)
136        .category(CATEGORY_GENERAL)
137        .subCategory(SUBCATEGORY_EMAIL)
138        .build(),
139      PropertyDefinition.builder(SMTP_PASSWORD)
140        .name("SMTP password")
141        .description("Password to use with authenticated SMTP.")
142        .defaultValue(SMTP_PASSWORD_DEFAULT)
143        .type(PropertyType.PASSWORD)
144        .category(CATEGORY_GENERAL)
145        .subCategory(SUBCATEGORY_EMAIL)
146        .build(),
147      PropertyDefinition.builder(FROM)
148        .name("From address")
149        .description("Emails will come from this address. For example - \"noreply@sonarsource.com\". Note that server may ignore this setting.")
150        .defaultValue(FROM_DEFAULT)
151        .category(CATEGORY_GENERAL)
152        .subCategory(SUBCATEGORY_EMAIL)
153        .build(),
154      PropertyDefinition.builder(PREFIX)
155        .name("Email prefix")
156        .description("Prefix will be prepended to all outgoing email subjects.")
157        .defaultValue(PREFIX_DEFAULT)
158        .category(CATEGORY_GENERAL)
159        .subCategory(SUBCATEGORY_EMAIL)
160        .build());
161  }
162}