2006-12-12 18:57:24 -05:00
|
|
|
package davmail;
|
|
|
|
|
|
|
|
import davmail.exchange.ExchangeSession;
|
2007-10-09 08:01:58 -04:00
|
|
|
import davmail.tray.DavGatewayTray;
|
2008-11-26 19:56:28 -05:00
|
|
|
import davmail.smtp.SmtpConnection;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.Socket;
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
import org.apache.commons.httpclient.util.Base64;
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
/**
|
|
|
|
* 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
|
2008-11-26 19:56:28 -05:00
|
|
|
public AbstractConnection(String name, Socket clientSocket) {
|
|
|
|
super(name+"-"+clientSocket.getPort());
|
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
|
2008-11-26 19:56:28 -05:00
|
|
|
in = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));
|
2006-12-12 18:57:24 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
/**
|
|
|
|
* Send message to client followed by CRLF.
|
|
|
|
* @param message message
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2006-12-12 18:57:24 -05:00
|
|
|
public void sendClient(String message) throws IOException {
|
|
|
|
sendClient(null, message);
|
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
/**
|
|
|
|
* Send prefix and message to client followed by CRLF.
|
|
|
|
* @param prefix prefix
|
|
|
|
* @param message message
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2006-12-12 18:57:24 -05:00
|
|
|
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());
|
2008-02-05 18:18:23 -05:00
|
|
|
os.write((char)13);
|
|
|
|
os.write((char)10);
|
2006-12-12 18:57:24 -05:00
|
|
|
os.flush();
|
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
/**
|
|
|
|
* Send only bytes to client.
|
|
|
|
* @param messageBytes content
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public void sendClient(byte[] messageBytes) throws IOException {
|
|
|
|
StringBuffer logBuffer = new StringBuffer("> ");
|
|
|
|
logBuffer.append(new String(messageBytes));
|
|
|
|
DavGatewayTray.debug(logBuffer.toString());
|
|
|
|
os.write(messageBytes);
|
|
|
|
os.flush();
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
/**
|
|
|
|
* 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();
|
2008-11-26 19:56:28 -05:00
|
|
|
// TODO : add basic authorization check
|
|
|
|
if (line != null) {
|
|
|
|
if (line.startsWith("PASS")) {
|
|
|
|
DavGatewayTray.debug("< PASS ********");
|
|
|
|
} else if (state == SmtpConnection.PASSWORD){
|
|
|
|
DavGatewayTray.debug("< ********");
|
|
|
|
} else if (line.startsWith("Authorization:")){
|
|
|
|
DavGatewayTray.debug("< Authorization: ********");
|
|
|
|
} else {
|
|
|
|
DavGatewayTray.debug("< " + line);
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
protected String base64Encode(String value) {
|
|
|
|
return new String(Base64.encode(value.getBytes()));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String base64Decode(String value) throws IOException {
|
|
|
|
return new String(Base64.decode(value.getBytes()));
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|