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