001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.mail;
029
030import org.opencms.main.CmsLog;
031
032import java.util.ArrayList;
033import java.util.Collections;
034import java.util.List;
035
036import org.apache.commons.logging.Log;
037
038/**
039 * Contains the settings for the OpenCms mail service.<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsMailSettings {
044
045    /** The default protocol for sending mail ("smtp"). */
046    public static final String MAIL_DEFAULT_PROTOCOL = "smtp";
047
048    /** The default mail from address. */
049    public static final String MAIL_DEFAULT_SENDER = "opencms@unconfigured.com";
050
051    /** The log object for this class. */
052    private static final Log LOG = CmsLog.getLog(CmsMailSettings.class);
053
054    /** The default mail "from" sender address. */
055    private String m_mailFromDefault;
056
057    /** The list of internal mail hosts. */
058    private List<CmsMailHost> m_mailHosts;
059
060    /** The default order if no order is given for a host. */
061    private int m_orderDefault;
062
063    /**
064     * Empty constructor, required for configuration.<p>
065     */
066    public CmsMailSettings() {
067
068        m_mailFromDefault = MAIL_DEFAULT_SENDER;
069        m_mailHosts = new ArrayList<CmsMailHost>();
070        if (LOG.isDebugEnabled()) {
071            LOG.debug(Messages.get().getBundle().key(Messages.LOG_EMPTY_CONSTRUCTOR_CALLED_1));
072        }
073    }
074
075    /**
076     * Adds a new mail host to the internal list of mail hosts with default port 25.<p>
077     *
078     * @param hostname the name of the mail host
079     * @param order the order in which the host is tried
080     * @param protocol the protocol to use (default "smtp")
081     * @param username the user name to use for authentication
082     * @param password the password to use for authentication
083     */
084    public void addMailHost(String hostname, String order, String protocol, String username, String password) {
085
086        addMailHost(hostname, "25", order, protocol, null, username, password);
087    }
088
089    /**
090       * Adds a new mail host to the internal list of mail hosts.<p>
091       *
092       * @param hostname the name of the mail host
093       * @param port the port of the mail host
094       * @param order the order in which the host is tried
095       * @param protocol the protocol to use (default "smtp")
096       * @param security the security mode
097       * @param username the user name to use for authentication
098       * @param password the password to use for authentication
099       */
100    public void addMailHost(
101        String hostname,
102        String port,
103        String order,
104        String protocol,
105        String security,
106        String username,
107        String password) {
108
109        addMailHost(hostname, "25", order, protocol, null, username, password, null, null);
110    }
111
112    /**
113       * Adds a new mail host to the internal list of mail hosts.<p>
114       *
115       * @param hostname the name of the mail host
116       * @param port the port of the mail host
117       * @param order the order in which the host is tried
118       * @param protocol the protocol to use (default "smtp")
119       * @param security the security mode
120       * @param username the user name to use for authentication
121       * @param password the password to use for authentication
122       * @param id the id of the mail host
123       * @param mailfrom the mail-from address of the mail host
124       */
125    public void addMailHost(
126        String hostname,
127        String port,
128        String order,
129        String protocol,
130        String security,
131        String username,
132        String password,
133        String id,
134        String mailfrom) {
135
136        Integer thePort;
137        try {
138            thePort = Integer.valueOf(port);
139        } catch (Throwable t) {
140            thePort = Integer.valueOf(25);
141        }
142        m_orderDefault += 10;
143        Integer theOrder;
144        try {
145            theOrder = Integer.valueOf(order);
146            if (theOrder.intValue() > m_orderDefault) {
147                m_orderDefault = theOrder.intValue();
148            }
149        } catch (Throwable t) {
150            // valueOf: use jdk int cache if possible and not new operator:
151            theOrder = Integer.valueOf(m_orderDefault);
152        }
153        CmsMailHost host = new CmsMailHost(
154            hostname,
155            thePort,
156            theOrder,
157            protocol,
158            security,
159            username,
160            password,
161            id,
162            mailfrom);
163        m_mailHosts.add(host);
164        if (CmsLog.INIT.isInfoEnabled()) {
165            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_ADD_HOST_1, host));
166        }
167        Collections.sort(m_mailHosts);
168    }
169
170    /**
171     * Returns the default mail host.<p>
172     *
173     * @return the default mail host
174     */
175    public CmsMailHost getDefaultMailHost() {
176
177        return m_mailHosts.get(0);
178    }
179
180    /**
181     * Returns the mail from default sender.<p>
182     *
183     * @return the mail from default sender
184     */
185    public String getMailFromDefault() {
186
187        return m_mailFromDefault;
188    }
189
190    /**
191     * Returns a mail host for a given id.<p>
192     *
193     * @param id the id
194     * @return the mail host
195     */
196    public CmsMailHost getMailHost(String id) {
197
198        if (id == null) {
199            return null;
200        }
201        List<CmsMailHost> filtered = new ArrayList<CmsMailHost>();
202        for (CmsMailHost mailHost : m_mailHosts) {
203            if ((mailHost.getId() != null) && mailHost.getId().equals(id)) {
204                filtered.add(mailHost);
205            }
206        }
207        if (filtered.isEmpty()) {
208            return null;
209        } else {
210            Collections.sort(filtered);
211            return filtered.get(filtered.size() - 1);
212        }
213    }
214
215    /**
216     * Returns an unmodifiable sorted list of all configured mail hosts.<p>
217     *
218     * @return an unmodifiable sorted list of all configured mail hosts
219     */
220    public List<CmsMailHost> getMailHosts() {
221
222        return Collections.unmodifiableList(m_mailHosts);
223    }
224
225    /**
226     * Sets the mail from default sender.<p>
227     *
228     * @param sender the mail from default sender to set
229     */
230    public void setMailFromDefault(String sender) {
231
232        m_mailFromDefault = sender;
233        if (CmsLog.INIT.isInfoEnabled()) {
234            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_DEFAULT_SENDER_1, m_mailFromDefault));
235        }
236    }
237
238    /**
239     * @see java.lang.Object#toString()
240     */
241    @Override
242    public String toString() {
243
244        StringBuffer sb = new StringBuffer();
245        sb.append("[hosts:" + m_mailHosts.toString());
246        sb.append(", order:" + m_orderDefault);
247        sb.append(", from:" + m_mailFromDefault);
248        return sb.toString();
249    }
250}