1
22
23 package com.liferay.portal.kernel.mail;
24
25 import com.liferay.portal.kernel.util.Validator;
26
27 import java.io.Serializable;
28
29
35 public abstract class Account implements Serializable {
36
37 public static final String PROTOCOL_IMAP = "imap";
38
39 public static final String PROTOCOL_IMAPS = "imaps";
40
41 public static final String PROTOCOL_POP = "pop3";
42
43 public static final String PROTOCOL_POPS = "pop3s";
44
45 public static final String PROTOCOL_SMTP = "smtp";
46
47 public static final String PROTOCOL_SMTPS = "smtps";
48
49 public static final int PORT_IMAP = 143;
50
51 public static final int PORT_IMAPS = 993;
52
53 public static final int PORT_POP = 110;
54
55 public static final int PORT_POPS = 995;
56
57 public static final int PORT_SMTP = 25;
58
59 public static final int PORT_SMTPS = 465;
60
61 public static Account getInstance(String protocol) {
62 return getInstance(protocol, 0);
63 }
64
65 public static Account getInstance(String protocol, int port) {
66 Account account = null;
67
68 if (protocol.startsWith(PROTOCOL_IMAP)) {
69 boolean secure = false;
70 int defaultPort = PORT_IMAP;
71
72 if (protocol.endsWith("s")) {
73 secure = true;
74 defaultPort = PORT_IMAPS;
75 }
76
77 if (port <= 0) {
78 port = defaultPort;
79 }
80
81 account = new IMAPAccount(protocol, secure, port);
82 }
83 else if (protocol.startsWith(PROTOCOL_POP)) {
84 boolean secure = false;
85 int defaultPort = PORT_POP;
86
87 if (protocol.endsWith("s")) {
88 secure = true;
89 defaultPort = PORT_POPS;
90 }
91
92 if (port <= 0) {
93 port = defaultPort;
94 }
95
96 account = new POPAccount(protocol, secure, port);
97 }
98 else {
99 boolean secure = false;
100 int defaultPort = PORT_SMTP;
101
102 if (protocol.endsWith("s")) {
103 secure = true;
104 defaultPort = PORT_SMTPS;
105 }
106
107 if (port <= 0) {
108 port = defaultPort;
109 }
110
111 account = new SMTPAccount(protocol, secure, port);
112 }
113
114 return account;
115 }
116
117 public String getHost() {
118 return _host;
119 }
120
121 public String getPassword() {
122 return _password;
123 }
124
125 public int getPort() {
126 return _port;
127 }
128
129 public String getProtocol() {
130 return _protocol;
131 }
132
133 public String getUser() {
134 return _user;
135 }
136
137 public boolean isRequiresAuthentication() {
138 if (Validator.isNotNull(_user) &&
139 Validator.isNotNull(_password)) {
140
141 return true;
142 }
143 else {
144 return false;
145 }
146 }
147
148 public boolean isSecure() {
149 return _secure;
150 }
151
152 public void setHost(String host) {
153 _host = host;
154 }
155
156 public void setPassword(String password) {
157 _password = password;
158 }
159
160 public void setPort(int port) {
161 _port = port;
162 }
163
164 public void setUser(String user) {
165 _user = user;
166 }
167
168 protected Account(String protocol, boolean secure, int port) {
169 _protocol = protocol;
170 _secure = secure;
171 _port = port;
172 }
173
174 private String _host;
175 private String _password;
176 private int _port;
177 private String _protocol;
178 private boolean _secure;
179 private String _user;
180
181 }