001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.mail;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.io.Serializable;
020    
021    /**
022     * @author Thiago Moreira
023     */
024    public abstract class Account implements Serializable {
025    
026            public static final int PORT_IMAP = 143;
027    
028            public static final int PORT_IMAPS = 993;
029    
030            public static final int PORT_POP = 110;
031    
032            public static final int PORT_POPS = 995;
033    
034            public static final int PORT_SMTP = 25;
035    
036            public static final int PORT_SMTPS = 465;
037    
038            public static final String PROTOCOL_IMAP = "imap";
039    
040            public static final String PROTOCOL_IMAPS = "imaps";
041    
042            public static final String PROTOCOL_POP = "pop3";
043    
044            public static final String PROTOCOL_POPS = "pop3s";
045    
046            public static final String PROTOCOL_SMTP = "smtp";
047    
048            public static final String PROTOCOL_SMTPS = "smtps";
049    
050            public static Account getInstance(String protocol) {
051                    return getInstance(protocol, 0);
052            }
053    
054            public static Account getInstance(String protocol, int port) {
055                    Account account = null;
056    
057                    if (protocol.startsWith(PROTOCOL_IMAP)) {
058                            boolean secure = false;
059                            int defaultPort = PORT_IMAP;
060    
061                            if (protocol.endsWith("s")) {
062                                    secure = true;
063                                    defaultPort = PORT_IMAPS;
064                            }
065    
066                            if (port <= 0) {
067                                    port = defaultPort;
068                            }
069    
070                            account = new IMAPAccount(protocol, secure, port);
071                    }
072                    else if (protocol.startsWith(PROTOCOL_POP)) {
073                            boolean secure = false;
074                            int defaultPort = PORT_POP;
075    
076                            if (protocol.endsWith("s")) {
077                                    secure = true;
078                                    defaultPort = PORT_POPS;
079                            }
080    
081                            if (port <= 0) {
082                                    port = defaultPort;
083                            }
084    
085                            account = new POPAccount(protocol, secure, port);
086                    }
087                    else {
088                            boolean secure = false;
089                            int defaultPort = PORT_SMTP;
090    
091                            if (protocol.endsWith("s")) {
092                                    secure = true;
093                                    defaultPort = PORT_SMTPS;
094                            }
095    
096                            if (port <= 0) {
097                                    port = defaultPort;
098                            }
099    
100                            account = new SMTPAccount(protocol, secure, port);
101                    }
102    
103                    return account;
104            }
105    
106            public String getHost() {
107                    return _host;
108            }
109    
110            public String getPassword() {
111                    return _password;
112            }
113    
114            public int getPort() {
115                    return _port;
116            }
117    
118            public String getProtocol() {
119                    return _protocol;
120            }
121    
122            public String getUser() {
123                    return _user;
124            }
125    
126            public boolean isRequiresAuthentication() {
127                    if (Validator.isNotNull(_user) && Validator.isNotNull(_password)) {
128                            return true;
129                    }
130                    else {
131                            return false;
132                    }
133            }
134    
135            public boolean isSecure() {
136                    return _secure;
137            }
138    
139            public void setHost(String host) {
140                    _host = host;
141            }
142    
143            public void setPassword(String password) {
144                    _password = password;
145            }
146    
147            public void setPort(int port) {
148                    _port = port;
149            }
150    
151            public void setUser(String user) {
152                    _user = user;
153            }
154    
155            protected Account(String protocol, boolean secure, int port) {
156                    _protocol = protocol;
157                    _secure = secure;
158                    _port = port;
159            }
160    
161            private String _host;
162            private String _password;
163            private int _port;
164            private String _protocol;
165            private boolean _secure;
166            private String _user;
167    
168    }