1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

Sitch to file based settings

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@10 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2006-12-14 15:14:18 +00:00
parent 7ddb77af7a
commit 4626c66e04
12 changed files with 197 additions and 86 deletions

View File

@ -16,8 +16,6 @@ public class AbstractConnection extends Thread {
protected Socket client; protected Socket client;
protected BufferedReader in; protected BufferedReader in;
protected OutputStream os; protected OutputStream os;
// exchange server url
protected String url;
// user name and password initialized through connection // user name and password initialized through connection
protected String userName = null; protected String userName = null;
protected String password = null; protected String password = null;
@ -27,8 +25,7 @@ public class AbstractConnection extends Thread {
protected ExchangeSession session; protected ExchangeSession session;
// Initialize the streams and start the thread // Initialize the streams and start the thread
public AbstractConnection(String url, Socket clientSocket) { public AbstractConnection(Socket clientSocket) {
this.url = url;
client = clientSocket; client = clientSocket;
try { try {
in = new BufferedReader(new InputStreamReader(client.getInputStream())); in = new BufferedReader(new InputStreamReader(client.getInputStream()));
@ -72,7 +69,11 @@ public class AbstractConnection extends Thread {
*/ */
public String readClient() throws IOException { public String readClient() throws IOException {
String line = in.readLine(); String line = in.readLine();
if (line != null && !line.startsWith("PASS")) {
DavGatewayTray.debug("< "+line); DavGatewayTray.debug("< "+line);
} else {
DavGatewayTray.debug("< PASS ********");
}
DavGatewayTray.switchIcon(); DavGatewayTray.switchIcon();
return line; return line;
} }

View File

@ -9,20 +9,18 @@ import java.io.IOException;
*/ */
public abstract class AbstractServer extends Thread { public abstract class AbstractServer extends Thread {
protected int port; protected int port;
protected String url;
protected ServerSocket serverSocket; protected ServerSocket serverSocket;
/** /**
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
*/ */
public AbstractServer(String url, int port) { public AbstractServer(int port) {
this.port = port; this.port = port;
this.url = url;
try { try {
serverSocket = new ServerSocket(port); serverSocket = new ServerSocket(port);
} catch (IOException e) { } catch (IOException e) {
fail(e, "Exception creating server socket"); DavGatewayTray.error("Exception creating server socket", e);
} }
} }
@ -46,7 +44,7 @@ public abstract class AbstractServer extends Thread {
DavGatewayTray.debug("Connection from " + clientSocket.getInetAddress() + " on port " + port); DavGatewayTray.debug("Connection from " + clientSocket.getInetAddress() + " on port " + port);
// only accept localhost connections for security reasons // only accept localhost connections for security reasons
if (clientSocket.getInetAddress().toString().indexOf("127.0.0.1") > 0) { if (clientSocket.getInetAddress().toString().indexOf("127.0.0.1") > 0) {
createConnectionHandler(url, clientSocket); createConnectionHandler(clientSocket);
} else { } else {
clientSocket.close(); clientSocket.close();
DavGatewayTray.warn("Connection from external client refused"); DavGatewayTray.warn("Connection from external client refused");
@ -54,10 +52,18 @@ public abstract class AbstractServer extends Thread {
System.gc(); System.gc();
} }
} catch (IOException e) { } catch (IOException e) {
fail(e, "Exception while listening for connections"); DavGatewayTray.warn("Exception while listening for connections", e);
} }
} }
public abstract void createConnectionHandler(String url, Socket clientSocket); public abstract void createConnectionHandler(Socket clientSocket);
public void close() {
try {
serverSocket.close();
} catch (IOException e) {
DavGatewayTray.warn("Exception closing server socket", e);
}
}
} }

View File

@ -1,6 +1,5 @@
package davmail; package davmail;
import davmail.imap.ImapServer;
import davmail.pop.PopServer; import davmail.pop.PopServer;
import davmail.smtp.SmtpServer; import davmail.smtp.SmtpServer;
@ -8,45 +7,59 @@ import davmail.smtp.SmtpServer;
* DavGateway main class * DavGateway main class
*/ */
public class DavGateway { public class DavGateway {
protected static final String USAGE_MESSAGE = "Usage : java davmail.DavGateway url [smtplistenport] [pop3listenport] [imaplistenport]"; protected static SmtpServer smtpServer;
protected static PopServer popServer;
/** /**
* Start the gateway, listen on spécified smtp and pop3 ports * Start the gateway, listen on spécified smtp and pop3 ports
*/ */
public static void main(String[] args) { public static void main(String[] args) {
int smtpPort = SmtpServer.DEFAULT_PORT; String configFilePath = System.getProperty("user.home") + "/.davmail.properties";
int popPort = PopServer.DEFAULT_PORT;
int imapPort = ImapServer.DEFAULT_PORT;
String url;
if (args.length >= 1) { if (args.length >= 1) {
url = args[0]; configFilePath = args[0];
try {
if (args.length >= 2) {
smtpPort = Integer.parseInt(args[1]);
}
if (args.length >= 3) {
popPort = Integer.parseInt(args[2]);
}
if (args.length >= 4) {
imapPort = Integer.parseInt(args[3]);
}
DavGatewayTray.init();
SmtpServer smtpServer = new SmtpServer(url, smtpPort);
PopServer popServer = new PopServer(url, popPort);
ImapServer imapServer = new ImapServer(url, imapPort);
smtpServer.start();
popServer.start();
imapServer.start();
DavGatewayTray.info("Listening on ports " + smtpPort + " "+popPort+" "+imapPort);
} catch (NumberFormatException e) {
System.out.println(DavGateway.USAGE_MESSAGE);
}
} else {
System.out.println(DavGateway.USAGE_MESSAGE);
} }
Settings.setConfigFilePath(configFilePath);
Settings.load();
DavGatewayTray.init();
start();
}
public static void start() {
// first stop existing servers
if (smtpServer != null) {
smtpServer.close();
try {
smtpServer.join();
} catch (InterruptedException e) {
DavGatewayTray.warn("Exception waiting for listener to die", e);
}
}
if (popServer != null) {
popServer.close();
try {
popServer.join();
} catch (InterruptedException e) {
DavGatewayTray.warn("Exception waiting for listener to die", e);
}
}
int smtpPort = Settings.getIntProperty("davmail.smtpPort");
if (smtpPort == 0) {
smtpPort = SmtpServer.DEFAULT_PORT;
}
int popPort = Settings.getIntProperty("davmail.popPort");
if (popPort == 0) {
popPort = PopServer.DEFAULT_PORT;
}
smtpServer = new SmtpServer(smtpPort);
popServer = new PopServer(popPort);
smtpServer.start();
popServer.start();
DavGatewayTray.info("DavMail gateway listening on SMTP port " + smtpPort +
" and POP port " + popPort);
} }
} }

View File

@ -0,0 +1,70 @@
package davmail;
import java.util.Properties;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Settings facade
*/
public class Settings {
protected static final Properties settings = new Properties();
protected static String configFilePath;
public static synchronized void setConfigFilePath(String value) {
configFilePath = value;
}
public static synchronized void load() {
try {
File configFile = new File(configFilePath);
if (configFile.exists()) {
settings.load(new FileReader(configFile));
} else {
settings.put("davmail.url", "http://exchangeServer");
settings.put("davmail.popPort", "110");
settings.put("davmail.smtpPort", "25");
settings.put("davmail.enableProxy", "false");
settings.put("davmail.proxyHost", "");
settings.put("davmail.proxyPort", "");
settings.put("davmail.proxyUser", "");
settings.put("davmail.proxyPassword", "");
save();
}
} catch (IOException e) {
DavGatewayTray.error("Unable to load settings: ", e);
}
}
public static synchronized void save() {
try {
settings.store(new FileWriter(configFilePath), "DavMail settings");
} catch (IOException e) {
DavGatewayTray.error("Unable to store settings: ", e);
}
}
public static synchronized String getProperty(String property) {
return settings.getProperty(property);
}
public static synchronized void setProperty(String property, String value) {
settings.setProperty(property, value);
}
public static synchronized int getIntProperty(String property) {
int value = 0;
try {
String propertyValue = settings.getProperty(property);
if (propertyValue != null && propertyValue.length() > 0) {
value = Integer.valueOf(propertyValue);
}
} catch (NumberFormatException e) {
DavGatewayTray.error("Invalid setting value in " + property, e);
}
return value;
}
}

View File

@ -22,9 +22,9 @@ public class SettingsFrame extends JFrame {
JPanel panel = new JPanel(new GridLayout(3, 2)); JPanel panel = new JPanel(new GridLayout(3, 2));
panel.setBorder(BorderFactory.createTitledBorder("Gateway settings")); panel.setBorder(BorderFactory.createTitledBorder("Gateway settings"));
final JTextField urlField = new JTextField("", 15); final JTextField urlField = new JTextField(Settings.getProperty("davmail.url"), 15);
final JTextField popPortField = new JTextField(4); final JTextField popPortField = new JTextField(Settings.getProperty("davmail.popPort"), 4);
final JTextField smtpPortField = new JTextField(4); final JTextField smtpPortField = new JTextField(Settings.getProperty("davmail.smtpPort"), 4);
addSettingComponent(panel, "OWA url: ", urlField); addSettingComponent(panel, "OWA url: ", urlField);
addSettingComponent(panel, "Local POP port: ", popPortField); addSettingComponent(panel, "Local POP port: ", popPortField);
@ -35,13 +35,14 @@ public class SettingsFrame extends JFrame {
panel = new JPanel(new GridLayout(5, 2)); panel = new JPanel(new GridLayout(5, 2));
panel.setBorder(BorderFactory.createTitledBorder("Proxy settings")); panel.setBorder(BorderFactory.createTitledBorder("Proxy settings"));
boolean enableProxy = "true".equals(Settings.getProperty("davmail.enableProxy"));
final JCheckBox enableProxyField = new JCheckBox(); final JCheckBox enableProxyField = new JCheckBox();
final JTextField httpProxyField = new JTextField(System.getProperty("http.proxyHost"), 15); enableProxyField.setSelected(enableProxy);
final JTextField httpProxyPortField = new JTextField(System.getProperty("http.proxyPort"), 4); final JTextField httpProxyField = new JTextField(Settings.getProperty("davmail.proxyHost"), 15);
final JTextField httpProxyUserField = new JTextField(System.getProperty("http.proxyUser"), 4); final JTextField httpProxyPortField = new JTextField(Settings.getProperty("davmail.proxyPort"), 4);
final JTextField httpProxyPasswordField = new JPasswordField (System.getProperty("http.proxyPassword"), 4); final JTextField httpProxyUserField = new JTextField(Settings.getProperty("davmail.proxyUser"), 4);
final JTextField httpProxyPasswordField = new JPasswordField (Settings.getProperty("davmail.proxyPassword"), 4);
boolean enableProxy = enableProxyField.isSelected();
httpProxyField.setEnabled(enableProxy); httpProxyField.setEnabled(enableProxy);
httpProxyPortField.setEnabled(enableProxy); httpProxyPortField.setEnabled(enableProxy);
httpProxyUserField.setEnabled(enableProxy); httpProxyUserField.setEnabled(enableProxy);
@ -70,8 +71,19 @@ public class SettingsFrame extends JFrame {
JButton ok = new JButton("Save"); JButton ok = new JButton("Save");
ActionListener save = new ActionListener() { ActionListener save = new ActionListener() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// TODO : save options // save options
Settings.setProperty("davmail.url", urlField.getText());
Settings.setProperty("davmail.popPort", popPortField.getText());
Settings.setProperty("davmail.smtpPort", smtpPortField.getText());
Settings.setProperty("davmail.enableProxy", String.valueOf(enableProxyField.isSelected()));
Settings.setProperty("davmail.proxyHost", httpProxyField.getText());
Settings.setProperty("davmail.proxyPort", httpProxyPortField.getText());
Settings.setProperty("davmail.proxyUser", httpProxyUserField.getText());
Settings.setProperty("davmail.proxyPassword", httpProxyPasswordField.getText());
Settings.save();
setVisible(false); setVisible(false);
// restart listeners with new config
DavGateway.start();
} }
}; };
ok.addActionListener(save); ok.addActionListener(save);

View File

@ -23,6 +23,8 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import davmail.Settings;
/** /**
* Exchange session through Outlook Web Access (DAV) * Exchange session through Outlook Web Access (DAV)
*/ */
@ -96,8 +98,6 @@ public class ExchangeSession {
*/ */
public final SimpleDateFormat dateFormatter; public final SimpleDateFormat dateFormatter;
protected String url;
/** /**
* Various standard mail boxes Urls * Various standard mail boxes Urls
*/ */
@ -119,8 +119,7 @@ public class ExchangeSession {
* *
* @param url Outlook Web Access URL * @param url Outlook Web Access URL
*/ */
public ExchangeSession(String url) { public ExchangeSession() {
this.url = url;
// SimpleDateFormat are not thread safe, need to create one instance for // SimpleDateFormat are not thread safe, need to create one instance for
// each session // each session
dateParser = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); dateParser = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
@ -130,7 +129,22 @@ public class ExchangeSession {
public void login(String userName, String password) throws Exception { public void login(String userName, String password) throws Exception {
try { try {
// TODO : support different ports String url = Settings.getProperty("davmail.url");
String enableProxy = Settings.getProperty("davmail.enableProxy");
String proxyHost = null;
String proxyPort = null;
String proxyUser = null;
String proxyPassword = null;
if ("true".equals(enableProxy)) {
proxyHost = Settings.getProperty("davmail.proxyHost");
proxyPort = Settings.getProperty("davmail.proxyPort");
proxyUser = Settings.getProperty("davmail.proxyUser");
proxyPassword = Settings.getProperty("davmail.proxyPassword");
}
// get proxy configuration from setttings properties
URL urlObject = new URL(url); URL urlObject = new URL(url);
// webdavresource is unable to create the correct url type // webdavresource is unable to create the correct url type
HttpURL httpURL; HttpURL httpURL;
@ -148,12 +162,6 @@ public class ExchangeSession {
// set httpclient timeout to 30 seconds // set httpclient timeout to 30 seconds
//wdr.retrieveSessionInstance().setTimeout(30000); //wdr.retrieveSessionInstance().setTimeout(30000);
// get proxy configuration from system properties
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort");
String proxyUser = System.getProperty("http.proxyUser");
String proxyPassword = System.getProperty("http.proxyPassword");
// get the internal HttpClient instance // get the internal HttpClient instance
HttpClient httpClient = wdr.retrieveSessionInstance(); HttpClient httpClient = wdr.retrieveSessionInstance();
@ -166,9 +174,9 @@ public class ExchangeSession {
httpClient.getState().setAuthenticationPreemptive(false); httpClient.getState().setAuthenticationPreemptive(false);
// configure proxy // configure proxy
if (proxyHost != null) { if (proxyHost != null && proxyHost.length() > 0) {
httpClient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort)); httpClient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
if (proxyUser != null) { if (proxyUser != null && proxyUser.length() > 0) {
// detect ntlm authentication (windows domain name in user name) // detect ntlm authentication (windows domain name in user name)
int backslashindex = proxyUser.indexOf("\\"); int backslashindex = proxyUser.indexOf("\\");
if (backslashindex > 0) { if (backslashindex > 0) {

View File

@ -18,8 +18,8 @@ public class ImapConnection extends AbstractConnection {
protected static final int AUTHENTICATED = 1; protected static final int AUTHENTICATED = 1;
// Initialize the streams and start the thread // Initialize the streams and start the thread
public ImapConnection(String url, Socket clientSocket) { public ImapConnection(Socket clientSocket) {
super(url, clientSocket); super(clientSocket);
} }
public void run() { public void run() {
@ -50,7 +50,7 @@ public class ImapConnection extends AbstractConnection {
sendClient(commandId + " OK CAPABILITY completed"); sendClient(commandId + " OK CAPABILITY completed");
} else if ("login".equalsIgnoreCase(command)) { } else if ("login".equalsIgnoreCase(command)) {
parseCredentials(tokens); parseCredentials(tokens);
session = new ExchangeSession(url); session = new ExchangeSession();
try { try {
session.login(userName, password); session.login(userName, password);
sendClient(commandId + " OK Authenticated"); sendClient(commandId + " OK Authenticated");

View File

@ -15,12 +15,12 @@ public class ImapServer extends AbstractServer {
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
*/ */
public ImapServer(String url, int port) { public ImapServer(int port) {
super(url, (port == 0) ? ImapServer.DEFAULT_PORT : port); super((port == 0) ? ImapServer.DEFAULT_PORT : port);
} }
public void createConnectionHandler(String url, Socket clientSocket) { public void createConnectionHandler(Socket clientSocket) {
new ImapConnection(url, clientSocket); new ImapConnection(clientSocket);
} }
} }

View File

@ -19,8 +19,8 @@ public class PopConnection extends AbstractConnection {
private List<ExchangeSession.Message> messages; private List<ExchangeSession.Message> messages;
// Initialize the streams and start the thread // Initialize the streams and start the thread
public PopConnection(String url, Socket clientSocket) { public PopConnection(Socket clientSocket) {
super(url, clientSocket); super(clientSocket);
} }
public long getTotalMessagesLength() { public long getTotalMessagesLength() {
@ -91,7 +91,7 @@ public class PopConnection extends AbstractConnection {
} else { } else {
password = tokens.nextToken(); password = tokens.nextToken();
try { try {
session = new ExchangeSession(url); session = new ExchangeSession();
session.login(userName, password); session.login(userName, password);
messages = session.getAllMessages(); messages = session.getAllMessages();
sendOK("PASS"); sendOK("PASS");

View File

@ -15,12 +15,12 @@ public class PopServer extends AbstractServer {
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
*/ */
public PopServer(String url, int port) { public PopServer(int port) {
super(url, (port == 0) ? PopServer.DEFAULT_PORT : port); super((port == 0) ? PopServer.DEFAULT_PORT : port);
} }
public void createConnectionHandler(String url, Socket clientSocket) { public void createConnectionHandler(Socket clientSocket) {
new PopConnection(url, clientSocket); new PopConnection(clientSocket);
} }
} }

View File

@ -21,8 +21,8 @@ public class SmtpConnection extends AbstractConnection {
protected static final int MAILDATA = 4; protected static final int MAILDATA = 4;
// Initialize the streams and start the thread // Initialize the streams and start the thread
public SmtpConnection(String url, Socket clientSocket) { public SmtpConnection(Socket clientSocket) {
super(url, clientSocket); super(clientSocket);
} }
public void run() { public void run() {
@ -58,7 +58,7 @@ public class SmtpConnection extends AbstractConnection {
String authType = tokens.nextToken(); String authType = tokens.nextToken();
if ("PLAIN".equals(authType) && tokens.hasMoreElements()) { if ("PLAIN".equals(authType) && tokens.hasMoreElements()) {
decodeCredentials(tokens.nextToken()); decodeCredentials(tokens.nextToken());
session = new ExchangeSession(url); session = new ExchangeSession();
try { try {
session.login(userName, password); session.login(userName, password);
sendClient("235 OK Authenticated"); sendClient("235 OK Authenticated");

View File

@ -3,6 +3,7 @@ package davmail.smtp;
import java.net.Socket; import java.net.Socket;
import davmail.AbstractServer; import davmail.AbstractServer;
import davmail.Settings;
public class SmtpServer extends AbstractServer { public class SmtpServer extends AbstractServer {
public final static int DEFAULT_PORT = 25; public final static int DEFAULT_PORT = 25;
@ -11,12 +12,12 @@ public class SmtpServer extends AbstractServer {
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
*/ */
public SmtpServer(String url, int port) { public SmtpServer(int port) {
super(url, (port == 0) ? SmtpServer.DEFAULT_PORT : port); super((port == 0) ? SmtpServer.DEFAULT_PORT : port);
} }
public void createConnectionHandler(String url, Socket clientSocket) { public void createConnectionHandler(Socket clientSocket) {
new SmtpConnection(url, clientSocket); new SmtpConnection(clientSocket);
} }
} }