1 /**
2 *
3 * Copyright 2003-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.javamail.store.pop3;
19
20 import javax.mail.AuthenticationFailedException;
21 import javax.mail.Folder;
22 import javax.mail.MessagingException;
23 import javax.mail.Session;
24 import javax.mail.Store;
25 import javax.mail.URLName;
26
27 /**
28 * POP3 implementation of javax.mail.Store POP protocol spec is implemented in
29 * org.apache.geronimo.javamail.store.pop3.POP3Connection
30 *
31 * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
32 */
33
34 public class POP3Store extends Store {
35
36 private POP3Connection pop3Con;
37
38 public POP3Store(Session session, URLName urlName) {
39 super(session, urlName);
40 }
41
42 /**
43 * @see javax.mail.Store#getDefaultFolder()
44 *
45 * There is only INBOX supported in POP3 so the default folder is inbox
46 */
47 public Folder getDefaultFolder() throws MessagingException {
48 return getFolder("INBOX");
49 }
50
51 /**
52 * @see javax.mail.Store#getFolder(java.lang.String)
53 */
54 public Folder getFolder(String name) throws MessagingException {
55
56 checkConnectionStatus();
57
58 if (!"INBOX".equalsIgnoreCase(name)) {
59 throw new MessagingException("Only INBOX is supported in POP3");
60 }
61 return new POP3Folder(this, session, pop3Con);
62 }
63
64 /**
65 * @see javax.mail.Store#getFolder(javax.mail.URLName)
66 */
67 public Folder getFolder(URLName url) throws MessagingException {
68 return getFolder(url.getFile());
69 }
70
71 /**
72 * @see javax.mail.Service#protocolConnect(java.lang.String, int,
73 * java.lang.String, java.lang.String)
74 */
75 protected synchronized boolean protocolConnect(String host, int portNum, String user, String passwd)
76 throws MessagingException {
77
78
79
80
81 if (host == null || passwd == null || user == null) {
82 return false;
83 }
84
85
86 if (portNum < 1) {
87 String portstring = session.getProperty("mail.pop3.port");
88 if (portstring != null) {
89 try {
90 portNum = Integer.parseInt(portstring);
91 } catch (NumberFormatException e) {
92 portNum = 110;
93 }
94 }
95 }
96
97
98
99
100
101 pop3Con = new POP3Connection(this.session, host, portNum);
102 try {
103 pop3Con.open();
104 } catch (Exception e) {
105 throw new MessagingException("Connection failed", e);
106 }
107
108
109
110
111
112 POP3Response resUser = null;
113 try {
114 resUser = pop3Con.sendCommand(POP3CommandFactory.getCOMMAND_USER(user));
115 } catch (Exception e) {
116 throw new MessagingException("Connection failed", e);
117 }
118
119 if (POP3Constants.ERR == resUser.getStatus()) {
120
121
122
123
124
125 try {
126 pop3Con.sendCommand(POP3CommandFactory.getCOMMAND_QUIT());
127 } catch (Exception e) {
128
129
130
131
132 }
133
134 throw new AuthenticationFailedException(resUser.getFirstLine());
135 }
136
137
138
139
140
141 POP3Response resPwd = null;
142 try {
143 resPwd = pop3Con.sendCommand(POP3CommandFactory.getCOMMAND_PASS(passwd));
144 } catch (Exception e) {
145 throw new MessagingException("Connection failed", e);
146 }
147
148 if (POP3Constants.ERR == resPwd.getStatus()) {
149
150
151
152
153
154 try {
155 pop3Con.sendCommand(POP3CommandFactory.getCOMMAND_QUIT());
156 } catch (Exception e) {
157
158
159
160
161 }
162
163 throw new AuthenticationFailedException(resPwd.getFirstLine());
164 }
165
166 return true;
167 }
168
169 /**
170 * @see javax.mail.Service#isConnected()
171 */
172 public boolean isConnected() {
173 POP3Response res = null;
174 try {
175 res = pop3Con.sendCommand(POP3CommandFactory.getCOMMAND_NOOP());
176 } catch (Exception e) {
177 return false;
178 }
179
180 return (POP3Constants.OK == res.getStatus());
181 }
182
183 /**
184 * @see javax.mail.Service#close()
185 */
186 public void close() throws MessagingException {
187
188 super.close();
189 try {
190 pop3Con.close();
191 } catch (Exception e) {
192
193
194
195 new MessagingException("", e);
196 }
197 }
198
199 private void checkConnectionStatus() throws MessagingException {
200 if (!this.isConnected()) {
201 throw new MessagingException("Not connected ");
202 }
203 }
204 }