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
030/**
031 * Contains the configuration of an individual mail host.<p>
032 *
033 * @since 6.0.0
034 */
035public class CmsMailHost implements Comparable<CmsMailHost> {
036
037    /** The name of the mail host. */
038    private String m_hostname;
039
040    /** The order of this mail host. */
041    private Integer m_order;
042
043    /** The password to use for authentication. */
044    private String m_password;
045
046    /** The port to use. */
047    private int m_port;
048
049    /** The protocol to use. */
050    private String m_protocol;
051
052    /** The user name to use for authentication. */
053    private String m_username;
054
055    /** The security setting. */
056    private String m_security;
057
058    /** The id of this mail host. */
059    private String m_id;
060
061    /** The mail from address of this host. */
062    private String m_mailfrom;
063
064    /**
065     * Creates a new mail host.<p>
066     *
067     * @param hostname the name of the mail host
068     * @param port the port, if < 0 then 25 is used
069     * @param order the order in which the host is tried
070     * @param protocol the protocol to use (default "smtp")
071     * @param security the security setting
072     * @param username the user name to use for authentication
073     * @param password the password to use for authentication
074     */
075    public CmsMailHost(
076        String hostname,
077        Integer port,
078        Integer order,
079        String protocol,
080        String security,
081        String username,
082        String password) {
083
084        this(hostname, port, order, protocol, security, username, password, null, null);
085    }
086
087    /**
088     * Creates a new mail host.<p>
089     *
090     * @param hostname the name of the mail host
091     * @param port the port, if < 0 then 25 is used
092     * @param order the order in which the host is tried
093     * @param protocol the protocol to use (default "smtp")
094     * @param security the security setting
095     * @param username the user name to use for authentication
096     * @param password the password to use for authentication
097     * @param id the id of the mail host
098     * @param mailfrom the mail-from address of the mail host
099     */
100    public CmsMailHost(
101        String hostname,
102        Integer port,
103        Integer order,
104        String protocol,
105        String security,
106        String username,
107        String password,
108        String id,
109        String mailfrom) {
110
111        m_hostname = hostname;
112        int portInt = port.intValue();
113        m_port = (portInt < 0) ? 25 : portInt;
114        m_protocol = (protocol != null) ? protocol : CmsMailSettings.MAIL_DEFAULT_PROTOCOL;
115        m_username = username;
116        m_password = password;
117        m_security = security;
118        m_order = order;
119        m_id = id;
120        m_mailfrom = mailfrom;
121    }
122
123    /**
124     * @see java.lang.Comparable#compareTo(java.lang.Object)
125     */
126    public int compareTo(CmsMailHost obj) {
127
128        if (obj == this) {
129            return 0;
130        }
131        return m_order.compareTo(obj.m_order);
132    }
133
134    /**
135     * @see java.lang.Object#equals(java.lang.Object)
136     */
137    @Override
138    public boolean equals(Object obj) {
139
140        if (obj == this) {
141            return true;
142        }
143        if (obj instanceof CmsMailHost) {
144            CmsMailHost other = (CmsMailHost)obj;
145            return m_hostname.equals(other.m_hostname)
146                && m_protocol.equals(other.m_protocol)
147                && m_username.equals(other.m_username);
148        }
149        return false;
150    }
151
152    /**
153     * Returns the host name.<p>
154     *
155     * @return the host name
156     */
157    public String getHostname() {
158
159        return m_hostname;
160    }
161
162    /**
163     * Returns the id of this mail host.<p>
164     *
165     * @return the id of this mail host
166     */
167    public String getId() {
168
169        return m_id;
170    }
171
172    /**
173     * Returns the mail from address of this host.
174     * @return the mail from address of this host
175     */
176    public String getMailfrom() {
177
178        return m_mailfrom;
179    }
180
181    /**
182     * Returns the order of this mail host.<p>
183     *
184     * @return the order of this mail host
185     */
186    public Integer getOrder() {
187
188        return m_order;
189    }
190
191    /**
192     * Returns the password used for authentication.<p>
193     *
194     * @return the password used for authentication
195     */
196    public String getPassword() {
197
198        return m_password;
199    }
200
201    /**
202     * Returns the port.<p>
203     *
204     * @return the port
205     */
206    public int getPort() {
207
208        return m_port;
209    }
210
211    /**
212     * Returns the protocol used for mail sending, default is "smtp".<p>
213     *
214     * @return the protocol used for mail sending
215     */
216    public String getProtocol() {
217
218        return m_protocol;
219    }
220
221    /**
222     * Gets the value of the security setting.<p>
223     *
224     * @return the value of the security setting
225     */
226    public String getSecurity() {
227
228        return m_security;
229    }
230
231    /**
232     * Returns the user name used for authentication.<p>
233     *
234     * @return the user name used for authentication
235     */
236    public String getUsername() {
237
238        return m_username;
239    }
240
241    /**
242     * @see java.lang.Object#hashCode()
243     */
244    @Override
245    public int hashCode() {
246
247        return (m_hostname.hashCode() * 1117) + (m_protocol.hashCode() * 2003) + m_username.hashCode();
248    }
249
250    /**
251     * Returns <code>true</code> only if authentication is enabled,
252     * the default is <code>false</code>.<p>
253     *
254     * Authentication is enabled only if both "username" and "password"
255     * are not <code>null</code>.<p>
256     *
257     * @return <code>true</code> only if authentication is enabled
258     */
259    public boolean isAuthenticating() {
260
261        return (m_username != null) && (m_password != null);
262    }
263
264    /**
265     * @see java.lang.Object#toString()
266     */
267    @Override
268    public String toString() {
269
270        StringBuffer buf = new StringBuffer(64);
271        buf.append(this.getClass().getName());
272        buf.append(" hostname=");
273        buf.append(getHostname());
274        buf.append(" port=");
275        buf.append(getPort());
276        buf.append(" order=");
277        buf.append(m_order);
278        buf.append(" protocol=");
279
280        buf.append(getProtocol());
281        buf.append(" security=" + getSecurity());
282        if (isAuthenticating()) {
283            buf.append(" user=");
284            buf.append(getUsername());
285            buf.append(" password=");
286            buf.append(getPassword());
287        }
288        buf.append(" id=");
289        buf.append(getId());
290        buf.append(" mailfrom=");
291        buf.append(getMailfrom());
292        return buf.toString();
293    }
294}