1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
30   * <a href="Account.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Thiago Moreira
33   *
34   */
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 }