001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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.Objects;
023import org.sonar.api.CoreProperties;
024import org.sonar.api.batch.BatchSide;
025import org.sonar.api.ce.ComputeEngineSide;
026import org.sonar.api.server.ServerSide;
027
028/**
029 * If batch extensions use this component, then batch must be executed with administrator rights (see properties sonar.login and sonar.password)
030 *
031 * @since 3.2
032 */
033@BatchSide
034@ServerSide
035@ComputeEngineSide
036public class EmailSettings {
037  public static final String SMTP_HOST = "email.smtp_host.secured";
038  public static final String SMTP_HOST_DEFAULT = "";
039  public static final String SMTP_PORT = "email.smtp_port.secured";
040  public static final String SMTP_PORT_DEFAULT = "25";
041  public static final String SMTP_SECURE_CONNECTION = "email.smtp_secure_connection.secured";
042  public static final String SMTP_SECURE_CONNECTION_DEFAULT = "";
043  public static final String SMTP_USERNAME = "email.smtp_username.secured";
044  public static final String SMTP_USERNAME_DEFAULT = "";
045  public static final String SMTP_PASSWORD = "email.smtp_password.secured";
046  public static final String SMTP_PASSWORD_DEFAULT = "";
047  public static final String FROM = "email.from";
048  public static final String FROM_DEFAULT = "noreply@nowhere";
049  public static final String PREFIX = "email.prefix";
050  public static final String PREFIX_DEFAULT = "[SONARQUBE]";
051
052  private final Settings settings;
053
054  public EmailSettings(Settings settings) {
055    this.settings = settings;
056  }
057
058  public String getSmtpHost() {
059    return get(SMTP_HOST, SMTP_HOST_DEFAULT);
060  }
061
062  public int getSmtpPort() {
063    return Integer.parseInt(get(SMTP_PORT, SMTP_PORT_DEFAULT));
064  }
065
066  public String getSecureConnection() {
067    return get(SMTP_SECURE_CONNECTION, SMTP_SECURE_CONNECTION_DEFAULT);
068  }
069
070  public String getSmtpUsername() {
071    return get(SMTP_USERNAME, SMTP_USERNAME_DEFAULT);
072  }
073
074  public String getSmtpPassword() {
075    return get(SMTP_PASSWORD, SMTP_PASSWORD_DEFAULT);
076  }
077
078  public String getFrom() {
079    return get(FROM, FROM_DEFAULT);
080  }
081
082  public String getPrefix() {
083    return get(PREFIX, PREFIX_DEFAULT);
084  }
085
086  public String getServerBaseURL() {
087    return get(CoreProperties.SERVER_BASE_URL, CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE);
088  }
089
090  private String get(String key, String defaultValue) {
091    return Objects.firstNonNull(settings.getString(key), defaultValue);
092  }
093}