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 java.io.BufferedOutputStream;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.net.InetSocketAddress;
26 import java.net.Socket;
27
28 import javax.mail.MessagingException;
29 import javax.mail.Session;
30
31 /**
32 * Represents a connection with the POP3 mail server. The connection is owned by
33 * a pop3 store and is only associated with one user who owns the respective
34 * POP3Store instance
35 *
36 * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
37 */
38
39 public class POP3Connection {
40
41 private Socket soc;
42
43 private Session session;
44
45 private String host;
46
47 private int port;
48
49 private PrintWriter writer;
50
51 private BufferedReader reader;
52
53 POP3Connection(Session session, String host, int port) {
54
55 this.session = session;
56 this.host = host;
57 this.port = port;
58 }
59
60 public void open() throws Exception {
61 try {
62 soc = new Socket();
63 soc.connect(new InetSocketAddress(host, port));
64
65 if (session.getDebug()) {
66 session.getDebugOut().println("Connection successful " + this.toString());
67 }
68
69 buildInputReader();
70 buildOutputWriter();
71
72
73 if (session.getDebug()) {
74 session.getDebugOut().println("Greeting from server " + reader.readLine());
75 } else {
76 reader.readLine();
77 }
78
79 } catch (IOException e) {
80 Exception ex = new Exception("Error opening connection " + this.toString(), e);
81 throw ex;
82 }
83 }
84
85 void close() throws Exception {
86 try {
87 soc.close();
88 if (session.getDebug()) {
89 session.getDebugOut().println("Connection successfuly closed " + this.toString());
90 }
91
92 } catch (IOException e) {
93 Exception ex = new Exception("Error closing connection " + this.toString(), e);
94 throw ex;
95 }
96
97 }
98
99 public synchronized POP3Response sendCommand(POP3Command cmd) throws MessagingException {
100 if (soc.isConnected()) {
101
102
103
104 if (soc.isOutputShutdown()) {
105 buildOutputWriter();
106 }
107
108
109
110 if (soc.isInputShutdown()) {
111 buildInputReader();
112 }
113
114 if (session.getDebug()) {
115 session.getDebugOut().println("\nCommand sent " + cmd.getCommand());
116 }
117
118 POP3Response res = null;
119
120
121
122 {
123 writer.write(cmd.getCommand());
124 writer.flush();
125 res = POP3ResponseBuilder.buildResponse(session, reader, cmd.isMultiLineResponse());
126 }
127
128 return res;
129 }
130
131 throw new MessagingException("Connection to Mail Server is lost, connection " + this.toString());
132 }
133
134 private void buildInputReader() throws MessagingException {
135 try {
136 reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
137 } catch (IOException e) {
138 throw new MessagingException("Error obtaining input stream " + this.toString(), e);
139 }
140 }
141
142 private void buildOutputWriter() throws MessagingException {
143 try {
144 writer = new PrintWriter(new BufferedOutputStream(soc.getOutputStream()));
145 } catch (IOException e) {
146 throw new MessagingException("Error obtaining output stream " + this.toString(), e);
147 }
148 }
149
150 public String toString() {
151 return "POP3Connection host: " + host + " port: " + port;
152 }
153 }