1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.mail;
16  
17  import com.liferay.portal.kernel.util.Validator;
18  
19  import java.io.Serializable;
20  
21  /**
22   * <a href="Account.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Thiago Moreira
25   */
26  public abstract class Account implements Serializable {
27  
28      public static final String PROTOCOL_IMAP = "imap";
29  
30      public static final String PROTOCOL_IMAPS = "imaps";
31  
32      public static final String PROTOCOL_POP = "pop3";
33  
34      public static final String PROTOCOL_POPS = "pop3s";
35  
36      public static final String PROTOCOL_SMTP = "smtp";
37  
38      public static final String PROTOCOL_SMTPS = "smtps";
39  
40      public static final int PORT_IMAP = 143;
41  
42      public static final int PORT_IMAPS = 993;
43  
44      public static final int PORT_POP = 110;
45  
46      public static final int PORT_POPS = 995;
47  
48      public static final int PORT_SMTP = 25;
49  
50      public static final int PORT_SMTPS = 465;
51  
52      public static Account getInstance(String protocol) {
53          return getInstance(protocol, 0);
54      }
55  
56      public static Account getInstance(String protocol, int port) {
57          Account account = null;
58  
59          if (protocol.startsWith(PROTOCOL_IMAP)) {
60              boolean secure = false;
61              int defaultPort = PORT_IMAP;
62  
63              if (protocol.endsWith("s")) {
64                  secure = true;
65                  defaultPort = PORT_IMAPS;
66              }
67  
68              if (port <= 0) {
69                  port = defaultPort;
70              }
71  
72              account = new IMAPAccount(protocol, secure, port);
73          }
74          else if (protocol.startsWith(PROTOCOL_POP)) {
75              boolean secure = false;
76              int defaultPort = PORT_POP;
77  
78              if (protocol.endsWith("s")) {
79                  secure = true;
80                  defaultPort = PORT_POPS;
81              }
82  
83              if (port <= 0) {
84                  port = defaultPort;
85              }
86  
87              account = new POPAccount(protocol, secure, port);
88          }
89          else {
90              boolean secure = false;
91              int defaultPort = PORT_SMTP;
92  
93              if (protocol.endsWith("s")) {
94                  secure = true;
95                  defaultPort = PORT_SMTPS;
96              }
97  
98              if (port <= 0) {
99                  port = defaultPort;
100             }
101 
102             account = new SMTPAccount(protocol, secure, port);
103         }
104 
105         return account;
106     }
107 
108     public String getHost() {
109         return _host;
110     }
111 
112     public String getPassword() {
113         return _password;
114     }
115 
116     public int getPort() {
117         return _port;
118     }
119 
120     public String getProtocol() {
121         return _protocol;
122     }
123 
124     public String getUser() {
125         return _user;
126     }
127 
128     public boolean isRequiresAuthentication() {
129         if (Validator.isNotNull(_user) &&
130             Validator.isNotNull(_password)) {
131 
132             return true;
133         }
134         else {
135             return false;
136         }
137     }
138 
139     public boolean isSecure() {
140         return _secure;
141     }
142 
143     public void setHost(String host) {
144         _host = host;
145     }
146 
147     public void setPassword(String password) {
148         _password = password;
149     }
150 
151     public void setPort(int port) {
152         _port = port;
153     }
154 
155     public void setUser(String user) {
156         _user = user;
157     }
158 
159     protected Account(String protocol, boolean secure, int port) {
160         _protocol = protocol;
161         _secure = secure;
162         _port = port;
163     }
164 
165     private String _host;
166     private String _password;
167     private int _port;
168     private String _protocol;
169     private boolean _secure;
170     private String _user;
171 
172 }