001
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
024 public abstract class Account implements Serializable {
025
026 public static final String PROTOCOL_IMAP = "imap";
027
028 public static final String PROTOCOL_IMAPS = "imaps";
029
030 public static final String PROTOCOL_POP = "pop3";
031
032 public static final String PROTOCOL_POPS = "pop3s";
033
034 public static final String PROTOCOL_SMTP = "smtp";
035
036 public static final String PROTOCOL_SMTPS = "smtps";
037
038 public static final int PORT_IMAP = 143;
039
040 public static final int PORT_IMAPS = 993;
041
042 public static final int PORT_POP = 110;
043
044 public static final int PORT_POPS = 995;
045
046 public static final int PORT_SMTP = 25;
047
048 public static final int PORT_SMTPS = 465;
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) &&
128 Validator.isNotNull(_password)) {
129
130 return true;
131 }
132 else {
133 return false;
134 }
135 }
136
137 public boolean isSecure() {
138 return _secure;
139 }
140
141 public void setHost(String host) {
142 _host = host;
143 }
144
145 public void setPassword(String password) {
146 _password = password;
147 }
148
149 public void setPort(int port) {
150 _port = port;
151 }
152
153 public void setUser(String user) {
154 _user = user;
155 }
156
157 protected Account(String protocol, boolean secure, int port) {
158 _protocol = protocol;
159 _secure = secure;
160 _port = port;
161 }
162
163 private String _host;
164 private String _password;
165 private int _port;
166 private String _protocol;
167 private boolean _secure;
168 private String _user;
169
170 }