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