2006-12-12 18:57:24 -05:00
|
|
|
package davmail;
|
|
|
|
|
|
|
|
import davmail.exchange.ExchangeSession;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.Socket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic connection common to pop3 and smtp implementations
|
|
|
|
*/
|
|
|
|
public class AbstractConnection extends Thread {
|
2007-05-15 05:37:55 -04:00
|
|
|
protected final Socket client;
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
protected BufferedReader in;
|
|
|
|
protected OutputStream os;
|
|
|
|
// user name and password initialized through connection
|
|
|
|
protected String userName = null;
|
|
|
|
protected String password = null;
|
|
|
|
// connection state
|
|
|
|
protected int state = 0;
|
|
|
|
// Exchange session proxy
|
|
|
|
protected ExchangeSession session;
|
|
|
|
|
|
|
|
// Initialize the streams and start the thread
|
2006-12-14 10:14:18 -05:00
|
|
|
public AbstractConnection(Socket clientSocket) {
|
2007-05-09 19:37:24 -04:00
|
|
|
this.client = clientSocket;
|
2006-12-12 18:57:24 -05:00
|
|
|
try {
|
2007-05-15 05:37:55 -04:00
|
|
|
//noinspection IOResourceOpenedButNotSafelyClosed
|
2006-12-12 18:57:24 -05:00
|
|
|
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
|
|
os = client.getOutputStream();
|
|
|
|
} catch (IOException e) {
|
2007-05-15 05:37:55 -04:00
|
|
|
close();
|
2006-12-12 18:57:24 -05:00
|
|
|
DavGatewayTray.error("Exception while getting socket streams", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendClient(String message) throws IOException {
|
|
|
|
sendClient(null, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendClient(String prefix, String message) throws IOException {
|
|
|
|
StringBuffer logBuffer = new StringBuffer("> ");
|
|
|
|
if (prefix != null) {
|
|
|
|
logBuffer.append(prefix);
|
|
|
|
os.write(prefix.getBytes());
|
|
|
|
}
|
|
|
|
logBuffer.append(message);
|
|
|
|
DavGatewayTray.debug(logBuffer.toString());
|
|
|
|
os.write(message.getBytes());
|
|
|
|
os.write('\r');
|
|
|
|
os.write('\n');
|
|
|
|
os.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a line from the client connection.
|
|
|
|
* Log message to stdout
|
2007-03-14 07:55:37 -04:00
|
|
|
*
|
2006-12-12 18:57:24 -05:00
|
|
|
* @return command line or null
|
2007-03-14 07:55:37 -04:00
|
|
|
* @throws IOException when unable to read line
|
2006-12-12 18:57:24 -05:00
|
|
|
*/
|
|
|
|
public String readClient() throws IOException {
|
|
|
|
String line = in.readLine();
|
2006-12-14 10:14:18 -05:00
|
|
|
if (line != null && !line.startsWith("PASS")) {
|
2007-03-14 07:55:37 -04:00
|
|
|
DavGatewayTray.debug("< " + line);
|
2006-12-14 10:14:18 -05:00
|
|
|
} else {
|
|
|
|
DavGatewayTray.debug("< PASS ********");
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
DavGatewayTray.switchIcon();
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2007-05-15 05:37:55 -04:00
|
|
|
/**
|
|
|
|
* Close client connection, streams and Exchange session .
|
|
|
|
*/
|
|
|
|
public void close() {
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (IOException e2) {
|
|
|
|
DavGatewayTray.warn("Exception closing client input stream", e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (os != null) {
|
|
|
|
try {
|
|
|
|
os.close();
|
|
|
|
} catch (IOException e2) {
|
|
|
|
DavGatewayTray.warn("Exception closing client output stream", e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
client.close();
|
|
|
|
} catch (IOException e2) {
|
|
|
|
DavGatewayTray.warn("Exception closing client socket", e2);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (session != null) {
|
|
|
|
session.close();
|
|
|
|
}
|
|
|
|
} catch (IOException e3) {
|
|
|
|
DavGatewayTray.warn("Exception closing gateway", e3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|