Refactor server/socket/connection layer

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@65 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2007-05-15 09:37:55 +00:00
parent 81ddfe21be
commit 81aa7c092c
10 changed files with 59 additions and 62 deletions

View File

@ -12,8 +12,8 @@ import java.net.Socket;
* Generic connection common to pop3 and smtp implementations
*/
public class AbstractConnection extends Thread {
protected Socket client;
protected final Socket client;
protected BufferedReader in;
protected OutputStream os;
// user name and password initialized through connection
@ -28,19 +28,13 @@ public class AbstractConnection extends Thread {
public AbstractConnection(Socket clientSocket) {
this.client = clientSocket;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
os = client.getOutputStream();
} catch (IOException e) {
try {
client.close();
} catch (IOException e2) {
DavGatewayTray.warn("Exception closing client socket", e2);
}
close();
DavGatewayTray.error("Exception while getting socket streams", e);
return;
}
// start the thread
this.start();
}
public void sendClient(String message) throws IOException {
@ -79,4 +73,36 @@ public class AbstractConnection extends Thread {
return line;
}
/**
* 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);
}
}
}

View File

@ -38,6 +38,7 @@ public abstract class AbstractServer extends Thread {
*/
public void run() {
Socket clientSocket = null;
AbstractConnection connection = null;
try {
//noinspection InfiniteLoopStatement
while (true) {
@ -45,7 +46,8 @@ public abstract class AbstractServer extends Thread {
DavGatewayTray.debug("Connection from " + clientSocket.getInetAddress() + " on port " + port);
// only accept localhost connections for security reasons
if (clientSocket.getInetAddress().toString().indexOf("127.0.0.1") > 0) {
createConnectionHandler(clientSocket);
connection = createConnectionHandler(clientSocket);
connection.start();
} else {
clientSocket.close();
DavGatewayTray.warn("Connection from external client refused");
@ -62,10 +64,13 @@ public abstract class AbstractServer extends Thread {
} catch (IOException e) {
DavGatewayTray.warn("Exception closing client socket", e);
}
if (connection != null) {
connection.close();
}
}
}
public abstract void createConnectionHandler(Socket clientSocket);
public abstract AbstractConnection createConnectionHandler(Socket clientSocket);
public void close() {
try {

View File

@ -47,7 +47,7 @@ public class BASE64EncoderStream extends FilterOutputStream {
/**
* The internal buffer of encoded output bytes.
*/
private byte[] output = new byte[4];
private final byte[] output = new byte[4];
/**
* The internal buffer of input bytes to be encoded.
@ -68,7 +68,7 @@ public class BASE64EncoderStream extends FilterOutputStream {
/**
* The maximum number of characters to output per line.
*/
private int maxLineLength;
private final int maxLineLength;
/**
* The index into the BASE64 alphabet to generate the next encoded

View File

@ -198,7 +198,7 @@ public class ExchangeSession {
}
public void login(String userName, String password) throws Exception {
public void login(String userName, String password) throws IOException {
try {
String url = Settings.getProperty("davmail.url");
@ -419,7 +419,7 @@ public class ExchangeSession {
}
}
protected Message buildMessage(ResponseEntity responseEntity) throws IOException {
protected Message buildMessage(ResponseEntity responseEntity) throws URIException {
Message message = new Message();
message.messageUrl = URIUtil.decode(responseEntity.getHref());
Enumeration propertiesEnum = responseEntity.getProperties();
@ -467,7 +467,6 @@ public class ExchangeSession {
}
message.preProcessHeaders();
return message;
}

View File

@ -129,19 +129,7 @@ public class ImapConnection extends AbstractConnection {
} catch (IOException e) {
DavGatewayTray.error("Exception handling client",e);
} finally {
try {
client.close();
} catch (IOException e2) {
DavGatewayTray.debug("Exception closing client",e2);
}
try {
if (session != null) {
session.close();
}
} catch (IOException e3) {
DavGatewayTray.debug("Exception closing gateway",e3);
}
close();
}
DavGatewayTray.resetIcon();
}

View File

@ -4,6 +4,7 @@ package davmail.imap;
import java.net.Socket;
import davmail.AbstractServer;
import davmail.AbstractConnection;
/**
* Pop3 server
@ -19,8 +20,8 @@ public class ImapServer extends AbstractServer {
super(port, ImapServer.DEFAULT_PORT);
}
public void createConnectionHandler(Socket clientSocket) {
new ImapConnection(clientSocket);
public AbstractConnection createConnectionHandler(Socket clientSocket) {
return new ImapConnection(clientSocket);
}
}

View File

@ -194,19 +194,7 @@ public class PopConnection extends AbstractConnection {
DavGatewayTray.debug("Exception sending error to client", e2);
}
} finally {
try {
client.close();
} catch (IOException e2) {
DavGatewayTray.debug("Exception closing client", e2);
}
try {
if (session != null) {
session.close();
}
} catch (IOException e3) {
DavGatewayTray.debug("Exception closing gateway", e3);
}
close();
}
DavGatewayTray.resetIcon();
}

View File

@ -2,6 +2,7 @@ package davmail.pop;
import davmail.AbstractServer;
import davmail.AbstractConnection;
import java.net.Socket;
@ -20,8 +21,8 @@ public class PopServer extends AbstractServer {
super(port, PopServer.DEFAULT_PORT);
}
public void createConnectionHandler(Socket clientSocket) {
new PopConnection(clientSocket);
public AbstractConnection createConnectionHandler(Socket clientSocket) {
return new PopConnection(clientSocket);
}
}

View File

@ -29,7 +29,6 @@ public class SmtpConnection extends AbstractConnection {
String line;
StringTokenizer tokens;
try {
ExchangeSession.checkConfig();
sendClient("220 DavMail SMTP ready at " + new Date());
@ -131,18 +130,7 @@ public class SmtpConnection extends AbstractConnection {
DavGatewayTray.debug("Exception sending error to client", e2);
}
} finally {
try {
client.close();
} catch (IOException e2) {
DavGatewayTray.debug("Exception closing client", e2);
}
try {
if (session != null) {
session.close();
}
} catch (IOException e3) {
DavGatewayTray.debug("Exception closing gateway", e3);
}
close();
}
DavGatewayTray.resetIcon();
}

View File

@ -1,6 +1,7 @@
package davmail.smtp;
import davmail.AbstractServer;
import davmail.AbstractConnection;
import java.net.Socket;
@ -15,8 +16,8 @@ public class SmtpServer extends AbstractServer {
super(port, SmtpServer.DEFAULT_PORT);
}
public void createConnectionHandler(Socket clientSocket) {
new SmtpConnection(clientSocket);
public AbstractConnection createConnectionHandler(Socket clientSocket) {
return new SmtpConnection(clientSocket);
}
}