001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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.BatchComponent;
024import org.sonar.api.CoreProperties;
025import org.sonar.api.ServerComponent;
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 */
032public class EmailSettings implements BatchComponent, ServerComponent {
033  public static final String SMTP_HOST = "email.smtp_host.secured";
034  public static final String SMTP_HOST_DEFAULT = "";
035  public static final String SMTP_PORT = "email.smtp_port.secured";
036  public static final String SMTP_PORT_DEFAULT = "25";
037  public static final String SMTP_SECURE_CONNECTION = "email.smtp_secure_connection.secured";
038  public static final String SMTP_SECURE_CONNECTION_DEFAULT = "";
039  public static final String SMTP_USERNAME = "email.smtp_username.secured";
040  public static final String SMTP_USERNAME_DEFAULT = "";
041  public static final String SMTP_PASSWORD = "email.smtp_password.secured";
042  public static final String SMTP_PASSWORD_DEFAULT = "";
043  public static final String FROM = "email.from";
044  public static final String FROM_DEFAULT = "noreply@nowhere";
045  public static final String PREFIX = "email.prefix";
046  public static final String PREFIX_DEFAULT = "[SONARQUBE]";
047
048  private final Settings settings;
049
050  public EmailSettings(Settings settings) {
051    this.settings = settings;
052  }
053
054  public String getSmtpHost() {
055    return get(SMTP_HOST, SMTP_HOST_DEFAULT);
056  }
057
058  public int getSmtpPort() {
059    return Integer.parseInt(get(SMTP_PORT, SMTP_PORT_DEFAULT));
060  }
061
062  public String getSecureConnection() {
063    return get(SMTP_SECURE_CONNECTION, SMTP_SECURE_CONNECTION_DEFAULT);
064  }
065
066  public String getSmtpUsername() {
067    return get(SMTP_USERNAME, SMTP_USERNAME_DEFAULT);
068  }
069
070  public String getSmtpPassword() {
071    return get(SMTP_PASSWORD, SMTP_PASSWORD_DEFAULT);
072  }
073
074  public String getFrom() {
075    return get(FROM, FROM_DEFAULT);
076  }
077
078  public String getPrefix() {
079    return get(PREFIX, PREFIX_DEFAULT);
080  }
081
082  public String getServerBaseURL() {
083    return get(CoreProperties.SERVER_BASE_URL, CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE);
084  }
085
086  private String get(String key, String defaultValue) {
087    return Objects.firstNonNull(settings.getString(key), defaultValue);
088  }
089}